作者在 2011-04-12 09:19:07 发布以下内容
#include<iostream>
using namespace std;
double result;
char k;
char putin,put;
struct char0
{
char c;
char0 *next;
};
struct num0
{
double n;
num0 *next;
};
void sum(num0 *x,char0 *y,char);
void dep(num0 *x,char0 *y,char);
void mal(num0 *x,char0 *y,char);
void div(num0 *x,char0 *y,char);
void inchar(char0 *x,char);
void innum(num0 *x,double);
char delback(char0 *x);
double del_back(num0 *x);
void calculate(num0 *x,char0 *y,char);
void choise(num0 *x,char0 *y,char,char);
int main()
{
char0 *C;
num0 *N;
cout<<"--------该程序是对程序员所输入的运算式进行计算并输出结果---!"<<endl;
C=new char0;
C->next=NULL;
N=new num0;
N->next=NULL;
cout<<"请输入你想计算的运算式:"<<endl;
putin=getchar();
while(putin!='=')
{
if(putin=='(')
calculate(N,C,putin);
else
{
innum(N,putin);
switch(putin)
{
case '+': sum(N,C,putin); break;
case '-': dep(N,C,putin); break;
case '*': mal(N,C,putin); break;
case '/': div(N,C,putin); break;
case '=':result=del_back(N); break;
}
}
}
cout<<result;
return 0;
}
void inchar(char0 *x,char y)
{
char0 *p,*q;
p=x;
while(p->next)
p=p->next;
q=new char0;
p->next=q;
q->c=y;
q->next=NULL;
}
void innum(num0 *x,double y)
{
num0 *p,*q;
p=x;
while(p->next)
p=p->next;
q=new num0;
p->next=q;
q->n=y+0.0;
q->next=NULL;
}
char delback(char0 *x)
{
char0 *p,*q;
p=x;
q=p->next;
while(q->next)
{
p=p->next;
q=p->next;
}
p->next=NULL;
return q->c;
}
double del_back(num0 *x)
{
num0 *p,*q;
p=x;
q=p->next;
while(q->next)
{
p=p->next;
q=p->next;
}
p->next=NULL;
return q->n;
}
void calculate(num0 *x,char0 *y,char z)
{
z=getchar();
while(z!=')')
{
innum(x,z+0.0);
z=getchar();
switch(z)
{
case '+': sum(x,y,z); break;
case '-': dep(x,y,z); break;
case '*': mal(x,y,z); break;
case '/': div(x,y,z); break;
case '=':result=del_back(x); break;
}
choise(x,y,delback(y),z);
z=getchar();
}
k=delback(y);
z=getchar();
}
void sum(num0 *x,char0 *y,char z)
{
if(z=='(')
calculate(x,y,z);
else
{
k=z;
z=getchar();
switch(z)
{
case '+': innum(x,del_back(x)+k);sum(x,y,z); break;
case '-': innum(x,del_back(x)+k);dep(x,y,z); break;
case '*': inchar(y,z);innum(x,k); break;
case '/': inchar(y,z);innum(x,k); break;
case '=':result=del_back(x); break;
}
choise(x,y,delback(y),k);
}
}
void dep(num0 *x,char0 *y,char z)
{
if(z=='(')
calculate(x,y,z);
else
{
k=z;
z=getchar();
switch(z)
{
case '+': innum(x,del_back(x)-k);sum(x,y,z); break;
case '-': innum(x,del_back(x)-k);dep(x,y,z); break;
case '*': inchar(y,z);innum(x,k); break;
case '/': inchar(y,z);innum(x,k); break;
case '=':result=del_back(x); break;
}
choise(x,y,delback(y),k);
}
}
void mal(num0 *x,char0 *y,char z)
{
if(z=='(')
calculate(x,y,z);
else
{
k=z;
z=getchar();
switch(z)
{
case '+': innum(x,del_back(x)*k);sum(x,y,z); break;
case '-': innum(x,del_back(x)*k);dep(x,y,z); break;
case '*': inchar(y,z);innum(x,del_back(x)*k); break;
case '/': inchar(y,z);innum(x,del_back(x)*k); break;
case '=':result=del_back(x); break;
}
choise(x,y,delback(y),k);
}
}
void div(num0 *x,char0 *y,char z)
{
if(z=='(')
calculate(x,y,z);
else
{
k=z;
z=getchar();
switch(z)
{
case '+': innum(x,del_back(x)/k);sum(x,y,z); break;
case '-': innum(x,del_back(x)/k);dep(x,y,z); break;
case '*': inchar(y,z);innum(x,del_back(x)/k); break;
case '/': inchar(y,z);innum(x,del_back(x)/k); break;
case '=':result=del_back(x); break;
}
choise(x,y,delback(y),k);
}
}
void choise(num0 *x,char0 *y,char w,char z)
{
if(y->next==NULL)
{
k=z;
z=getchar();
if(z=='=')
{
result=del_back(x);
}
else
choise(x,y,delback(y),k);
}
k=z;
z=getchar();
switch(w)
{
case '(': calculate(x,y,k);
case '+': sum(x,y,z);
case '-': dep(x,y,z);
case '*': mal(x,y,z);
case '/': div(x,y,z);
}
}
using namespace std;
double result;
char k;
char putin,put;
struct char0
{
char c;
char0 *next;
};
struct num0
{
double n;
num0 *next;
};
void sum(num0 *x,char0 *y,char);
void dep(num0 *x,char0 *y,char);
void mal(num0 *x,char0 *y,char);
void div(num0 *x,char0 *y,char);
void inchar(char0 *x,char);
void innum(num0 *x,double);
char delback(char0 *x);
double del_back(num0 *x);
void calculate(num0 *x,char0 *y,char);
void choise(num0 *x,char0 *y,char,char);
int main()
{
char0 *C;
num0 *N;
cout<<"--------该程序是对程序员所输入的运算式进行计算并输出结果---!"<<endl;
C=new char0;
C->next=NULL;
N=new num0;
N->next=NULL;
cout<<"请输入你想计算的运算式:"<<endl;
putin=getchar();
while(putin!='=')
{
if(putin=='(')
calculate(N,C,putin);
else
{
innum(N,putin);
switch(putin)
{
case '+': sum(N,C,putin); break;
case '-': dep(N,C,putin); break;
case '*': mal(N,C,putin); break;
case '/': div(N,C,putin); break;
case '=':result=del_back(N); break;
}
}
}
cout<<result;
return 0;
}
void inchar(char0 *x,char y)
{
char0 *p,*q;
p=x;
while(p->next)
p=p->next;
q=new char0;
p->next=q;
q->c=y;
q->next=NULL;
}
void innum(num0 *x,double y)
{
num0 *p,*q;
p=x;
while(p->next)
p=p->next;
q=new num0;
p->next=q;
q->n=y+0.0;
q->next=NULL;
}
char delback(char0 *x)
{
char0 *p,*q;
p=x;
q=p->next;
while(q->next)
{
p=p->next;
q=p->next;
}
p->next=NULL;
return q->c;
}
double del_back(num0 *x)
{
num0 *p,*q;
p=x;
q=p->next;
while(q->next)
{
p=p->next;
q=p->next;
}
p->next=NULL;
return q->n;
}
void calculate(num0 *x,char0 *y,char z)
{
z=getchar();
while(z!=')')
{
innum(x,z+0.0);
z=getchar();
switch(z)
{
case '+': sum(x,y,z); break;
case '-': dep(x,y,z); break;
case '*': mal(x,y,z); break;
case '/': div(x,y,z); break;
case '=':result=del_back(x); break;
}
choise(x,y,delback(y),z);
z=getchar();
}
k=delback(y);
z=getchar();
}
void sum(num0 *x,char0 *y,char z)
{
if(z=='(')
calculate(x,y,z);
else
{
k=z;
z=getchar();
switch(z)
{
case '+': innum(x,del_back(x)+k);sum(x,y,z); break;
case '-': innum(x,del_back(x)+k);dep(x,y,z); break;
case '*': inchar(y,z);innum(x,k); break;
case '/': inchar(y,z);innum(x,k); break;
case '=':result=del_back(x); break;
}
choise(x,y,delback(y),k);
}
}
void dep(num0 *x,char0 *y,char z)
{
if(z=='(')
calculate(x,y,z);
else
{
k=z;
z=getchar();
switch(z)
{
case '+': innum(x,del_back(x)-k);sum(x,y,z); break;
case '-': innum(x,del_back(x)-k);dep(x,y,z); break;
case '*': inchar(y,z);innum(x,k); break;
case '/': inchar(y,z);innum(x,k); break;
case '=':result=del_back(x); break;
}
choise(x,y,delback(y),k);
}
}
void mal(num0 *x,char0 *y,char z)
{
if(z=='(')
calculate(x,y,z);
else
{
k=z;
z=getchar();
switch(z)
{
case '+': innum(x,del_back(x)*k);sum(x,y,z); break;
case '-': innum(x,del_back(x)*k);dep(x,y,z); break;
case '*': inchar(y,z);innum(x,del_back(x)*k); break;
case '/': inchar(y,z);innum(x,del_back(x)*k); break;
case '=':result=del_back(x); break;
}
choise(x,y,delback(y),k);
}
}
void div(num0 *x,char0 *y,char z)
{
if(z=='(')
calculate(x,y,z);
else
{
k=z;
z=getchar();
switch(z)
{
case '+': innum(x,del_back(x)/k);sum(x,y,z); break;
case '-': innum(x,del_back(x)/k);dep(x,y,z); break;
case '*': inchar(y,z);innum(x,del_back(x)/k); break;
case '/': inchar(y,z);innum(x,del_back(x)/k); break;
case '=':result=del_back(x); break;
}
choise(x,y,delback(y),k);
}
}
void choise(num0 *x,char0 *y,char w,char z)
{
if(y->next==NULL)
{
k=z;
z=getchar();
if(z=='=')
{
result=del_back(x);
}
else
choise(x,y,delback(y),k);
}
k=z;
z=getchar();
switch(w)
{
case '(': calculate(x,y,k);
case '+': sum(x,y,z);
case '-': dep(x,y,z);
case '*': mal(x,y,z);
case '/': div(x,y,z);
}
}
---暂时只能输入整数计算()+-*/--
可以运行,得不到结果