C++程序设计笔记---运算的流程控制

结构化程序设计

目标:设计出结构清晰\可读性强\易于分工合作编写和调试的程序.
步骤:
**自顶向下分析:**把复杂问题分解成若干小问题以后再解决
**模块化设计:**讲程序划分为若干个模块,每个模块独立存放\完成一个特定功能.
**结构化编码:**使用基本控制结构控制程序的执行流程.

模块

**模块组成:**可以是一条语句,一段程序,一个函数等.
**基本特征:**仅有一个入口和一个出口
**模块间关系:**相互独立,内聚性很强.

基本控制结构

顺序结构
选择结构
循环结构

C++在顺序结构中使用的语句

说明语句
赋值语句
I/O语句
复合语句和空语句

C++在其他控制结构中使用的语句

流程控制语句
选择语句
条件分支(if…else…)

(1)表示条件的表达式要能判断真假,如a>0,a%2==0
(2)当语句序列仅包含一条语句时,可以省略花括号
1.单路分支语句

double a, b, max;
	cout << "输入a和b的值:" << endl;
	cin >> a >> b;
	max = a;
	if (a < b)
		max = b;
	cout << max << endl;

2.双路分支语句

double a, b, max;
	cout << "请输入a和b的值:" << endl;
	cin >> a >> b;
	if (a < b)
		max = b;
	else
		max = a;
	cout << max << endl;

3.关系表达式作为条件
4.分支嵌套

double a, b, c, max;
	cout << "输入a,b,c的值:";
	cin >> a >> b >> c;
	if (a > b) {
		if (a > c)
			max = a;
		else
			max = c;
	}
	else {
		if (b < c)
			max = c;
		else
			max = b;
	}
	cout << max << endl;
double a, b, c, max;
	cout << "请输入a,b,c的值:";
	cin >> a >> b >> c;
	max = a;
	if (a > b && a > c) {
		if (b > c)
			max = b;
		else
			max = c;
	}
	cout << max << endl;
double a, b, c, max;
	cout << "请输入a,b,c的值:";
	cin >> a >> b >> c;
	max = a;
	if (max < b)
		max = b;
	if (max < c)
		max = c;
	cout << max << endl;

5.多路分支

int old_grade, new_grade;
	cout << "输入成绩old_grade:";
	cin >> old_grade;
	if (old_grade >= 90 && old_grade <= 100) {
		new_grade = 5;
	}
	if (old_grade >= 80 && old_grade <= 89)
		new_grade = 4;
	if (old_grade >= 70 && old_grade <= 79)
		new_grade = 3;
	if (old_grade >= 60 && old_grade <= 69)
		new_grade = 2;
	if (old_grade <= 59)
		new_grade = 1;
	cout << new_grade << endl;
开关分支(switch…case…)

switch…case语句格式
(1)作为运算的表达式可以是整型,字符型,布尔型和枚举型
(2)如果在语句序列中加入break语句可以直接跳过所有的后续分支

int old_grade, new_grade;
	cout << "请输入old_grade的值:";
	cin >> old_grade;
	switch (old_grade / 10) {
	case 10:
		new_grade = 5;
		break;
	case 9:
		new_grade = 5;
		break;
	case 8:
		new_grade = 4;
		break;
	case 7:
		new_grade = 3;
		break;
	case 6:
		new_grade = 2;
		break;
	default:
		new_grade = 1;
	}
	cout << new_grade;
循环语句
while语句

while(表达式){
语句序列
}
求e值得程序1

double e = 1.0, u = 1.0;
	int n = 1;
	while (u >= 1.0E-7) {
		u = u / n;
		e = e + u;
		n = n + 1;
	}
	cout << "e=" << e << "(n=" << n << ")" << endl;
for语句

for(表达式1;表达式2;表达式3){
语句序列;
}
(1)表达式之间的;不能省略;
(2)for(;;)在语法上是正确的,表示死循环.
计算累加和的程序:

int sum = 0, i, n;
	cout << "请输入n的值:";
	for (i = 1; i <= n; i++)
		sum = sum + i;
	cout << sum << endl;

乘法表的程序:

int i, j;
	for (i = 1; i <= 9; i++) {
		for (j = 1; j <= i; j++) {
			cout << j << "*" << i << "=" << j * i << "\t";
		}
		cout << endl;
	}
do…while语句

求e值的程序2

double e = 1.0, u = 1.0;
	int n = 1;
	do {
		u = u / n;
		e = e + u;
		n = n + 1;
	} while (u >= 1.0E-7);
	cout << "e=" << e << "(n=" << n << ")" << endl;
辅助控制语句
break
continue

计算实数n次方根的程序

double x, n;
	cout << "请输入x和n:";
	while (1) {
		cin >> x >> n;
		if (x == 0 && n == 0) {
			cout << "Program terminated" << endl;
			break;
		}
		else
		{
			if ((x < 0 && n <= 0) || (x < 0 && n != int(1 / n))) {
				cout << "error reinput" << endl;
				continue;
			}
		}
		cout << x << "\t" << n << "th root" << pow(x, 1.0 / n) << endl;
	}
goto
return
例题

解一元二次方程
输入一元二次方程的a,b,c三个系数,解一元二次方程
ax2 +bx+c=0
输出两个根(含复根).

double a, b, c;//定义变量保存系数
	double delta;//表示根的判别式
	double x, x1, x2;//表示根
	cout << "请输入一元二次方程的三个系数a,b,c:";
	cin >> a >> b >> c;//输入一元二次方程的系数
	if (a == 0) {//二次项系数等于0
		if (b == 0) {//一次项系数也等于0,不是方程
			cout << "输入的系数不构成方程" << endl;
		}
		else {//二次项系数等于0,一次项系数不为0
			x = -c / b;//计算单根
			cout << "实际为一元一次方程,根为" << x << endl;
		}
	}
	else {//二次项系数不为0
		delta = b * b - 4 * a * c;//计算判别式的值
		if (delta >= 0) {//判别式大于等于0,有实根
			delta = sqrt(delta);//判别式开方
			x1 = (-b + delta) / 2.0 / a;//根1
			x2 = (-b - delta) / 2.0 / a;//根2
			cout << "方程有实根,它们是:" << endl;
			cout << "x1=" << x1 << ",x2=" << x2 << endl;
		}
		else {//判别式小于0,有复根
			delta = sqrt(-delta);//判别式编好开方
			x1 = -b / 2.0 / a;//实部
			x2 = delta / 2.0 / a;//虚部
			cout << "方程有复根,它们是:" << endl;
			cout << "x1=" << x1 << "+j" << x2 << ",x2=" << x1 << "-j" << x2 << endl;
		}
	}

一个简单的计算器
编程一个简单的计算器功能,实现简单的加,减,乘,除表达式的计算.

double num1, num2;
	char op;
	double result;//声明变量,存放计算机结果
	char caption1[20] = "Error,Divided by 0!";
	char caption2[20] = "Invalid opereator!";
	cout << "Please input the expression:";
	cin >> num1 >> op >> num2;
	switch (op) {
	case '+':
		result = num1 + num2;
		cout << num1 << op << num2 << "=" << result << endl;
		break;
	case '-':
		result = num1 - num2;
		cout << num1 << op << num2 << "=" << result << endl;
		break;
	case '*':
		result = num1 * num2;
		cout << num1 << op << num2 << "=" << result << endl;
		break;
	case '/':
		if (fabs(num2) < 1.0e-8) {
			cout << caption1 << endl;
		}
		else {
			result = num1 / num2;
			cout << num1 << op << num2 << "=" << result << endl;
		}
		break;
	default://以上情况都不是
		cout << caption2 << endl;
		
	}

全部代码:

#include<iostream>
#include<cmath>
using namespace std;
int main() {

	/*int old_grade, new_grade;
	cout << "请输入old_grade的值:";
	cin >> old_grade;
	switch (old_grade / 10) {
	case 10:
		new_grade = 5;
		break;
	case 9:
		new_grade = 5;
		break;
	case 8:
		new_grade = 4;
		break;
	case 7:
		new_grade = 3;
		break;
	case 6:
		new_grade = 2;
		break;
	default:
		new_grade = 1;
	}
	cout << new_grade;*/


	/*int sum = 0, i, n;
	cout << "请输入n的值:";
	for (i = 1; i <= n; i++)
		sum = sum + i;
	cout << sum << endl;*/


	/*int i, j;
	for (i = 1; i <= 9; i++) {
		for (j = 1; j <= i; j++) {
			cout << j << "*" << i << "=" << j * i << "\t";
		}
		cout << endl;
	}*/


	/*double e = 1.0, u = 1.0;
	int n = 1;
	while (u >= 1.0E-7) {
		u = u / n;
		e = e + u;
		n = n + 1;
	}
	cout << "e=" << e << "(n=" << n << ")" << endl;*/


	/*double e = 1.0, u = 1.0;
	int n = 1;
	do {
		u = u / n;
		e = e + u;
		n = n + 1;
	} while (u >= 1.0E-7);
	cout << "e=" << e << "(n=" << n << ")" << endl;*/


	/*double x, n;
	cout << "请输入x和n:";
	while (1) {
		cin >> x >> n;
		if (x == 0 && n == 0) {
			cout << "Program terminated" << endl;
			break;
		}
		else
		{
			if ((x < 0 && n <= 0) || (x < 0 && n != int(1 / n))) {
				cout << "error reinput" << endl;
				continue;
			}
		}
		cout << x << "\t" << n << "th root" << pow(x, 1.0 / n) << endl;
	}*/


	//double num1, num2;
	//char op;
	//double result;//声明变量,存放计算机结果
	//char caption1[20] = "Error,Divided by 0!";
	//char caption2[20] = "Invalid opereator!";
	//cout << "Please input the expression:";
	//cin >> num1 >> op >> num2;
	//switch (op) {
	//case '+':
	//	result = num1 + num2;
	//	cout << num1 << op << num2 << "=" << result << endl;
	//	break;
	//case '-':
	//	result = num1 - num2;
	//	cout << num1 << op << num2 << "=" << result << endl;
	//	break;
	//case '*':
	//	result = num1 * num2;
	//	cout << num1 << op << num2 << "=" << result << endl;
	//	break;
	//case '/':
	//	if (fabs(num2) < 1.0e-8) {
	//		cout << caption1 << endl;
	//	}
	//	else {
	//		result = num1 / num2;
	//		cout << num1 << op << num2 << "=" << result << endl;
	//	}
	//	break;
	//default://以上情况都不是
	//	cout << caption2 << endl;
	//	
	//}

	/*double a, b, c;//定义变量保存系数
	double delta;//表示根的判别式
	double x, x1, x2;//表示根
	cout << "请输入一元二次方程的三个系数a,b,c:";
	cin >> a >> b >> c;//输入一元二次方程的系数
	if (a == 0) {//二次项系数等于0
		if (b == 0) {//一次项系数也等于0,不是方程
			cout << "输入的系数不构成方程" << endl;
		}
		else {//二次项系数等于0,一次项系数不为0
			x = -c / b;//计算单根
			cout << "实际为一元一次方程,根为" << x << endl;
		}
	}
	else {//二次项系数不为0
		delta = b * b - 4 * a * c;//计算判别式的值
		if (delta >= 0) {//判别式大于等于0,有实根
			delta = sqrt(delta);//判别式开方
			x1 = (-b + delta) / 2.0 / a;//根1
			x2 = (-b - delta) / 2.0 / a;//根2
			cout << "方程有实根,它们是:" << endl;
			cout << "x1=" << x1 << ",x2=" << x2 << endl;
		}
		else {//判别式小于0,有复根
			delta = sqrt(-delta);//判别式编好开方
			x1 = -b / 2.0 / a;//实部
			x2 = delta / 2.0 / a;//虚部
			cout << "方程有复根,它们是:" << endl;
			cout << "x1=" << x1 << "+j" << x2 << ",x2=" << x1 << "-j" << x2 << endl;
		}
	}*/


	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值