printf("%10s", f_value[i]);
时间: 2024-04-22 16:26:17 浏览: 18
这行代码使用printf函数来格式化输出字符串。让我来解释一下:
```c
printf("%10s", f_value[i]);
```
在这里,`"%10s"`是格式控制字符串,它告诉printf函数如何格式化输出。`%10s`表示输出一个字符串,并且占据10个字符的宽度。如果输出的字符串长度不足10个字符,那么会在左边用空格填充。
`f_value[i]`是一个字符串变量,代表要输出的字符串。
例如,假设`f_value[i]`的值为"Hello",那么上述代码将会以10个字符的宽度输出字符串"Hello",并在左边用空格填充剩余的字符。输出结果如下:
```
Hello
```
需要注意的是,如果`f_value[i]`的长度超过了10个字符,则不会进行截断,而是按照实际长度输出。如果希望以固定宽度输出,并在超过宽度时进行截断,可以使用字符串截断函数(如strncpy)来处理字符串。
另外,还有其他格式控制符可以用于printf函数来格式化输出不同类型的数据,如整数(%d)、浮点数(%f)、字符(%c)等。通过使用不同的格式控制符和修饰符,可以灵活地控制输出的格式。
相关问题
#include<stdio.h> #include<string.h> char prog[80],token[8],ch; int syn,p,m,n,sum; char rwtab[6]={"begin","if","then","while","do","end"}; void scaner(); int main() { p=0; printf("\n please input a string(end with '#'):\n"); do{ scanf("%c",&ch); prog[p++]=ch; }while(ch!='#'); p=0; do{ scaner(); switch(syn) { case 11:printf("(%-10d%5d)\n",sum,syn); break; case -1:printf("you have input a wrong string:\n"); getchar(); return 0; default:printf("(%-10s%5d)\n",token,syn); break;} }while(syn!=0); getchar(); return 0; } void scaner(){ sum=0; for(m=0;m<8;m++) token[m++]=NULL; ch=prog[p++]; m=0; while((ch==' ')||(ch=='\n')) ch=prog[p++]; if(((ch<='z')&&(ch>='a'))||((ch<='Z')&&(ch>'A'))) {while(((ch<='z')&&(ch>='a'))||((ch<='Z')&&(ch>'A'))||((ch>='0')&&(ch<='9'))) {token[m++]=ch; ch=prog[p++];} p--; syn=10; for(n=0;n<6;n++) if(strcmp(token,rwtab[n])==0) {syn=n+1; break;} } else if((ch>='0')&&(ch<='9')) {while((ch>='0')&&(ch<='9')) { sum=sum10+ch-'0'; ch=prog[p++]; } p--; syn=11; } else switch(ch) { case '<':token[m++]=ch; ch=prog[p++]; if(ch=='=') { syn=22; token[m++]=ch; } else{ syn=20; p--; } break; case '>':token[m++]=ch; ch=prog[p++]; if(ch=='=') { syn=24; token[m++]=ch; } else{ syn=23; p--; } break; case '+':token[m++]=ch; ch=prog[p++]; if(ch=='+') { syn=17; token[m++]=ch; } else{ syn=13; p--; } break; case '-':token[m++]=ch; ch=prog[p++]; if(ch=='-') { syn=29; token[m++]=ch; } else{ syn=14; p--; } break; case '!':ch=prog[p++]; if(ch=='=') { syn=21; token[m++]=ch; } else{ syn=3; p--; } break; case '=':token[m++]=ch; ch=prog[p++]; if(ch=='=') { syn=25; token[m++]=ch; } else{ syn=18; p--; } break; case '*':syn=15; token[m++]=ch; break; case '/':syn=16; token[m++]=ch; break; case '(':syn=27; token[m++]=ch; break; case ')':syn=28; token[m++]=ch; break; case '{':syn=5; token[m++]=ch; break; case '}':syn=6; token[m++]=ch; break; case ';':syn=26; token[m++]=ch; break; case '"':syn=30; token[m++]=ch; break; case '#':syn=0; token[m++]=ch; break; case ':':syn=17; token[m++]=ch; break; default:syn=-1; break; } token[m++]='\0'; }怎么让输出结果value在前和code在后
在switch语句中,根据不同的情况,可以将value和code分别存储到两个不同的数组中,最后遍历两个数组,分别输出value和code即可。以下是修改后的代码:
```
#include<stdio.h>
#include<string.h>
char prog[80],token[8],ch;
int syn,p,m,n,sum;
char rwtab[6]={"begin","if","then","while","do","end"};
void scaner();
int main() {
p=0;
printf("\n please input a string(end with '#'):\n");
do{
scanf("%c",&ch); prog[p++]=ch;
}while(ch!='#');
p=0;
do{
scaner();
switch(syn) {
case 11: printf("(%-10d%5d)\n",sum,syn); break;
case -1: printf("you have input a wrong string:\n");
getchar(); return 0;
default: break;
}
}while(syn!=0);
// 存储value和code
char value_arr[100][10];
char code_arr[100][10];
int value_index = 0;
int code_index = 0;
p = 0;
do{
scaner();
switch(syn) {
case 11: sprintf(value_arr[value_index], "%d", sum);
strcpy(code_arr[code_index], "value");
value_index++;
code_index++;
break;
case -1: printf("you have input a wrong string:\n");
getchar(); return 0;
default: strcpy(value_arr[value_index], token);
strcpy(code_arr[code_index], "code");
value_index++;
code_index++;
break;
}
}while(syn!=0);
// 输出value和code
for (int i = 0; i < value_index; i++) {
if (strcmp(code_arr[i], "value") == 0) {
printf("(%-10s%5s)\n", value_arr[i], "");
} else {
printf("(%-10s%5s)\n", "", code_arr[i]);
}
}
getchar();
return 0;
}
void scaner(){
sum=0;
for(m=0;m<8;m++) token[m++]=NULL;
ch=prog[p++];
m=0;
while((ch==' ')||(ch=='\n')) ch=prog[p++];
if(((ch<='z')&&(ch>='a'))||((ch<='Z')&&(ch>'A'))) {
while(((ch<='z')&&(ch>='a'))||((ch<='Z')&&(ch>'A'))||((ch>='0')&&(ch<='9'))) {
token[m++]=ch;
ch=prog[p++];
}
p--;
syn=10;
for(n=0;n<6;n++)
if(strcmp(token,rwtab[n])==0) {
syn=n+1; break;}
}
else if((ch>='0')&&(ch<='9')) {
while((ch>='0')&&(ch<='9')) {
sum=sum*10+ch-'0';
ch=prog[p++];
}
p--;
syn=11;
}
else
switch(ch) {
case '<':token[m++]=ch; ch=prog[p++];
if(ch=='=') { syn=22; token[m++]=ch; }
else{ syn=20; p--; }
break;
case '>':token[m++]=ch; ch=prog[p++];
if(ch=='=') { syn=24; token[m++]=ch; }
else{ syn=23; p--; }
break;
case '+':token[m++]=ch; ch=prog[p++];
if(ch=='+') { syn=17; token[m++]=ch; }
else{ syn=13; p--; }
break;
case '-':token[m++]=ch; ch=prog[p++];
if(ch=='-') { syn=29; token[m++]=ch; }
else{ syn=14; p--; }
break;
case '!':ch=prog[p++];
if(ch=='=') { syn=21; token[m++]=ch; }
else{ syn=3; p--; }
break;
case '=':token[m++]=ch; ch=prog[p++];
if(ch=='=') { syn=25; token[m++]=ch; }
else{ syn=18; p--; }
break;
case '*':syn=15; token[m++]=ch; break;
case '/':syn=16; token[m++]=ch; break;
case '(':syn=27; token[m++]=ch; break;
case ')':syn=28; token[m++]=ch; break;
case '{':syn=5; token[m++]=ch; break;
case '}':syn=6; token[m++]=ch; break;
case ';':syn=26; token[m++]=ch; break;
case '"':syn=30; token[m++]=ch; break;
case '#':syn=0; token[m++]=ch; break;
case ':':syn=17; token[m++]=ch; break;
default:syn=-1; break;
}
token[m++]='\0';
}
```
在上述代码中,我们新建了两个数组`value_arr`和`code_arr`,分别用来存储value和code。在第一次遍历中,如果当前`syn`的值为11,则说明当前token是一个数字,我们就将其存入`value_arr`中,并将`code_arr`对应位置的值设为"value"。如果当前不是数字,则说明当前token是一个代码标识符,我们将其存入`value_arr`中,并将`code_arr`对应位置的值设为"code"。在第二次遍历中,我们就可以遍历两个数组,分别输出value和code了。
用c语言编写程序:输入一个源程序(可有多行)(不包含头文件和宏定义)实现对源程序的关键字、标识符、运算符、界符和常量单词的信息提取,信息包含所处位置(行号、列号)、类别信息,存入一个结构体数组或链表中,并输出,其中标识符:用来表示各种名字,如变量名,数组名,函数名。标识符以字母或下划线开头,后面跟字母、数字、下划线的任意字符序列,不可以数字开头,标识符不能使用关键字。标识符的长度不得超过8个字符。
以下是我编写的程序,可以实现对源程序的关键字、标识符、运算符、界符和常量单词的信息提取,并存入一个结构体数组中,并输出:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX_IDENTIFIER_LEN 8
// 关键字表
char *keywords[] = {
"auto", "break", "case", "char", "const", "continue", "default", "do", "double", "else",
"enum", "extern", "float", "for", "goto", "if", "int", "long", "register", "return",
"short", "signed", "sizeof", "static", "struct", "switch", "typedef", "union", "unsigned", "void",
"volatile", "while"
};
// 运算符表
char *operators[] = {
"+", "-", "*", "/", "%", "=", "==", "!=", ">", "<", ">=", "<="
};
// 界符表
char *delimiters[] = {
"(", ")", "[", "]", "{", "}", ",", ";"
};
// 单词类型枚举
enum word_type {
KEYWORD,
IDENTIFIER,
OPERATOR,
DELIMITER,
CONSTANT
};
// 单词信息结构体
struct word_info {
int line_num; // 所在行号
int col_num; // 所在列号
enum word_type type; // 单词类型
char *value; // 单词值
};
// 判断一个字符串是否为关键字
int is_keyword(char *str) {
int i;
for (i = 0; i < sizeof(keywords) / sizeof(char *); i++) {
if (strcmp(str, keywords[i]) == 0) {
return 1;
}
}
return 0;
}
// 判断一个字符是否为运算符
int is_operator(char ch) {
int i;
for (i = 0; i < sizeof(operators) / sizeof(char *); i++) {
if (ch == operators[i][0]) {
return 1;
}
}
return 0;
}
// 判断一个字符串是否为界符
int is_delimiter(char *str) {
int i;
for (i = 0; i < sizeof(delimiters) / sizeof(char *); i++) {
if (strcmp(str, delimiters[i]) == 0) {
return 1;
}
}
return 0;
}
// 判断一个字符是否为数字
int is_digit(char ch) {
return isdigit(ch);
}
// 判断一个字符是否为字母或下划线
int is_letter_or_underscore(char ch) {
return isalpha(ch) || ch == '_';
}
// 从源代码中提取单词信息并存入结构体数组中
void extract_word_info(char *source_code, struct word_info *word_infos, int *word_count) {
int line_num = 1; // 当前行号
int col_num = 1; // 当前列号
int i = 0; // 当前字符索引
int start_index; // 单词起始位置
int end_index; // 单词结束位置
char ch; // 当前字符
char *word_value; // 单词值
while (source_code[i] != '\0') {
ch = source_code[i];
if (ch == '\n') { // 如果是换行符,更新行号和列号
line_num++;
col_num = 1;
} else if (isspace(ch)) { // 如果是空白字符,更新列号
col_num++;
} else if (is_operator(ch)) { // 如果是运算符
start_index = i;
end_index = i + 1;
word_value = malloc(2);
word_value[0] = ch;
word_value[1] = '\0';
word_infos[*word_count].line_num = line_num;
word_infos[*word_count].col_num = col_num;
word_infos[*word_count].type = OPERATOR;
word_infos[*word_count].value = word_value;
(*word_count)++;
col_num++;
i++;
} else if (is_letter_or_underscore(ch)) { // 如果是字母或下划线
start_index = i;
end_index = i;
while (is_letter_or_underscore(source_code[end_index + 1]) || is_digit(source_code[end_index + 1])) {
end_index++;
}
word_value = malloc(end_index - start_index + 2);
strncpy(word_value, source_code + start_index, end_index - start_index + 1);
word_value[end_index - start_index + 1] = '\0';
if (is_keyword(word_value)) { // 如果是关键字
word_infos[*word_count].line_num = line_num;
word_infos[*word_count].col_num = col_num;
word_infos[*word_count].type = KEYWORD;
word_infos[*word_count].value = word_value;
(*word_count)++;
} else { // 如果是标识符
if (!isdigit(word_value[0]) && strlen(word_value) <= MAX_IDENTIFIER_LEN) {
word_infos[*word_count].line_num = line_num;
word_infos[*word_count].col_num = col_num;
word_infos[*word_count].type = IDENTIFIER;
word_infos[*word_count].value = word_value;
(*word_count)++;
} else {
free(word_value);
}
}
col_num += end_index - start_index + 1;
i = end_index + 1;
} else if (is_digit(ch)) { // 如果是数字
start_index = i;
end_index = i;
while (is_digit(source_code[end_index + 1])) {
end_index++;
}
if (source_code[end_index + 1] == '.') { // 如果是浮点数
end_index++;
while (is_digit(source_code[end_index + 1])) {
end_index++;
}
}
word_value = malloc(end_index - start_index + 2);
strncpy(word_value, source_code + start_index, end_index - start_index + 1);
word_value[end_index - start_index + 1] = '\0';
word_infos[*word_count].line_num = line_num;
word_infos[*word_count].col_num = col_num;
word_infos[*word_count].type = CONSTANT;
word_infos[*word_count].value = word_value;
(*word_count)++;
col_num += end_index - start_index + 1;
i = end_index + 1;
} else if (is_delimiter(&source_code[i])) { // 如果是界符
start_index = i;
end_index = i + strlen(&source_code[i]) - 1;
word_value = malloc(end_index - start_index + 2);
strncpy(word_value, source_code + start_index, end_index - start_index + 1);
word_value[end_index - start_index + 1] = '\0';
word_infos[*word_count].line_num = line_num;
word_infos[*word_count].col_num = col_num;
word_infos[*word_count].type = DELIMITER;
word_infos[*word_count].value = word_value;
(*word_count)++;
col_num += end_index - start_index + 1;
i = end_index + 1;
} else { // 其他字符
col_num++;
i++;
}
}
}
// 输出单词信息
void print_word_info(struct word_info *word_infos, int word_count) {
int i;
printf("%-10s%-10s%-15s%s\n", "Line", "Column", "Type", "Value");
for (i = 0; i < word_count; i++) {
printf("%-10d%-10d", word_infos[i].line_num, word_infos[i].col_num);
switch (word_infos[i].type) {
case KEYWORD:
printf("%-15s", "Keyword");
break;
case IDENTIFIER:
printf("%-15s", "Identifier");
break;
case OPERATOR:
printf("%-15s", "Operator");
break;
case DELIMITER:
printf("%-15s", "Delimiter");
break;
case CONSTANT:
printf("%-15s", "Constant");
break;
}
printf("%s\n", word_infos[i].value);
}
}
int main() {
char source_code[1000]; // 源代码
struct word_info word_infos[1000]; // 单词信息结构体数组
int word_count = 0; // 单词数量
printf("请输入源程序(可有多行):\n");
fgets(source_code, sizeof(source_code), stdin);
extract_word_info(source_code, word_infos, &word_count);
print_word_info(word_infos, word_count);
return 0;
}
```
以上程序实现了对源程序的关键字、标识符、运算符、界符和常量单词的信息提取,并存入一个结构体数组中,并输出。你可以将源程序复制粘贴到程序中进行测试。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)