- 1
- 0
- 约8.36万字
- 约 66页
- 2018-06-03 发布于河南
- 举报
C高手编程笔记
C++简单程序典型案例
【案例2-1】设计一个编写仅包含C++程序基本构成元素的程序/*????? //注释行开始This is the first C++ program.?????? Designed by zrf */???? //注释行结束#include iostream? ??//包含头文件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 iostreamusing 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 iostream 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】多重计数器——前置和后置自增运算符#includeiostream??using 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 iostreamusing namespace std;int main()???? {?? cout 2010= (2010) endl;??//按位与运算??? cout 20^10= (20^10) endl;??//按位异或运算??? cout 20|10= (20|10) endl;??//按位或运算??? cout ~20= (~20) endl;????????? //按位取反运算??? cout 203= (203) endl;??//左移位运算??? cout -203= (-203) endl;??//左移位运算??? cout 203= (203) endl;??//右移位运算??? cout -203= (-203) endl;??//右移位运算?return 0;}
?
【案
原创力文档

文档评论(0)