- 论坛徽章:
- 0
|
20可用积分
这个是k&R 第五章的课后题(第十题)
我自己写的,现在有些问题,有一个bug
/**stack.h**/
- void pushnum(double num);
- double popnum(void);
- int stackcount();
复制代码
/**main.c*/
- #include "stack.h"
- #include <stdio.h>
- #include <stdlib.h>
- int main(int argc, char *argv[])
- {
- int ch;
- int tempch;
- double num;
- while(--argc > 0 && (ch = (*++argv)[0])){
- if(isdigit(ch)){
- num = atof(*argv);
- pushnum(num);
- continue; /*Continue is also OK!!!Try it later on!*/
- }
- switch(ch){
- case '+' :
- pushnum(popnum() + popnum());
- break;
- case '-' :
- num = popnum();
- pushnum(popnum() - num);
- break;
- case '*' :
- pushnum(popnum() * popnum());
- break;
- case '/' :
- num = popnum();
- if(num != 0.0)
- pushnum(popnum() / num);
- else
- printf("error: zero divisor\n");
- break;
- default :
- printf("error: unknown command\n");
- break;
- }
- }
- if(stackcount() == 1)
- printf("\t Result :%g\n", popnum());
- else
- printf("Error input!\n");
- return 0;
- }
复制代码
/**stack.c**/
- #include "stack.h"
- #include <stdio.h>
- #define MAXSIZE 1024
- static double mystack[MAXSIZE];
- static int ix = 0;
- void pushnum(double num)
- {
- if(ix < MAXSIZE)
- mystack[ix++] = num;
- else
- printf("The stack is full!\n");
- }
- double popnum(void)
- {
- if(ix != 0)
- return mystack[--ix];
- else{
- printf("The stack is empty!\n");
- return -1;
- }
- }
- int stackcount()
- {
- return ix;
- }
复制代码
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会将*解释为通配符,在调用程序时会扩展为相应的通配项.使用"*"代替*试试
|