C program, Stack Calculator
有人能帮我看一下下面的代码吗?我已经花了好几个小时试图解决这个问题,但我不知道出了什么问题。这是一个用C语言编写的程序,它应该接受堆栈计算器的操作,并存储数学表达式的操作数。执行操作时,将删除堆栈上最后两个值并将其用作操作数,然后将操作结果放在堆栈上。但是,我得不到正确的数字。请看一下我的密码。我知道很长,但我很感激。谢谢。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 | #include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> #define SIZE 10 #define MAXINPUT 255 void printStack(int stack[], int tos) { if (isEmpty(tos)) { printf("Stack is empty "); printf("--------------------------------------- "); return; } printf("Stack:"); while (tos < SIZE) { printf("[%d]" , stack[tos]); tos++; } printf(" --------------------------------------- "); } int top (int stack[], int tos) { if(isEmpty(tos)) return; return stack [tos]; } int isEmpty(int tos) { if (tos < 0) return 1; } int isFull (int tos) { if(tos >= SIZE - 1) return 1; } void push(int val, int stack [], int *tos) { if(isFull(*tos)) return; (*tos)++; stack[*tos] = val; } int pop (int stack [], int *tos) { if(isEmpty(*tos)) return; int val = stack[*tos]; (*tos)--; return val; } void clear(int *tos) { *tos = -1; } int getInput (char *input) { printf("+------------------------------{Choose an option}------------------------------+ "); printf("| (q) : quit the program. | " "| (integer value) : an integer value (either positive or negative) to push | " "| (c) : clear the stack | " "| (=) : display top value on the stack | " "| (+) : addition | " "| (-) : subtraction | " "| (*) : multiplication | " "| (/) : division - integer division only | " "| (%) : modulus - remainder from an integer division | " "| (^) : exponentiation (x raised to the power of y) | " "+------------------------------------------------------------------------------+ "); printf("Input:"); gets(input); if(strcmp(input,"q") == 0) { printf("Exiting... "); return 0; } return 1; } int isNum(char *input) { int i; for(i = 0; i < strlen(input); i++) { if(!isdigit(input[i])) return 0; } return 1; } int hasTwo(tos) { if((SIZE - tos) >= 2) return 1; printf(" Stack size is 1, must have 2 or more "); return 0; } void mathOp (char op, int stack[], int *tos) { if(!isEmpty(*tos)) return; if(!hasTwo(*tos)) return; int right = pop(stack, tos); int left = pop(stack, tos); switch(op) { case '+': push((left + right), stack, tos); break; case '-': push((left - right), stack, tos); break; case '*': push((left * right), stack, tos); break; case '/': push((left/right), stack, tos); break; case '%': push((left % right), stack, tos); break; case '^': push(pow(left, right), stack, tos); break; } } int main(int argc, char **argv) { int verbose = 0; int debugMode = 0; if (argc == 2 && argv[1][0] == '-' && argv[1][1] == 'd') { debugMode = 1; if (strcmp("-dv", argv[1]) == 0) { verbose = 1; } } int stack[SIZE]; int tos = -1; char input[MAXINPUT]; while (getInput(input)) { int result = 0; if (strcmp(input,"c") == 0) clear(&tos); else if (strcmp(input,"=") == 0) { result = top(stack, tos); printf("Top of Stack is [%d] ", result); } else if (isNum(input)) push(atoi(input), stack, &tos); else if(strcmp(input,"+") == 0 || strcmp(input,"-") == 0 || strcmp(input,"*") == 0 || strcmp(input,"/") == 0 || strcmp(input,"%") == 0 || strcmp(input,"^") == 0 ) mathOp(input[0], stack, &tos); else printf("Invalid input "); if (debugMode) printStack(stack, tos); } return 0; } |
这段代码中有很多问题。使用
每个需要返回某些内容的函数都必须以返回语句结束。C中没有某些类型的"默认返回值"。
例如:
1 2 3 4 5 6 7 | int isFull (int tos) { if(tos >= SIZE - 1) return 1; return 0; // <-- not full, you probably want to return 0 } |
另外,您需要在帮助文本中使用
编辑以修复所有问题:
那么它就可以工作了。