首页 > 解决方案 > 如果数据类型像 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 intint *(int space star)这样的数据类型出现,那么它也能够打印确切的变量。

标签: ctoken

解决方案


推荐阅读