主题:请高手帮我完整这个程序!是求算术表达式的!
#include "stdio.h"
typedef struct
{
int datas[100];
top;
}NUMSTACK;
typedef struct
{
char datas[100];
top;
}OPSTACK;
int jfirst(char oper1,char oper2,char s);
float caculate(float num1,float num2,char oper);
main()
{
OPSTACK op;
NUMSTACK num;
//初始化
op.datas[0]=';';
op.top=1;
num.top=0;
//输入
s=gets();
for (int i=0;i<strlen(s);i++)//字符的扫描
{ if s[i] //如果是数字
num.datas[num.top]=s[i];
num.top++;
//如果是字符
if (jfirst(op.datas[op.top-1],s[i]))
//压栈
{
op.datas[op.top]=s[i];
op.top++;
}
else
{
while !(jfirst(op.datas[op.top-1],s[i]))
{
//弹出操作数
num2=num.datas[num.top-1];
num.top--;
num1=num.datas[num.top-1]
num.top--;
oper=op.datas[op.top-1];
op.top--;
result=caculate(num1,num2,oper);
//压栈
num.datas[num.top]=result;
num.top++;
}
}
}
}
float caculate(float num1,float num2,char oper)
{
switch(oper)
{
case '-': return num1-num2;break;
case '+': return num1+num2;break;
case '*': return num1*num2;break;
case '/': return num1/num2;break;
default: return 0;
}
}
int jfirst(char oper1,char oper2)
{
if (oper1=='*' || oper1=='/' || oper2==';')
return 0;
else
{
if (oper2=='-' || oper2=='+')
return 0;
else
return 1;
}
}
typedef struct
{
int datas[100];
top;
}NUMSTACK;
typedef struct
{
char datas[100];
top;
}OPSTACK;
int jfirst(char oper1,char oper2,char s);
float caculate(float num1,float num2,char oper);
main()
{
OPSTACK op;
NUMSTACK num;
//初始化
op.datas[0]=';';
op.top=1;
num.top=0;
//输入
s=gets();
for (int i=0;i<strlen(s);i++)//字符的扫描
{ if s[i] //如果是数字
num.datas[num.top]=s[i];
num.top++;
//如果是字符
if (jfirst(op.datas[op.top-1],s[i]))
//压栈
{
op.datas[op.top]=s[i];
op.top++;
}
else
{
while !(jfirst(op.datas[op.top-1],s[i]))
{
//弹出操作数
num2=num.datas[num.top-1];
num.top--;
num1=num.datas[num.top-1]
num.top--;
oper=op.datas[op.top-1];
op.top--;
result=caculate(num1,num2,oper);
//压栈
num.datas[num.top]=result;
num.top++;
}
}
}
}
float caculate(float num1,float num2,char oper)
{
switch(oper)
{
case '-': return num1-num2;break;
case '+': return num1+num2;break;
case '*': return num1*num2;break;
case '/': return num1/num2;break;
default: return 0;
}
}
int jfirst(char oper1,char oper2)
{
if (oper1=='*' || oper1=='/' || oper2==';')
return 0;
else
{
if (oper2=='-' || oper2=='+')
return 0;
else
return 1;
}
}