C++典型案例分析.doc_第1页
C++典型案例分析.doc_第2页
C++典型案例分析.doc_第3页
C++典型案例分析.doc_第4页
C++典型案例分析.doc_第5页
已阅读5页,还剩9页未读 继续免费阅读

下载本文档

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

文档简介

C+简单程序典型案例【案例2-1】设计一个编写仅包含C+程序基本构成元素的程序/* /注释行开始This is the first C+ program. Designed by zrf */ /注释行结束#include /包含头文件using namespace std; /打开命名空间std/ This is the main function/单行注释语句int main(void) /主函数,程序入口/块作用域开始int age; /声明一个变量 age= 20; /赋值语句 coutThe age is:n; /输出一个字符串 coutageendl; /输出变量中的值return 0; /主函数返回0/块作用域结束 【案例2-2】计算圆的周长和面积C+语言中常量、变量#include using namespace std;int main()const float PI=3.1415926; /float 型常量float r=2.0; /用float 型常量初始化变量coutr=rendl;/输出圆的半径float length; /float型变量声明length=2*PI*r; /计算圆的周长coutLength=lengthendl;/输出圆的周长float area=PI*r*r; /计算圆的面积coutArea=areaendl;/输出圆的面积return 0;【案例2-3】整数的简单运算除法、求余运算法和增量减量运算符#include using namespace std; int main() int x, y; x = 10; y = 3; cout x / y is x / y /整数的除法操作 with x % y is x % y endl; /整数的取余操作x +; -y ;/使用增量减量运算符cout x / y is x / y n /整数的除法操作 x % y is x % yendl; /整数的取余操作return 0; 【案例2-4】多重计数器前置和后置自增运算符#includeusing namespace std;int main()int iCount=1;iCount=(iCount+)+(iCount+)+(iCount+);/后置+coutThe first iCount=iCountendl;iCount=1;iCount=(+iCount)+(+iCount)+(+iCount);/前置+coutThe second iCount=iCountendl;iCount=1;iCount=-iCount+;/后置+coutThe third iCount=iCountendl;iCount=1;iCount=-+iCount;/前置+coutThe fourth iCount=iCountendl;return 0;【案例2-5】对整数“10”和“20”进行位运算位运算的应用#include using namespace std;int main() cout 20&10= (20&10) endl;/按位与运算 cout 2010= (2010) endl;/按位异或运算 cout 20|10= (20|10) endl;/按位或运算 cout 20= (20) endl; /按位取反运算 cout 203= (203) endl;/左移位运算 cout -203= (-203) endl;/左移位运算 cout 3= 3) endl;/右移位运算 cout 3= 3) endl;/右移位运算return 0;【案例2-6】实现逻辑“异或”运算逻辑运算应用#include using namespace std; int main() bool p, q; p = true; q = true; cout p XOR q is ( (p | q) & !(p & q) ) n; /输出异或结果p = false; q = true; cout p XOR q is ( (p | q) & !(p & q) ) n; /输出异或结果p = true; q = false; cout p XOR q is ( (p | q) & !(p & q) ) n; /输出异或结果p = false; q = false; cout p XOR q is ( (p | q) & !(p & q) ) n; /输出异或结果return 0; 【案例2-7】高效筛选器用条件运算符“?”构建条件表达式#includeusing namespace std;int main()int iNum1=1,iNum2,iMax;coutiNum1iNum2;iMax = iNum1iNum2 ? iNum1 : iNum2; /使用条件运算符构建条件表达式coutThe max integer is: iMaxendl; return 0;【案例2-8】“多计算与单提取”功能的实现逗号表达式#includeusing namespace std;int main() int Val1, Val2, Val3, Left, Midd, Righ;Left = 10;Midd = 20; Righ = 30; Val1 = (Left+, -Midd, Righ+); /使用逗号表达式Val2 = (Righ+, Left+, -Midd); /使用逗号表达式Val3 = ( -Midd, Righ+,Left+); /使用逗号表达式 cout Val1=tVal1 nVal2=tVal2 nVal3=tVal3endl; return 0;【案例2-9】高效的算术运算符复合赋值运算符#include using namespace std;int main()int n=20; cout n = n endl;n += 8; cout After n += 8, n = n endl; /使用复合的赋值运算符+=n -= 6; cout After n -= 6, n = n endl; /使用复合的赋值运算符-=n *= 1; cout After n *= 1, n = n endl;/使用复合的赋值运算符*=n /= 4; cout After n /= 4, n = n endl; /使用复合的赋值运算符/=n %= 3; cout After n %= 3, n = n endl; /使用复合的赋值运算符%=return 0;【案例2-10】计算不同数据类型的存储容量sizeof运算符#include using namespace std ;int main()cout The size of an int is:tt sizeof(int) bytes.n;cout The size of a short int is:t sizeof(short) bytes.n;cout The size of a long int is:t sizeof(long) bytes.n;cout The size of a char is:tt sizeof(char) bytes.n;cout The size of a wchar_t is:t sizeof(wchar_t) bytes.n;cout The size of a float is:tt sizeof(float) bytes.n;cout The size of a double is:t sizeof(double) bytes.n;return 0;【案例2-11】巧妙获取整数部分double和int数据类型的转换#include using namespace std;int main() int nn=10,mm; double xx=4.741,yy; coutnn*xx=nn*xxendl; /表达式类型转换 mm=xx; yy=nn; /赋值类型转换 coutmm=mmendl yy=yyendl; coutint(xx)=int(xx)endl (int)xx=(int)xxendl; /强制类型转换 coutint(1.412+xx)=int(1.412+xx)endl; /强制类型转换 cout(int)1.412+xx=(int)1.412+xxendl; /强制类型转换return 0;【案例2-12】将分数转换为小数强制类型转换#include using namespace std; int main()for( int i=1; i = 5; +i ) cout i / 3 is: (float) i / 3 endl; /强制类型转换return 0; 【案例2-13】安全的除法计算器#include using namespace std; int main() int a, b; cout a; cout b; if(b) cout Divide Result is: a / b n; /排除除数为零的情况else cout Divide by zero!n; return 0; 【案例2-14】猜数游戏嵌套的if条件语句#include #include using namespace std; int main() int MagNum, GueNum; MagNum = rand(); /产生随机数cout GueNum; if (GueNum = MagNum) /if语句块起始位置cout * It is Right *n MagNum is the Magess number.n; /if语句块结束位置else / else语句块起始位置cout Sorry, youre wrong. MagNum) cout Guessed number is too high.n; else cout Guessed number is too low.n; /else语句块结束位置return 0; 【案例2-15】根据输入月份输出从年初到本月底的天数不带break的switch#include using namespace std;int main()int year,month,days=0;coutyearmonth;switch (month) /每个case分支均没有break语句 case 12: days +=31;case 11: days +=30; case 10: days +=31;case 9: days +=30;case 8: days +=31;case 7: days +=31;case 6: days +=30;case 5: days +=31;case 4: days +=30;case 3: days +=31; case 2: /判断是否为闰年if (year % 4=0 & year % 100!=0 | year %400=0) days +=29;else days +=28;case 1: days +=31;if (days=0) cout Wrong monthendl;else cout Total days is: days endl;return 0;【案例2-16】计算数的阶乘do-while循环语句#include using namespace std;int main() long limits; cout limits; cout Factorial numbers of 0 is 1endl; cout Factorial numbers of 1 is 1endl; long fac=1, i=1; do/使用do-while循环 fac *= +i; cout Factorial numbers of i is facendl; while (fac limits); return 0;【案例2-17】计算数的阶乘for循环#include using namespace std;int main() long limits; cout limits; cout Factorial numbers of 0 is 1endl; cout Factorial numbers of 1 is 1endl; long fac=1;for(int i=2;fac=limits;i+)/使用for 循环 fac *= i; cout Factorial numbers of i is facendl;return 0;【案例2-18】筛选素数步长为2的for循环#include #include using namespace std;int main()long n;cout n;if (n 2) cout n is not prime. endl;else if (n 4) cout n is prime. endl;else if (n%2 = 0) cout n = 2* n/2 endl;elsefor (int i=3; i = n/2; i += 2)/步长为2 if (n%i = 0) cout n = i * n/i endl; exit(0); cout n is prime. endl;return 0;【案例2-19】输出120之间的偶数continue语句#include using namespace std; int main() coutThe even numbers are as follows:endl; for(int i=0; i=20; i+) if(i%2) continue; /根据条件使用continue结束本次循环cout i ; return 0; 【案例2-20】统计输入整数的个数并求和exit()函数#include #include using namespace std; int main() int sum=0,num=0,m; coutPlease input integers (0:end):m; num+; sum+=m; if(m=0) coutEntered numbers:num integers.n;coutThe sum is:sumendl;exit(0);/ 使用exit()函数终止程序 while(1);return 0; 【案例2-21】“剪刀、石头、布”游戏枚举类型#include using namespace std;enum Choice ROCK, CLOTH, SCISS;/声明枚举类型Choiceenum Winner Play1, Play2, Tie; /声明枚举类型Winnerint main()int n; Choice cho1, cho2; Winner winner;cout Choose rock (0), cloth (1), or Sciss (2): endl;cout n;cho1 = Choice(n);cout n;cho2 = Choice(n);if (cho1 = cho2) winner = Tie;else if (cho1 = ROCK)if (cho2 = CLOTH) winner = Play2;else winner = Play1;else if (cho1 = CLOTH)if (cho2 = SCISS) winner = Play2;else winner = Play1;else if (cho2 = ROCK) winner = Play2;else winner = Play1;if (winner = Tie) cout tTied!n;else if (winner = Play1) cout tPlayer No. 1 wins. endl;else cout tPlayer No. 2 wins. endl;return 0;【案例2-22】简单的学生信息类型结构体#include #include using namespace std;struct student /学生信息结构体 int num; char name20; char gender; int age; stu1=1001,Zhang San,M,19;int main()student stu2=1002,Li Si,M,20;/声明结构体变量并初始化student stu3=1003,Wang Hong,F,22; /声明结构体变量并初始化 coutsetw(7)stu1.numsetw(20)setw(3)st

温馨提示

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

最新文档

评论

0/150

提交评论