需要帮助了解如何使用 get char 在终端中输入句子
Need help understanding how to use get char to type out sentences in terminal
#include <stdio.h>
#include <string.h>
int row;
int col;
int input;
int offset;
char ascii[7][580] = {
" * * * * * * ** ** * * * * *** ***** ***** ***** ***** ***** ***** ***** ***** ***** * * ** *** *** **** **** **** ***** ***** **** * * ***** ***** * * * * * * * *** **** *** **** **** ***** * * * * * * * * * * ***** *** * *** * * * * ** * ** * ** * ** ",
" * * * * * **** ** * * * * * * * * * * * * * ***** ***** ***** ***** ***** ***** ***** ***** ***** ** ** * * * * * * * * * * * * * * * * * * * * * * ** ** ** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ",
" * * * ***** * * * * * * * * *** * * * * ***** ***** ***** ***** ***** ***** ***** ***** ***** ** ** * ***** * * * ** * * * * * * * * * * * * * * * * * ** ** ** * * * * * * * * * * * * * * * * * * * * * * * * * * * * *** * *** * *** * **** * * * * * * **** **** *** **** **** * ** *** ***** * * * * * * * * * * ***** * * * * ",
" * * * *** * * * * * *** ***** * * * ***** ***** ***** ***** ***** ***** ***** ***** ***** * * ** * * * ***** **** * * * ***** ***** * *** ***** * * ** * * * * * * * * * **** * * **** *** * * * * * * * * * * * * * * * **** * * **** * * ***** * * **** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *",
" * ***** * * * * * * * * *** * ** ** * * * ***** ***** ***** ***** ***** ***** ***** ***** ***** ** ** * ***** * * * * * * * * * * * * * * * * * * * * * * * * * ** * * * * * * * * * * * * * * ** ** * * * * * * * **** * * * * * ***** * **** * * * * ** * * * * * * * * **** **** * *** * * * * * * * * * * * * * * * ",
" * * **** * ** * * * * * * * * ** ** * * * ***** ***** ***** ***** ***** ***** ***** ***** ***** ** ** * * * * * * * * * * * * * * * * * * * * * * * * * ** * * * * ** * * * * * * * * ** ** * * * * * * * * * * * * * * * * * * * * * * ** * * * * * * * * * * * * * * * * * * * * * * * * * * * * ",
" * * * * ** ** * * * * * *** ***** ***** ***** ***** ***** ***** ***** ***** ***** * * * * *** * * **** **** **** ***** * *** * * ***** ** * * ***** * * * * *** * **** * * **** * *** * * * * * * ***** *** * *** ***** *** *** *** *** *** * *** * * *** ** * * *** * * * * * *** * ** * *** ** **** * * * * * * * ***** ** * ** ",
};
int main(int argc, char const *argv[]){
//for loop for the rows down
for(row = 0; row < 7; row++){
printf("%c", ascii[row][col]);
//columns for each segment of stars or space
for(col = 0; col < 6; col++){
}//end of cols
}//end of rows
}
我使用了一个二维数组来存储 ascii 值的模型,我没有添加数字我希望能够使用终端输入字母和符号然后让它们以星号打印任何人都可以帮助我了解如何执行此操作。
假设注释掉的for循环将当前字符赋值给变量ch
:
putchar(ascii[row][(ch - 32)*6 + col]);
(ch-32)
将带您到 "font" 的开头。
(ch-32)*6
将跳过字体中所有先前字符的所有 "star columns"。
(ch-32)*7 + col
选择当前"star column"当前字符
void print_line(char *str)
{
char ch, *p;
int row, col;
for (row = 0; row < 7; row++)
{
for (p = str; ch = *p; p++)
{
/* Check that ch is in the printable range of ASCII. */
if (ch < 32 || ch > 95)
ch = '?'; /* or continue; */
for (col = 0; col < 6; col++)
putchar(ascii[row][(ch - 32)*6 + col]);
}
putchar('\n');
}
}
此函数一次只能处理一行。
您几乎肯定希望以不同的方式构建数据。
看起来每个字符都定义为 7 行,每行 6 个字符(其中一个始终是 space - 即空列)。
考虑这样的结构:
typedef struct big_char {
unsigned char the_char;
char *representation[7];
} big_char;
现在,您可以这样表示单个字符:
big_char asterisk = { '*' , {
" ",
"* * *",
" * * ",
" * ",
" * * ",
"* * *",
" "
}};
你可以用来打印的
for (row = 0; row < 7; ++row) {
printf ("%s\n", asterisk.representation[row]);
}
您可以看到这是如何构成打印大字符的简单函数的基础。
你的ascii
table就是这些结构的数组。
当用户输入一个字符时,您搜索数组以查找与 ascii[i].the_char
的匹配项,然后打印出该条目。
如果你想用类似于你的结构的东西来做,你也可以只计算一个偏移量。以下代码示例为您计算偏移量。每个 Ascii 符号都有一个介于 0 和 127 之间的对应整数值,其中 [space] 为 32。因此您的偏移量是 6 列乘以所需 ascii 符号的整数 - 32(因为 [space] 不应该有偏移量)。
编辑:我用正确的 ascii 码测试了它,现在它可以工作了
// variable to read in letter
char input = 0;
int offset = 0;
// read in of letter
printf("Please enter the letter you want: \n");
scanf("%c", &input);
// get offset from letter. [space] is ascii symbol nb. 32
offset = 6*( (int) input - 32);
// print letter
for(int row = 0; row < 7; row++){
for(int col = 0; col < 6; col++){
printf("%c", ascii[row][offset+col]);
}
printf("\n");
}
return 0;
#include <stdio.h>
#include <string.h>
int row;
int col;
int input;
int offset;
char ascii[7][580] = {
" * * * * * * ** ** * * * * *** ***** ***** ***** ***** ***** ***** ***** ***** ***** * * ** *** *** **** **** **** ***** ***** **** * * ***** ***** * * * * * * * *** **** *** **** **** ***** * * * * * * * * * * ***** *** * *** * * * * ** * ** * ** * ** ",
" * * * * * **** ** * * * * * * * * * * * * * ***** ***** ***** ***** ***** ***** ***** ***** ***** ** ** * * * * * * * * * * * * * * * * * * * * * * ** ** ** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ",
" * * * ***** * * * * * * * * *** * * * * ***** ***** ***** ***** ***** ***** ***** ***** ***** ** ** * ***** * * * ** * * * * * * * * * * * * * * * * * ** ** ** * * * * * * * * * * * * * * * * * * * * * * * * * * * * *** * *** * *** * **** * * * * * * **** **** *** **** **** * ** *** ***** * * * * * * * * * * ***** * * * * ",
" * * * *** * * * * * *** ***** * * * ***** ***** ***** ***** ***** ***** ***** ***** ***** * * ** * * * ***** **** * * * ***** ***** * *** ***** * * ** * * * * * * * * * **** * * **** *** * * * * * * * * * * * * * * * **** * * **** * * ***** * * **** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *",
" * ***** * * * * * * * * *** * ** ** * * * ***** ***** ***** ***** ***** ***** ***** ***** ***** ** ** * ***** * * * * * * * * * * * * * * * * * * * * * * * * * ** * * * * * * * * * * * * * * ** ** * * * * * * * **** * * * * * ***** * **** * * * * ** * * * * * * * * **** **** * *** * * * * * * * * * * * * * * * ",
" * * **** * ** * * * * * * * * ** ** * * * ***** ***** ***** ***** ***** ***** ***** ***** ***** ** ** * * * * * * * * * * * * * * * * * * * * * * * * * ** * * * * ** * * * * * * * * ** ** * * * * * * * * * * * * * * * * * * * * * * ** * * * * * * * * * * * * * * * * * * * * * * * * * * * * ",
" * * * * ** ** * * * * * *** ***** ***** ***** ***** ***** ***** ***** ***** ***** * * * * *** * * **** **** **** ***** * *** * * ***** ** * * ***** * * * * *** * **** * * **** * *** * * * * * * ***** *** * *** ***** *** *** *** *** *** * *** * * *** ** * * *** * * * * * *** * ** * *** ** **** * * * * * * * ***** ** * ** ",
};
int main(int argc, char const *argv[]){
//for loop for the rows down
for(row = 0; row < 7; row++){
printf("%c", ascii[row][col]);
//columns for each segment of stars or space
for(col = 0; col < 6; col++){
}//end of cols
}//end of rows
}
我使用了一个二维数组来存储 ascii 值的模型,我没有添加数字我希望能够使用终端输入字母和符号然后让它们以星号打印任何人都可以帮助我了解如何执行此操作。
假设注释掉的for循环将当前字符赋值给变量ch
:
putchar(ascii[row][(ch - 32)*6 + col]);
(ch-32)
将带您到 "font" 的开头。
(ch-32)*6
将跳过字体中所有先前字符的所有 "star columns"。
(ch-32)*7 + col
选择当前"star column"当前字符
void print_line(char *str)
{
char ch, *p;
int row, col;
for (row = 0; row < 7; row++)
{
for (p = str; ch = *p; p++)
{
/* Check that ch is in the printable range of ASCII. */
if (ch < 32 || ch > 95)
ch = '?'; /* or continue; */
for (col = 0; col < 6; col++)
putchar(ascii[row][(ch - 32)*6 + col]);
}
putchar('\n');
}
}
此函数一次只能处理一行。
您几乎肯定希望以不同的方式构建数据。
看起来每个字符都定义为 7 行,每行 6 个字符(其中一个始终是 space - 即空列)。
考虑这样的结构:
typedef struct big_char {
unsigned char the_char;
char *representation[7];
} big_char;
现在,您可以这样表示单个字符:
big_char asterisk = { '*' , {
" ",
"* * *",
" * * ",
" * ",
" * * ",
"* * *",
" "
}};
你可以用来打印的
for (row = 0; row < 7; ++row) {
printf ("%s\n", asterisk.representation[row]);
}
您可以看到这是如何构成打印大字符的简单函数的基础。
你的ascii
table就是这些结构的数组。
当用户输入一个字符时,您搜索数组以查找与 ascii[i].the_char
的匹配项,然后打印出该条目。
如果你想用类似于你的结构的东西来做,你也可以只计算一个偏移量。以下代码示例为您计算偏移量。每个 Ascii 符号都有一个介于 0 和 127 之间的对应整数值,其中 [space] 为 32。因此您的偏移量是 6 列乘以所需 ascii 符号的整数 - 32(因为 [space] 不应该有偏移量)。
编辑:我用正确的 ascii 码测试了它,现在它可以工作了
// variable to read in letter
char input = 0;
int offset = 0;
// read in of letter
printf("Please enter the letter you want: \n");
scanf("%c", &input);
// get offset from letter. [space] is ascii symbol nb. 32
offset = 6*( (int) input - 32);
// print letter
for(int row = 0; row < 7; row++){
for(int col = 0; col < 6; col++){
printf("%c", ascii[row][offset+col]);
}
printf("\n");
}
return 0;