#include
#include
#include
char pause;
int len(char*source)
{ int retval=0;
while(*(source+retval++)!=0){}
return --retval;
}
class CStr{
private:
int nlen;
char *pstr;
public:
CStr(){};
CStr(char *str){nlen=len(str);pstr=str;}
int Getlen(){return nlen;}
char*Getstr(){return pstr;}
CStr(CStr&str){nlen=str.Getlen();pstr=str.Getstr();}
char * Getpstr(){return pstr;}
void midstr(CStr & str1,int start,int length);
void left(CStr & str1,int length);
void right(CStr & str1,int length);
calculate();
friend int charinstr(char);
double val();
str(double val);
friend istream &operator>>(istream &,CStr &);
int Judge();
};
void CStr::left(CStr & str1,int length)
{ char*destination=pstr;char *source=str1.Getstr();
*(destination+--length+1)=0;
while(length>=0){
*(destination+length)=*(source+length--);
}
}
void CStr::midstr(CStr & str1,int start,int length)
{ char *source=str1.Getstr();
source+=start-1;
char*destination=pstr;
*(destination+--length+1)=0;
while(length>=0){
*(destination+length)=*(source+length--);
}
}
void CStr::right(CStr & str1,int length)
{ char *source=str1.Getstr();
while(*source!=0){
source++;
}
char*destination=pstr;
source-=length;
*(destination+--length+1)=0;
while(length>=0){
*(destination+length)=*(source+length--);
}
}
int charinstr( char *destination,char char_to_find)
{
int pos=0;
while(*(destination+pos)!=0){
if(char_to_find==*(destination+pos++)){
return pos;
}
}
return 0;
}
CStr::str(double value)
{
char*tempdest=pstr;
int a=0;
int b=(int)value;
double c=value-b;
int multiplier=1000000000;
for (multiplier=1000000000;multiplier!=0;multiplier/=10){
*tempdest='0'+(char)(b/multiplier);
b-=(b/multiplier)*multiplier;
if((*tempdest!='0')||(a)){
a++;tempdest++;
}
}
if(c==0){*tempdest=0;}
else{*tempdest++='.';
for(a=1;a<=4;a++)
{c*=10;int d=(int)c;
if(d!=0||a<4){*tempdest++='0'+d;}
c-=d;
}
*tempdest=0;
}
}
char*addstrings(char*destination,char*source1,char*source2)
{ char*tempdest=destination;
while(*source1!=0){*(tempdest++)=*(source1++);}
while(*source2!=0){*(tempdest++)=*(source2++);}
*tempdest=0;
return destination;
}
double pwr(double a,double b)
{ double result=1;
for(int c=1;c<=b;c++){result*=a;}
return result;
}
double CStr::val()
{
char*source=pstr;double result=0;CStr nstr(source);
int z=charinstr(source,'.');
if(z==0){int multiplier=(int)pwr(10,len(source)-1);
while(*source!=0){
result+=(*(source++)-'0')*multiplier;multiplier/=10;
}
return result;
}
else{
char a1[50];
CStr A1(a1);
A1.left(nstr,z-1);
char*nint=A1.Getstr();
int multiplier=(int)pwr(10,len(nint)-1);
while(*nint!=0){
result+=(double)(*(nint++)-'0')*multiplier;multiplier/=10;
}
char a3[50];
CStr A3(a3);
A3.midstr(nstr,z+1,len(source)-z);
char *ndouble=A3.Getstr();
for(multiplier=10;*ndouble!=0;ndouble++){
result+=((double)(*(ndouble)-'0'))/(double)multiplier;
multiplier*=10;
}
}
return result;
}
char*assignstr(char*source,char*destination)
{ char*tempdest=destination;
while (source!=0){*(tempdest++)=*(source++);}
*tempdest=0;
return destination;
}
CStr::calculate()
{
char*string=pstr;
CStr nstr(string);
char buf1[50],buf2[50],buf3[50],buf4[50],buf5[50];
CStr cuf1(buf1),cuf2(buf2),cuf3(buf3),cuf4(buf4),cuf5(buf5);
char opstr[6]="^/*+-";
double leftnr;
double rightnr;
int oppos;
int z;
double result;
for(int pos_in_opstr=0;pos_in_opstr<=4;pos_in_opstr++){
while(charinstr(string,opstr[pos_in_opstr])){
oppos=charinstr(string,opstr[pos_in_opstr]);
for (z=oppos-2;z>=0;z--){
if ((*(string+z)=='+')||(*(string+z)=='/')||(*(string+z)=='-')||(*(string+z)=='*')||(*(string+z)=='^')){
cuf1.midstr(nstr,z+2,oppos-z-2);
leftnr=cuf1.val();
z=-1;
}
else if(z==0){
cuf1.left(nstr,oppos-1);
leftnr=cuf1.val();
}
}
for(z=oppos;z
cuf2.midstr(nstr,oppos+1,z-oppos);
rightnr=cuf2.val();z=len(string);
}
else if(z==len(string)-1){
cuf2.right(nstr,len(string)-oppos);
rightnr=cuf2.val();
}
}
if (opstr[pos_in_opstr]=='+'){result=leftnr+rightnr;}
else if (opstr[pos_in_opstr]=='-'){result=leftnr-rightnr;}
else if (opstr[pos_in_opstr]=='/'){result=leftnr/rightnr;}
else if (opstr[pos_in_opstr]=='*'){result=leftnr*rightnr;}
else if (opstr[pos_in_opstr]=='^'){result=pwr(leftnr,rightnr);}
cuf4.left(nstr,oppos-len(&buf1[0])-1);
cuf5.str(result);
addstrings(&buf3[0],cuf4.pstr,cuf5.pstr);
cuf5.right(nstr,len(string)-oppos-len(&buf2[0]));
addstrings(string,cuf3.pstr,cuf5.pstr);
}
}
cout<<"运行的结果是"<
istream & operator>>(istream&is,CStr&ss)
{ char a[50];
char *x=a;
char *y=ss.pstr;
cin.getline(x,50);
while(*x!=0){*(y++)=*(x++);}
*y=0;
return is;
}
int CStr::Judge()
{
char *p,*s,*r,*source=pstr;
p=source;s=p+1;
while(*p!=0)
{r=p;
if(*p==' '){
while(*s!=0){*p=*s;p++;s++;}
*p=0;p=r;s=p+1;
continue;
}
p=r+1;s=p+1;
}
cout<<"去除空格后的算式:"<
while(*destination!=0)
{if('('<=*destination&&*destination<='+'||'-'<=*destination&&*destination<='/'||'0'<=*destination&&*destination<='9'||*destination=='^'||*destination==' ')
{destination++;}
else{return 0;}
}
destination=pstr;
if(*destination=='^'||*destination=='*'||*destination=='/'||*destination=='+'||*destination=='-')
{ return 0;}
int pos_in_opstr;
int pos;int z;
char opstr[6]="^/*+-";
for(pos_in_opstr=0;pos_in_opstr<=4;pos_in_opstr++)
{
while(charinstr(source,opstr[pos_in_opstr]))
{
pos=charinstr(source,opstr[pos_in_opstr]);z=pos;
if(*(source+z)=='^'||*(source+z)=='*'||*(source+z)=='/'||*(source+z)=='+'||*(source+z)=='-'||*(source+z)==0)
{return 0;}
z-=2;
if(*(source+z)=='^'||*(source+z)=='*'||*(source+z)=='/'||*(source+z)=='+'||*(source+z)=='-'||*(source+z)==0)
{return 0;}
else{source+=pos;}
}
}
return 1;
}
void main()
{ CStr myrecord;
cout<<"\2 欢迎使用四则运算程序 \2 \n";
cout<<"\2 \2 \n";
cout<<"\2 \2 \n";
cout<<"\2 \2 \n";
cout<<"\2 \2 \n";
cout<<"\2 \2 \n";
cout<<"\2 设计者: ** \2 \n";
cout<<"\2 \2 \n";
cout<<"\2 \2 \n";
cout<<"\2 按回车键继续 \2 \n";
cin.get(pause);
system("cls");
int choice=1;
while(choice){
char strn[50],duf1[50],duf2[50],duf3[50],duf4[50],duf5[50];
CStr buf1(duf1),buf2(duf2),buf3(duf3),buf4(duf4),buf5(duf5),origin(strn),oristr(strn);
int z,lastopen;
cout<<"请输入一个算式\n";
operator >>(cin,oristr);
if(oristr.Judge()==0){;
cout<<"输入有误,请重新输入\n";
continue;
}
else{
cout<<"输入的算式是:"<
while(charinstr(&strn[0],'(')){
for(z=0;z<=len(&strn[0]);z++){
if(strn[z]=='('){lastopen=z;}
if(strn[z]==')'){
buf1.midstr(oristr,lastopen+2,z-lastopen-1);
cout<<"在"<
buf2.right(oristr,len(&strn[0])-z-1);
buf1.calculate();
addstrings(&strn[0],addstrings(&duf4[0],buf3.Getstr(),buf1.Getstr()),buf2.Getstr());
cout<<"新的式子是:"<
}
}
}
}
oristr.calculate();
cout<<"计算结果为:"<
cin.get();
}
cout<<"现在退出运算!\n";
}
四则运算原理就是先乘除后加减咯,遇到括号就先处理括号里面的就可以啦!最简单的用入栈出栈原理就可以了!例如:4*8+3*(8-4)=?
先解析式子,从右到左扫描进栈,栈1的序列是)4-8(*3+8*4
然后从栈1开始出栈4,是数字,存入变量a,*,是符号,存入变量b,8是数字,存入变量c,然后判断后面一个符号是否高级于第一个符号,如果是就先做后面再abc,判断b是什么符号,可以先设定b=1代表+,b=2代表-,b=3代表*,b=4代表/,否则直接abc
如果没遇到括号的话而且结果需要再做一次运算,则先做好运算再入栈2
如遇到括号则可以先把括号里面的放进栈3先运算,如果遇到多重括号,可以从栈一出来后分开栈放,然后一个一个算好了,算出了最终结果才写入栈2,最后把栈2的计算出来结果就可以了!
栈其实就是一个数组,只要创建足够多足够大的数组就可以解决问题了!
真没有,C的圣经应该是谭浩强先生的吧
第四条厉害,佩服