怎么在c++中利用字符串实现除法运算

  介绍

怎么在c++中利用字符串实现除法运算?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

<强>项目要点

1。大数指的是远超长长的int的数据

2。将大数用矩阵进行存储,并通过矩阵实现运算

3。本人采用字符串进行存储,应注意char的特点

比如:char=161;

,,,,, cout<& lt; (int);

此时会输出-95年,而不是161字符类型首个比特位是作为正负号的

除法,除法主要采用的还是笔算的思想,需要用到之前博客当中的减法函数-

<强>核心思想:强比如861/21

,,,,,,,,,先用86去减21知道数值小于21时停止也就是2

,,,,,,,,,再添加上下一位,就变成了21

,,,,,,,,,如果这个数大于等于21日,则再用这个数减去它

,,,,,,,,,21-21=0,记录其减的次数,就是结果,该结果只保留了整数部分,余数会在之后的取模运算博客中讲到

,,,,,,,,,其实可以明显看出余数就是减完所有可以减掉的21之后的数,也就是0

运行截图及代码如下:

怎么在c++中利用字符串实现除法运算

# include   # include   # include   using  namespace 性传播疾病;   # define  n  10   string  dezero (string )//用来去掉正数前面的0,也就是说可以输入000001类似这样的数字   {   ,long  int 我;   ,(i=0; i< a.length();我+ +)   ,{   ,如果(a.at (i)在48),打破;   ,}   ,如果(我==a.length ()), return “0”;   ,a.erase (0, 1);   ,return ;   }   int 法官(string  string  b)//判断两个正数的大小   {   ,如果(a.length()在b.length ()), return  1;   ,如果(a.length () & lt; b.length ()), return  1;   ,long  int 我;   ,(i=0; i< a.length();我+ +)   ,{   ,如果(a.at (i)在在(我)),return  1;   ,如果(a.at(我)& lt;在(我)),return  1;   ,}   ,return  0;   }   string  - (string  string  b)//自然数减法(在之前博客中写到过,这里直接挪过来调用了)   {   ,=dezero ();   ,b=dezero (b);   ,long  int  i, j=0;   ,string  c=?”;   ,string  c1, c2;   ,string  d=安澄?;   ,如果(法官(a, b)==0), return  c;   ,如果法官(a, b)==1)   ,{   ,c1=一个;   ,c2=b;   ,}   ,如果法官(a, b)==1)   ,{   ,c1=b;   ,c2=一个;   ,j=1;   ,}   ,反向(c1.begin (), c1.end ());   ,反向(c2.begin (), c2.end ());   ,(i=0; i< c2.length();我+ +)   ,{   ,如果(c2.at (i)在=48,,c2.at(我)& lt;=57), c2.at (i) -=48;   ,如果(c2.at (i)在=97,,c2.at(我)& lt;=122), c2.at (i) -=87;   ,}   ,(i=0; i< c1.length();我+ +)   ,{   ,如果(c1.at (i)在=48,,c1.at(我)& lt;=57), c1.at (i) -=48;   ,如果(c1.at (i)在=97,,c1.at(我)& lt;=122), c1.at (i) -=87;   ,}   ,(i=0; i< c2.length();我+ +)   ,{   ,c1.at (i)=c1.at (i) -c2.at(我);   ,}   ,(i=0; i< c1.length() 1;我+ +)   ,{   ,如果(c1.at(我)& lt; 0)   ,{   ,c1.at(我)+=n;   ,c1.at (i + 1);   ,}   ,}   ,(i=c1.length () 1; i>=0;我——)   ,{   ,如果(c1.at (i)在0),打破;   ,}   ,c1.erase (i + 1, c1.length ());   ,(i=0; i< c1.length();我+ +)   ,{   ,如果(c1.at (i)在=10),c1.at (i) +=87;   ,如果(c1.at(我)& lt; 10), c1.at(我)+=48;   ,}   ,反向(c1.begin (), c1.end ());   ,如果(j==1), c1.insert (0, d);   ,return  c1;   }   string 分裂(string  string  b)//自然数除法   {   ,如果(b.length()==1,,在(0)==48),return “error";   ,long  int  i, j。   ,string  c1, c2, d, e;   ,如果(法官(a, b)==0), return “1“;;   ,如果法官(a, b)==1)   ,{   ,return “0”;   ,}   c1=dezero,(一个);   ,c2=dezero (b);   ,d=?“;   ,e=?“;   ,(i=0; i< c1.length();我+ +)   ,{   ,j=0;   ,d=d + c1.at(我);   ,d=dezero (d);   ,而(法官(d, b)在=0)   ,{   ,d=- (d、b);//调用之前的减法函数减去,在本文中也加了进来   ,d=dezero (d);   ,+ +;   ,}   ,e=e +“0”;   ,e.at (i)=j;   ,}   ,(i=0; i< e.length();我+ +)   ,{   ,如果(e.at (i)在=10),e.at (i) +=87;   ,如果(e.at(我)& lt; 10), e.at(我)+=48;   ,}   ,e=dezero (e);   ,return  e;   }   int  main ()   {   ,string  a、b;   ,而(cout

怎么在c++中利用字符串实现除法运算