中国地质大学C++指导书代码大全(精品)_第1页
中国地质大学C++指导书代码大全(精品)_第2页
中国地质大学C++指导书代码大全(精品)_第3页
中国地质大学C++指导书代码大全(精品)_第4页
中国地质大学C++指导书代码大全(精品)_第5页
已阅读5页,还剩50页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、第一章实习任务一#include int main()std:couti love c+ very much!std:endl;std:coutprogramming c+ is easy and interesting.n;return 0;实习任务二#include int main()int a=5,b=5;std:coutthe sum is: a+bstd:endl;return 0;实习任务三#include double main() const double pi = 3 double r;std:cinr;double s = pi * r * r;

2、double c = 2 * pi * r;std:couts=sstd:endl;std:coutc=cstd:endl;return 0;课后练习#include int getarea( int width, int height );int main()int area=0;int width=5, height=6;area = getarea( width,height );std:coutarea:areastd:endl;return 0;int getarea( int width, int height )return width * height;第二章实习任务一(1)#

3、includeint main() double numone=2.5; int numtwo=3; double quotient=numone/2; std:coutquotient:quotientstd:endl; quotient=numtwo/2; std:coutquotient:quotientstd:endl; return 0;实习任务一(2)#includeint main() int number=103; int digit,tens,hundreds; digit=number%10; tens=(number/10)%10; hundreds=(number/10

4、0)%10; std:couthundreds:hundreds,tens:tens,digit:digitstd:endl; return 0;实习任务二#includeint main()int grade;std:coutgrade;if(grade100|grade0) std:couttry againn;else if(grade=85)std:coutexcellentn;else if(70=grade&grade85)std:coutpassn; elsestd:coutfailn; return 0;实习任务三#includefloat main() float salar

5、y; const int s=3500; std:cout0):; std:cinsalary; if(salary=0&salary=3500) std:coutthe tax is0std:endl; else if(salary=5000) std:coutthe tax is(salary-s)*0.03std:endl; else if(salary=8000) std:coutthe tax is(salary-s)*0.10-105std:endl; else if(salary=12500) std:coutthe tax is(salary-s)*0.20-555std:en

6、dl; else if(salary=38500) std:coutthe tax is(salary-s)*0.25-1005std:endl; else if(salary=58500) std:coutthe tax is(salary-s)*0.30-2755std:endl; else if(salary=83500) std:coutthe tax is(salary-s)*0.35-550583500) std:coutthe tax is(salary-s)*0.45-13505std:endl; return 0;课后练习#includeint main() int num1

7、,num2,result; char op; std:coutnum1; std:cinop; std:cinnum2; if(op=+) result=num1+num2; else if(op=-) result=num1-num2; else if(op=*) result=num1*num2; else if(op=/) result=result=num1/num2; else std:couterror input!n; return 0; std:coutnum1opnum2=resultn; return 0;第三章实习任务一(1)#include void checkweig

8、ht(int height,int weight);int main()int height,weight;std:coutheight;std:coutweight;checkweight(height,weight);return 0;void checkweight(int height,int weight)int idealweight=height-105;if(idealweight=weight) std:coutweight)std:coutslim!n;elsestd:coutfatty!n;实习任务一(2)#include bool check(int score,int

9、 baseline=60);int main()int score;std:coutscore;if(check(score)=true)std:coutpassed!n;elsestd:coutfailed!n;if(check(score,70)=true)std:coutpassed!n;elsestd:cout=baseline)return true;return false;实习任务二#include int max(int a,int b,int c);double max(double a,double b,double c);char max(char a,char b,ch

10、ar c);int main()int ia,ib,ic;char ca,cb,cc;double da,db,dc;std:coutiaibic;std:coutmax of int:max(ia,ib,ic)std:endl;std:coutcacbcc;std:coutmax of char:max(ca,cb,cc)std:endl;std:coutdadbdc;std:coutmax of double:max(da,db,dc)b?a:b)c?(ab?a:b):c;double max(double a,double b,double c)double d;d=ab?a:b;d=d

11、c?d:c;return d;char max(char a,char b,char c)return (ab?a:b)c?(ab?a:b):c;实习任务三#include #include double getarea(double a,double b,double c);int main()double a,b,c;std:coutabc;double area=getarea(a,b,c);std:coutarea:sqrt(area)std:endl;return 0;double getarea(double a,double b,double c)double s,k;s=(a+

12、b+c)/2;k=s*(s-a)*(s-b)*(s-c);return sqrt(k);课后练习一#includeint menu();int main()int num1=20,num2=6;int choice;int result;choice=menu();if(choice=1)result=num1+num2;else if(choice=2)result=num1-num2;else if(choice=3)result=num1*num2;else if(choice=4)result=num1/num2;else/非预期情况,通过return语句直接结束程序return 0;

13、std:coutresult:resultstd:endl;return 0;int menu()int choice;std:cout1)add two numbern;std:cout2)sub two numbern;std:cout3)multiply two numbern;std:cout4)divide two numbern;std:cout0)quitn;std:coutchoice;return choice; 课后练习二#include#includeusing namespace std;double getarea(double a,double b,double c

14、);bool validate(double a,double b,double c);int main()double a,b,c;coutabc;bool isvalidate;isvalidate=validate(a,b,c);if(isvalidate=false)coutisvalidate trianglen;return 0;double area;area=getarea(a,b,c);coutthe area is:area0)&(b0)&(c0)if(dc)&(eb)&(fa)return true;elsereturn false;return false;课后练习三#

15、include#includeenum triangletypeinvalid,normal,isosceles,equilateral;triangletype checktriangle(double a,double b,double c);bool validate(double a,double b,double c);int main()double a,b,c;std:coutabc;triangletype type=checktriangle(a,b,c);if (type=invalid)std:coutinvalid triangle!n;else if (type=no

16、rmal)std:coutnormal triangle!n;else if (type=isosceles)std:coutisosceles triangle!n;else if (type=equilateral)std:cout0)&(b0)&(c0)if(dc)&(eb)&(fa)return true;elsereturn false;triangletype checktriangle(double a,double b,double c)bool triangle;triangle = validate(a,b,c);if (triangle=false)return inva

17、lid;else if (a=b) if (b=c) return equilateral;else return isosceles;else if (b=c)return isosceles;else if (a=c)return isosceles;else return normal;第四章实习任务一(1)#includeint main()int score;std:coutscore;char level;switch(score/10)case 10:case 9:level=a; break;case 8:level=b; break;case 7:level=c; break

18、;case 6:level=d; break;default:level=e; break;std:coutlevel:levelstd:endl;return 0;实习任务一(2)#includeint gcd(int m,int n);int main()int m,n;int counter=0;while(counter3)std:coutmn;int divisor=gcd(m,n);std:coutcommon divisor of mandnis:divisorm)c=m;m=n;n=c;while(n!=0)d=m%n;m=n;n=d;return m;实习任务二#includ

19、e#includebool isprime(int num);int main()int counter=0;int num=2;while(counter100)if(isprime(num)std:coutnum ;counter+;num+;return 0;bool isprime(int num)bool flag=true;int r=static_cast(sqrt(num);for(int i=2; i=r;i+)if(num%i=0)break;if(i=r) flag=false;else flag=true;return flag;实习任务三#includeint mai

20、n() int big,mid,small; std:coutbigtmidtsmalln; for(big=0;big=33;big+) for(mid=0;mid=50;mid+) for(small=0;small=100;small+) if(big+mid+small=100 & 3*big+2*mid+0.5*small=100) std:coutbigtmidtsmalln; return 0;课后练习一#include #include using namespace std;int gcd(int m, int n)if(n = 0)return m;return gcd(n

21、, m % n);int main()int a, b;coutab;couta和b的最大公约数是:gcd(a, b)endl;return 0;课后练习二#include #include int menu();bool answerquestion(int choice,int num1,int num2);int main()int questionnum=0;int correctnum=0; srand(unsigned)time(null);/设置随机数序列的种子 while(questionnum10)int choice=menu();if(choice4) continue;

22、if(choice=0)break;int num1,num2;num1=rand()%90+10;num2=rand()%90+10;if(answerquestion(choice,num1,num2)=true)correctnum+;questionnum+;if(questionnum=0)std:coutyou didnt answer any question!n;elsestd:coutanswer questions:questionnum,correct:correctnumn;return 0;int menu()int choice;std:cout1)add two

23、numbern; std:cout2)sub two numbern;std:cout3)multiply two numbern;std:cout4)divided two numbern;std:cout0)quitn;std:coutchoice;return choice;bool answerquestion(int choice,int num1,int num2)int result,answer;char operation;if(choice=2&num1num2)int temp=num1;num1=num2;num2=temp;switch(choice)case 1:r

24、esult=num1+num2;operation=+;break;case 2: result=num1-num2;operation=-;break;case 3:result=num1*num2;operation=*;break;case4:num1*=num2;result = num1/num2;operation=/;break;std:coutnum1operationnum2answer;if(result=answer)std:coutgreat! your answer is absolutely correct!n;return true;std:coutsorry!n

25、;return false;课后练习三#include#include#includeint main()srand(unsigned)time(null);int num,count;for(count=1;count=100;count+)num=rand()%10000+1;std:coutstd:setw(8)num;if(count % 5=0)std:coutn;return 0;实习任务一(1)#includeint main()char word50;std:coutword;for(int i=0;wordi!=0;+i)if(wordi=a & wordi=z)wordi-

26、=32;std:coutupper case:wordstd:endl;return 0;实习任务一(2)#includeint main()char word50;std:coutword;int pos=0;for(int i=0;wordi!=0;+i)if(wordi9)wordpos=wordi;+pos;wordpos=0;std:coutresult:wordstd:endl;return 0;实习任务二(1)#includeint main()const int course_num=5;int scorescourse_num=80,75,92,84,68;double av

27、escore=0;for(int i=0; icourse_num;i+)avescore+=scoresi;avescore=avescore/course_num;std:coutaverage score:avescoren;return 0;实习任务二(2)#includeint main()const int course_num=5;int scorescourse_num;double avescore=0;int i;for(i=0; icourse_num;i+)std:coutinput scoresi+1scoresi;for(i=0; icourse_num;i+)av

28、escore+=scoresi;avescore=avescore/course_num;std:coutaverage score:avescoren;return 0;实习任务三#includeint main()const int arraysize=5;int arrayarraysize;int temp;int i,j;for(i=0;iarraysize;i+)std:coutenter array iarrayi;for(i=0,j=arraysize-1;ij;i+,j-)temp=arrayi;arrayi=arrayj;arrayj=temp;for(i=0;iarray

29、size;i+)std:coutarrayi ;std:coutstd:endl;return 0;课后练习一#include int scoretogradepoint(int score);int main()const int coursenum=5;int creditcoursenum=4,4,3,2,4;int scorecoursenum;int gradepointcoursenum;double avegp=0;int i;for(i=0;icoursenum;i+)std:coutenter scoreiscorei;gradepointi=scoretogradepoin

30、t(scorei);double sum1=0.0, sum2=0.0;for(i=0;icoursenum;i+)sum1+=gradepointi*crediti;sum2+=crediti;avegp=sum1/sum2;std:coutthe avegp is :avegpn;return 0;int scoretogradepoint(int gradepoint)switch(gradepoint/10)case 10:case 9: gradepoint=4; break;case 8: gradepoint=3; break;case 7: gradepoint=2; brea

31、k;case 6: gradepoint=1; break;default: gradepoint=0; break;return gradepoint;课后练习二#includeusing namespace std;int main()const int size = 5;int arraysize, i, j;for(i=0;isize;i+)cout输入元素iarrayi;cout排序前的数组:endl;for(i=0;isize;i+)coutarrayit;coutendl;for(i=0;isize-1;i+)int pos=i;for(j=i+1;jarrayj)pos=j;/

32、j循环int tem;tem=arraypos;arraypos=arrayi;arrayi=tem;/i循环cout排序前的数组:endl;for(i=0;isize;i+)coutarrayit;return 0;课后练习三#include#includeusing namespace std;bool isvowel(char letter);int main()string word, result;cout请输入字符串:word)if(isvowel(word0)result= word + ay;else result=word.substr(1)+word0+ay;coutres

33、ultendl;return 0;bool isvowel(char letter)if(letter=a | letter=e | letter=i | letter=o | letter=u)return true;elsereturn false;实习任务一(1)#includeclass tempconvertpublic:void settempcelsius(double temp);void settempfahrenheit(double temp);double gettempcelsius();double gettempfahrenheit();private:doubl

34、e tempcelsius;double celsiustofahrenheit(double tempcelsius);double fahrenheittocelsius(double tempfahrenheit);void tempconvert:settempcelsius(double temp)tempcelsius=temp;void tempconvert:settempfahrenheit(double temp)tempcelsius=fahrenheittocelsius(temp);double tempconvert:gettempcelsius()return t

35、empcelsius;double tempconvert:gettempfahrenheit()return celsiustofahrenheit(tempcelsius);double tempconvert:celsiustofahrenheit(double tempcelsius)return tempcelsius*9.0/5.0+32;double tempconvert:fahrenheittocelsius(double tempfahrenheit)return 5.0/9.0*(tempfahrenheit-32);int main()tempconvert conve

36、rtor;double temp;std:couttemp;convertor.settempcelsius(temp);std:coutcelsius: convertor.gettempcelsius()n;std:coutfahrenheit: convertor.gettempfahrenheit()n;std:couttemp;convertor.settempfahrenheit(temp);std:coutcelsius: convertor.gettempcelsius()n;std:coutfahrenheit: convertor.gettempfahrenheit()n;

37、return 0;实习任务一(2)#includeclass tempconvertpublic:void settempcelsius(double temp);void settempfahrenheit(double temp);double gettempcelsius();double gettempfahrenheit();private:double tempfahrenheit;double celsiustofahrenheit(double tempcelsius);double fahrenheittocelsius(double tempfahrenheit);void

38、 tempconvert:settempcelsius(double temp)tempfahrenheit=celsiustofahrenheit(temp);void tempconvert:settempfahrenheit(double temp)tempfahrenheit=temp;double tempconvert:gettempcelsius()return fahrenheittocelsius(tempfahrenheit);double tempconvert:gettempfahrenheit()return tempfahrenheit;double tempcon

39、vert:celsiustofahrenheit(double tempcelsius)return tempcelsius*9.0/5.0+32;double tempconvert:fahrenheittocelsius(double tempfahrenheit)return 5.0/9.0*(tempfahrenheit-32);int main()tempconvert convertor;double temp;std:couttemp;convertor.settempcelsius(temp);std:coutcelsius: convertor.gettempcelsius(

40、)n;std:coutfahrenheit: convertor.gettempfahrenheit()n;std:couttemp;convertor.settempfahrenheit(temp);std:coutcelsius: convertor.gettempcelsius()n;std:coutfahrenheit: convertor.gettempfahrenheit()n;return 0;实习任务二#includeusing namespace std;class fractionpublic:fraction();fraction(int n);fraction(int n, int d);void setvalue(int n, int d);int getnum();int getden();double getdoublevalue();void output();private:int num;int den;fraction:fraction()num = 0;den = 1;fraction:

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论