主题:在线求助啊!!!急!
写了个四则运算的程序,错误提示error C2601: '+' : local function definitions are illegal。。。。。。。。。麻烦帮忙看下!!!!judge()函数暂时没写。。
#include <iostream.h>
#include <math.h>
#include <stdio.h>
class CStr
{private:
int nLen;
char * pStr;
public:
CStr(){nLen=1;pStr="0";}
CStr(CStr & str); //①拷贝的构造函数
~CStr(){if(pStr) delete []pStr;} //析构函数
void SetLen(){int retval=0;while(*(pStr+retval++)!=0);} // 设置字符串的长度
int GetLen(){return nLen;} //返回字符串的长度
CStr & midstr(CStr &str,int nStart,int nLength); //② 返回指定字符串类从nStart 序号开始nLength 长度的字符串
CStr & Left(CStr & str,int nLength); // ③返回指定字符串类中从左边开始nLength个字符
CStr & Right(CStr & str,int nLength); // ④返回指定字符串类中从右边开始nLength个字符
void Calculate(); //⑤计算该字符串所代表的四则运算的值(内无括号)
int charinstr(char chChar); //判断字符 chChar 是否在字符串中
int Val(); //⑥求字符串代表的数字字符的值
char & GetChar(int i){return *(pStr+i);} //返回字符串中第 i 个字符
CStr Str(int val); //⑦将数值表示成字符串的形式
CStr & operator=(CStr & ); //⑧重载赋值运算符
friend CStr & operator +(CStr & ,CStr & ); //⑨友元 实现字符字符串的加法
operator char *(){return (char *)pStr;} //将字符串类转换成字符数组
friend istream & operator>>(istream &,CStr & ); //⑩重载输入运算符
int Judge(); //判断输入是否合法,滤除空格
void showstr(){cout<<pStr;}
};
long pwr(int a, int b) //计算a^b,即a的b次方
{ long result=1;
for(int c=1;c<=b;c++)
{result*=a;}
return result;
}
int CStr::charinstr(char chChar)
{ int pos=0;
while (*(pStr+pos)!=0)
{if(chChar=*(pStr+pos++))return pos;}
return 0;
}
CStr::CStr(CStr & str) //①
{ while(* str.pStr!=0){*(pStr++)=*(str.pStr++);}
nLen=str.nLen;
}
CStr & CStr::midstr(CStr & str,int nStart,int nlength) //② str 为destination 调用函数的对象为source
{ pStr+=nStart-1;
*(str.pStr+--nlength+1)=0;
while(nlength>=0)
{*(str.pStr+nlength)=*(pStr+nlength--);}
return str;
}
CStr & CStr::Left(CStr & str,int nLength) // ③
{ *(str.pStr+--nLength+1)=0;
while(nLength >=0)
{*(str.pStr+nLength)=*(pStr+nLength--);}
return str;
}
CStr & CStr::Right(CStr & str,int nlength) // ④
{ while(* pStr !=0){pStr++;}
pStr-=nlength;
*(str+--nlength+1)=0;
while(nlength>=0)
{
*(pStr+nlength)=*(pStr+nlength--);
}
return str;
}
void CStr::Calculate() //⑤
{ cout<<"\n Calculate start,parameter is"<<pStr<<".";
CStr Str1,Str2,Str3,Str4,Str5,Str6;
char opstr[6]="^*/+-";
int leftnr;
int rightnr;
int oppos;
int z;
int result;
for(int pos_in_opstr=0;pos_in_opstr<=4;pos_in_opstr++)
{ while(charinstr(opstr[pos_in_opstr]))
{ oppos=charinstr(opstr[pos_in_opstr]);
for(z=oppos-2;z>=0;z--)
{ if((*(pStr+z)=='+')||(*(pStr+z)=='/')||
(*(pStr+z)=='-')||(*(pStr+z)=='*')||
(*(pStr+z)=='+'))
{ midstr(Str1,z+2,oppos-z-2);
leftnr=Str1.Val();
z=-1;
}
else if(z==nLen-1)
{Left(Str1,oppos-1);leftnr=Str1.Val();}
}
for(z=oppos;z<nLen;z++)
{ if((*(pStr+z)=='+')||(*(pStr+z)=='/')||
(*(pStr+z)=='-')||(*(pStr+z)=='*')||
(*(pStr+z)=='+'))
{ midstr(Str2,oppos+1,z-oppos);
rightnr=Str2.Val();
z=nLen;
}
else if(z==nLen-1)
{Right(Str2,nLen-oppos);rightnr=Str2.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=leftnr^rightnr;}
Left(Str4,oppos-Str1.nLen-1);
Str3=Str4+Str5.Str(result);
Right(Str5,nLen-oppos-Str2.nLen);
Str6=Str3+Str5;
pStr=Str6.pStr;nLen=Str6.nLen;
cout<<"\n While scanning for"<<opstr[pos_in_opstr]<<"'s,I found"
<<leftnr<<" "<<opstr[pos_in_opstr]<<" "<<rightnr
<<" ,new string is"<<pStr<<".";
}
}
cout<<"\n Calculation end,returning"<<pStr;
}
int CStr::Val() //⑥
{ int result;
int multiplier=pwr(10,charinstr('0')-2);
while(*pStr !=0)
{result+=(*(pStr++)-'0')*multiplier;multiplier/=10;}
return result;
}
CStr CStr::Str(int val) //⑦
{ CStr tempStr;
int a=0;
int multiplier=1000000000;
for(multiplier=1000000000;multiplier!=0;multiplier/=10)
{*tempStr.pStr='0'+(val/multiplier);
val-=(val/multiplier)*multiplier;
if((*tempStr!='0')||(a))
{a++;tempStr.pStr++;}}
pStr=tempStr.pStr;
nLen=tempStr.nLen;
return tempStr;
}
CStr & CStr::operator=(CStr & str ) //⑧
{ nLen=str.nLen;
char * tempstr=str.pStr;
for(int i=0;i<=nLen;i++)
{pStr=str.pStr++;pStr++;
return str;
}
CStr & operator+(CStr & str1,CStr & str2 ) //⑨
{
CStr tempstr;
while(*str1.pStr!=0) {*(tempstr.pStr)=*(str1.pStr++);tempstr.pStr++;}
while(*str2.pStr!=0) {*(tempstr.pStr)=*(str2.pStr++);tempstr.pStr++;}
while(*tempstr.pStr!=0) {*(str1.pStr)=*(tempstr.pStr++);str1.pStr++;}
tempstr.nLen=str1.nLen+str2.nLen;
return str1;
}
istream & operator>>(istream &,CStr & str ) //⑩
{cin>>*str.pStr;}
int CStr::Judge()
{int a=0;}
int main()
{ CStr str0,str1,str2,str3,str4,str5,str6,str7;
CStr oristr;
char choice=0;
int z,lastopen;
cout<<"\n Little Mathematical Proggy by zhang,11-11-1111"
<<"\n\n Note:this program is foolproof."
<<"\n\n Press q or Q to quit this program!";
while(choice!='q'&&choice!='Q')
{ cout<<"\n\nPlease enter a string:";
cin>>oristr;
oristr.SetLen();
str0=oristr;
cout<<"\nStrating bracket removal loop:";
while(str0.charinstr('('))
{ for(z=0;z<=str0.GetLen();z++)
{ if(str0.GetChar(z)=='(')lastopen=z;
if(str0.GetChar(z)==')')
{cout<<"\n I found "
<<str0.midstr( str1,lastopen+2,z-lastopen-1)
<<"in";
str0.showstr();
cout<<"\n now calling calculation unit.";
str0.Left(str3,lastopen);
str0.midstr( str4,lastopen+2,z-lastopen-1);
str4.Calculate();
str0.Right( str2,str0.GetLen()-z-1);
str1=str3+str4;
str0=str1+str2;
cout<<"\n New string:";
str0.showstr();
z=str0.GetLen()+1;
}
}
}
cout<<"\nBrackets removed,final strings is :";
str0.showstr();
cout<<",calculating...";
str0.Calculate();
if(str0.GetChar(0)==0){str0=str6;}
cout<<"\nDone\n\n";
oristr.showstr();
cout<<"=";
str0.showstr();
cout<<"\n\n Press q or Q to quit this program!";
}
return 0;
}
#include <iostream.h>
#include <math.h>
#include <stdio.h>
class CStr
{private:
int nLen;
char * pStr;
public:
CStr(){nLen=1;pStr="0";}
CStr(CStr & str); //①拷贝的构造函数
~CStr(){if(pStr) delete []pStr;} //析构函数
void SetLen(){int retval=0;while(*(pStr+retval++)!=0);} // 设置字符串的长度
int GetLen(){return nLen;} //返回字符串的长度
CStr & midstr(CStr &str,int nStart,int nLength); //② 返回指定字符串类从nStart 序号开始nLength 长度的字符串
CStr & Left(CStr & str,int nLength); // ③返回指定字符串类中从左边开始nLength个字符
CStr & Right(CStr & str,int nLength); // ④返回指定字符串类中从右边开始nLength个字符
void Calculate(); //⑤计算该字符串所代表的四则运算的值(内无括号)
int charinstr(char chChar); //判断字符 chChar 是否在字符串中
int Val(); //⑥求字符串代表的数字字符的值
char & GetChar(int i){return *(pStr+i);} //返回字符串中第 i 个字符
CStr Str(int val); //⑦将数值表示成字符串的形式
CStr & operator=(CStr & ); //⑧重载赋值运算符
friend CStr & operator +(CStr & ,CStr & ); //⑨友元 实现字符字符串的加法
operator char *(){return (char *)pStr;} //将字符串类转换成字符数组
friend istream & operator>>(istream &,CStr & ); //⑩重载输入运算符
int Judge(); //判断输入是否合法,滤除空格
void showstr(){cout<<pStr;}
};
long pwr(int a, int b) //计算a^b,即a的b次方
{ long result=1;
for(int c=1;c<=b;c++)
{result*=a;}
return result;
}
int CStr::charinstr(char chChar)
{ int pos=0;
while (*(pStr+pos)!=0)
{if(chChar=*(pStr+pos++))return pos;}
return 0;
}
CStr::CStr(CStr & str) //①
{ while(* str.pStr!=0){*(pStr++)=*(str.pStr++);}
nLen=str.nLen;
}
CStr & CStr::midstr(CStr & str,int nStart,int nlength) //② str 为destination 调用函数的对象为source
{ pStr+=nStart-1;
*(str.pStr+--nlength+1)=0;
while(nlength>=0)
{*(str.pStr+nlength)=*(pStr+nlength--);}
return str;
}
CStr & CStr::Left(CStr & str,int nLength) // ③
{ *(str.pStr+--nLength+1)=0;
while(nLength >=0)
{*(str.pStr+nLength)=*(pStr+nLength--);}
return str;
}
CStr & CStr::Right(CStr & str,int nlength) // ④
{ while(* pStr !=0){pStr++;}
pStr-=nlength;
*(str+--nlength+1)=0;
while(nlength>=0)
{
*(pStr+nlength)=*(pStr+nlength--);
}
return str;
}
void CStr::Calculate() //⑤
{ cout<<"\n Calculate start,parameter is"<<pStr<<".";
CStr Str1,Str2,Str3,Str4,Str5,Str6;
char opstr[6]="^*/+-";
int leftnr;
int rightnr;
int oppos;
int z;
int result;
for(int pos_in_opstr=0;pos_in_opstr<=4;pos_in_opstr++)
{ while(charinstr(opstr[pos_in_opstr]))
{ oppos=charinstr(opstr[pos_in_opstr]);
for(z=oppos-2;z>=0;z--)
{ if((*(pStr+z)=='+')||(*(pStr+z)=='/')||
(*(pStr+z)=='-')||(*(pStr+z)=='*')||
(*(pStr+z)=='+'))
{ midstr(Str1,z+2,oppos-z-2);
leftnr=Str1.Val();
z=-1;
}
else if(z==nLen-1)
{Left(Str1,oppos-1);leftnr=Str1.Val();}
}
for(z=oppos;z<nLen;z++)
{ if((*(pStr+z)=='+')||(*(pStr+z)=='/')||
(*(pStr+z)=='-')||(*(pStr+z)=='*')||
(*(pStr+z)=='+'))
{ midstr(Str2,oppos+1,z-oppos);
rightnr=Str2.Val();
z=nLen;
}
else if(z==nLen-1)
{Right(Str2,nLen-oppos);rightnr=Str2.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=leftnr^rightnr;}
Left(Str4,oppos-Str1.nLen-1);
Str3=Str4+Str5.Str(result);
Right(Str5,nLen-oppos-Str2.nLen);
Str6=Str3+Str5;
pStr=Str6.pStr;nLen=Str6.nLen;
cout<<"\n While scanning for"<<opstr[pos_in_opstr]<<"'s,I found"
<<leftnr<<" "<<opstr[pos_in_opstr]<<" "<<rightnr
<<" ,new string is"<<pStr<<".";
}
}
cout<<"\n Calculation end,returning"<<pStr;
}
int CStr::Val() //⑥
{ int result;
int multiplier=pwr(10,charinstr('0')-2);
while(*pStr !=0)
{result+=(*(pStr++)-'0')*multiplier;multiplier/=10;}
return result;
}
CStr CStr::Str(int val) //⑦
{ CStr tempStr;
int a=0;
int multiplier=1000000000;
for(multiplier=1000000000;multiplier!=0;multiplier/=10)
{*tempStr.pStr='0'+(val/multiplier);
val-=(val/multiplier)*multiplier;
if((*tempStr!='0')||(a))
{a++;tempStr.pStr++;}}
pStr=tempStr.pStr;
nLen=tempStr.nLen;
return tempStr;
}
CStr & CStr::operator=(CStr & str ) //⑧
{ nLen=str.nLen;
char * tempstr=str.pStr;
for(int i=0;i<=nLen;i++)
{pStr=str.pStr++;pStr++;
return str;
}
CStr & operator+(CStr & str1,CStr & str2 ) //⑨
{
CStr tempstr;
while(*str1.pStr!=0) {*(tempstr.pStr)=*(str1.pStr++);tempstr.pStr++;}
while(*str2.pStr!=0) {*(tempstr.pStr)=*(str2.pStr++);tempstr.pStr++;}
while(*tempstr.pStr!=0) {*(str1.pStr)=*(tempstr.pStr++);str1.pStr++;}
tempstr.nLen=str1.nLen+str2.nLen;
return str1;
}
istream & operator>>(istream &,CStr & str ) //⑩
{cin>>*str.pStr;}
int CStr::Judge()
{int a=0;}
int main()
{ CStr str0,str1,str2,str3,str4,str5,str6,str7;
CStr oristr;
char choice=0;
int z,lastopen;
cout<<"\n Little Mathematical Proggy by zhang,11-11-1111"
<<"\n\n Note:this program is foolproof."
<<"\n\n Press q or Q to quit this program!";
while(choice!='q'&&choice!='Q')
{ cout<<"\n\nPlease enter a string:";
cin>>oristr;
oristr.SetLen();
str0=oristr;
cout<<"\nStrating bracket removal loop:";
while(str0.charinstr('('))
{ for(z=0;z<=str0.GetLen();z++)
{ if(str0.GetChar(z)=='(')lastopen=z;
if(str0.GetChar(z)==')')
{cout<<"\n I found "
<<str0.midstr( str1,lastopen+2,z-lastopen-1)
<<"in";
str0.showstr();
cout<<"\n now calling calculation unit.";
str0.Left(str3,lastopen);
str0.midstr( str4,lastopen+2,z-lastopen-1);
str4.Calculate();
str0.Right( str2,str0.GetLen()-z-1);
str1=str3+str4;
str0=str1+str2;
cout<<"\n New string:";
str0.showstr();
z=str0.GetLen()+1;
}
}
}
cout<<"\nBrackets removed,final strings is :";
str0.showstr();
cout<<",calculating...";
str0.Calculate();
if(str0.GetChar(0)==0){str0=str6;}
cout<<"\nDone\n\n";
oristr.showstr();
cout<<"=";
str0.showstr();
cout<<"\n\n Press q or Q to quit this program!";
}
return 0;
}