第2章 C++的简单程序设计(一)

C++语言概述

命名空间

避免命名冲突:
std是C++标准库的命名空间( namespace)名
using namespace std表示打开std命名空间

#include <iostream> //编译预处理命令
using namespace std; //避免命名冲突
int main(){
	cout<< "Hello!" << endl;
	return 0;
}

基本数据类型、常量、变量

//初始化
int a=0;
int a(0);
int a={0};
int a{0};

//常量
const float PI=3.1415926;

程序举例

读入并显示数据

int main()
{	
    //读入并显示整数
	const double pi(3.14); //符号常量
	int radius(0); //变量初始化
	cout << "The initial value of radius is " << radius << "\n";
	cout << "Please enter the radius!\n";
	cin >> radius;
	cout << "The radius is " << radius << "\n";
	cout << "Pi is " << pi << "\n";

	cout << "Please enter a different radius!\n";
	cin >> radius;
	cout << "Now the radius is " << radius;
	return 0;
}

运算与表达式

sizeof, 位运算

sizeof运算

语法形式: sizeof (类型名) 或 sizeof 表达式

结果值:
“类型名”所指定的类型,或“表达式”的结果类型所占的字节数。

例:
sizeof(short)
sizeof x

位运算

按位与:某些位置0
按位或:某些位置1
按位异或:相同为0,不同为1。某些位翻转

数据的输入和输出

cout<<表达式1<<表达式2
int a,b;
cin>>a>>b;

选择结构

if 语句

if (x>y) cout<<x;

if (x>y) cout <<x;
else cout<<y;

if (表达式1) 语句1;
else if (表达式2) 语句2;
else if (表达式3) 语句3;
...
else 语句n;
//输入一个年份,判断是否是闰年
#include <iostream>
using namespace std;
int main()
{
	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;
	return 0;	
}
//输入两个整数,比较两个整数的大小
#include <iostream>
using namespace std;
int main()
{
	int x, y;
	cout << "enter x and y:";
	cin >> x >> y;
	if (x > y)
		cout << "x>y" << endl;
	else if (x == y)
		cout << "x=y" << endl;
	else
		cout << "x<y";
	return 0;
}

switch语句

在这里插入图片描述

//输入一个0-6的整数,并且转换为星期输出
#include <iostream>
using namespace std;
int main()
{	
	int number;
	cout << "Enter a number (0-6):";
	cin >> number;
	switch(number){
		case 0: cout << "Sunday" << endl; break;
		case 1: cout << "Monday" << endl; break;
		case 2:cout << "Tuesday" << endl; break;
		case 3:cout << "Wednesday" << endl; break;
		case 4: cout << "Thursday" << endl; break;
		case 5:cout << "Friday" << endl; break;
		case 6:cout << "Saturday" << endl; break;
		default: cout << "Day out of range." << endl; break;
	}
	return 0;
}

循环结构

while循环

//求自然数1-10的和
#include <iostream>
#using namespace std;
int main()
{	
	int i(1),sum(0);
	while (i <= 10)
	{
		sum += i;
		i += 1;
	}
	cout << sum << endl;
	return 0;
}

do while

//输入一个数,将各位数字翻转后输出
#include <iostream>
using namespace std;
int main()
{	
	cout << "enter a number:" << endl;
	int number;
	cin >> number;
	do
	{
		cout << number % 10;
		number /= 10;
	} while (number != 0);
	return 0;
}
//输出0-10的和
#include <iostream>
using namespace std;
int main()
{	
	int i = 1, sum = 0;
	do {
		sum += i;
		i += 1;
	} while (i <= 10);
	cout << sum << endl;
	return 0;
}

for 语句

在这里插入图片描述

//输入一个整数,求出他所有的因子
#include <iostream>
using namespace std;
int main()
{	
	int number;
	cout << "enter a postive number:";
	cin >> number;
	cout << "number " << number << " factors:";
	for (int i = 1; i <= number; i++) {
		if (number%i == 0) {
			cout << i<<" ";
		}
	}
	return 0;
}

嵌套的控制结构、其他控制语句

//输入一系列整数,统计出正整数个数i和负整数个数j,读入0则结束
using namespace std;
int main()
{	
	int pos = 0, neg = 0, n;
	cout << "enter some integers (enter 0 to quit):";
	cin >> n;
	while (n != 0) {
		if (n > 0) pos += 1;
		else if (n < 0) neg += 1;
		cin >> n;
	}
	cout << pos << " postive integers" << endl;
	cout << neg << " negative integers" << endl;
	return 0;
}

其他控制语句

在这里插入图片描述

自定义类型

类型别名:为已有类型另外命名

在这里插入图片描述

枚举类型

在这里插入图片描述在这里插入图片描述

//设某次体育比赛的结果有4种可能:胜(WIN),负(LOSE),平局(TIE),比赛取消(CANCEL),编写程序顺序输出这四种情况
using namespace std;
enum GameResult {WIN,LOSE,TIE,CANCEL};
int main()
{	
	GameResult result;
	enum GameResult omit = CANCEL;
	for (int count = WIN; count <= CANCEL; count++) {
		result = GameResult(count);
		if (result == omit) cout << "The game was cancelled." << endl;
		else
		{
			cout << "The game was played" ;
			if (result == WIN) cout << " and we won!";
			if (result == LOSE) cout << " and we lost!";
			cout << endl;
		}
	}
	return 0;
}


auto类型与decltype类型

在这里插入图片描述

实验

面积

#include <iostream>
using namespace std;
const float PI = 3.1416;
int main()
{	
	cout << "please select a type (1-circle, 2-rectangle 3-square):";
	int type;
	cin >> type;
	switch (type) {
	case 1: 
		cout << "please enter r:";
		float r;
		cin >> r;
		cout << "the area is " << PI * r*r << endl;
		break;
	case 2:
		cout << "please enter the width and height:";
		float wid, hei;
		cin >> wid >> hei;
		cout << "the area is " << wid * hei << endl;
		break;
	case 3:
		cout << "please enter the length:";
		float len;
		cin >> len;
		cout << "the area is " << len * len << endl;
		break;
	defalt:
		cout << "sorry. please enter a correct type." << endl;
	}
	return 0;
}

时间

声明一个表示时间的结构体,可以精确表示年、月、日、小时、分、秒,提示用户输入年、月、日、小时、分、秒,然后完整地显示出来。

#include <iostream>
using namespace std;
const float PI = 3.1416;
struct myTimeStruct
{
	unsigned int year;
	unsigned int month;
	unsigned int day;

	unsigned int hour;
	unsigned int min;
	unsigned int sec;
};

int main()
{	
	myTimeStruct myTime;
	cout << "enter the year:";
	cin >> myTime.year;
	cout << "enter the month:";
	cin >> myTime.month;
	cout << "enter the day:";
	cin >> myTime.day;
	cout << "enter the hour:";
	cin >> myTime.hour;
	cout << "enter the min:";
	cin >> myTime.min;
	cout << "enter the sec:";
	cin >> myTime.sec;
	cout << "the time is set to " 
		<< myTime.year << "/" 
		<< myTime.month << "/" 
		<< myTime.day << "  " 
		<< myTime.hour << ":" 
		<< myTime.min << ":" 
		<< myTime.sec << endl;
	return 0;
}

作业题

在这里插入图片描述

#include <iostream>
using namespace std;

int main()
{	
	int n;
	cin >> n;
	int max, min;

	int num,sum=0;
	for (int i = 1; i <= n; i++) {
		cin >> num;
		sum += num;
		if (i == 1) {
			max = num;
			min = num;
		}
		else {
			if (num > max) max = num;
			if (num < min) min = num;
		}
	}
	cout << sum << " " << min << " " << max << endl;
}

在这里插入图片描述

#include <iostream>
using namespace std;

int main()
{	
	int n;
	cin >> n;
	int space, symbol;
	for (int i = 1; i <= n; i++) {
		symbol = 2 * i - 1;	//number of symbols
		space = (2 * n - 1 - symbol) / 2; //number of symbols
		for (int j = 1; j <= space; j++) 
			cout << " ";
		for (int j = 1; j <= symbol; j++)
			cout << "*";
		for (int j = 1; j <= space; j++)
			cout << " ";
		cout << endl;
	}

	for (int i = n - 1; i >= 1; i--) {
		symbol = 2 * i - 1;	//number of symbols
		space = (2 * n - 1 - symbol) / 2; //number of symbols
		for (int j = 1; j <= space; j++)
			cout << " ";
		for (int j = 1; j <= symbol; j++)
			cout << "*";
		for (int j = 1; j <= space; j++)
			cout << " ";
		cout << endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/XindiOntheWay/article/details/83010774