我写的是一个是逆波兰式的计算器,通过命令行方式传参,
当我输入参数时exercise5_1013*
最后一个参数得到的不是‘*’
通过输出argv,显示的是1,3和一些以前编译过的C文件名,各位大牛谁能告诉我这是为什么?

#include"stdio.h"
#include"stdlib.h"
#include"ctype.h"
#defineMAX100

structStack
{
double*base;
inttop;
};
voidinit(structStack*s);
voidpush(structStack*s,doubleval);
doublepop(structStack*s);
intmy_atof(char*str,double*val);
intmain(intargc,char*argv[])
{
intc,i;
doubleval,op;
structStack*S=(structStack*)malloc(sizeof(structStack));
init(S);
if(argc==1)
{
printf("Pleaseinputexpression!\n");
return0;
}
i=1;
for(;i<argc;i++)
{
printf("%s\n",argv[i]);
}
while(i<argc&&(c=my_atof(*(argv+i),&val)))
{
switch(c)
{
case1:
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("Expressionisinvalid!\n");
return0;
}
push(S,pop(S)/op);
break;
default:
break;
}
i++;
}
if(c==0)
{
printf("Expressionisinvalid\n");
}
if(i==argc)
{
printf("%f\n",pop(S));
}
return0;
}
voidinit(structStack*S)
{
S->base=(double*)malloc(MAX*sizeof(double));
S->top=0;
}
voidpush(structStack*S,doubleval)
{
if(S->top>=MAX)
{
printf("Stackisfull!\n");
}
else
{
S->base[S->top++]=val;
}
}
doublepop(structStack*S)
{
if(S->top==0)
{
printf("Stackisempty!\n");
}
else
{
S->top--;
returnS->base[S->top];
}
}
intmy_atof(char*str,double*val)
{
doublepower;
intsign;
while(isspace(*str))
{
str++;
}
if(*str=='*'||*str=='/')
{
str++;
if(*str!='\0')
{

return0;
}
if(*str=='\0')
{
return*(str-1);
}
}
sign=(*str=='-')?-1:1;
if(*str=='-'||*str=='+')
{
str++;
if(!isdigit(*str)&&*str!='\0')
{
return0;
}
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')
{
return0;
}
*val*=sign;
return1;
}

4 个解决方案

#1


在bash下,将*写成\*就可以了

更多相关文章

  1. linux 输入参数利用getopt、struct option、getopt_long、getopt
  2. 使用Bash编写Linux Shell脚本-9. 参数和子壳
  3. linux-参数-argparse模块-(未完待续)
  4. 在PreparedStatement中重用参数?
  5. LINUX下用SHELL脚本执行带输入输出参数的ORACLE存储过程并得到结
  6. C#中操作Oracle时的SQL语句参数的用法
  7. 彻底理解初始化参数SERVICE_NAMES和客户端TNS中的SERVICE_NAME
  8. sql 存储过程参数为空则不作为条件
  9. mysql参数优化辅助工具之tuning-primer.sh

随机推荐

  1. 设计模式之组合模式
  2. 设计模式之工厂模式
  3. 设计模式之命令模式
  4. java网络编程(1)基础知识
  5. 既然硕士毕业也去搞开发,那我还读个喵的研
  6. 深入分析java中的反射机制
  7. java集合系列(7)Stack
  8. Qt on Android,输入法弹出后,界面无法自适
  9. 设计模式之抽象工厂模式
  10. 深入理解java中的泛型机制