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
C++長整數、高精度計算器
C++長整數、高精度計算器
設計一個程序實現兩個任意長的整數(包括正數和負數)、任意精度實數的算術運算.
要求:
(1)用動態(tài)鏈表存貯數據,每結點含一個整型變量,表示若干位數.
(2)整數輸入和輸出按中國對于長整數的習慣表示,每3位1組,組間用逗號隔開.
(3)實現加、減運算.
(4)程序運行界面清晰實用.
設計一個程序實現兩個任意長的整數(包括正數和負數)、任意精度實數的算術運算.
要求:
(1)用動態(tài)鏈表存貯數據,每結點含一個整型變量,表示若干位數.
(2)整數輸入和輸出按中國對于長整數的習慣表示,每3位1組,組間用逗號隔開.
(3)實現加、減運算.
(4)程序運行界面清晰實用.
其他人氣:800 ℃時間:2019-09-09 18:38:50
優(yōu)質解答
我來回答
類似推薦
- 電子計算機最主要的工作特點是()A.高精度C.存儲程序與自動控制D.記憶力強
- 1.△ABC中,a,b,c分別是∠A,∠B,∠C的對邊,a=3,b=4,b<c,且c為整數,求c的長
- 一個直角三角形三邊的長a,b,c都是整數,且滿足a
- 作文《我們》怎么寫?
- 合唱隊女生人數比男生人數的2倍多5比男生的3倍少16,男女生各多少人?
- 一條環(huán)形跑道長400米,甲練習騎自行車,平均每分鐘550米,乙練習跑步,平均每分鐘250米,兩人同時同地出發(fā).若兩人同向而行,則他們經過多長時間首次相遇?
- 過直線外一點可以作無數條直線與已知直線平行.(_)
- 指數函數定義域,值域?
- 《別懂大二首(其一)》勉勵與自信的詩句是?
- 1.若n為正整數,(n+11)²-n²的值總可以被k整除,則k等于( )
- 兩個正整數的積是540,它們最大公因數是6,求這兩個數
- 英語翻譯
猜你喜歡
- 1英語翻譯
- 2一個時鐘的分針長8厘米,它從上午8點走到12點,分針的針尖共走了多少厘米?分針掃過的面積是多少?
- 3x+10-23.5=17 = =
- 4請問這道英語語法題
- 5冰水混合物是純凈物嗎?
- 61.My mother __a teacher.
- 7等腰三角形的底邊長20 cm,面積為100/33cm2,求它的各內角.
- 8形容喜歡女生的詞語
- 9將30攝氏度100克硝酸鉀的飽和溶液蒸發(fā)掉10克水后,仍冷卻到30攝氏度,則蒸發(fā)前后溶液中保持不變的是
- 10a.2g氫氣b.2molNH3.c.9g水.d.1.806*10^24個硫酸分子,
- 11平行線分線段成比例逆定理是什么?有圖說明就更好了!
- 12落霞與孤鶩齊飛,秋水共長天一色的整詩誰知道