版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、第一章5:#include <iostream>using namespace std;int main() cout<<"This"<<"is" cout<<"a"<<"C+" cout<<"program."<<endl; return 0;6:#include <iostream>using namespace std;int main() int a,b,c; a=10; b=23; c=a+
2、b; cout<<"a+b=" cout<<c; cout<<endl; return 0;7:#include <iostream>using namespace std;int main() int a,b,c; int f(int x,int y,int z); cin>>a>>b>>c; c=f(a,b,c); cout<<c<<endl; return 0;int f(int x,int y,int z) int m; if (x<y) m=x; el
3、se m=y; if (z<m) m=z; return(m); 8: #include <iostream>using namespace std;int main() int a,b,c; cin>>a>>b; c=a+b; cout<<"a+b="<<a+b<<endl; return 0; 9: #include <iostream>using namespace std;int main()int add(int x,int y); int a,b,c; cin>>
4、;a>>b; c=add(a,b); cout<<"a+b="<<c<<endl; return 0;int add(int x,int y)int c; c=x+y; return(c);10:#include <iostream>using namespace std;int main()void sort(int x,int y,int z); int x,y,z; cin>>x>>y>>z; sort(x,y,z); return 0;void sort(int x, i
5、nt y, int z) int temp; if (x>y) temp=x;x=y;y=temp; / 內(nèi)3個(gè)語(yǔ)句的作用是將x和y的值互換) if (z<x) cout<<z<<','<<x<<','<<y<<endl; else if (z<y) cout<<x<<','<<z<<','<<y<<endl; else cout<<x<<'
6、;,'<<y<<','<<z<<endl; 11:#include <iostream>using namespace std;int main()int max(int a,int b,int c=0); int a,b,c; cin>>a>>b>>c; cout<<"max(a,b,c)="<<max(a,b,c)<<endl; cout<<"max(a,b)="<<max
7、(a,b)<<endl; return 0;int max(int a,int b,int c)if(b>a) a=b; if(c>a) a=c; return a;12:#include <iostream>using namespace std;int main() void change(int ,int ); int a,b; cin>>a>>b; if(a<b) change(a,b); cout<<"max="<<a<<" min="<
8、<b<<endl; return 0;void change(int ,int ) int r1,r2,temp; temp=r1; r1=r2; r2=temp; 13:#include <iostream>using namespace std;int main()void sort(int &,int &,int &); int a,b,c,a1,b1,c1; cout<<"Please enter 3 integers:" cin>>a>>b>>c; a1=a;b1
9、=b;c1=c; sort(a1,b1,c1); cout<<a<<" "<<b<<" "<<c<<" in sorted order is " cout<<a1<<" "<<b1<<" "<<c1<<endl; return 0;void sort(int &i,int &j,int &k) void change(int &a
10、mp;,int &); if (i>j) change(i,j); if (i>k) change(i,k); if (j>k) change(j,k);void change(int &x,int &y) int temp; temp=x; x=y; y=temp;14:#include <iostream>#include <string>using namespace std;int main() string s1="week",s2="end" cout<<"
11、;s1="<<s1<<endl; cout<<"s2="<<s2<<endl; s1=s1+s2; cout<<"The new string is:"<<s1<<endl; return 0; 15:#include <iostream>#include <string>using namespace std;int main() string str; int i,n; char temp; cout<<&qu
12、ot;please input a string:" cin>>str; n=str.size(); for(i=0;i<n/2;i+)temp=stri;stri=strn-i-1;strn-i-1=temp; cout<<str<<endl; return 0; 16:#include <iostream>#include <string>using namespace std;int main() int i; string str5="BASIC","C","F
13、ORTRAN","C+","PASCAL" void sort(string ); sort(str); cout<<"the sorted strings :"<<endl; for(i=0;i<5;i+) cout<<stri<<" " cout<<endl; return 0;void sort(string s)int i,j; string t; for (j=0;j<5;j+) for(i=0;i<5-j;i+) i
14、f (si>si+1) t=si;si=si+1;si+1=t; 17: #include <iostream>#include <string>using namespace std;int main() long c5=10100,-123567, 1198783,-165654, 3456; int a5=1,9,0,23,-45; float b5=2.4, 7.6, 5.5, 6.6, -2.3 ; void sort(int ); void sort(float ); void sort(long ); sort(a); sort(b); sort(c
15、); return 0;void sort(int a)int i,j,t; for (j=0;j<5;j+) for(i=0;i<5-j;i+) if (ai>ai+1) t=ai;ai=ai+1;ai+1=t; cout<<"the sorted numbers :"<<endl; for(i=0;i<5;i+) cout<<ai<<" " cout<<endl<<endl; void sort(long a)int i,j; long t; for (j
16、=0;j<5;j+) for(i=0;i<5-j;i+) if (ai>ai+1) t=ai;ai=ai+1;ai+1=t; cout<<"the sorted numbers :"<<endl; for(i=0;i<5;i+) cout<<ai<<" " cout<<endl<<endl;void sort(float a)int i,j; float t; for (j=0;j<5;j+) for(i=0;i<5-j;i+) if (ai>
17、;ai+1) t=ai;ai=ai+1;ai+1=t; cout<<"the sorted numbers :"<<endl; for(i=0;i<5;i+) cout<<ai<<" " cout<<endl<<endl;18: #include <iostream>#include <string>using namespace std;template <typename T>void sort(T a) int i,j,min; T t
18、; for(i=0;i<5;i+)min=i; for (j=i+1;j<5;j+) if(amin>aj) min=j; t=ai; ai=amin; amin=t; cout<<"the sorted numbers :"<<endl; for(i=0;i<5;i+) cout<<ai<<" " cout<<endl<<endl;int main() int a5=1,9,0,23,-45; float b5=2.4, 7.6, 5.5, 6.6, -2.
19、3 ; long c5=10100,-123567, 1198783,-165654, 3456; sort(a); sort(b); sort(c); return 0;第二章1#include <iostream>using namespace std;class Time public:void set_time();void show_time();private: /成員改為公用的 int hour; int minute; int sec; ;void Time:set_time() /在main函數(shù)之前定義 cin>>hour; cin>>mi
20、nute; cin>>sec; void Time:show_time() /在main函數(shù)之前定義 cout<<hour<<":"<<minute<<":"<<sec<<endl; int main()Time t1;t1.set_time(); t1.show_time(); return 0; 2:#include <iostream>using namespace std;class Time public: void set_time(void) c
21、in>>hour; cin>>minute; cin>>sec; void show_time(void) cout<<hour<<":"<<minute<<":"<<sec<<endl; private: int hour; int minute; int sec; ;Time t;int main() t.set_time(); t.show_time(); return 0; 3:#include <iostream>using
22、namespace std;class Time public: void set_time(void); void show_time(void); private: int hour; int minute; int sec; ;void Time:set_time(void) cin>>hour; cin>>minute; cin>>sec; void Time:show_time(void)cout<<hour<<":"<<minute<<":"<<s
23、ec<<endl;Time t;int main() t.set_time(); t.show_time(); return 0; 4:/xt2-4-1.cpp(main.cpp)#include <iostream>using namespace std;#include "xt2-4.h"int main()Student stud; stud.set_value(); stud.display(); return 0;/xt2-4-2.cpp(即student.cpp)#include "xt2-4.h" /在此文件中進(jìn)行函
24、數(shù)的定義#include <iostream>using namespace std; /不要漏寫此行void Student:display( ) cout<<"num:"<<num<<endl; cout<<"name:"<<name<<endl; cout<<"sex:"<<sex<<endl;void Student:set_value() cin>>num; cin>>name; c
25、in>>sex;5:/xt2-5-1.cpp(file1.cpp)#include <iostream>#include "xt2-5.h"int main() Array_max arrmax; arrmax.set_value(); arrmax.max_value(); arrmax.show_value(); return 0; /xt2-5-2.cpp(arraymax.cpp)#include <iostream>using namespace std;#include "xt2-5.h"void Arra
26、y_max:set_value() int i; for (i=0;i<10;i+) cin>>arrayi; void Array_max:max_value() int i; max=array0; for (i=1;i<10;i+) if(arrayi>max) max=arrayi; void Array_max:show_value() cout<<"max="<<max<<endl; 6:解法一#include <iostream>using namespace std;class B
27、oxpublic: void get_value(); float volume(); void display(); public: float lengh; float width; float height; ;void Box:get_value() cout<<"please input lengh, width,height:" cin>>lengh; cin>>width; cin>>height;float Box:volume() return(lengh*width*height);void Box:dis
28、play() cout<<volume()<<endl;int main()Box box1,box2,box3; box1.get_value(); cout<<"volmue of bax1 is " box1.display(); box2.get_value(); cout<<"volmue of bax2 is " box2.display(); box3.get_value(); cout<<"volmue of bax3 is " box3.display();
29、 return 0;解法二:#include <iostream>using namespace std;class Boxpublic: void get_value(); void volume(); void display(); public: float lengh; float width; float height; float vol; ;void Box:get_value() cout<<"please input lengh, width,height:" cin>>lengh; cin>>width;
30、cin>>height;void Box:volume() vol=lengh*width*height;void Box:display() cout<<vol<<endl;int main()Box box1,box2,box3; box1.get_value(); box1.volume(); cout<<"volmue of bax1 is " box1.display(); box2.get_value(); box2.volume(); cout<<"volmue of bax2 is &qu
31、ot; box2.display(); box3.get_value(); box3.volume(); cout<<"volmue of bax3 is " box3.display(); return 0;第三章2:#include <iostream>using namespace std;class Date public: Date(int,int,int); Date(int,int); Date(int); Date(); void display(); private: int month; int day; int year; ;D
32、ate:Date(int m,int d,int y):month(m),day(d),year(y) Date:Date(int m,int d):month(m),day(d) year=2005;Date:Date(int m):month(m) day=1; year=2005; Date:Date() month=1; day=1; year=2005; void Date:display() cout<<month<<"/"<<day<<"/"<<year<<endl;i
33、nt main() Date d1(10,13,2005); Date d2(12,30); Date d3(10); Date d4; d1.display(); d2.display(); d3.display(); d4.display(); return 0; 3:#include <iostream>using namespace std;class Date public: Date(int=1,int=1,int=2005); void display(); private: int month; int day; int year; ;Date:Date(int m
34、,int d,int y):month(m),day(d),year(y) void Date:display() cout<<month<<"/"<<day<<"/"<<year<<endl;int main() Date d1(10,13,2005); Date d2(12,30); Date d3(10); Date d4; d1.display(); d2.display(); d3.display(); d4.display(); return 0;4: #include
35、<iostream>using namespace std;class Student public: Student(int n,float s):num(n),score(s) void display(); private: int num; float score; ;void Student:display() cout<<num<<" "<<score<<endl;int main()Student stud5= Student(101,78.5),Student(102,85.5),Student(1
36、03,98.5), Student(104,100.0),Student(105,95.5); Student *p=stud; for(int i=0;i<=2;p=p+2,i+) p->display(); return 0; 5:#include <iostream>using namespace std;class Student public: Student(int n,float s):num(n),score(s) int num; float score; ;void main()Student stud5= Student(101,78.5),Stu
37、dent(102,85.5),Student(103,98.5), Student(104,100.0),Student(105,95.5); void max(Student* ); Student *p=&stud0; max(p); void max(Student *arr)float max_score=arr0.score; int k=0; for(int i=1;i<5;i+) if(arri.score>max_score) max_score=arri.score;k=i; cout<<arrk.num<<" "
38、;<<max_score<<endl;6:#include <iostream>using namespace std;class Student public: Student(int n,float s):num(n),score(s) void change(int n,float s) num=n;score=s; void display()cout<<num<<" "<<score<<endl; private: int num; float score; ;int main()
39、Student stud(101,78.5); stud.display(); stud.change(101,80.5); stud.display(); return 0;7: 解法一#include <iostream>using namespace std;class Student public: Student(int n,float s):num(n),score(s) void change(int n,float s) num=n;score=s; void display() cout<<num<<" "<<
40、;score<<endl; /可改為:void display() const cout<<num<<" "<<score<<endl; private: int num; float score; ;int main()const Student stud(101,78.5); stud.display(); /stud.change(101,80.5); stud.display(); return 0;解法二:#include <iostream>using namespace std;class
41、 Student public: Student(int n,float s):num(n),score(s) void change(int n,float s) const num=n;score=s; void display() const cout<<num<<" "<<score<<endl; private: mutable int num; mutable float score; ;int main()const Student stud(101,78.5); stud.display(); stud.cha
42、nge(101,80.5); stud.display(); return 0;解法三:#include <iostream>using namespace std;class Student public: Student(int n,float s):num(n),score(s) void change(int n,float s) num=n;score=s; void display() cout<<num<<" "<<score<<endl; private: int num; float score;
43、 ;int main()Student stud(101,78.5); Student *p=&stud; p->display(); p->change(101,80.5); p->display(); return 0;8:#include <iostream>using namespace std;class Student public: Student(int n,float s):num(n),score(s) void change(int n,float s) num=n;score=s; void display() cout<&l
44、t;num<<" "<<score<<endl; private: int num; float score; ;int main()Student stud(101,78.5); void fun(Student&); fun(stud); return 0; void fun(Student &stu)stu.display(); stu.change(101,80.5); stu.display();9:#include <iostream>using namespace std;class Produc
45、t public: Product(int n,int q,float p):num(n),quantity(q),price(p); void total(); static float average(); static void display(); private: int num; int quantity; float price; static float discount; static float sum; static int n; ;void Product:total() float rate=1.0; if(quantity>10) rate=0.98*rate
46、; sum=sum+quantity*price*rate*(1-discount); n=n+quantity; void Product:display() cout<<sum<<endl; cout<<average()<<endl; float Product:average() return(sum/n);float Product:discount=0.05;float Product:sum=0;int Product:n=0;int main() Product Prod3= Product(101,5,23.5),Product
47、(102,12,24.56),Product(103,100,21.5) ; for(int i=0;i<3;i+) Prodi.total(); Product:display(); return 0; 10:#include <iostream>using namespace std;class Date;class Time public: Time(int,int,int); friend void display(const Date &,const Time &); private: int hour; int minute; int sec; ;
48、 Time:Time(int h,int m,int s) hour=h; minute=m; sec=s; class Date public: Date(int,int,int); friend void display(const Date &,const Time &); private: int month; int day; int year; ;Date:Date(int m,int d,int y) month=m; day=d; year=y; void display(const Date &d,const Time &t) cout<
49、<d.month<<"/"<<d.day<<"/"<<d.year<<endl; cout<<t.hour<<":"<<t.minute<<":"<<t.sec<<endl; int main() Time t1(10,13,56); Date d1(12,25,2004); display(d1,t1); return 0; 11:#include <iostream&g
50、t;using namespace std;class Time;class Date public: Date(int,int,int); friend Time; private: int month; int day; int year; ;Date:Date(int m,int d,int y):month(m),day(d),year(y) class Time public: Time(int,int,int); void display(const Date &); private: int hour; int minute; int sec; ;Time:Time(in
51、t h,int m,int s):hour(h),minute(m),sec(s) void Time:display(const Date &d) cout<<d.month<<"/"<<d.day<<"/"<<d.year<<endl; cout<<hour<<":"<<minute<<":"<<sec<<endl; int main() Time t1(10,
52、13,56); Date d1(12,25,2004); t1.display(d1); return 0;12:#include <iostream>using namespace std;template<class numtype>class Compare public: Compare(numtype a,numtype b); numtype max(); numtype min(); private: numtype x,y; ;template <class numtype>Compare<numtype>:Compare(num
53、type a,numtype b) x=a;y=b;template <class numtype>numtype Compare<numtype>:max() return (x>y)?x:y;template <class numtype>numtype Compare<numtype>:min() return (x<y)?x:y;int main()Compare<int> cmp1(3,7); cout<<cmp1.max()<<" is the Maximum of two i
54、nteger numbers."<<endl; cout<<cmp1.min()<<" is the Minimum of two integer numbers."<<endl<<endl; Compare<float> cmp2(45.78,93.6); cout<<cmp2.max()<<" is the Maximum of two float numbers."<<endl; cout<<cmp2.min()<
55、<" is the Minimum of two float numbers."<<endl<<endl; Compare<char> cmp3('a','A'); cout<<cmp3.max()<<" is the Maximum of two characters."<<endl; cout<<cmp3.min()<<" is the Minimum of two characters."<
56、<endl; return 0;第四章1:#include <iostream>using namespace std;class Complex public: Complex()real=0;imag=0; Complex(double r,double i)real=r;imag=i; double get_real(); double get_imag(); void display(); private: double real; double imag; ;double Complex:get_real()return real;double Complex:ge
57、t_imag()return imag;void Complex:display()cout<<"("<<real<<","<<imag<<"i)"<<endl;Complex operator + (Complex &c1,Complex &c2) return Complex(c1.get_real()+c2.get_real(),c1.get_imag()+c2.get_imag();int main()Complex c1(3,4),c2(
58、5,-10),c3; c3=c1+c2; cout<<"c3=" c3.display(); return 0;2:#include <iostream>using namespace std;class Complex public: Complex()real=0;imag=0; Complex(double r,double i)real=r;imag=i; Complex operator+(Complex &c2); Complex operator-(Complex &c2); Complex operator*(Comp
59、lex &c2); Complex operator/(Complex &c2); void display(); private: double real; double imag; ;Complex Complex:operator+(Complex &c2)Complex c; c.real=real+c2.real; c.imag=imag+c2.imag; return c;Complex Complex:operator-(Complex &c2)Complex c; c.real=real-c2.real; c.imag=imag-c2.imag; return c;Complex Complex:operator*(Complex &c2)Complex c; c.real=real*c2.real-imag*c2
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 海南鋪面租賃合同書電子版
- 合同產(chǎn)生質(zhì)量事故考核
- 2024高考政治一輪復(fù)習(xí)課時(shí)練16中國(guó)特色社會(huì)主義最本質(zhì)的特征含解析新人教版
- 2024年高考生物二輪復(fù)習(xí)第一篇專題6考向3生物的進(jìn)化和生物多樣性學(xué)案
- 完美國(guó)際黃昏圣殿裝備屬性、所需材料系列介紹(武器篇)投
- 2024購(gòu)買服務(wù)的合同協(xié)議書
- 2024新疆事業(yè)編制合同到期后單位可以選擇不續(xù)簽
- 2024機(jī)動(dòng)車輛保險(xiǎn)合同樣本
- 2024北京市豬肉入市場(chǎng)廠掛鉤合同范本
- 2024消防工程改造合同
- 20200310公園安全風(fēng)險(xiǎn)辨識(shí)清單
- 華中科技大學(xué)官方信紙
- 60立方油罐容積細(xì)表
- WI-QA-02-034A0 燈具成品檢驗(yàn)標(biāo)準(zhǔn)
- 農(nóng)業(yè)信息技術(shù) chapter5 地理信息系統(tǒng)
- 部編版六年級(jí)上語(yǔ)文閱讀技巧及解答
- 斯派克max操作手冊(cè)
- 項(xiàng)目四 三人表決器ppt課件
- 結(jié)合子的機(jī)械加工工藝規(guī)程及銑槽的夾具設(shè)計(jì)
- 林武樟 完整陽(yáng)宅講義 筆記版[方案]
- 《會(huì)滾的汽車》ppt課件
評(píng)論
0/150
提交評(píng)論