📄 count.cpp
字号:
#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
typedef struct{
double number[100];
int top;
}opd;
typedef struct{
char ope;
int x;
}operat;
typedef struct{
operat oper[100];
int top;
}opt;
void push_opd(opd &s,double e){
s.number[++s.top]=e;
}
void push_opt(opt &s,operat e){
s.oper[++s.top]=e;
}
void pop_opd(opd &s,double &e){
e=s.number[s.top--];
}
void pop_opt(opt &s,char &e){
e=s.oper[s.top--].ope;
}
double run(double n,double m,char o){
switch(o){
case '+':
return (n+m);
case '-':
return (n-m);
case '*':
return (n*m);
case '/':
return (n/m);
case '^':
return pow(n,m);
}
return 0;
}
int main(){
opd opdn;
opt optr;
char num[100],c,o,b='y';
int i;
double g;
operat y;
double m,n;
while (b=='y'){
cout<<"请输入多项式:"<<endl;
opdn.top=-1;
optr.top=0;
optr.oper[0].ope='#';
optr.oper[0].x=-1;
c=getchar();
for(;c!='#';){
num[0]='\0';
for(i=0;;i++){
if(c!='+'&&c!='-'&&c!='*'&&c!='/'&&c!='('&&c!=')'&&c!='#'&&c!='^'){
num[i]=c;
c=getchar();
continue;
}
else
num[i]='\0';
break;
}
if(num[0]!='\0'){
g=atof(num);
push_opd(opdn,g);
}
y.ope=c;
if(c=='+'||c=='-')
y.x=1;
if(c=='*'||c=='/')
y.x=2;
if(c=='#')
y.x=-1;
if(c==')')
y.x=0;
if(c=='(')
y.x=5;
if(c=='^')
y.x=3;
for(;;){
if(y.x>optr.oper[optr.top].x||(optr.oper[optr.top].x==5&&y.x!=0)){
push_opt(optr,y);
c=getchar();
break;
}
if(y.x<=optr.oper[optr.top].x&&y.x-optr.oper[optr.top].x!=-5&&optr.oper[optr.top].ope!='#'){
pop_opt(optr,o);
pop_opd(opdn,m);
pop_opd(opdn,n);
push_opd(opdn,run(n,m,o));
continue;
}
if(y.x-optr.oper[optr.top].x==-5){
pop_opt(optr,o);
c=getchar();
y.ope=c;
if(c=='+'||c=='-')
y.x=1;
if(c=='*'||c=='/')
y.x=2;
if(c=='#')
y.x=-1;
if(c==')')
y.x=0;
if(c=='(')
y.x=5;
if(c=='^')
y.x=3;
if(c!='#'&&c!=')'){
push_opt(optr,y);
c=getchar();
break;
}
else if(c==')')
continue;
}
if(optr.oper[optr.top].ope=='#')
break;
}
}
cout<<"结果为:"<<opdn.number[opdn.top]<<endl;
do{
cout<<"是否继续进行(y/n):";
cin>> b;
}while(b!='y'&&b!='n');
}
return(0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -