// // main.cpp // homework5 // // Created by joun depression gloomy on 2017/4/19. // Copyright © 2017年 joun depression gloomy. All rights reserved. //void progress_3(int& G,int& V,int dog,int* fu,int* pen) #include <iostream> #include<cstdlib> using namespace std; class call { private: int* fu; int* pen; int carry,bit; void progress_1(); public: int A,B,dog; void set_1(); void set_3(); void set_4(); void set_5(); void set_6(); void progress_3(); void progress_2(); void set_7(); }; void call::set_1() { cout<<"請輸入位數:"; cin>>dog; pen=new int[dog]; } void call::set_3() { cout<<"輸入第二個位數:"; cin>>bit; fu=new int[bit]; } void call:: set_4() { if (bit>dog) { dog=bit; }; } void call::set_5() { int i=0; while(i<dog) //設置迴圈,將所有陣列裡的數歸零以防亂數產生 { fu[i]=0; pen[i]=0; i++; } } void call::progress_3() { int i; cout<<"A="; for(i=0;i<dog;i++) //將輸入的數值做位數拆解 { pen[i]=A%10; A=A/10; cout<<pen[i]; } cout<<"B="; for (i=0; i<dog; i++) { fu[i]=B%10; B=B/10; cout<<fu[i]; } } void call:: progress_2() { int i; cout<<"FINAL TOTAL="; if (pen[dog-1]<0) //將所有負數前面輸出一個負號的字串 { cout<<"-"; } for (i=0; i<dog; i++) //將負號做修正變成正,由判斷式決定。 { if (pen[i]<0) { pen[i]=0-pen[i]; } } for (i=dog-1; i>=0; i--) //將i值序列做倒反:[0][1][2]>>[2][1][0] { cout<<pen[i]; } } void call:: progress_1() { int i; carry=0; for (i=0; i<dog; i++) //設置迴圈,統整兩個陣列的數值丟到第三個陣列做運算 { pen[i]+=carry+fu[i]; carry=pen[i]/10; // 根據某一位>=10做判斷,如果兩位相加是17,carry=1,將該數加在其他位數。 pen[i]%=10; //留下17/10的餘數替代為該為超出十以上的數,即mother[0]=7 } } void call::set_6() { int i; progress_1(); if (carry>0) //避免999+1會出現000的情況,將其直接進位用carry值取代 { pen[dog]=carry; dog++; } for (i=0; i<dog; i++)//判斷負數時的運算,避免各個列陣值直接計算,例如:990 -1-1-1 = 88-1(正常應為-2-10) { if (pen[i]<0&&pen[dog-1]>0) { pen[i]+=10; pen[i+1]-=1; } else { if (pen[i]>0&&pen[dog-1]<0) { pen[i]-=10; pen[i+1]+=1; } } } while(pen[dog-1]==0&&(dog-1)!=0) //將多出的零的相對高位數消除:陣列輸出100>>1 { dog--; } } void call::set_7() { int i,k,j; A=0, i=0, k=1, j=0; while (i<dog) //將第一次結果的總和陣列pen[i]傳回A,pen[i]必須將所有數組成十位百位(123>>1 20 300) { while (j<i) { k*=10; j++; } A+=pen[i]*k; i++; } } int main() { call object1; object1.set_1(); cout<<"請輸入整數:"; cin>>object1.A; while(1) { object1.set_3(); cout<<"輸入第二個整數:"; cin>>object1.B; object1.set_4(); object1.set_5(); object1.progress_3(); cout<<endl; object1.set_6(); object1.set_7(); object1.progress_2(); system("pause"); } }
Stars
1
Forks
0
Watchers
1
Open Issues
0
Overall repository health assessment
No language data available
No package.json found
This might not be a Node.js project
1
commits