LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
楼主: bmsdr

新手写代码(改过),还希望大虾再看下

[复制链接]
 楼主| 发表于 2005-1-15 10:01:15 | 显示全部楼层
下是我的错误提示

[root@localhost c]# gcc a2c2.c
a2c2.c: In function `main':
a2c2.c:50: parse error at end of input

我的a2c2.c代码只有49行  他说我50行出错是!!!!

上次那位大哥,,,,,我机子很烂只可以跑LINUX亚
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-1-15 10:05:20 | 显示全部楼层
还是上次的将 阿拉伯数字转化成大写中文的代码!

经过上次各位大虾的教悔,
修改了一下,再给大虾看看.

  1. #include <stdio.h>
  2. main()
  3. {
  4. double x,y;
  5. char *ch[]={"1","2","3","4","5","6","7","8","9"};
  6. char *ch1[]={"w","q","b","s"};
  7. char num[2][4];
  8. char snum[3];
  9. long i,n,j,t,o,y1;
  10. printf("input:");
  11. scanf("%f",&x);
  12. n=(long)x;
  13. y=x-n;
  14.                                     [color=Blue]//将整数部分放进数组[/color]
  15. for(i=0;i!=3;i++)                [color=Blue]//这里是将过位数的一个数字,分被方放进数组中:例如12345678[/color]
  16. {for(o=0;o!=4;o++)               [color=Blue]//存放的格式就是[/color]
  17. {                            [color=Blue]//num[0][]={9,8,7,6} num[1][]={5,4,3,2} num[2][]={1,0,0,0}[/color]
  18. num[i][o]=(char)(n%10);           [color=Blue]//我这样作的目的是为了通过num[i][] i 就可以判断是输出 元[/color]
  19. n/=10;                       [color=Blue]//万 亿 同样也可以同过num[][o] o来判断十 百 千[/color]
  20. }
  21. }
  22. [color=Blue]//将小数部分放进数组[/color]
  23. y=y*100+0.5;

  24. y1=(long)y;
  25. for(i=0;y1!=0;i++)
  26. {snum[i]=(char)(y1%10);
  27. y1=y1/10;}
  28.                                     [color=Blue]//判断数组中的数 输出响影的 中文[/color]
  29. for(i=2;i>=0;i--){
  30. for(o=3;o>=0;o--){
  31. if((int)num[i][o]>1 && (int)num[i][o]<9)      [color=Blue]//如果num[][] 是1-9之间就在这处理[/color]
  32. {printf("%s",ch[(int)num[i][o]]);
  33. printf("%s",ch1[o-1]);                         [color=Blue]//输出1-9的中文[/color]
  34. }
  35. else if(num[i][o-1]==0);            [color=Blue]//如果是num[][o]是0 就判断num[][o-1]是否是0 不是就[/color]
  36. else printf("0");                   [color=Blue]//输出0的中文 是就跳过[/color]
  37. if(i==0) printf("yi");                [color=Blue]//判断输出 元 万 亿[/color]
  38. else if(i==1) printf("wan");
  39. else if(i==2) printf("yuan");
  40. }

  41. if(snum[0]+snum[1]>0)
  42. {
  43. if(snum[0]>0) printf("%sjiao",ch[(int)snum[0]]);
  44. else printf("0");
  45. if(snum[1]>0) printf("%sfen",ch[(int)snum[0]]);
  46. }
  47. }

复制代码
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-1-15 10:09:13 | 显示全部楼层
[root@localhost c]# gcc a2c2.c
a2c2.c: In function `main':
a2c2.c:50: parse error at end of input

我只有49行 怎么 50行出错亚~~`

上面的大哥.我的机子很烂,667的CPU跑VC有点慢~~~~只有在LINUX下了
再次谢谢,,你的指点~~~~你让我看到了我的代码有多烂
回复 支持 反对

使用道具 举报

发表于 2005-1-15 10:11:53 | 显示全部楼层
请用[code]保持代码缩进。不然太难看了。
ps:不要重复发贴,同一个问题集中在起讨论,有助于大家了解问题的背景。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-1-15 19:35:56 | 显示全部楼层
不好意思~~~~

我把发新贴当回复来用了~~~
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-1-16 02:41:03 | 显示全部楼层
这是我最新的代码~~~~还请大哥帮忙看下
错误提示是  a2c2.c:50:2: warning: no newline at end of file  //现在没有了在最后一行回车一下就可以了.
现在的运行结果是
[root@localhost c]# ./a.out
input:123456
yuanyuanyuanyuanwanwanwanwanyiyiyi0yi0JAVA_HOME=/usr/java/jdkfen[root@localhost c]#

怎么我的.baseh设置也出来了亚

后面我贴出我的GDB结果来~~~以为刚学使用有很多不明白的
还请大家谅解


  1. #include <stdio.h>                           
  2. main()
  3. {
  4. double x,y;
  5. char *ch[]={"零","一","二","三","四","五","六","七","八","九"};
  6. char *ch1[]={"万","千","百","十"};
  7. char num[3][4];
  8. char snum[2];
  9. long i,n,o,y1;
  10. printf("input:");
  11. scanf("%f",&x);
  12. n=(long)x;
  13. y=x-n;

  14. for(i=0;i!=3;i++)
  15.   {for(o=0;o!=4;o++)
  16.     {
  17.       num[i][o]=(char)(n%10);  
  18.       n/=10;
  19.     }
  20.   }
  21. y=y*100+0.5;

  22. y1=(long)y;
  23. for(i=0;y1!=0;i++)
  24.     {snum[i]=(char)(y1%10);
  25.       y1=y1/10;}
  26.       
  27. for(i=2;i>=0;i--){
  28.     for(o=3;o>=0;o--){
  29.         if((int)num[i][o]>1 && (int)num[i][o]<9)
  30.         {printf("%s",ch[(int)num[i][o]]);
  31.          printf("%s",ch1[o-1]);
  32.         }
  33.         else if((int)num[i][o-1]==0)
  34.         ;
  35.                 else printf("0");
  36.         if(i==0) printf("yi");
  37.         else if(i==1) printf("wan");
  38.                 else if(i==2) printf("yuan");
  39.     }
  40.     }

  41. if((int)snum[0]+(int)snum[1]>0)
  42.   {
  43.         if(snum[0]>0) printf("%sjiao",ch[(int)snum[0]]);
  44.                       else printf("0");
  45.            if(snum[1]>0) printf("%sfen",ch[(int)snum[1]]);
  46.   }     
  47. }
复制代码
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-1-16 02:49:03 | 显示全部楼层
第一次用GDB用的不好,请大家谅解

  1. [root@localhost c]# gdb a2c
  2. GNU gdb Red Hat Linux (5.3post-0.20021129.18rh)
  3. Copyright 2003 Free Software Foundation, Inc.
  4. GDB is free software, covered by the GNU General Public License, and you are
  5. welcome to change it and/or distribute copies of it under certain conditions.
  6. Type "show copying" to see the conditions.
  7. There is absolutely no warranty for GDB.  Type "show warranty" for details.
  8. This GDB was configured as "i386-redhat-linux-gnu"...
  9. (gdb) l
  10. 1       #include <stdio.h>
  11. 2       main()
  12. 3       {
  13. 4       double x,y;
  14. 5       char *ch[]={"零","一","二","三","四","五","六","七","八","九"};
  15. 6       char *ch1[]={"万","千","百","十"};
  16. 7       char num[3][4];
  17. 8       char snum[2];
  18. 9       long i,n,o,y1;
  19. 10      printf("input:");
  20. (gdb) l
  21. 11      scanf("%f",&x);
  22. 12      n=(long)x;
  23. 13      y=x-n;
  24. 14
  25. 15      for(i=0;i!=3;i++)
  26. 16        {for(o=0;o!=4;o++)
  27. 17          {
  28. 18            num[i][o]=(char)(n%10);
  29. 19            n/=10;
  30. 20          }
  31. (gdb) break 11
  32. Breakpoint 1 at 0x80483b0: file a2c2.c, line 11.
  33. (gdb) break 12
  34. Breakpoint 2 at 0x80483c4: file a2c2.c, line 12.
  35. (gdb) break 13
  36. Breakpoint 3 at 0x80483ef: file a2c2.c, line 13.
  37. (gdb) break 18
  38. Breakpoint 4 at 0x8048421: file a2c2.c, line 18.
  39. (gdb) l
  40. 21        }
  41. 22      y=y*100+0.5;
  42. 23
  43. 24      y1=(long)y;
  44. 25      for(i=0;y1!=0;i++)
  45. 26          {snum[i]=(char)(y1%10);
  46. 27            y1=y1/10;}
  47. 28
  48. 29      for(i=2;i>=0;i--){
  49. 30          for(o=3;o>=0;o--){
  50. (gdb) break 24
  51. Breakpoint 5 at 0x804848d: file a2c2.c, line 24.
  52. (gdb) l
  53. 31              if((int)num[i][o]>1 && (int)num[i][o]<9)
  54. 32              {printf("%s",ch[(int)num[i][o]]);
  55. 33               printf("%s",ch1[o-1]);
  56. 34              }
  57. 35              else if((int)num[i][o-1]==0)
  58. 36              ;
  59. 37                      else printf("0");
  60. 38              if(i==0) printf("yi");
  61. 39              else if(i==1) printf("wan");
  62. 40                      else if(i==2) printf("yuan");
  63. (gdb) break 32
  64. Breakpoint 6 at 0x8048564: file a2c2.c, line 32.
  65. (gdb) break 33
  66. Breakpoint 7 at 0x804858f: file a2c2.c, line 33.
  67. (gdb) l
  68. 41          }
  69. 42          }
  70. 43
  71. 44      if((int)snum[0]+(int)snum[1]>0)
  72. 45        {
  73. 46              if(snum[0]>0) printf("%sjiao",ch[(int)snum[0]]);
  74. 47                      else printf("0");
  75. 48              if(snum[1]>0) printf("%sfen",ch[(int)snum[1]]);
  76. 49        }
  77. 50      }  
  78. (gdb) r                       [color=DarkRed]前面我设置好了断点,地址是我以为出错的地方[/color]
  79. Starting program: /home/c/a2c

  80. Breakpoint 1, main () at a2c2.c:11
  81. 11      scanf("%f",&x);
  82. (gdb) n
  83. input:12345      [color=DarkRed]输入数字[/color]

  84. Breakpoint 2, main () at a2c2.c:12
  85. 12      n=(long)x;
  86. (gdb) p x           [color=DarkRed]在这我发现X值不对亚[/color]
  87. $1 = 4.8527425639341028e-270
  88. (gdb) p n           [color=DarkRed]N也不对亚[/color]
  89. $2 = 869
  90. (gdb) n

  91. Breakpoint 3, main () at a2c2.c:13
  92. 13      y=x-n;
  93. (gdb) p y                  [color=DarkRed]在这我发现什么值都不对[/color]
  94. $3 = 20443369604.764618
  95. (gdb) n
  96. 15      for(i=0;i!=3;i++)
  97. (gdb) n
  98. 16        {for(o=0;o!=4;o++)
  99. (gdb) n

  100. Breakpoint 4, main () at a2c2.c:18
  101. 18            num[i][o]=(char)(n%10);
  102. (gdb) p num              [color=DarkRed]我查看NUM数组,里面全国是乱骂[/color]
  103. $4 = {"\024\n\023B", "柱??, "ろ??}
  104. (gdb) l
  105. 13      y=x-n;
  106. 14
  107. 15      for(i=0;i!=3;i++)
  108. 16        {for(o=0;o!=4;o++)
  109. 17          {
  110. 18            num[i][o]=(char)(n%10);
  111. 19            n/=10;
  112. 20          }
  113. 21        }
  114. 22      y=y*100+0.5;
  115. (gdb) n
  116. 19            n/=10;
  117. (gdb) p n
  118. $5 = 0
  119. (gdb) n
  120. 16        {for(o=0;o!=4;o++)
  121. (gdb) n

  122. Breakpoint 4, main () at a2c2.c:18
  123. 18            num[i][o]=(char)(n%10);
  124. (gdb) quit    [color=DarkRed]没勇气再看了[/color]
  125. The program is running.  Exit anyway? (y or n) y
复制代码
回复 支持 反对

使用道具 举报

发表于 2005-1-16 08:42:24 | 显示全部楼层
算法不对,在计算机中十进制数不能精确表示,所以用除10的办法,会永远就余数。转换下来的结果也是错误的。
要先把浮点数转换成字符串,然后再作处理。
这里有链接,你参考一下别人的代码吧。
http://bbs.chinaunix.net/forum/v ... 1%D0%B4%D7%AA%BB%BB
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-1-16 16:46:23 | 显示全部楼层
原来要先把浮点数转换成字符串,然后再作处理。

多谢指点````
回复 支持 反对

使用道具 举报

发表于 2005-1-16 19:23:58 | 显示全部楼层
注意代码的格式,该缩进的地方就缩进,该空格的地方就空格。。。
不过代码看起来很吃力的。。
回复 支持 反对

使用道具 举报

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

本版积分规则

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