免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1551 | 回复: 3
打印 上一主题 下一主题

[C] 大家帮忙看一下这个程序 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-11-13 19:32 |只看该作者 |倒序浏览
20可用积分
这个是k&R 第五章的课后题(第十题)

我自己写的,现在有些问题,有一个bug
/**stack.h**/
  1. void pushnum(double num);
  2. double popnum(void);
  3. int stackcount();
复制代码


/**main.c*/
  1. #include "stack.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. int main(int argc, char *argv[])
  5. {
  6.     int ch;
  7.     int tempch;
  8.     double num;

  9.     while(--argc > 0 && (ch = (*++argv)[0])){
  10.         if(isdigit(ch)){
  11.             num = atof(*argv);
  12.             pushnum(num);
  13.             continue;    /*Continue is also OK!!!Try it later on!*/
  14.         }
  15.         switch(ch){
  16.             case '+' :
  17.                 pushnum(popnum() + popnum());
  18.                 break;
  19.             case '-' :
  20.                 num = popnum();
  21.                 pushnum(popnum() - num);
  22.                 break;
  23.             case '*' :
  24.                 pushnum(popnum() * popnum());
  25.                 break;
  26.             case '/' :
  27.                 num = popnum();
  28.                 if(num != 0.0)
  29.                     pushnum(popnum() / num);
  30.                 else
  31.                     printf("error: zero divisor\n");
  32.                 break;
  33.             default :
  34.                 printf("error: unknown command\n");
  35.                 break;
  36.         }
  37.     }
  38.     if(stackcount() == 1)
  39.         printf("\t Result :%g\n", popnum());
  40.     else
  41.         printf("Error input!\n");
  42.     return 0;
  43. }
复制代码


/**stack.c**/
  1. #include "stack.h"
  2. #include <stdio.h>

  3. #define MAXSIZE 1024
  4. static double mystack[MAXSIZE];
  5. static int ix = 0;


  6. void pushnum(double num)
  7. {
  8.     if(ix < MAXSIZE)
  9.         mystack[ix++] = num;
  10.     else
  11.         printf("The stack is full!\n");
  12. }

  13. double popnum(void)
  14. {
  15.     if(ix != 0)
  16.         return mystack[--ix];
  17.     else{
  18.         printf("The stack is empty!\n");
  19.         return -1;
  20.     }
  21. }

  22. int stackcount()
  23. {
  24.     return ix;
  25. }
复制代码



atrexl@atrexl:5_10$ ./expr 4 2 /
     Result :2
atrexl@atrexl:5_10$ ./expr 4 2 + 2 /
     Result :3
atrexl@atrexl:5_10$ ./expr 4 2 + 2 *
error: unknown command
error: unknown command
error: unknown command
error: unknown command
error: unknown command
error: unknown command
error: unknown command
Error input!
atrexl@atrexl:5_10$ ./expr 4 2 *
error: unknown command
error: unknown command
error: unknown command
error: unknown command
error: unknown command
error: unknown command
error: unknown command
Error input!

目前就乘法不可以,我用gdb调试,发现当应该读 * 的时候,它直接进入defaul,因为他读到的不是 * ,而是 'e' ,然后 'm'等
而且不知道为什么,假设我调试  
$ gdb expr

(gdb) r 4 2 *
Starting program: /home/atrexl/TCPL/chapter5/5_10/expr 4 2 *

Breakpoint 1, main (argc=10, argv=0xbfb0f054) at main.c:10
10        while(--argc > 0 && (ch = (*++argv)[0])){

argc 为什么会是10呢????为什么呢? 按道理是4才对吧???
然后4压栈,2 压栈,当要读 *的时候,它就连续读入 101(也就是字符e),109(也就是字符m),109,109,115,115,115(也就是字符s)

为什么啊????

谁来帮我解决下这个问题!
谢谢了

最佳答案

查看完整内容

大部份SHELL会将*解释为通配符,在调用程序时会扩展为相应的通配项.使用"*"代替*试试

论坛徽章:
2
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:56:11
2 [报告]
发表于 2008-11-13 19:32 |只看该作者
大部份SHELL会将*解释为通配符,在调用程序时会扩展为相应的通配项.
使用"*"代替*试试

论坛徽章:
0
3 [报告]
发表于 2008-11-13 19:53 |只看该作者
shell给你把*解析了,用"*"就好了

论坛徽章:
0
4 [报告]
发表于 2008-11-13 20:17 |只看该作者
原来是这么回事儿,呵呵!

谢谢两位了
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP