c++课程设计四则运算.doc

{ }

char*destination=pstr; char *source=str1.Getstr(); *(destination+--length+1)=0;

//目的字符串以0 结尾

while(length>=0) //赋值Length个字符

{ *(destination+length)=*(source+length--); }

//从参数指向的地址处从start位

void CStr::midstr(CStr & str1,int start,int length) 置开始取length个字符赋给目的字符串 { }

void CStr::right(CStr & str1,int length) 字符串 { }

int charinstr( char *destination,char char_to_find) 目的字符串中,若在,返回其所在位置,若不再返回0 { int pos=0; }

CStr::str(double value) { char*tempdest=pstr; int a=0; int b=(int)value;

double c=value-b;

int multiplier=1000000000; while(*(destination+pos)!=0) }

return 0;

{ return pos; }

//循环查找

{ if(char_to_find==*(destination+pos++))

//找到,返回其位置

char *source=str1.Getstr(); while(*source!=0){ source++; } char*destination=pstr; source-=length;

//跳至欲赋值的首地址

*(destination+--length+1)=0; char *source=str1.Getstr();

source+=start-1; //开始赋值处的地址 *(destination+--length+1)=0; {

char*destination=pstr;

//目的字符串以0 结尾

while(length>=0) //赋值Length个字符

*(destination+length)=*(source+length--); }

//从参数指向的地址右边取Length个字符赋给目的

//移至字符串尾部

//目的字符串以0 结尾

while(length>=0) //赋值Length个字符

{ *(destination+length)=*(source+length--); }

//判断字符串char_to_find是否在

//将value的值转换为字符串的形式返回,加入浮点数

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) //如果value为整数 {*tempdest=0;}

else //如果value为浮点数 { }

//两个字符串合并,结果在第

*tempdest++='.'; for(a=1;a<=6;a++) { }

*tempdest=0; //赋值字符串最后一位为0

c*=10;int d=(int)c; if(d!=0&&a<=6) {*tempdest++='0'+d;} c-=d;

char*addstrings(char*destination,char*source1,char*source2) 一个参数中

{ char*tempdest=destination;

while(*source1!=0){*(tempdest++)=*(source1++);} while(*source2!=0){*(tempdest++)=*(source2++);} }

double pwr(double a,double b) {

double result=1;

//计算a的b次方

*tempdest=0; //NULL return destination;

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,'.'); //从source字符串中找到小数点

if(z==0) //如果为整数 { }

else //如果为小数,此时指针指向小数点的位置 {

char a1[50]; A1.left(nstr,z-1);

//返回此时nstr指向的地址左边的z-1个字符,即所需浮点数的整

CStr A1(a1);

int multiplier=(int)pwr(10,len(source)-1); while(*source!=0) {

//依次遍历每一个字符

result+=(*(source++)-'0')*multiplier;multiplier/=10;} //将相应字符转化为对应

位数的整数,并依次累加获得所需整数数

return result;

数部分

}

char*nint=A1.Getstr(); while(*nint!=0) {

//将A1的内容赋值给nint指针

int multiplier=(int)pwr(10,len(nint)-1); //定义multiplier等于10的整数位数的乘方

result+=(double)(*(nint++)-'0')*multiplier;multiplier/=10; }

//将浮点数整

数部分字符串转换为数值

char a3[50];

//返回从nstr指向地址的z+1位置开始的

CStr A3(a3);

A3.midstr(nstr,z+1,len(source)-z); }

return result;

char *ndouble=A3.Getstr();

(len(source)-z)个字符,即所需浮点数的小数部分

//将A3的内容赋值给ndouble指针

//依次遍历ndouble指向的每一个字符

for(multiplier=10;*ndouble!=0;ndouble++)

{ result+=((double)(*(ndouble)-'0'))/(double)multiplier; multiplier*=10; } //将相应字符除以对应数位的10的乘方获得该位的小数并依次相加

char*assignstr(char*source,char*destination)//字符串赋值 {

char*tempdest=destination;

while (source!=0){*(tempdest++)=*(source++);}

*tempdest=0; return destination; }

CStr::calculate()

//计算数字算式形式的字符串,将其转换为数字形式并计算出来,然后再将结果转换为字符串的形式,此算式没有括号,遵循的原则是先乘除后加减 {

char*string=pstr;

char buf1[50],buf2[50],buf3[50],buf4[50],buf5[50]; 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

//先找优先级高的运算符,直

CStr nstr(string);

CStr cuf1(buf1),cuf2(buf2),cuf3(buf3),cuf4(buf4),cuf5(buf5);

至将算式中所有的运算符都找出来

取出转换为数字

((*(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

if((*(string+z)=='+')||(*(string+z)=='/')||(*(string+z)=='-')||(*(string+z)=='*')||(*(string+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();

}

//对这两个数字进行计算,结果在result中

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;

}

}

联系客服:779662525#qq.com(#替换为@)