写一个计算器,计算长整数的加法和减法 [英] writting a calculator of the addition and subtraction of the long int

查看:84
本文介绍了写一个计算器,计算长整数的加法和减法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码有很多错误,我不知道该怎么办,请帮助,这非常紧急

my code has many bugs, I don''t know how to do,please help,and it''s very urgent

#include<iostream>
using namespace std;
struct node
{int data;node *next,*previous;};
void creat(node *&head)
{node *s,*p;
 char num[100];
 cin>>num;
 int i=0;
 s=new node;
 s->data=num[i]-48;
 while(num[i]!='\0')
 { if(head==NULL) head=s;
   else {p->next=s;s->previous=p;}
 i++; 
 p=s;
 s=new node;
 s->data=num[i]-48;
 }
 p->next=NULL;
 head->previous=NULL;
 delete s;
 return;
}
void plus(node *head1,node *head2)
{int number[100];
 int q=1,p=1,m=0,i;
 while(head1->next!=NULL) {head1=head1->next;p++;}
 while(head2->next!=NULL) {head2=head2->next;q++;}
if(p==q)
{for(i=0;p>0;i++,p--,q--)
 {number[i]= head1->data+head2->data+m;
    if(number[i]>9) m=1,number[i]-=10;
  else m=0;
head1=head1->previous;
head2=head2->previous;
}

if(m==1) number[i]=1,i++;
}
else if(p>q)
{for(i=0;q>0;i++,q--,p--)
 {number[i]= head1->data+head2->data+m;
    if(number[i]>9) m=1,number[i]-=10;
  else m=0;
head1=head1->previous;
head2=head2->previous;}
	while(p>0) 
{number[i]=head1->data+m;
 if(number[i]>9) m=1,number[i]-=10;
 else m=0;
 i++;
 p--;
 head1=head1->previous;}
	if(m==1) number[i]=1;i++;
}
       else
{for(i=0;p>0;i++,p--,q--)
 {number[i]= head1->data+head2->data+m;
    if(number[i]>9) m=1,number[i]-=10;
  else m=0;
head1=head1->previous;
head2=head2->previous;}
while(q>0)
{number[i]=head2->data+m;
if(number[i]>9) m=1,number[i]-=10;
else m=0;
i++;
q--;
head2=head2->previous;}
if(m==1) number[i]=1,i++;
}
for(i=i-1;i>=0;i--)
{cout<<number[i];}
cout<<endl;
}
void minus(node *head1,node *head2)
{int number[100];
 int q=1,p=1,m=0,i,a,b,z;
 node *head;
 while(head1->next!=NULL) {head1=head1->next;p++;}
 while(head2->next!=NULL) {head2=head2->next;q++;}
if(p>q)
{for(i=0;q>0;q--,p--,i++)
{if((head1->data-m)>=head2->data) number[i]=head1->data-head2->data-m;
else number[i]=head1->data+10-head2->data-m,m=1;
head1=head1->previous;
head2=head2->previous;
}
for( ;p>0;p--,i++)
	{if(number[i]=head1->data-m<0) number[i]=head1->data-m,m=0;
else number[i]=head1->data+10-m;m=1;
	 head1=head1->previous;}
i=i-1;
for( ;i>=0;i--) 
{cout<<number[i];}
cout<<endl;
}
else if(p<q)>
{for(i=0;p>0;q--,p--,i++)
{if((head2->data-m)>=head1->data) number[i]=head2->data-head1->data-m;
else {number[i]=head2->data+10-head1->data-m;m=1;}
head1=head1->previous;
head2=head2->previous;}
for( ;q>0;q--,i++)
{if(number[i]=head2->data-m<0) number[i]=head2->data-m,m=0;
else number[i]=head2->data+10-m;m=1;
	 head2=head2->previous;}
i=i-1;
cout<<'-';
for( ;i>=0;i--) 
{cout<<number[i];}
cout<<endl;
}
else
{while(head1->previous!=NULL){head1=head1->previous;}
 while(head1->previous!=NULL) {head2=head2->previous;}
for( ;head1->next!=NULL;head1=head1->next,head2=head2->next)
{a=head1->data;
 b=head2->data;
 if(b>a) {head=head1;head1=head2;head2=head;z=0;break;}
 if(a>b) {z=1;break;}
 z=1;
}
while(head1->next!=NULL) {head1=head1->next;}
while(head2->next!=NULL) {head2=head2->next;}
for(i=0;q>0;q--,i++)
{if((head1->data-m)>=head2->data) number[i]=head1->data-head2->data-m;
else {number[i]=head1->data-head2->data-m+10;m=1;}
head1=head1->previous;
head2=head2->previous;
}
i=i-1;
while(number[i]==0) {i--;}
if(i==-1) cout<<'0';
else 
{if(z==0) cout<<'-';
for( ;i>=0;i--) 
{cout<<number[i];}}
cout<<endl;
}

}

int main()
{node *head1=NULL,*head2=NULL;
creat(head1);
creat(head2);
minus(head1,head2);
}

推荐答案

1.您需要说明错误在哪里以及它们会导致什么问题.
2.除您以外的任何人都不是紧急的.
1. You need to explain where the bugs are and what problems they cause.
2. It''s not urgent to anyone except you.


根据我对您代码的了解,您正在尝试编写一些对每位数字都进行无限精度算术运算的东西作为链接列表中的节点.

这是您的代码的大问题-它太大而又毛茸茸.它需要它的双腿劈开并刮胡子.扔掉已有的内容,返回要尝试实现的算法,使用标准库(cincout不计算在内)并在最直接的方法.

让我们以加法为例...从最低位开始,将每个数字和前一个加法中的任何进位相加,直到两个数字都用完为止.那是您必须要做的相当简单的声明,但是您的代码至少有40行.快速的伪代码实现将类似于:

-符号扩展较小的数字,以便与较长的数字相同.
-将进位设置为零
-对于每个地方值
-将这些数字加到这些位置值并进位并赋值给结果并进位

嗯,就是这样-一个循环,可能是一两个条件.

因此,请重新考虑并重新开始.紧急或否,它会偿还您.
From what I can see of your code you''re trying to write something that does infinite precision arithmetic with each digit being held as a node in a linked list.

Here''s the big problem with your code - it''s way too big and hairy. It needs it''s legs chopping off and being given a shave. Throw out what you''ve got, go back to the algorithm(s) you''re trying to implement, use the standard library (cin and cout don''t count) and implement it(them) in the most direct way.

Let''s take addition as an example... Starting with the lowest place value add each digit and any carry from the previous addition until both of the numbers run out of digits. That''s a fairly simple statement of what you have to do but your code is at least 40 lines long. A quick pseudo code implementation would be something like:

- sign extend the smaller number so it''s got the same number of digits in it as the longer
- set carry to zero
- for each place value
- add the digits at these place values and carry and assign then to the result and carry

Er, that''s it - one loop and maybe a conditional or two.

So go and have a rethink and start again. Urgent or no it''ll pay you back.


这篇关于写一个计算器,计算长整数的加法和减法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆