高精度四则运算(万进制)C++

合集下载

高精度运算c++

高精度运算c++
… 18
17
… …
5
4
3
2
1

18
17
… …
5
4
3
2
1
a

0 1
5 4
1
3 4
3 0
8 3 9
7 6
b
0 … 0
9
9
2
7
5
9
la la 最高位a[la]=0,则a的长度-1
lb
高精度运算
高精度运算中需要处理的问题: 1.数据的接收与存储 2.进位、借位处理
加法进位:a[i]=a[i]+b[i];
{将a,b数组对应元素相加的和存入a数组}
图4
i 100 … a … 4
8
7
6
5 20 0 1
4 81 1 6
3 96 6 8
2 0
1 0
47 119 110 0 7 3 9 9
la
g
4g
11g
11g
2g
8g
9g
0g
0
编写精确计算n的阶乘 ( 编写精确计算 的阶乘n!(7<n<50)的程序 的阶乘 )
∵ 50!<5050<10050=(102)50=10100 !可以用 个数组元素a[1],a[2],…,a[100]来存放 一个数组元 来存放,一个数组元 ∴ 50!可以用100个数组元素 个数组元素 来存放 素存放一个数位上的数字。 素存放一个数位上的数字。
5468 0001 4096 75468 1200001 494096 322 5128 2111
la
7 i
120i 120i
49 i

c语言高精度运算模板

c语言高精度运算模板

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)$中。

你可以根据实际需求对模板进行修改和调整,以满足不同的计算需求。

如果你还有其他问题,请随时向我提问。

第1章--高精度计算(C++版)PPT课件

第1章--高精度计算(C++版)PPT课件

2021/6/4
856 × 25 4280 1712 21400
图3
A3A2A1 × B2B1 C’4C’3 C’2 C’1 C”5C”4C”3C”2 C6C 5C4C 3C2C1 图4
11
高精度乘法的参考程序: #include<iostream> #include<cstring> #include<cstdio> using namespace std; int main() {
示例:123456789 ÷45 = 1’ 2345’ 6789 ÷ 45 = 274’ 3484
∵ 1 / 45 = 0 , 1%45=1 ∴ 取12345 / 45 = 274 ∵ 12345 % 45 = 15 ∴ 取156789/45 = 3484 ∴ 答案为2743484, 余数为156789%45 = 9
//按位相除 //删除前导0
2021/6/4
16
实质上,在做两个高精度数运算时候,存储高精度数的 数组元素可以不仅仅只保留一个数字,而采取保留多位数 (例如一个整型或长整型数据等),这样,在做运算(特别 是乘法运算)时,可以减少很多操作次数。例如图5就是采用 4位保存的除法运算,其他运算也类似。具体程序可以修改上 述例题予以解决,程序请读者完成。
图5
2021/6/4
【算法分析】 做除法时,每一次上商的值都在0~9,每次求得的余
数连接以后的若干位得到新的被除数,继续做除法。因此, 在做高精度除法时,要涉及到乘法运算和减法运算,还有移 位处理。当然,为了程序简洁,可以避免高精度除法,用0~ 9次循环减法取代得到商的值。这里,我们讨论一下高精度数 除以单精度数的结果,采取的方法是按位相除法。

高精度算法

高精度算法

高精度算法问题的引入由于计算机运算是有模运算,数据范围的表示有一定限制,如整型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++实现任意长整数的四则运算

计算机C++实现任意长整数的四则运算

计算机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语言版)

d = 0; for(i = k - 1; i >= 0 ; i--) {
d = d * 10 + a[i]; c[i] = d / b; d = d % b; } while(c[k - 1] == 0 && k > 1) k--; printf("商="); for(i = k - 1; i >= 0; i--) printf("%d", c[i]); printf("\n 余数=%d", d); }
/*输入两个高精度数据 */ /*an 纪录 b1 的位数 */ /*bn 纪录 b2 的位数 */ /*判断数组的符号 */
if(b2[0]==45) { bn--; fb=-1;bi=0;}
for (i=0; i<an; i++,ai++) {a1[i]=b1[an-ai]-'0'; printf("%d",a1[i]);}
/*判断最后结果的位数*/
if(fa<0&&q||fa<0) printf("-"); for(i=k-1;i>=0;i--) printf("%d",c[i]);
/*输出结果*/
return;
}
else subtraction(a,b,1);
return;
}
subtraction(int a[],int b[],int q) {
}
c[i]=b[i]-a[i];
} while(!c[k-1]&&k>1) k--;
/*判断最后结果的位数*/

用C语言实现超长整数的加减乘除四则运算

用C语言实现超长整数的加减乘除四则运算
链表结点定义为: struct nodc{ in t node; struct node 3 nex t; } 具体处理步骤如下: ①以字符串形式输入两长整数。 ②将各字符串转换成数字链表, 链表每一结点存 储字符串中的一位数字, 转化后低位在前, 高位在后。 ③将链表 ha、hb 从前至后, 即低位至高位对应结 点相加, 并将结果存至结果链表 hc 对应结点中, 有进位 要保留记入一位, 结果链表 hc 边使用边增加新结点。 如: 输入字符串数据 1234、4567, 生成的链表 ha、 hb 及相加结果 hc 为:
转, 高位在前低位在后, 然后是取被除数的前几位和除 数作循环减法, 不够减时加一位继续减直到被除数结 束, 即化除法为减法。 除法函数中, 循环相减最后剩下 的 s 串为相除余数。
2 程序实现
2. 1 加法 char 3 add (char 3 a, char 3 b)
加法函数, 传入加数、被加数, 返回加和结果。 如传入 ab 字串为 1234、5678, 则返回加和结果为 6912 { in t i= 0, 1a= strlen (a) , lb= strlen (b) , lc= la> lb? la: lb; cha r 3 s= m a lloc (1c+ 2) ; struct node 3 ha = in itlink ( ) , 3 hb = in itlink ( ) , 3 hc = in itlink () , 3 p , 3 q, 3 h; strto link (ha, a) ; strto link (hb, b) ; 将数字字符串 a、b 转化为链表, 低 位在前高位在后 p = ha > ; nex t; q hb > nex t; p、q 分别指向加数、被加数个位结点 w h ile (p ! = NU LL && q! = NU LL ) { h m alloc (sizcof (struct node) ) ; 申请相加和结点 h > data= p = > data+ q > data+ i; 结点相加, i 为进位 i= h > data 10; 取相加进位, 加和大于 10 时为 1, 否 则为 0 h > data= h > data&10; 加和大于 10 时, 和结点只 取个位 h nex t= hc ?nex t; hc > nex t h; 将和结点 h, 插入 和链表 hc p = p > nex t; q q > nex t; 加数、被加数下移一位 } w h ile (p ! = NU LL ) 加数已结束, 被加数还有数据 { h= m a lloc (sizeof (struct node) ) ; h > da ta= p > da ta+ i; i= h > da ta 10; h > da ta= h > da ta% 10; h > nex t= hc > nex t; hc > nex t= h; p = p > nex t; } w h ilc (q! = NU LL ) 被加数已结束, 加数还有数据 { h= m a lloc (sizeof (struct node) ) ; h > da ta= q > da ta+ i; i= h > da ta 10;

C语言简单计算器实现四则运算可带括号

C语言简单计算器实现四则运算可带括号

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语言四则运算计算器的实现。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

压位高精(万进制)//头文件: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;}。

相关文档
最新文档