简单的算术表达式求助高手帮忙

szj6904 2009-12-15 01:23:17
#include<iostream>
#include<string>
#include<math.h>
using namespace std;
char precede(char a,char b)
{
int i,j;
char com[8][8]={
' ','+','-','*','/','(',')','#',
'+','>','>','<','<','<','>','>',
'-','>','>','<','<','<','>','>',
'*','>','>','>','>','<','>','>',
'/','>','>','>','>','<','>','>',
'(','<','<','<','<','<','=',' ',
')','>','>','>','>',' ','>','>',
'#','<','<','<','<','<',' ','=',
};
for(i=0;i<8;i++)
if(com[i][0]==a)break;
for(j=0;j<8;j++)
if(com[0][j]==b) break;
return com[i][j];
}
int in(char c)
{
if(c=='+'||c=='-'||c=='*'||c=='/'||c=='('||c==')'||c=='#'||c=='.') return 1;
else return 0;
};
float operate(float a,char b,float c){
float d=a;
switch(b){
case'+':
d=a+c;
break;
case'-':
d=a-c;
break;
case'*':
d=a*c;
break;
case'/':
d=a/c;
break;
}
return d;
};
struct sign
{
int top;
char a[200];
};
char pop(sign &b)
{
char d;
if(b.top<0)
{
cout<<"栈空"<<endl;
return '0';
}
else
{
d=b.a[b.top];
b.top--;
return d;
}
};
void push(sign &b,char k)
{
if(b.top>200)
{
cout<<"栈满"<<endl;
}
else
{
b.top++;
b.a[b.top]=k;
}
};
char gettop(sign &b)
{
char e;
if(b.top==-1 ) return 0;
e=b.a[b.top-1];
return e;
}
struct number
{
int top;
float aa[200];
};
float pop1(number &c)
{
float d;
if(c.top<0)
{
cout<<"栈空"<<endl;
return 0;
}
else
{
d=c.aa[c.top];
c.top--;
return d;
}
};
void push1(sign &c,float k)
{
if(c.top>200)
{
cout<<"栈满"<<endl;
}
else
{
c.top++;
c.a[c.top]=k;
}
};
float change(char *l,int &i)
{
float sum=0,num=0.1;
while(!in(l[i]))
{
if(l[i]!='.')
{
sum=sum*10+l[i]-'0';

}
else
{
sum=sum+(l[i]-'0')*num;
num=num*0.1;

}
i++;
}

return sum;
};
float menu()
{
char yy[200];
char tt;
float vv=0;
int j=0;
sign a;
a.top=-1;
number s;
s.top=-1;
cout<<"输入算术表达式(以#结束)"<<endl;
cin>>yy;
push(a,'#');
while(yy[j]!='#')
{
if(!in(yy[j]))
{

push1(s,change(yy,j));

}
else
{
tt=precede(gettop(a),yy[j]);
switch(tt) ))
{

case'<':
push(a,yy[j]);
j++;
break;
case'>':
vv=operate(pop1(s),pop(a),pop1(s));
push1(s,vv);
j++;
break;
case'=':
pop(a);
j++;
break;
}
}
}
}
void main()
{
cout<<menu();
}



...全文
87 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
hlyces 2009-12-15
  • 打赏
  • 举报
回复
經鑒定:

莫名其妙的代碼!

蘭州你根本不會編程嘛!
szj6904 2009-12-15
  • 打赏
  • 举报
回复
--------------------Configuration: 新建 文本文档 - Win32 Debug--------------------
Compiling...
新建 文本文档.cpp
C:\Documents and Settings\Administrator\桌面\最新\新建 文本文档.cpp(120) : warning C4305: 'initializing' : truncation from 'const double' to 'float'
C:\Documents and Settings\Administrator\桌面\最新\新建 文本文档.cpp(157) : error C2664: 'push1' : cannot convert parameter 1 from 'struct number' to 'struct sign &'
A reference that is not to 'const' cannot be bound to a non-lvalue
C:\Documents and Settings\Administrator\桌面\最新\新建 文本文档.cpp(163) : error C2059: syntax error : ')'
C:\Documents and Settings\Administrator\桌面\最新\新建 文本文档.cpp(163) : error C2059: syntax error : ')'
C:\Documents and Settings\Administrator\桌面\最新\新建 文本文档.cpp(172) : error C2664: 'push1' : cannot convert parameter 1 from 'struct number' to 'struct sign &'
A reference that is not to 'const' cannot be bound to a non-lvalue
Error executing cl.exe.

新建 文本文档.exe - 4 error(s), 1 warning(s)
错误是这样的 在线等 要交的作业
szj6904 2009-12-15
  • 打赏
  • 举报
回复
'push1' : cannot convert parameter 1 from 'number' to 'sign &'这是不会 还有括号
cattycat 2009-12-15
  • 打赏
  • 举报
回复

void push1(sign &c,float k)
{
if(c.top>200)
{
cout < <"栈满" < <endl;
}
else
{
c.top++;
c.a[c.top]=k; //这里不能把float转换为char字符的,有错。
}
};
sj13426074890 2009-12-15
  • 打赏
  • 举报
回复


void push1(number &c,float k)
{
if(c.top>200)
{
cout <<"栈满" <<endl;
}
else
{
c.top++;
c.aa[c.top]=k;
}
};


push1是数字进栈
bastenf 2009-12-15
  • 打赏
  • 举报
回复
Error 40 error C2664: 'push1' : cannot convert parameter 1 from 'number' to 'sign &'

这个不好改吗? 好好回去学学....
szj6904 2009-12-15
  • 打赏
  • 举报
回复
有4个错误,不知道怎么改

65,210

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧