1 / 11
文档名称:

C 简单程序典型案例.doc

格式:doc   大小:39KB   页数:11页
下载后只包含 1 个 DOC 格式的文档,没有任何的图纸或源代码,查看文件列表

如果您已付费下载过本站文档,您可以点这里二次下载

分享

预览

C 简单程序典型案例.doc

上传人:drp539607 2019/7/27 文件大小:39 KB

下载得到文件列表

C 简单程序典型案例.doc

文档介绍

文档介绍:【案例2-2】计算圆的周长和面积——C++语言中常量、变量#include<iostream>usingnamespacestd;intmain(){ constfloatPI=;                     //float型常量 floatr=;                                                //用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<<"Thefirst iCount="<<iCount<<endl; iCount=1; iCount=(++iCount)+(++iCount)+(++iCount);         //前置++ cout<<"ThesecondiCount="<<iCount<<endl; iCount=1; iCount=-iCount++;                                                  //后置++ cout<<"Thethird iCount="<<iCount<<endl; iCount=1; iCount=-++iCount;                                                 //前置++ cout<<"Thefourth iCount="<<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