C++长整数、高精度计算器设计一个程序实现两个任意长的整数(-查字典问答网
分类选择

来自潘立的问题

  C++长整数、高精度计算器设计一个程序实现两个任意长的整数(包括正数和负数)、任意精度实数的算术运算.要求:(1)用动态链表存贮数据,每结点含一个整型变量,表示若干位数.(2)整

  C++长整数、高精度计算器

  设计一个程序实现两个任意长的整数(包括正数和负数)、任意精度实数的算术运算.

  要求:

  (1)用动态链表存贮数据,每结点含一个整型变量,表示若干位数.

  (2)整数输入和输出按中国对于长整数的习惯表示,每3位1组,组间用逗号隔开.

  (3)实现加、减运算.

  (4)程序运行界面清晰实用.

1回答
2020-07-16 00:05
我要回答
请先登录
田晓华

  dos界面

  我刚编过,实现加减乘除:从文件读取数字和符号,文件格式是

  1000

  2000

  +

  299

  188

  -

  这样的:

  程序如下,你可以按你要求修改:

  //readafilename

  //thefilecontainsnonnegtiveintegersandoperatorsincluding+-*

  //theformatistwolinesofintegersandalineofoperator

  //theintegerscanbearbitrarilylarge

  #include

  #include

  #include

  usingnamespacestd;

  structNode{

  intdigit;

  Node*next;

  Node*previous;

  };

  voidinsert(Node*&h,Node*&e,intnum)

  {

  Node*p=h;

  if(!p){

  Node*temp=newNode;

  temp->digit=num;

  temp->next=NULL;

  temp->previous=NULL;

  h=temp;

  e=temp;

  return;

  }

  while(p->next){

  p=p->next;

  }

  Node*temp=newNode;

  temp->digit=num;

  temp->next=NULL;

  temp->previous=p;

  p->next=temp;

  e=temp;

  return;

  }

  Node*add(Node*e1,Node*e2)

  {

  Node*p1=e1;

  Node*p2=e2;

  Node*p3=NULL;

  intcarry=0;

  intdigit=0;

  while(p1&&p2){

  digit=p1->digit+p2->digit+carry;

  carry=digit/10;

  digit%=10;

  Node*temp=newNode;

  temp->next=NULL;

  temp->digit=digit;

  temp->next=p3;

  p3=temp;

  p1=p1->previous;

  p2=p2->previous;

  }

  while(p1){

  digit=p1->digit+carry;

  carry=digit/10;

  digit%=10;

  Node*temp=newNode;

  temp->digit=digit;

  temp->next=p3;

  p3=temp;

  p1=p1->previous;

  }

  while(p2){

  intdigit=p2->digit+carry;

  carry=digit/10;

  digit%=10;

  Node*temp=newNode;

  temp->digit=digit;

  temp->next=p3;

  p3=temp;

  p2=p2->previous;

  }

  if(carry){

  Node*temp=newNode;

  temp->digit=carry;

  temp->next=p3;

  p3=temp;

  }

  returnp3;

  }

  Node*multiply(Node*h1,Node*e1,Node*h2,Node*e2)

  {

  Node*p2=NULL;

  Node*pos=NULL;

  Node*p=NULL;

  Node*pre=NULL;

  intdigit=0;

  intcarry;

  while(e2){

  carry=0;

  p=e1;

  pre=pos;

  p2=pos;

  while(p){

  digit=(e2->digit*p->digit+carry);

  if(p2){

  digit+=p2->digit;

  p2->digit=digit%10;

  carry=digit/10;

  }

  elseif(!p2&&!pre){

  Node*temp=newNode;

  temp->next=NULL;

  temp->previous=NULL;

  temp->digit=digit%10;

  carry=digit/10;

  p2=temp;

  pos=temp;

  }

  else{

  Node*temp=newNode;

  temp->next=pre;

  temp->previous=NULL;

  pre->previous=temp;

  temp->digit=digit%10;

  carry=digit/10;

  p2=temp;

  }

2020-07-16 00:07:54

最新问答

推荐文章

猜你喜欢

附近的人在看

推荐阅读

拓展阅读

  • 大家都在看
  • 小编推荐
  • 猜你喜欢
  •