如果您无法下载资料,请参考说明:
1、部分资料下载需要金币,请确保您的账户上有足够的金币
2、已购买过的文档,再次下载不重复扣费
3、资料包下载后请先用软件解压,在使用对应软件打开
C++简单程序典型案例【案例2-1】设计一个编写仅包含C++程序基本构成元素的程序/*//注释行开始ThisisthefirstC++program.Designedbyzrf*///注释行结束#include<iostream>//包含头文件usingnamespacestd;//打开命名空间std//Thisisthemainfunction//单行注释语句intmain(void)//主函数,程序入口{//块作用域开始intage;//声明一个变量age=20;//赋值语句cout<<"Theageis:\n";//输出一个字符串cout<<age<<endl;//输出变量中的值return0;//主函数返回0}//块作用域结束【案例2-2】计算圆的周长和面积——C++语言中常量、变量#include<iostream>usingnamespacestd;intmain(){constfloatPI=3.1415926;//float型常量floatr=2.0;//用float型常量初始化变量cout<<"r="<<r<<endl;//输出圆的半径floatlength;//float型变量声明length=2*PI*r;//计算圆的周长cout<<"Length="<<length<<endl;//输出圆的周长floatarea=PI*r*r;//计算圆的面积cout<<"Area="<<area<<endl;//输出圆的面积return0;}【案例2-3】整数的简单运算——除法、求余运算法和增量减量运算符#include<iostream>usingnamespacestd;intmain(){intx,y;x=10;y=3;cout<<x<<"/"<<y<<"is"<<x/y//整数的除法操作<<"withx%yis"<<x%y<<endl;//整数的取余操作x++;--y;//使用增量减量运算符cout<<x<<"/"<<y<<"is"<<x/y<<"\n"//整数的除法操作<<x<<"%"<<y<<"is"<<x%y<<endl;//整数的取余操作return0;}【案例2-4】多重计数器——前置和后置自增运算符#include<iostream>usingnamespacestd;intmain(){intiCount=1;iCount=(iCount++)+(iCount++)+(iCount++);//后置++cout<<"ThefirstiCount="<<iCount<<endl;iCount=1;iCount=(++iCount)+(++iCount)+(++iCount);//前置++cout<<"ThesecondiCount="<<iCount<<endl;iCount=1;iCount=-iCount++;//后置++cout<<"ThethirdiCount="<<iCount<<endl;iCount=1;iCount=-++iCount;//前置++cout<<"ThefourthiCount="<<iCount<<endl;return0;}【案例2-5】对整数“10”和“20”进行位运算——位运算的应用#include<iostream>usingnamespacestd;intmain(){cout<<"20&10="<<(20&10)<<endl;//按位与运算cout<<"20^10="<<(20^10)<<endl;//按位异或运算cout<<"20|10="<<(20|10)<<endl;//按位或运算cout<<"~20="<<(~20)<<endl;//按位取反运算cout<<"20<<3="<<(20<<3)<<endl;//左移位运算cout<<"-20<<3="<<(-20<<3)<<endl;//左移位运算cout<<"20>>3="<<(20>>3)<<endl;//右移位运算cout<<"-20>>3="<<(-20>>3)<<endl;//右移位运算return0;}【案例2-6】实现逻辑“异或”运算——逻辑运算应用#include<iostream>usingnamespacestd;intmain(){boolp,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";//