linux 下命令行传参数问题
我写的是一个是逆波兰式的计算器,通过命令行方式传参,
当我输入参数时 exercise5_10 1 3 *
最后一个参数得到的不是‘*’
通过输出argv, 显示的是1,3 和一些以前编译过的C文件名, 各位大牛谁能告诉我这是为什么?
#include "stdio.h"#include "stdlib.h"#include "ctype.h"#define MAX 100struct Stack { double *base; int top;};void init (struct Stack *s);void push (struct Stack *s, double val);double pop (struct Stack *s);int my_atof (char *str, double *val);int main(int argc, char *argv[]){ int c, i; double val, op; struct Stack *S = (struct Stack *)malloc (sizeof (struct Stack)); init (S); if (argc == 1) { printf ("Please input expression!\n"); return 0; } i = 1; for (; i < argc; i ++) { printf ("%s\n", argv[i]); } while (i < argc && (c = my_atof (*(argv + i), &val)) ) { switch (c) { case 1: push (S, val); break; case '+': push (S, pop (S) + pop (S)); break; case '-': op = pop (S); push (S, pop (S) - op); break; case '*': push (S, pop (S) * pop (S)); break; case '/': op = pop (S); if (op == 0) { printf ("Expression is invalid!\n"); return 0; } push (S, pop (S) / op); break; default : break; } i ++; } if (c == 0) { printf ("Expression is invalid\n"); } if (i == argc) { printf ("%f\n", pop (S)); } return 0;}void init (struct Stack *S){ S->base = (double *)malloc (MAX * sizeof (double)); S->top = 0;}void push (struct Stack *S, double val){ if (S->top >= MAX) { printf ("Stack is full !\n"); } else { S->base[S->top ++] = val; }}double pop (struct Stack *S){ if (S->top == 0) { printf ("Stack is empty!\n"); } else { S->top --; return S->base[S->top]; }}int my_atof (char *str, double *val){ double power; int sign; while (isspace (*str)) { str ++; } if (*str == '*' || *str == '/') { str ++; if (*str != '\0') { return 0; } if (*str == '\0') { return *(str - 1); } } sign = (*str == '-') ? -1 : 1; if (*str == '-' || *str == '+') { str ++; if (! isdigit (*str) && *str != '\0') { return 0; } if (*str == '\0') { return *(str - 1); } } *val = 0; while (isdigit (*str)) { *val = *val * 10 + *str - '0'; str ++; } if (*str == '.') { power = 1; while (isdigit (*++str)) { *val = *val * 10 + *str - '0'; power *= 10; str ++; } *val /= power; } if (*str != '\0') { return 0; } *val *= sign; return 1;}