c - 如果数据类型像 unsigned long int 等,如何标记 ac 程序
问题描述
下面给出了我的问题 - 编写一个程序,该程序获取输入,一个程序(来自文件)并将其中的所有变量打印为输出。
前任。输入程序.txt
#include<stdio.h>
int main(void)
{
int a, b, c=10;
char p, *ptr=NULL;
unsigned long long int t;
}
输出必须包含:a,b,c,p,ptr,t。
我的程序是 -
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
int isDataType(char buffer[]){
char types[19][10] = {"char","const","double","float","int","long","register","short","signed",
"static","struct","union","unsigned","void","volatile","int*","char*","float*","enum"};
int i, flag = 0;
for(i = 0; i < 19; ++i){
if(strcmp(types[i], buffer) == 0){
flag = 1;
break;
}
}
return flag;
}
int main(){
char ch, buffer[15], operators[] = "+-*/%=.#";
FILE *fp;
int i,j=0;
fp = fopen("inputprogram.txt","r");
if(fp == NULL){
printf("error while opening the file\n");
exit(0);
}
while((ch = fgetc(fp)) != EOF){
for(i = 0; i < 8; ++i){
if(ch == operators[i])
printf("%c is an operator\n", ch);
}
if(isalpha(ch)){
buffer[j++] = ch;
}
else if((ch == ' ' || ch == '\n' || ch == ',' || (isdigit(ch))== 1) && (j != 0)){
buffer[j] = '\0';
j = 0;
if(isDataType(buffer) == 1)
printf("%s is a data type\n", buffer);
else
printf("%s is a variable\n", buffer);
}
}
fclose(fp);
return 0;
}
unsigned long long int
如果txt 文件中的数据类似,则此代码不会给出预期的结果。如何以这样的方式优化代码,如果像unsigned long int
或int *
(int space star)这样的数据类型出现,那么它也能够打印确切的变量。
解决方案
推荐阅读
- android - 膨胀 ImageButtons 时移动的图像
- java - How to extract key/value pairs in JSON body from ResponseEntity in Spring
- android - Android TextView HTML 文本在行尾换行
- android - 读取构建配置文件 build.json 在 cordova 运行 android 时冻结。@Cordova-developers:有什么想法吗?
- c# - xUnitTest 管道执行 Sql on premious
- css - 当屏幕宽度减小时,如何创建媒体查询自由流体宽度以增加?
- c# - 创建一个列表
对象上的类 - java - 如何删除字符串值中的所有前缀数字?
- asp.net - .net core 2.2 无法从请求中检索 JWT 令牌
- string - 字符串中连续 1 的最大数量