高精度四则运算(万进制)C++
- 格式:docx
- 大小:15.70 KB
- 文档页数:12
c语言高精度运算模板
好的,下面为你提供一个C语言高精度运算模板,包括高精度加法、减法和乘法:
高精度加法:直接对位相加,当某位超过10直接取模运算,然后下一位直接对加相应多出的数即可。
高精度减法:当输入的$ss1$小于$ss2$时,需要交换两者的顺序,并率先输出一个负号。
然后执行循环,在循环中如果$a(i)<b(i)$,则直接向下一位借位,然后本位加10,$a(i)$加10。
高精度乘法:使用双重循环,$c$下标对应关系为$i+j-1$($i=j=1$开始)。
在循环中,将$a(i)$和$b(j)$相乘后的结果累加到$c(i+j-1)$中。
你可以根据实际需求对模板进行修改和调整,以满足不同的计算需求。
如果你还有其他问题,请随时向我提问。
高精度算法问题的引入由于计算机运算是有模运算,数据范围的表示有一定限制,如整型int(C++中int 与long相同)表达范围是(-2^31~2^31-1),unsigned long(无符号整数)是(0~2^32-1),都约为几十亿.如果采用实数型,则能保存最大的double只能提供15~16位的有效数字,即只能精确表达数百万亿的数。
因此,在计算位数超过十几位的数时,不能采用现有类型,只能自己编程计算。
目前在青少年信息学奥林匹克竞赛中所涉及到的高精度计算包括加(addition)、减(subtract)、乘(multiply)、除(divide)四种基本运算。
此外,在C++语言中,int类型(4个字节/32位计算机)元素存储十进制的一位数字非常浪费空间,并且运算量也非常大,因此常将程序代码优化为万进制,即数组的每个元素存储高精数字的四位。
(为什么选择万进制,而不选择更大的进制呢?十万进制中的最大值99999相乘时得到的值是9999800001超过4个字节的存储范围而溢出,从而导致程序计算错误。
)本文以暂时以10进制为例讲述高精度算法一、高精度数字的存储高精度计算通用方法:高精度计算时一般用一个数组来存储一个数,数组的一个元素对应于数的一位(当然,在以后的学习中为了加快计算速度,也可用数组的一个元素表示数的多位数字,暂时不讲),表示时,由于数计算时可能要进位,因此为了方便,将数由低位到高位依次存在数组下标对应由低到高位置上,另外,我们申请数组大小时,一般考虑了最大的情况,在很多情况下,表示有富余,即高位有很多0,可能造成无效的运算和判断,因此,我们一般将数组的第0个下标对应位置来存储该数的位数.如数:3485(三千四百八十五),表达在数组a[10]上情况是:下标0 1 2 3 4 5 6 7 8 9内容 4 5 8 4 3 0 0 0 0 0说明:位数个位十位百位千位例:一个不超过200位的非负整数,可能有多余的前导0。
计算机C++实现任意长整数的四则运算一、什么是任意长整数任意长整数,也称作大整数或无限长整数,可以表示出任意长度的整数,是由多个整数构成的有限序列。
它的最大特征是其位数不受限制,可以用来表示任意大小的整数。
二、任意长整数四则运算1、四则运算任意长整数四则运算是指对任意长整数进行加、减、乘、除四种基本运算的操作。
2、C++实现任意长整数的四则运算(1)首先要明确,任意长整数是由多个整数构成的有限序列,所以要想实现四则运算,必须将单个整数进行相应的计算。
(2)因此,可以采用逐位计算的方法来实现任意长整数的四则运算。
具体的步骤如下:(a)以字符串的形式表示任意长整数,并转换成整型数组,每个元素代表任意长整数中的一位数字;(b)从数组的末尾开始,依次取出每一位数字,根据相应的运算符进行计算;(c)将计算结果存入到一个新的数组中;(d)最后,把新数组中的元素按照从小到大的顺序组合成一个新的字符串,这就是任意长整数的四则运算的结果。
三、C++实现任意长整数的四则运算的算法(1)定义函数原型:string Cal(stringstr1,string str2,char op);(2)申请内存空间:int *arr1 = newint[str1.length()]; int *arr2 = newint[str2.length()]; int *res = newint[max(str1.length(),str2.length())];(3)将字符串转化为整型数组:for(int i=0;i <str1.length();i++) arr1[i] = str1[i] - '0'; for(int j=0;j < str2.length();j++) arr2[j] = str2[j] - '0';(4)根据所传入的运算符,进行相应的运算:switch (op) {case '+': //加法运算break; case '-': //减法运算break; case '*': //乘法运算break; case '/': //除法运算break;}(5)将计算结果存入到新的数组中:for(intk=0;k<max(str1.length(),str2.length());k++) res[k] = add[k];(6)将计算结果的数组转换成字符串:string result=""; for(intl=0;l<max(str1.length(),str2.length());l++) result += to_string(res[l]);(7)返回计算结果return result;(8)释放内存空间delete[] arr1; delete[] arr2; delete[] res;四、总结任意长整数四则运算是指对任意长整数进行加、减、乘、除四种基本运算的操作。
C语言简单计算器实现四则运算可带括号```c#include <stdio.h>#include <stdlib.h>int priority(char op)if (op == '+' , op == '-')return 1;else if (op == '*' , op == '/')return 2;else if (op == '(')return 0;elsereturn -1;void calculate(char op, int *numStack, int *top, char *opStack, int *opTop)int num2 = numStack[(*top)--];int num1 = numStack[(*top)--];switch (op)case '+':numStack[++(*top)] = num1 + num2; break;case '-':numStack[++(*top)] = num1 - num2; break;case '*':numStack[++(*top)] = num1 * num2; break;case '/':numStack[++(*top)] = num1 / num2; break;}int eval(char *expr)int numStack[100];char opStack[100];int numTop = -1;int opTop = -1;int num = 0;int sign = 1;while (*expr != '\0')if (*expr >= '0' && *expr <= '9')num = num * 10 + (*expr - '0');} else if (*expr == '+' , *expr == '-')numStack[++numTop] = num * sign;num = 0;sign = (*expr == '+') ? 1 : -1;} else if (*expr == '*' , *expr == '/')while (opTop >= 0 && priority(*expr) <=priority(opStack[opTop]))calculate(opStack[opTop--], numStack, &numTop, opStack, &opTop);}opStack[++opTop] = *expr;} else if (*expr == '(')opStack[++opTop] = '(';} else if (*expr == ')')while (opStack[opTop] != '(')calculate(opStack[opTop--], numStack, &numTop, opStack, &opTop);}opTop--;}expr++;}numStack[++numTop] = num * sign;while (opTop >= 0)calculate(opStack[opTop--], numStack, &numTop, opStack, &opTop);}return numStack[0];int maichar expr[100];printf("请输入表达式:");scanf("%s", expr);int result = eval(expr);printf("计算结果:%d\n", result);return 0;```以上是一个简单的C语言四则运算计算器的实现。
C语⾔实现⼤数四则运算⼀、简介众所周知,C语⾔中INT类型是有限制,不能进⾏超过其范围的运算,⽽如果采⽤float类型进⾏运算,由于float在内存中特殊的存储形式,⼜失去了计算的进度。
要解决整个问题,⼀种解决⽅法是通过字符串数组实现数据的存储,然后实现它们之间四则运算的函数。
⼆、数据结构为了实现字符数组之间的运算,要考虑数值的正负性,数字的长度以及具体存储的数字typedef struct num{int len; //数值长度char symbol; //数字正负形int number[LEN]; //数组}NUM,*SNUM;三、函数整个程序使⽤了⼀下的函数SNUM expToNum(char exp[]);//将输⼊字符串转换为对应结构体void reverse(int a[],int len);//数组逆序int compareAbs(SNUM left,SNUM right);//⽐较两数绝对值⼤⼩SNUM anti_add(SNUM left,SNUM right);//元加法SNUM anti_sub(SNUM left,SNUM right);//元减法SNUM add(SNUM left,SNUM right); //加法SNUM sub(SNUM left,SNUM right); //减法SNUM multiply(SNUM left,SNUM right); //乘法SNUM divide(SNUM left,SNUM right); //除法SNUM mod(SNUM left,SNUM right);//求摸运算函数的定义1 SNUM multiply(SNUM left,SNUM right){2//left作为被乘数,right作为乘数3 SNUM mul = (struct num*)malloc(sizeof(struct num));4int i,j;5for(i=0;i<LEN;i++){6 mul->number[i]=0;7 }89if(left->symbol==right->symbol){10 mul->symbol='+';11 }else{12 mul->symbol='-';13 }14151617for(i=0;i<right->len;i++){18for(j=0;j<left->len;j++){19 mul->number[i+j]+=left->number[j]*right->number[i];20 }21 }22232425////进位化简26int len = left->len+right->len-1; //长度2728//29for(i=0;i<len;i++){30 mul->number[i+1]+=mul->number[i]/10;31 mul->number[i]%=10;3233if(i==len-1){34if(mul->number[i+1]!=0){ //还存在⾼位35 len++;36 }else{ //进位完毕,退出37break;38 }39 }40 }4145for(i=len-1;i>=0;i--){46if(mul->number[i]==0){47 len--;48 }else{49break;50 }51 }52if(len==0){53 len=1;54 }5556 mul->len=len;5758free(left);59free(right);60return mul;61 }62636465//减⼀个数等于加上⼀个数的相反数66 SNUM sub(SNUM left,SNUM right){67 right->symbol=(right->symbol=='+'?'-':'+');68return add(left,right);69 }7071//⽐较两数绝对值⼤⼩72int compareAbs(SNUM left,SNUM right){73if(left->len>right->len){ //left的位数更多74return1;75 }else if(left->len<right->len){ //right的位数更多76return -1;77 }else{78int i=left->len-1;79while(i>=0){ //从⾼位开始⽐较80if(left->number[i]>right->number[i]){81return1;82 }83if(left->number[i]<right->number[i]){84return -1;85 }86 i--;87 }88return0; //两者绝对值相等89 }90 }919293 SNUM expToNum(char exp[]){9495 SNUM temp=(struct num*)malloc(sizeof(struct num)); 9697int locan=0;98//确定正负号99if(exp[0]=='+'||exp[0]=='-'){100 temp->symbol=exp[0];101 locan++;102 }else{103 temp->symbol='+';104 }105106//输⼊到数组107int count=0;108while(exp[locan]!='\0'){109 temp->number[count]=exp[locan]-'0';110 locan++;111 count++;112 }113114int i=count;115for(i=count;i<LEN-1;i++){116 temp->number[i]=0;117 }118119 temp->len=count;120121122//数组逆序从个位开始计算123 reverse(temp->number,temp->len);124125return temp;129void reverse(int a[],int len){130int i,temp;131for(i=0;i<len/2;i++){132 temp = a[i];133 a[i] = a[len-1-i];134 a[len-1-i] = temp;135 }136 }137138139140//元加法,假设left和right都为正数或0141 SNUM anti_add(SNUM left,SNUM right){142int i=0;143144while(i<left->len||i<right->len){145int sum=0;146 sum=left->number[i]+right->number[i]; 147if(sum>=10){148 left->number[i]=sum%10;149 left->number[i+1]+=sum/10; //进位150 }else{151 left->number[i]=sum; //不进位152 }153154 i++;155 }156157if(left->number[i]!=0){158 i+=1;159 }160161 left->len=i;162return left;163 }164165//实现正数或负数的加法166 SNUM add(SNUM left,SNUM right){167 SNUM temp;168if(left->symbol==right->symbol){169 temp = anti_add(left,right);170 }else{171if(compareAbs(left,right)>=0){172 temp = anti_sub(left,right);173174 }else{175 temp = anti_sub(right,left);176 }177 }178return temp;179 }180181//元减法,假设left>=right,left和right均为正数或0 182 SNUM anti_sub(SNUM left,SNUM right){183int i=0;184int count=0;185while(i<left->len){186int temp = left->number[i]-right->number[i]; 187if(temp<0){188 left->number[i+1]-=1;189 left->number[i]=temp+10; //退位190 }else{191 left->number[i]=temp;192 }193194 count+=1;195196 i++;197 }198199200201202//舍掉多余的0203for(i=count-1;i>=0;i--){204if(left->number[i]==0){205 count--;206 }else{207break;208 }213 }214215 left->len=count; 216return left;217218 }。
压位高精(万进制)//头文件:thp.h#ifndef _cstring_#define _cstring_#include <cstring>#endif#ifndef _cstdlib_#define _cstdlib_#include <cstdlib>#endifconst int THP_MAXLEN=1000;const int THP_MAXSTRLEN=4040;const int L0=10000;class thp{friend const thp operator-(const thp&,const thp&);friend const thp operator+(const thp&,const thp&);friend const thp operator*(const thp&,const thp&);friend const thp operator/(const thp&,const thp&);friend void thpdiv(const thp&,const thp&,thp&,thp&);friend const thp operator%(const thp&,const thp&);public:const thp& operator=(const thp&);const thp& operator=(const char*);bool operator==(const thp&)const;bool operator>=(const thp&)const;bool operator<=(const thp&)const;inline bool operator!=(const thp&)const;bool operator>(const thp&)const;bool operator<(const thp&)const;inline void shl(const int);const thp& operator ++ ();inline void makeempty();const char *tostr()const;inline thp& operator-=(const thp&);thp();thp(const char*);const int thpstd();protected:int _data[THP_MAXLEN],_high,_sign;private:};// 源代码 thp.cpp#include "thp.h"const int thp::thpstd(){ int i;for(i=0;i<=_high;i++){ while(_data[i]<0){ _data[i]+=L0;_data[i+1]--;}}return(0);}const thp operator % (const thp &a,const thp &b) { thp re1,re2;thpdiv(a,b,re1,re2);return(re2);}const thp& thp::operator ++ (){ _data[0]++;if(_data[0]>=L0)this->thpstd();return(*this);}void thp::shl(const int n){ int i;for(i=_high;i>=0;i--){ _data[i+n]=_data[i];}_high+=n;if(_data[_high]==0)_high=0;}thp& thp::operator -= (const thp &o){ int tint,i,len;for(i=0;i<=_high;i++){ tint=_data[i]-o._data[i];if(tint<0){ tint+=L0;_data[i+1]--;}_data[i]=tint;}for(len=this->_high;len>=0;len--){ if(_data[len]!=0)break;if(len==0)break;}_high=len;return(*this);}void thpdiv(const thp &a,const thp &b,thp &c,thp &d) { int i;d.makeempty();c.makeempty();for(i=a._high;i>=0;i--){ d.shl(1);d._data[0]=a._data[i];while(b<=d){ d-=b;c._data[i]++;}if(i==0)break;}c._high=0;for(i=a._high;i>0;i--){ if(c._data[i]!=0){ c._high=i;break;}}}inline bool thp::operator != (const thp &o)const{ return(!(0==memcmp(this,&o,sizeof(thp))));}bool thp::operator == (const thp &o)const{ return(0==memcmp(this,&o,sizeof(thp)));}inline bool thp::operator < (const thp &o)const{ int i;if(*this==o)return(false);{ if(this->_high!=o._high)return(this->_high<o._high);for(i=this->_high;i>=0;i--){ if(this->_data[i]!=o._data[i])return(this->_data[i]<o._data[i]);if(i==0)break;}}return(false);}bool thp::operator > (const thp &o)const{ if(*this==o)return(false);return(!(*this<o));}bool thp::operator <= (const thp &o)const{ return(*this<o||*this==o);}bool thp::operator >= (const thp &o)const{ return(!(*this<o));}inline int max(int a,int b){ return(a>b?a:b);}thp::thp(){ makeempty();}thp::thp(const char *s){ *this=s;}const thp operator-(const thp &a,const thp &b){ int i,len,tint;thp re;len=max(a._high,b._high);for(i=0;i<=len;i++){ tint=a._data[i]-b._data[i]+re._data[i];if(tint<0){ tint+=L0;re._data[i+1]--;}re._data[i]=tint;}while(len>0&&re._data[len]<1)len--;re._high=len;return(re);}const thp operator/(const thp &a,const thp &b){ thp re1,re2;thpdiv(a,b,re1,re2);return(re1);}const thp operator*(const thp &a,const thp &b){ int i,j,tint,len;thp re;for(i=0;i<=a._high;i++)for(j=0;j<=b._high;j++){ tint=a._data[i]*b._data[j]+re._data[i+j];re._data[i+j]=tint%L0;re._data[i+j+1]+=tint/L0;}len=i+j;if(re._data[len+1]!=0)len++;re._high=len;return(re);}const thp operator+(const thp &a,const thp &b){ int i,len,tint;thp re;len=max(a._high,b._high);for(i=0;i<=len;i++){ tint=a._data[i]+b._data[i]+re._data[i];re._data[i]=tint%L0;re._data[i+1]=tint/L0;}if(re._data[len+1]!=0)len++;re._high=len;return(re);}const char *thp::tostr()const{ char *re;char ts[5];int i,j,tint;re=(char *)malloc(THP_MAXSTRLEN);memset(ts,0,sizeof(ts));memset(re,0,THP_MAXSTRLEN);for(i=_high;i>=0;i--){ tint=_data[i];for(j=0;j<4;j++){ ts[3-j]=tint%10+'0';tint/=10;}strcat(re,ts);}while(strlen(re)>1&&*re=='0')re++;return(re);}inline void thp::makeempty(){ memset(this,0,sizeof(thp));}const thp& thp::operator = (const char *s) { char ts[5];memset(ts,0,sizeof(ts));int len,i;makeempty();len=strlen(s);_high=(len-1)/4;i=(len-1)%4+1;strncpy(ts,s,i);s+=i;_data[_high]=atoi(ts);i=_high;while(i>0){ i--;memset(ts,0,sizeof(ts));strncpy(ts,s,4);s+=4;_data[i]=atoi(ts);}return(*this);}const thp& thp::operator = (const thp &hp) { memcpy(this,&hp,sizeof(thp));return(*this);//测试代码cpp.cpp#ifndef _iostream_#define _iostream_#include <iostream>#endif#ifndef _ctime_#define _ctime_#include <ctime>#endif#ifndef _conio_h_#define _conio_h_#include <conio.h>#endif#include "thp.h"using std::cout;using std::endl;int main(){ clock_t t1=clock();thpa="123123122344444444444234444444444444411222222333333333333333333333 333333333333333333333\ 342342333333333333333333333333333333333333333333333333333333333333333 3333333333333333333333\235234534563452342342344444987724352345234234234000000000000000000000 00000000000000000000000000\ 666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666 6\ 342342333333333333333333333333333333333333333333333333333333333333333 3333333333333333333333\ 235234534563452342342344444987724352345234234234000000000000000000000 00000000000000000000000000\ 666666666666666666666666666666666666666666666666666666666666666666666 666666666666666666666666666666666666666666666666666666666666666666666 6\3423423333333333333333333333333333333333333333333333333333333333333 333333333333333333333333\ 235234534563452342342344444987724352345234234234000000000000000000000 00000000000000000000000000\ 666666666666666666666666666666666666666666666666666666666666666666666 666666666666666666666666666666666666666666666666666666666666666666666 6\3423423333333333333333333333333333333333333333333333333333333333333 333333333333333333333333\ 235234534563452342342344444987724352345234234234000000000000000000000 00000000000000000000000000\ 666666666666666666666666666666666666666666666666666666666666666666666 666666666666666666666666666666666666666666666666666666666666666666666 6\3423423333333333333333333333333333333333333333333333333333333333333 333333333333333333333333\ 235234534563452342342344444987724352345234234234000000000000000000000 00000000000000000000000000\ 666666666666666666666666666666666666666666666666666666666666666666666 666666666666666666666666666666666666666666666666666666666666666666666 6\ 444444444444444444444444444444444444444444444443423452234234342";thp b="23423433444444444444444444444444444444444423423423423423\ 342342333333333333333333333333333333333333333333333333333333333333333 3333333333333333333333\ 235234534563452342342344444987724352345234234234000000000000000000000 00000000000000000000000000\ 666666666666666666666666666666666666666666666666666666666666666666666 666666666666666666666666666666666666666666666666666666666666666666666 6\3333333333323423";thp c=a/b;thp d=c*b;thp e=d+a%b;cout<<e.tostr()<<endl;cout<<"Run time="<<(clock()-t1)<<"ms"<<endl;getch();return 0;}。
c语言高精度加法
C语言高精度加法是一种实现两个大数相加的算法,它可以处理任意长度的数据。
该算法的核心思想是将大数的每一位数分别相加,然后将运算结果存入结果数组中。
首先,我们需要两个指针,分别指向两个大数的最后一位(也就是低位),然后逐位相加,运算结果存储在结果数组中。
每次相加时,需要考虑进位情况,并将进位情况保存。
进位可能会在多次相加时累积,因此在每次相加时,需要先将进位加到当前位上,再进行相加,最后将相加得到的结果加上进位,得到最终结果。
当两个大数长度不相等时,我们需要把长的一个大数继续加到结果数组中,直到所有位都加完为止,最后将结果数组中的元素按逆序排序,即为最终结果。
C语言高精度加法算法可以准确而快速地实现大数相加,可以满足现实生活中大数相加的需求,被广泛应用于计算机科学中。
压位高精(万进制)//头文件:thp.h#ifndef _cstring_#define _cstring_#include <cstring>#endif#ifndef _cstdlib_#define _cstdlib_#include <cstdlib>#endifconst int THP_MAXLEN=1000;const int THP_MAXSTRLEN=4040;const int L0=10000;class thp{friend const thp operator-(const thp&,const thp&);friend const thp operator+(const thp&,const thp&);friend const thp operator*(const thp&,const thp&);friend const thp operator/(const thp&,const thp&);friend void thpdiv(const thp&,const thp&,thp&,thp&);friend const thp operator%(const thp&,const thp&);public:const thp& operator=(const thp&);const thp& operator=(const char*);bool operator==(const thp&)const;bool operator>=(const thp&)const;bool operator<=(const thp&)const;inline bool operator!=(const thp&)const;bool operator>(const thp&)const;bool operator<(const thp&)const;inline void shl(const int);const thp& operator ++ ();inline void makeempty();const char *tostr()const;inline thp& operator-=(const thp&);thp();thp(const char*);const int thpstd();protected:int _data[THP_MAXLEN],_high,_sign;private:};// 源代码 thp.cpp#include "thp.h"const int thp::thpstd(){ int i;for(i=0;i<=_high;i++){ while(_data[i]<0){ _data[i]+=L0;_data[i+1]--;}}return(0);}const thp operator % (const thp &a,const thp &b) { thp re1,re2;thpdiv(a,b,re1,re2);return(re2);}const thp& thp::operator ++ (){ _data[0]++;if(_data[0]>=L0)this->thpstd();return(*this);}void thp::shl(const int n){ int i;for(i=_high;i>=0;i--){ _data[i+n]=_data[i];}_high+=n;if(_data[_high]==0)_high=0;}thp& thp::operator -= (const thp &o){ int tint,i,len;for(i=0;i<=_high;i++){ tint=_data[i]-o._data[i];if(tint<0){ tint+=L0;_data[i+1]--;}_data[i]=tint;}for(len=this->_high;len>=0;len--){ if(_data[len]!=0)break;if(len==0)break;}_high=len;return(*this);}void thpdiv(const thp &a,const thp &b,thp &c,thp &d) { int i;d.makeempty();c.makeempty();for(i=a._high;i>=0;i--){ d.shl(1);d._data[0]=a._data[i];while(b<=d){ d-=b;c._data[i]++;}if(i==0)break;}c._high=0;for(i=a._high;i>0;i--){ if(c._data[i]!=0){ c._high=i;break;}}}inline bool thp::operator != (const thp &o)const{ return(!(0==memcmp(this,&o,sizeof(thp))));}bool thp::operator == (const thp &o)const{ return(0==memcmp(this,&o,sizeof(thp)));}inline bool thp::operator < (const thp &o)const{ int i;if(*this==o)return(false);{ if(this->_high!=o._high)return(this->_high<o._high);for(i=this->_high;i>=0;i--){ if(this->_data[i]!=o._data[i])return(this->_data[i]<o._data[i]);if(i==0)break;}}return(false);}bool thp::operator > (const thp &o)const{ if(*this==o)return(false);return(!(*this<o));}bool thp::operator <= (const thp &o)const{ return(*this<o||*this==o);}bool thp::operator >= (const thp &o)const{ return(!(*this<o));}inline int max(int a,int b){ return(a>b?a:b);}thp::thp(){ makeempty();}thp::thp(const char *s){ *this=s;}const thp operator-(const thp &a,const thp &b){ int i,len,tint;thp re;len=max(a._high,b._high);for(i=0;i<=len;i++){ tint=a._data[i]-b._data[i]+re._data[i];if(tint<0){ tint+=L0;re._data[i+1]--;}re._data[i]=tint;}while(len>0&&re._data[len]<1)len--;re._high=len;return(re);}const thp operator/(const thp &a,const thp &b){ thp re1,re2;thpdiv(a,b,re1,re2);return(re1);}const thp operator*(const thp &a,const thp &b){ int i,j,tint,len;thp re;for(i=0;i<=a._high;i++)for(j=0;j<=b._high;j++){ tint=a._data[i]*b._data[j]+re._data[i+j];re._data[i+j]=tint%L0;re._data[i+j+1]+=tint/L0;}len=i+j;if(re._data[len+1]!=0)len++;re._high=len;return(re);}const thp operator+(const thp &a,const thp &b){ int i,len,tint;thp re;len=max(a._high,b._high);for(i=0;i<=len;i++){ tint=a._data[i]+b._data[i]+re._data[i];re._data[i]=tint%L0;re._data[i+1]=tint/L0;}if(re._data[len+1]!=0)len++;re._high=len;return(re);}const char *thp::tostr()const{ char *re;char ts[5];int i,j,tint;re=(char *)malloc(THP_MAXSTRLEN);memset(ts,0,sizeof(ts));memset(re,0,THP_MAXSTRLEN);for(i=_high;i>=0;i--){ tint=_data[i];for(j=0;j<4;j++){ ts[3-j]=tint%10+'0';tint/=10;}strcat(re,ts);}while(strlen(re)>1&&*re=='0')re++;return(re);}inline void thp::makeempty(){ memset(this,0,sizeof(thp));}const thp& thp::operator = (const char *s) { char ts[5];memset(ts,0,sizeof(ts));int len,i;makeempty();len=strlen(s);_high=(len-1)/4;i=(len-1)%4+1;strncpy(ts,s,i);s+=i;_data[_high]=atoi(ts);i=_high;while(i>0){ i--;memset(ts,0,sizeof(ts));strncpy(ts,s,4);s+=4;_data[i]=atoi(ts);}return(*this);}const thp& thp::operator = (const thp &hp) { memcpy(this,&hp,sizeof(thp));return(*this);//测试代码cpp.cpp#ifndef _iostream_#define _iostream_#include <iostream>#endif#ifndef _ctime_#define _ctime_#include <ctime>#endif#ifndef _conio_h_#define _conio_h_#include <conio.h>#endif#include "thp.h"using std::cout;using std::endl;int main(){ clock_t t1=clock();thpa="123123122344444444444234444444444444411222222333333333333333333333 333333333333333333333\ 342342333333333333333333333333333333333333333333333333333333333333333 3333333333333333333333\235234534563452342342344444987724352345234234234000000000000000000000 00000000000000000000000000\ 666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666 6\ 342342333333333333333333333333333333333333333333333333333333333333333 3333333333333333333333\ 235234534563452342342344444987724352345234234234000000000000000000000 00000000000000000000000000\ 666666666666666666666666666666666666666666666666666666666666666666666 666666666666666666666666666666666666666666666666666666666666666666666 6\3423423333333333333333333333333333333333333333333333333333333333333 333333333333333333333333\ 235234534563452342342344444987724352345234234234000000000000000000000 00000000000000000000000000\ 666666666666666666666666666666666666666666666666666666666666666666666 666666666666666666666666666666666666666666666666666666666666666666666 6\3423423333333333333333333333333333333333333333333333333333333333333 333333333333333333333333\ 235234534563452342342344444987724352345234234234000000000000000000000 00000000000000000000000000\ 666666666666666666666666666666666666666666666666666666666666666666666 666666666666666666666666666666666666666666666666666666666666666666666 6\3423423333333333333333333333333333333333333333333333333333333333333 333333333333333333333333\ 235234534563452342342344444987724352345234234234000000000000000000000 00000000000000000000000000\ 666666666666666666666666666666666666666666666666666666666666666666666 666666666666666666666666666666666666666666666666666666666666666666666 6\ 444444444444444444444444444444444444444444444443423452234234342";thp b="23423433444444444444444444444444444444444423423423423423\ 342342333333333333333333333333333333333333333333333333333333333333333 3333333333333333333333\ 235234534563452342342344444987724352345234234234000000000000000000000 00000000000000000000000000\ 666666666666666666666666666666666666666666666666666666666666666666666 666666666666666666666666666666666666666666666666666666666666666666666 6\3333333333323423";thp c=a/b;thp d=c*b;thp e=d+a%b;cout<<e.tostr()<<endl;cout<<"Run time="<<(clock()-t1)<<"ms"<<endl;getch();return 0;}。