我编了个逆波兰程序,老出错,求助
#define NUMBER '0'void push( double f );//入栈操作
double pop ( void );//出栈
int getop( char s[],char line[]);//获取元素
#include <stdio.h>
#include<stdlib.h>
#include "exptr.h"
#define MAXOP 100
//从命令行中输入逆波兰表达式实现计算
int main(int argc, char *argv[])
{
int type;
double op2;
char s[MAXOP];
while (*++argv){
type = getop(s,*argv);//从命令行中取操作数
switch(type){
case NUMBER:
push(atof(s));//数字入栈
break;
case '+'://运算符
push( pop() + pop() );
break;
case '*':
push( pop() * pop() );
break;
case '-':
op2 = pop();
push( pop() - op2 );
break;
case '/':
op2 = pop();
if( op2 != 0 )
push( pop()/op2 );
else
printf( "error : zero divisor\n " );
break;
default :
printf( "error : unkown command %s\n", s );
break;
}
}
printf ( "the result is \t%.8g\n", pop() );//命令行结束输出结果
return 0;
}
#include <stdio.h>
#include <ctype.h>
#include "exptr.h"
int getop( char s[],char line[])
{
int i;
char c;
i = 0;
c = line[0];
if ( !isdigit( c ) && c != '.') //不是数
return c;
if( isdigit(c) )//收集整数部分
while( isdigit( s[i++] = c = line[i++] ) )
;
s[i] = '\0';
if( c == '.' )//收集小数部分
while( isdigit( s[i++] = c = line[i++] ) )
;
s[i] = '\0';
return NUMBER;
}
#include <stdio.h>
#include "exptr.h"
#define MAXVAL 100
int sp = 0;
double val[MAXVAL];
void push( double f )//入栈操作
{
if( sp < MAXVAL )
val[sp++] = f;
else
printf( "error : stack full, can't push %g\n", f );
getchar();
}
double pop (void)//出栈操作
{
if ( sp >0 )
return val[--sp];
else
{
printf( "error: stack empty\n" );
return 0.0;
}
}