c++實現5個程序_第1頁
c++實現5個程序_第2頁
c++實現5個程序_第3頁
c++實現5個程序_第4頁
c++實現5個程序_第5頁
已閱讀5頁,還剩12頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、1 求兩個數的最大公約數 32. n! 43. 循環(huán)計數器 54堆棧 65. 實現大數的加減運算 96. 總結 18. 題目(1). 求兩個數的最大公約數1. 題目描述和要求:輸入兩個正整數,輸出他們的最大公約數 2代碼:#includeint main( )int p,r,n,m,temp;coutnm;if(nm) temp=n;n=m; m=temp;p=n*m; while(m!=0) r=n%m;n=m;m=r; coutHCF=nn; coutLCD=p/nendl; return 0;運行結果:盲*1E:C+C + +程序DEbugV丈金約甄寸please enter- tuo

2、positiue integrei nuimliei*s n:78HCF-1LCD-56Press an 1/to continue(2 ).n!1.題目描述和要求:輸入一個一定范圍內的正整數,計算出它的階乘2代碼:#i ncludelong int factorial (in t);long int main()int i,n=20; long int factor;for(i=2;i=20;i+) factor=factorial(i);coutvi!=vvfactorvn;return 0;long int factorial(i nt f) long int temp=1;for(i

3、nt j=1;j=f;j+)temp=temp*j;return temp;運行結果:E:C+ +C+程序DebugnLew ei*=2i?=6if=24i*=120iT=720 i!-5040 i=40320 i!362880 i=3628800 =39916800 i!=479001600 i!=1932053504 !=1278945280 if=2004310816 i!=2004189184 i!=-288522240 i!898433024 i=109641728 if=-2102132736 Press any Jeep to contInue(3).循環(huán)計數器1.題目描述和要求

4、:實現循環(huán)2代碼: #i ncludeclass CIRCULAR_NUMBERS public:CIRCULAR_NUMBERS(i nt min ,i nt max,i nt value);int get_value();void in creme nt ();void decreme nt ();private:int min_val;int max_val;int curre nt;CIRCULAR_NUMBERS:CIRCULAR_NUMBERS(int min,int max,int value) min_val=(mi n=min)? max: min; if(valuemax_

5、val) curre nt=max_val;else current=value;return;int CIRCULAR_NUMBERS:get_value( )return current;void CIRCULAR_NUMBERS:increment( )int mode=max_val-min_val+1; current=(current-min_val)+1)%mode+min_val; return;void CIRCULAR_NUMBERS:decrement( )int mode=max_val-min_val+1;current =(current-min_val)-1+mo

6、de)%mode+min_val; return;int main( )int loop;CIRCULAR_NUMBERS angle(0,359,250); CIRCULAR_NUMBERS month(1,12,11);coutThe initial angle is angle.get_value( )endl; coutThe initial month is month.get_value( )endl; for(loop=1;loop=100;loop=loop+1) angle.increment( ); for(loop=1;loop=300;loop=loop+1) angl

7、e.decrement( ); for(loop=1;loop=10;loop=loop+1) month.increment( ); for(loop=1;loop=15;loop=loop+1) month.decrement( ); coutThe last angle isangle.get_value( )endl; coutThe last month ismonth.get_value( )endl; return 0;運行結果:式花匸+匚卡+程序Debug循環(huán)計轂器亡畑“The in itial angle is 25BThe in it ial nonth is 11The

8、last anle is50The last nonth isbPpess an9 key to contlnuE(4).堆棧1 題目描述和要求:將用戶輸入的若干個整數逆序輸出2.代碼:#in elude typedef int ELEMENT; struct NODE ELEMENT eleme nt;NODE * link;;class STACK public:STACK ();STACK();void push(ELEMENT obj);void pop();ELEMENT get_top();int is_empty();void display();private:NODE * t

9、op;#in elude STACK:STACK() top=NULL;return;STACK:STACK( )NODE * ptr;while(top!=NULL) ptr=top;top=top-link;delete ptr;return;void STACK:push(ELEMENT obj)NODE * temp;temp=new NODE;if(temp!=NULL) temp-link=top; temp-element=obj; top=temp; else coutlink;delete temp; else coutError: Pop from empty stack.

10、n;exit (1);ELEMENT STACK:get_top( )if(top=NULL) coutelement;int STACK:is_empty( )return (top=NULL);void STACK:display( ) NODE * loop; loop=top;while(loop!=NULL) coutelementlink;coutn; return;const int max_input=5;int main( )STACK turner;ELEMENT user_input;int loop; for(loop=1;loop=max_input;loop=loo

11、p+1) coutInput no.loopuser_input; turner.push(user_input);turner.display( );for(loop=1;loop=max_input;loop=loop+1) if(!turner.is_empty( ) user_input=turner.get_top( ); turner.pop( );coutOutput no.loop:; coutuser_inputn; return 0; 運行結果:甌EC+C+ 程序Debu gstdck.eKeInput no . 1:8 Input r)o . 2Input no . 31

12、 nput no . 4 Input no . 5 :4Output no.1:4Output no.2:5Output no 3 : 6Output no _ 4:7Output no_&:8Press any key to cont inue(5).實現大數的加減運算1 題目描述和要求:以數組的方式輸入兩個大數,輸出經加減運算后的結果2代碼:#i nclude#in cludeclass INTEGERpublic:INTEGER。;INTEGER(c onst INTEGER & other);INTEGER add(co nst INTEGER & other);INTEGER sub

13、tract(co nst INTEGER & other);void prin t();protected:long in teger1O;int fuhao;INTEGER:INTEGER()fuhao=1;int a;coutvv你輸入的是幾位數字: cina;if(a10)cout=0;j-) integerj=0; for(int i=10-a;iintegeri; if(integeri9)cout 輸入錯誤 endl;cout 請重新輸入這位數字: endl;coutendl;i-;else b+;INTEGER:INTEGER(const INTEGER& other)fuhao

14、=1;for(int i=0;i=0;i-) integeri=integeri+egeri+carry; if(integeri9)if(integer09)cout 錯誤 endl;exit(1);else integeri%=10;carry=1;else carry=0;return *this;INTEGER INTEGER:subtract(const INTEGER& other)int carry;for(int j=0;egerj)fuhao=1;if(integerj=0;i-)if(egeri) in

15、tegeri=10+(egeri); carry=1; integeri-1=integeri-1-carry;else integeri=egeri;if(fuhao=0)cout0;if(fuhao=1)for(int x=0;x=9;x+)integerx=integerx;if(fuhao=-1)int temp10;for(int a=0;a=9;a+)tempa=9;for(int b=0;b=9;b+) integerb=tempb-integerb;integer9=integer9+1;return *t

16、his;void INTEGER:print()if(fuhao=0) cout0;if(fuhao=-1) cout-;int a=1;for(int i=0;i=9;i+)if(integeri!=0)break;a=i;for(int j=a;j=9;j+) coutintegerj;coutendl;int main()int a;for(int t=0;t=20;t+) cout0. 退出 n;couta;if(a=1)system(cls);coutf*下 面 是 兩 個 小 于 10 位 的mega_integer 類型數字*、endl;INTEGER i1;cout* 輸出的第

17、一個數字是: i1.print();INTEGER i2;cout* 輸出的第二個數字是: ;i2.print();int choice=1;while (choice!=0)cout 請輸入你的選擇: endl;cout0. 退出至選擇界面。 n;cout1. 第一個數加第二個數。 n; coutchoice;switch (choice)int b;case 0:break;case 1: INTEGER j1(i1);INTEGER j2(i2);j1.add(j2);coutendl;system(cls);cout 計算結果是 :;j1.print();coutendl;cout 請

18、再輸入你的選擇: n;cout1. 繼續(xù)計算。 n;cout2. 退出。 n;coutb;if(b=1)system(cls);if(b=2)cout2)system(cls);endl;cout 你的輸入是錯誤的,重新輸入你的選擇cout1. 繼續(xù)計算。 n;coutb;if(b=1) system(cls);if(b=2)cout2) system(cls); cout 您的輸入又錯誤了 .n; break; case 2: INTEGER k1(i1);INTEGER k2(i2); k1.subtract(k2);coutendl; system(cls);cout 計算結果是: ; k1.print();coutendl;cout 請再輸入你的選擇: n;cout1. 繼續(xù)計算。 n;cout2. 退出。 n; coutb;if(b=1)system(cls);if(b=2)cout2)system(cls);n;cout 你的輸入是錯誤的,重新輸入你的選擇 cout1.

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論