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

问题描述:

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

要求:
(1)用动态链表存贮数据,每结点含一个整型变量,表示若干位数.
(2)整数输入和输出按中国对于长整数的习惯表示,每3位1组,组间用逗号隔开.
(3)实现加、减运算.
(4)程序运行界面清晰实用.

dos 界面
我刚编过,实现加减乘除:从文件读取数字和符号,文件格式是
1000
2000
+
299
188
-
这样的:
程序如下,你可以按你要求修改:
//read a file name
//the file contains nonnegtive integers and operators including + - *
//the format is two lines of integers and a line of operator
//the integers can be arbitrarily large
#include
#include
#include
using namespace std;
struct Node {
int digit;
Node* next;
Node* previous;
};
void insert(Node* &h,Node* &e,int num)
{
Node* p=h;
if(!p){
Node*temp=new Node;
temp->digit=num;
temp->next=NULL;
temp->previous=NULL;
h=temp;
e=temp;
return ;
}
while(p->next){
p=p->next;
}
Node* temp=new Node;
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;
int carry=0;
int digit=0;
while(p1&&p2){
digit=p1->digit+p2->digit+carry;
carry=digit/10;
digit%=10;
Node* temp=new Node;
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=new Node;
temp->digit=digit;
temp->next=p3;
p3=temp;
p1=p1->previous;
}
while(p2){
int digit=p2->digit+carry;
carry=digit/10;
digit%=10;
Node* temp=new Node;
temp->digit=digit;
temp->next=p3;
p3=temp;
p2=p2->previous;
}
if(carry){
Node* temp=new Node;
temp->digit=carry;
temp->next=p3;
p3=temp;
}
return p3;
}
Node* multiply(Node* h1,Node* e1,Node* h2,Node* e2)
{
Node* p2=NULL;
Node* pos=NULL;
Node* p=NULL;
Node* pre=NULL;
int digit=0;
int carry;
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;
}
else if(!p2&&!pre){
Node* temp=new Node;
temp->next=NULL;
temp->previous=NULL;
temp->digit=digit%10;
carry=digit/10;
p2=temp;
pos=temp;
}
else{
Node* temp=new Node;
temp->next=pre;
temp->previous=NULL;
pre->previous=temp;
temp->digit=digit%10;
carry=digit/10;
p2=temp;
}
p=p->previous;
pre=p2;
p2=p2->previous;
}
if(carry){
Node* temp=new Node;
temp->digit=carry;
temp->next=pre;
pre->previous=temp;
temp->previous=NULL;
p2=temp;
pre=p2;
}
pos=pos->previous;
e2=e2->previous;
}
return pre;
}
bool cmp_digit(Node* e1,Node* h1,Node* e2,Node* h2)
{
int n_digit1=0;
int n_digit2=0;
Node* p1=e1;
Node* p2=e2;
Node* q1=h1;
Node* q2=h2;
while(p1){
n_digit1++;
p1=p1->previous;
}
while(p2){
n_digit2++;
p2=p2->previous;
}
if(n_digit1>n_digit2){
return true;
}
else if(n_digit1digit>q2->digit){
return true;
}
else if(q1->digitdigit){
return false;
}
else{
q1=q1->next;
q2=q2->next;
}
}
}
return true;
}
Node* subtract(Node* h1,Node* e1,Node* h2,Node* e2)
{
Node* p1h=h1;
Node* p1e=e1;
Node* p2h=h2;
Node* p2e=e2;
int digit=0;
bool flag=(cmp_digit(p1e,p1h,p2e,p2h));
if(flag==true){
Node* p3=NULL;
while(e2){
if(e2->digit>e1->digit){
digit=10+e1->digit-e2->digit;
e1->previous->digit--;
Node *temp=new Node;
temp->digit=digit;
temp->next=p3;
p3=temp;
}
else{
digit=e1->digit-e2->digit;
Node* temp=new Node;
temp->digit=digit;
temp->next=p3;
p3=temp;
}
e2=e2->previous;
e1=e1->previous;
}
while(e1){
Node *temp=new Node;
if(e1->digitdigit+=10;
e1->previous->digit--;
}
temp->digit=e1->digit;
temp->next=p3;
p3=temp;
e1=e1->previous;
}
return p3;
}
else{
Node* p3=NULL;
while(e1){
if(e1->digit>e2->digit){
digit=10+e2->digit-e1->digit;
e2->previous->digit--;
Node *temp=new Node;
temp->digit=digit;
temp->next=p3;
p3=temp;
}
else{
digit=e2->digit-e1->digit;
Node* temp=new Node;
temp->digit=digit;
temp->next=p3;
p3=temp;
}
e2=e2->previous;
e1=e1->previous;
}
while(e2){
Node *temp=new Node;
if(e2->digitdigit+=10;
e2->previous->digit--;
}
temp->digit=e2->digit;
temp->next=p3;
p3=temp;
e2=e2->previous;
}
return p3;
}
}
void output(Node* h)
{
Node*p=h;
while(p&&p->digit==0){
p=p->next;
}
if(!p){
cout