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;
}