鱼C论坛

 找回密码
 立即注册
查看: 1589|回复: 3

[技术交流] 程序有问题,大神看看

[复制链接]
发表于 2015-4-1 13:03:26 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
这个程序没有实际意义,目的在于删除程序中自带的一些注释和宏定义,但是编译会报错,请看看哪里不对。
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>               

  4. #define debug(fmt,args,...)                fprintf(stderr,fmt,##args)

  5. int get_input_type(char * word)
  6. {
  7.         if(strcmp(word," ") == 0)
  8.                 return 0;
  9.                
  10.         if(strcmp(word,"\t") == 0)
  11.                 return 0;
  12.                
  13.         if(strcmp(word,"#") == 0)
  14.                 return 1;
  15.                
  16.         if(strcmp(word,"define") == 0)
  17.                 return 2;
  18.                
  19.         if(strcmp(word,"\n") == 0)
  20.                 return 4;

  21.         return 3;
  22. }

  23. char c;
  24. char word[64];
  25. char word_buf[64];
  26. char buf[128];

  27. struct macro
  28. {
  29.         char name[64];
  30.         char value[64];
  31. } macros[16];

  32. int macro_counter = 0;

  33. void act_print_word(void)
  34. {
  35.         int i = 0;
  36.         for(i=0;i<macro_counter;i++)
  37.         {
  38.                 if(strcmp(word,macros[i].name) == 0)
  39.                 {
  40.                         printf("%s",macros[i].value);
  41.                         return;
  42.                 }
  43.                         
  44.                 break;
  45.         }
  46.         
  47.         if(i == macro_counter)
  48.                 printf("%s",word);
  49.         
  50.         return;
  51. }

  52. void act_save_to_buf(void)
  53. {
  54.         strcat(buf,word);
  55.         
  56.         return;
  57. }

  58. void act_print_buf_and_word(void)
  59. {
  60.         printf("%s",buf);
  61.         printf("%s",word);
  62.         
  63.         return;
  64. }

  65. void act_save_word(void)
  66. {
  67.         strcat(word_buf,word);
  68.         
  69.         return;
  70. }


  71. void act_get_macro_name(void)
  72. {
  73.         printf("name = <%s>\n",word_buf);
  74.         
  75.         strcpy(macros[macro_counter].name,word_buf);
  76.         strcpy(word_buf,"");

  77.         return;
  78. }

  79. void act_get_macro_value(void)
  80. {
  81.         printf("value = <%s>\n",word_buf);                        
  82.         
  83.         strcpy(macros[macro_counter].value,word_buf);
  84.         strcpy(word_buf,"");
  85.         
  86.         macro_counter++;
  87.         
  88.         return;
  89. }

  90. void act_null(void)
  91. {
  92.         return;
  93. }

  94. enum {s0 = 0,s1,s2,s3,s4,s5,s6};
  95. int state_transition[7][5] =
  96.                 {
  97.                         s0,                s1,                s0,                s0,                s0,

  98.                         s1,                s0,                s2,                s0,                s0,
  99.                         
  100.                         s3,                s0,                s0,                s0,                s0,
  101.                         
  102.                         s3,                s4,                s4,                s4,                s0,
  103.                         
  104.                         s5,                s5,                s4,                s4,                s0,
  105.                
  106.                         s5,                s6,                s6,                s6,                s0,
  107.                         
  108.                         s6,                s6,                s6,                s6,                s0,
  109.                 };

  110. #define a0 act_print_word
  111. #define a1 act_save_to_buf
  112. #define a2 act_print_buf_and_word
  113. #define a3 act_save_word
  114. #define a4 act_get_macro_name
  115. #define a5 act_get_macro_value
  116. #define a6 act_null

  117. #if 1        
  118. typedef void (*PF)(void);
  119. PF act_table[7][5] =
  120. {
  121.         a0,                a1,                a0,                a0,                a0,
  122.         a1,                a2,                a1,                a2,                a2,
  123.         a1,                a2,                a2,                a2,                a2,
  124.         a1,                a3,                a3,                a3,                a2,
  125.         a4,                a4,                a3,                a3,                a5,
  126.         a6,                a3,                a3,                a3,                a5,               
  127.         a3,                a3,                a3,                a3,                a5,
  128. };
  129. #endif

  130. void getword(char * word)
  131. {
  132.         char c;
  133.         
  134.         c = getchar();
  135.         
  136.         if(c == EOF)
  137.         {
  138.                 *word = '\0';
  139.                 return;
  140.         }
  141.         
  142.         // if c is not a alpha
  143.         if(isalpha(c) == 0) // or  if(!isalpha(c))
  144.         {
  145.                 *word = c;
  146.                 word ++;
  147.                 *word = '\0';
  148.                 return;
  149.         }
  150.         
  151.         do
  152.         {
  153.                 *word++ = c;
  154.                 c = getchar();
  155.         }while(isalnum(c) || c == '_');
  156.         
  157.         // current c is $ or anything else
  158.         ungetc(c,stdin);
  159.         *word = '\0';
  160.         
  161.         return;
  162. }
  163.         
  164. int main(void)
  165. {        
  166.         int state = 0;
  167.         
  168.         while(1)
  169.         {
  170.                
  171.                 int input = 0;
  172.                 void (*pf)(void);
  173.                 //char word[64];
  174.                
  175.                 //c = getchar();
  176.                 //input = get_input_type(c);
  177.                 getword(word);
  178.                 input = get_input_type(word);
  179.                
  180.                 //printf("c = %c,input = %d\n",c,input);  it's debug sentence.
  181.                
  182.                 //if(c == EOF)   
  183.                 //        break;
  184.                 if(strcmp(word,"") == 0)
  185.                         break;
  186.                
  187.                 pf = act_table[state][input];
  188.                 pf();
  189.                
  190.                 state = state_transition[state][input];
  191.                
  192.                 debug("word = <%s>,input = %d,state = %d\n",word,input,state);
  193.         }

  194.         return 0;
  195. }
复制代码


小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2015-4-5 20:29:49 | 显示全部楼层
五天了,没人懂?还是没人理。这人缘~~~~~:cry
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-4-5 20:46:34 | 显示全部楼层
本帖最后由 haiouda 于 2015-4-5 20:50 编辑

我是菜鸟,看着有点乱;
哥们,给你提点意见(可能是我水平实在太差):
哥们,要不你就把你这程序是怎么实现的,说一说(思路);
再不,你就多加些注释,这样一来,也好读懂;
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-4-6 06:53:08 | 显示全部楼层
本帖最后由 xxzj01 于 2015-4-6 11:17 编辑
haiouda 发表于 2015-4-5 20:46
我是菜鸟,看着有点乱;
哥们,给你提点意见(可能是我水平实在太差):
哥们,要不你就把你这程序是怎么 ...

这个程序没有实际意义,目的在于删除程序中自带的一些注释和宏定义.程序来自李明新概念C语言(git-hub).报错如下:pasting "," and "word" does not give a valid preprocessing token
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-12 19:44

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表