C++基础2---表达式控制语句

C++基础2---表达式控制语句,第1张

学习网站:C语言网.
C语言基础:C语言基础.
笔记及源码gitee地址:C++基础笔记及源码.
编译器:Red Panda Dev-C++.



1.C++的数据类型
// 1.常用数据类型:整型int、字符型char、单精度浮点型float、双精度浮点型double;
#include 
using namespace std;
int main(){
	int studentID;
	char sex;
	double scoreOne;
	double scoreTwo;
	double scoreThree;
	
	cout << "Please input student's ID、'M' or 'W'、scoreOne、scoreTwo、scoreThree" << endl;
	cin >> studentID >> sex >> scoreOne >> scoreTwo >> scoreThree;
	cout << "student ID:" << studentID << endl;
	cout << "student sex:" << sex << endl;
	cout << "student scoreOne:" << scoreOne << endl;
	cout << "student scoreTwo:" << scoreTwo << endl;
	cout << "student scoreThree:" << scoreThree << endl;
	cout << "student total score:" << scoreOne + scoreTwo + scoreThree << endl;
	
	return 0;
}
// 2.布尔类型
#include 
using namespace std;
int main(){
	int numberOne = 584;
	int numberTwo = 520;
	bool r;		// bool类型占一个字节; 
	
	r = numberOne > numberTwo;
	cout << "The result of numberOne > numberTwo:" << r << endl;
	cout << "The size of bool is:" << sizeof(r) << endl;
	
	return 0;
}
2.算数运算符
// 1.+、-、*、/、%;
#include 
using namespace std;
int main(){
	// 拆分位数
	int number;
	int onesPlace;		// 个位 
	int tensPlace;		// 十位 
	int hundredsPlace;	// 百位
	
	cin >> number;
	onesPlace = number % 10;
	tensPlace = number % 100 / 10;
	hundredsPlace = number / 100;
	
	cout << "The initial number is:" << number << endl;
	cout << "The ones place of number is:" << onesPlace << endl;
	cout << "The tens place of number is:" << tensPlace << endl;
	cout << "The hundreds place of number is:" << hundredsPlace << endl;
	
	return 0; 
}
// 分段函数求解
#include 
using namespace std;
int main(){
	// 分段函数
	int x,y;
	
	cout << "Please input x:" << endl;
	cin >> x; 
	
	if(x < 1){
		y = x;
	}
	else if(1 <= x && x < 10){		// 使用&&
		y = 2 * x - 1;
	}
	else{
		y = 3 * x - 11;
	}
	cout << "The value of x:" << x << endl;
	cout << "The result of y:" << y << endl;
	
	return 0;
}
3.bool类型
// bool类型:只能表示false(假)或true(真),占1个字节;
#include 
using namespace std;
int main(){
	bool boolOne = true;
	bool boolTwo = false;
	
	cout << "The result of true:" << boolOne << endl;
	cout << "The result of false:" << boolTwo << endl;
	
	return 0;
}
4.自增自减
// 自增:先加后用和先用后加;
// 自减:先减后用和先用后减;
#include 
using namespace std;
int main(){
	int number1,number2;
	int number3,number4;
	
	number1 = number2 = 10;
	number3 = number4 = 10;
	
	cout << "Initial value of number1:" << number1 << endl;
	cout << "Initial value of number2:" << number2 << endl;
	cout << "Initial value of number3:" << number3 << endl;
	cout << "Initial value of number4:" << number4 << endl;
	
	cout << "====================================" << endl;
	
	cout << "The value of number1++:" << number1++ << endl;
	cout << "The value of ++number2:" << ++number2 << endl;
	cout << "The value of number3--:" << number3-- << endl;
	cout << "The value of --number4:" << --number4 << endl;
	
	return 0;
}
5.赋值运算符
// 赋值运算符:=;
#include 
using namespace std;
int main(){
	int numberOne = 520;		// 定义变量并初始化; 
	int numberTwo;			
	
	numberTwo = numberOne;	// 把numberOne的值赋值给numberTwo; 
	cout << "The value of numberOne:" << numberOne << endl;
	cout << "The value of numberTwo:" << numberTwo << endl;
	 
	return 0;
}
6.关系运算符
// 1.关系运算符:>、<、>=、<=、!=、==;
// 2.关系运算符表达式结果,非0即1;
#include 
using namespace std;
int main(){
	cout << "The result of (10 > 9):" << (10 > 9) << endl;
	cout << "The result of (10 >= 9):" << (10 >= 9) << endl;
	cout << "The result of (10 < 9):" << (10 < 9) << endl;
	cout << "The result of (10 <= 9):" << (10 <= 9) << endl;
	cout << "The result of (10 != 9):" << (10 != 9) << endl;
	cout << "The result of (10 == 9):" << (10 == 9) << endl;
	
	return 0;
}
7.逻辑运算符
// 1.逻辑与&&
// 如果运算符左右两边表达式均为真,表达式整体为真,否则为假;

// 2.逻辑或||
// 如果运算符两边表达式有一个为真,表达式整体为真,否则为假;

// 3.逻辑非!
// 真的变为假,假的变为真;
#include 
using namespace std;
int main(){
	cout << "The result of (1 && 0):" << (1 && 0) << endl;
	cout << "The result of (2 && 0):" << (2 && 0) << endl;
	cout << "The result of (1 && 2):" << (1 && 2) << endl;
	cout << endl;
	
	cout << "The resulf of (1 || 0):" << (1 || 0) << endl;
	cout << "The resulf of (2 || 0):" << (2 || 0) << endl;
	cout << "The resulf of (1 || 2):" << (1 || 2) << endl;
	cout << endl;
	
	cout << "The resulf of (!1):" << (!1) << endl;
	cout << "The resulf of (!2):" << (!2) << endl;
	cout << "The resulf of (!0):" << (!0) << endl;
	
	return 0;
}
8.if选择结构
// 选择结构:if形式、if-else形式、else-if形式、switch结构;
#include 
using namespace std;
int main(){
    // 猜数
	int result = 584;
	int guess;
	
	cout << "Please input your guess number(500-600):" << endl;
	cin >> guess;
	
	if(guess == result){
		cout << "Good.You are right." << endl;
	}
	else{
		cout << "Sorry.You are wrong." << endl;
	}
	
	return 0;
}
#include 
using namespace std;
int main(){
	int result = 584;
	int guess;
	
	cout << "Please input your guess number(500-600):" << endl;
	cin >> guess;
	
	// 猜数完善-->告诉用户输入的数比正确的数大还是小;
    if(guess == result){
		cout << "Good.You are right." << endl;
	}
	else if(500 <= guess && guess < 584){
		cout << "The number is smaller." << endl;
	}
	else if(584 < guess && guess <= 600){
		cout << "The number is bigger." << endl;
	}
	else{
		cout << "Error Cause." << endl;
	}
	
	return 0;
}
9.switch结构
/*
switch结构形式:

switch(表达式)
{
	case 常量表达式1:语句1;break;
	case 常量表达式2:语句2;break;
	...
	case 常量表达式n:语句n;break;
	default:语句n+1;break;
}
*/
#include 
using namespace std;
int main(){
	int controlNumber;
	
	cout << "Please input your operation:" << endl;
	cout << "*****************" << endl;
	cout << "1(forward)" << endl;
	cout << "2(backward)" << endl;
	cout << "3(turnRight)" << endl;
	cout << "4(turnLeft)" << endl;
	cout << "*****************" << endl;
	
	cin >> controlNumber;
	switch (controlNumber) {
		case 1:
			cout << "AGV forward." << endl;
			break;
		case 2:
			cout << "AGV backward." << endl;
			break;
		case 3:
			cout << "AGV turnRight." << endl;
			break;
		case 4:
			cout << "AGV turnLeft." << endl;
			break;
		default:
			cout << "AGV Stop." << endl;
			break;
	}
	
	return 0;
}
10.循环
// 1.while循环
#include 
using namespace std;
int main(){
	int numberOne,numberTwo;
	
	// 使用多组数测试程序
	// cin的返回值:istream的流对象,如果遇到问题接收失败,返回false; 
	while(cin >> numberOne >> numberTwo){
		cout << "The value of numberOne is:" << numberOne << endl;
		cout << "The value of numberTwo is:" << numberTwo << endl;
		cout << "The result of (numberOne + numberTwo):" << (numberOne + numberTwo) << endl;
	}
	return 0;
}
// 2.do-while循环
/*
do-while循环形式:
do
{
	循环体语句;
}while(表达式);
*/
#include 
using namespace std;
int main(){
	int cycleNumber;
	int sum = 0;
	int i = 0;
	
	cin >> cycleNumber;
    // 计算n项和
	do{
		sum += i;
		i++;
	}while(i <= cycleNumber);
	
	cout << "The resulf of sum:" << sum << endl;
	
	return 0;
}
// 3.for循环
/*
一般形式:
for(初始化表达式;判断表达式;更新表达式)
{
	循环体语句;
}
*/
#include 
using namespace std;
int main(){
	int sum = 0;
	int number;
	
	cout << "Please input the number you want to cal sum:" << endl;
	cin >> number;
	
    // 计算前n项和
	for(int i = 0;i <= number;i++){
		sum += i;
	}
	cout << "The resulf of sum:" << sum << endl;
	
	return 0;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/874746.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-13
下一篇 2022-05-13

发表评论

登录后才能评论

评论列表(0条)

保存