C典型案例及常见错误分析

上传人:ni****g 文档编号:410199057 上传时间:2022-12-25 格式:DOCX 页数:61 大小:128.60KB
返回 下载 相关 举报
C典型案例及常见错误分析_第1页
第1页 / 共61页
C典型案例及常见错误分析_第2页
第2页 / 共61页
C典型案例及常见错误分析_第3页
第3页 / 共61页
C典型案例及常见错误分析_第4页
第4页 / 共61页
C典型案例及常见错误分析_第5页
第5页 / 共61页
点击查看更多>>
资源描述

《C典型案例及常见错误分析》由会员分享,可在线阅读,更多相关《C典型案例及常见错误分析(61页珍藏版)》请在金锄头文库上搜索。

1、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= 20i赋值语句coutThe age is:n;输出个字符串coutageendl;输出变量中的值return 0;主函数返回)块作用域结束【案例2-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; 计算圆的周长cout“Length=lengthendl; 输出圆的周长float area=PI*r*r; 计算圆的面积coutArea=areaendl; 输出圆的面积return 0;【案例2-3】整数的简单运算除法、求余运算法和增

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 /z x % yendl;整数的取余操作return 0;【案例2-41多重计数器前置和后置自增运算符#include using namespace std; int main() int iCount=l; iCount= (iC

4、ount+) + (iCount+) + (iCount+): 后置+ cout“The firstiCount=iCountendl;iCount=l; iCount= (+iCount) + (+iCount) + (+iCount): 前置+ coutThe second iCount=iCountendl;iCount=l; iCount=-iCount+:后置+coutThe third iCount=iCountendl;iCount=l; iCount=+iCount;前置+coutThe fourth iCount=iCountendl; return 0;【案例2-5】对整数

5、“10”和“20”进行位运算一位运算的应用#include using namespace std;int main ()cout,20&10= (20&10) endl;按位与运算cout“2010= (2010) endl;/按位异或运算cout“20,10= (20110) endl;按位或运算cout-20= C20) endl;/按位取反运算cout203= (203) endl;左移位运算cout-203= (-203) endl;左移位运算cout203= (203) endl:右移位运算cout -203= (-203) endli右移位运算return 0;【案例2-6】实现

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;c

7、out p XOR q is ( (p | | q) & ! (p & q) ) n: 输出异或结果 return 0;【案例2-7】高效筛选器用条件运算符“?”构建条件表达式#includeusing namespace std;int main () int iNuml=l, iNum2, iMax;cout“Please input two integers:n/z;ciniNumliNum2;iMax = iNumliNum2 ? iNuml : iNum2;使用条件运算符构建条件表达式cout Fhe max integer is: riMaxendl;return 0;【案例2-8

8、】“多计算与单提取”功能的实现逗号表达式#includeusing namespace std;int main () int Vali, Val2, Val3, Left, Midd, Righ;Left = 10; Midd = 20; Righ = 30;Vali = (Left+, -Midd, Righ+);使用逗号表达式Val2 = (Righ+, Left+, Midd);使用逗号表达式Val3 = ( Midd, Righ+, Left+);使用逗号表达式cout Vall=tVall nVal2=tVal2 nVal3=tVal3endl; return 0;【案例2-9】高

9、效的算术运算符复合赋值运算符#include 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;using namespace std;使用复合的赋值运算符+= 使用复合的赋值运算符一二使用

10、复合的赋值运算符二使用复合的赋值运算符/二 使用复合的赋值运算符紀return 0;【案例2-10计算不同数据类型的存储容量sizeof运算符#include using namespace std int main () cout The size of an int is:tt sizeof(int) bytes. n;cout cout cout cout cout cout The The The The The Thesize size size size size sizeof of of of of ofshort int is:t sizeof(short) “ bytes.

11、n”; long int is:t sizeof(long) bytes.n”; char is:tt sizeof(char) bytes.n; wchar_t is:t sizeof(wchar_t) bytes. n; float is:tt sizeof(float) bytes. n; 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, y

12、y; cout nn*xx nn*xx endl;表达式类型转换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;)【案例272】将分数转换为小数强制类型转换#include using namespace std; int main () for( int i二1; i = 5; +i )cout i / 3 is: (float) i / 3 endlJ强制类型转换return 0;【案例2-13】安全的除法计算器#include using namespace std; int main() int a, b;cout Enter numerator: ; cin a;cout zEnter denominator:cin b;if (b) cout Divide Result is: a / b 、n;排除除数为零的情况else cout Divid

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 商业/管理/HR > 商业计划书

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号