《C++语言程序设计》第二章-C++简单程序设计.ppt
文本预览下载声明
第二章 C++简单程序设计;本章主要内容;C++语言的产生;C++的特点;C++程序实例—例2-1;C++字符集;词法记号;标识符的构成规则; 数据类型 ——常量与变量;#include iostream
using namespace std;
int main(void)
{
const int PRICE=30;
int num,total;
float v ,r,h;
num=10;
total=num*PRICE;
couttotal endl;
r=2.5;
h=3.2;
v=3.14159*r*r*h;
coutv endl;
};数据类型 —整型数据及取值范围;#include iostream
using namespace std;
int main(void)
{
const int PRICE=30;
int num,total;
float v ,r,h;
num=10;
total=num*PRICE;
couttotalendl;
r=2.5;
h=3.2;
v=3.14159*r*r*h;
coutvendl;
}; 数据类型 ——字符型数据(一);字符数据的使用方法
字符数据和整型数据之间可以运算。
字符数据与整型数据可以互相赋值。
字符串常量
例:CHINA a a
所以:char c; c=a;; 数据类型 ——布尔型数据; 数据类型 ——变量初始化;数据类型 —混合运算时的类型转换;变量的存储类型;算术运算符与算术表达式;赋值运算符和赋值表达式 简单的赋值运算符=;赋值运算符和赋值表达式 复合的赋值运算符;赋值运算符和赋值表达式 ——赋值表达式举例;逗号运算和逗号表达式;关系运算与关系表达式;逻辑运算与逻辑表达式;条件运算符与条件表达式;条件运算符与条件表达式;sizeof 运算符;位运算——按位与();位运算——按位或(|);位运算——按位异或(^);位运算——按位异或(^);位运算——取反(~);位运算——移位;运算符优先级;混合运算时数据类型的转换 ——隐含转换;混合运算时数据类型的转换 ——隐含转换;混合运算时数据类型的转换 ——强制类型转换;语句;表达式语句;复合语句;简单的输入、输出;顺序结构
分支结构
循环结构;如何解决分支问题?;#include iostream
using namespace std;
int main(void)
{ int year;
bool IsLeapYear;
cout Enter the year: ;
cin year;
IsLeapYear = ((year % 4 == 0 year % 100 != 0)||(year % 400 == 0));
if (IsLeapYear)
cout year is a leap year endl;
else
cout year is not a leap year endl;
};运行结果:
Enter the year: 2000
2000 is a leap year;if (表达式) 语句
例:if (xy) coutx;
if (表达式) 语句1 else 语句2
例:if (xy) coutx;
else couty;
if (表达式1) 语句1else if (表达式2) 语句2else if (表达式3) 语句3 …else 语句 n;如何解决多分问题?;#includeiostream
using namespace std;
int main()
{
int x,y;
coutEnter x and y:;
cinxy;
if (x!=y)
if (xy)
coutxyendl;
else
coutxyendl;
els
显示全部