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;      //赋值语句
cout<<"The age is:\n";  //输出⼀个字符串
cout<<age<<endl;      //输出变量中的值
return 0;    //主函数返回0while语句简单例子
}    //块作⽤域结束
【案例2-2】计算圆的周长和⾯积——C++语⾔中常量、变量
#include <iostream>
using namespace std;
int main()
{ const float PI=3.1415926;  //float 型常量
float r=2.0;    //⽤float 型常量初始化变量
cout<<"r="<<r<<endl;  //输出圆的半径
float length;    //float型变量声明
length=2*PI*r;    //计算圆的周长
cout<<"Length="<<length<<endl; //输出圆的周长
float area=PI*r*r;  //计算圆的⾯积
cout<<"Area="<<area<<endl; //输出圆的⾯积
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 % y<<endl;  //整数的取余操作
return 0;
}
【案例2-4】多重计数器——前置和后置⾃增运算符
#include<iostream>
using namespace std;
int main()
{ int iCount=1; iCount=(iCount++)+(iCount++)+(iCount++); //后置++
cout<<"The first  iCount="<<iCount<<endl;
iCount=1; iCount=(++iCount)+(++iCount)+(++iCount); //前置++
cout<<"The second iCount="<<iCount<<endl;
iCount=1; iCount=-iCount++;    //后置++
cout<<"The third  iCount="<<iCount<<endl;
iCount=1; iCount=-++iCount;    //前置++
cout<<"The fourth  iCount="<<iCount<<endl;
return 0;
}
【案例2-5】对整数“10”和“20”进⾏位运算——位运算的应⽤
#include <iostream>
using namespace std;
int main()
{  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;  //右移位运算
return 0;
}
【案例2-6】实现逻辑“异或”运算——逻辑运算应⽤
#include <iostream>
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】⾼效筛选器——⽤条件运算符“?”构建条件表达式
#include<iostream>
using namespace std;
int main()
{ int iNum1=1,iNum2,iMax;
cout<<"Please input two integers:\n";
cin>>iNum1>>iNum2;
iMax = iNum1>iNum2 ? iNum1 : iNum2;  //使⽤条件运算符构建条件表达式
cout<<"The max integer is: "<<iMax<<endl;
return 0;
}
【案例2-8】“多计算与单提取”功能的实现——逗号表达式
#include<iostream>
using 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=\t"<<Val1 <<"\nVal2=\t"<<Val2 <<"\nVal3=\t"<<Val3<<endl;
return 0;
}
【案例2-9】⾼效的算术运算符——复合赋值运算符
#include <iostream>
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 <iostream>
using namespace std ;
int main()
{ cout << "The size of an int is:\t\t" << 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:\t\t" << 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:\t\t" << sizeof(float) << " bytes.\n";
cout << "The size of a double is:\t" << sizeof(double) << " bytes.\n";
return 0;
}
【案例2-11】巧妙获取整数部分——double和int数据类型的转换
#include <iostream>
using namespace std;
int main()
{  int nn=10,mm;
double xx=4.741,yy;
cout<<"nn*xx="<<nn*xx<<endl;    //表达式类型转换
mm=xx;    yy=nn;        //赋值类型转换
cout<<"mm="<<mm<<endl <<"yy="<<yy<<endl;
cout<<"int(xx)="<<int(xx)<<endl <<"(int)xx="<<(int)xx<<endl;    //强制类型转换    cout<<"int(1.412+xx)="<<int(1.412+xx)<<endl;      //强制类型转换
cout<<"(int)1.412+xx="<<(int)1.412+xx<<endl;    //强制类型转换
return 0;
}
【案例2-12】将分数转换为⼩数——强制类型转换
#include <iostream>
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 <iostream>
using namespace std;
int main()
{ int a, b;
cout << "Enter numerator: ";    cin >> a;
cout << "Enter denominator: ";    cin >> b;
if(b) cout << "Divide Result is: " << a / b << '\n';  //排除除数为零的情况
else cout << "Divide by zero!\n";
return 0;
}
【案例2-14】猜数游戏——嵌套的if条件语句
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{ int MagNum, GueNum;
MagNum = rand();      //产⽣随机数
cout << "Enter the Guess number: ";  cin >> GueNum;
if (GueNum == MagNum)
{        //if语句块起始位置
cout << "* It is Right *\n"<< MagNum << " is the Magess number.\n";
}      //if语句块结束位置
else
{        // else语句块起始位置
cout << "Sorry, you're wrong."<<endl;
if(GueNum > MagNum)
cout <<"Guessed number is too high.\n";
else
cout << "Guessed number is too low.\n";
}        //else语句块结束位置
return 0;
}
【案例2-15】根据输⼊⽉份输出从年初到本⽉底的天数——不带break的switch #include <iostream>
using namespace std;
int main()
{ int year,month,days=0;
cout<<"Input year and month:";  cin>>year>>month;
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 month"<<endl;
else    cout << "Total days is:" <<days<< endl;
return 0;
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。