【C++】2. 简单程序设计

2022/1/23 14:34:19

本文主要是介绍【C++】2. 简单程序设计,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

文章目录

  • 1. 闰年判断
  • 2. 比较两个数的大小
  • 3. switch语句用法
  • 4. 循环语句用法
  • 5. enum枚举
  • 5. typedef声明

1. 闰年判断

知识点:

  1. cout,cin,箭头方向
  2. 创建简单的类和对象(纯纯力扣模板)
  3. 主函数的return 0;
#include<iostream>
using namespace std;

class Solution {
	public:
		void isLeap() {
			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;
			}
		}
};

int main() {
	Solution solution;
	solution.isLeap();
	return 0;
}

2. 比较两个数的大小

知识点:

  1. 连续输入用 >> 隔开,实际输入用空格隔开;
  2. 两个及以上同类型变量声明用逗号;
  3. if … else语句格式。
#include<iostream>
using namespace std;

int main() {
	int x, y;
	cout << "Enter x and y: ";
	cin >> x >> y;
	if (x != y) {
		if (x > y) {
			cout<<"x > y"<<endl;
		} else {
			cout<<"x < y"<<endl;
		}
	} else {
		cout<<"x = y"<<endl;
	}
	return 0;
}

3. switch语句用法

  1. 执行完需要break来退出switch
  2. 没有符合要求的则执行default
#include<iostream>
using namespace std;

int main() {
	int day;
	cout << "Enter the day: ";
	cin >> day;
	switch (day) {
		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 Sunday ... Saturday" << endl; 
	}
} 

4. 循环语句用法

知识点:

  1. while
  2. do … while 末尾结束要分号
  3. for
#include<iostream>
using namespace std;

int useWhile(int n) {
	int i = 1, sum = 0;
	while (i <= n) {
		sum += i;
		i++;
	}
	return sum;
}

void useDoWhile(int n) {
	int cur;
	do {
		cur = n % 10;
		cout << cur;
		n /= 10;
	} while (n != 0);
	cout << endl;
	int i = 1, sum = 0;
	do {
		sum += i;
		i++;
	} while (i <= 100);
	cout << "sum = " << sum << endl;
}

void useFor(int n) {
	for (int i = 1; i <= n; i++) {
		if (n % i == 0) 
			cout << i << " ";
	}
	cout << endl;
}

int main() {
	cout << useWhile(100) << endl;
	useDoWhile(102);
	useFor(24);
	return 0;
}

5. enum枚举

头是0,尾是n - 1

#include<iostream>
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 lose.";
			cout << endl;
		}
	}
	return 0;
}

5. typedef声明

typedef 已有类型名 新类型名表;
#include<iostream>
using namespace std;

int main() {
	typedef double Area;
	typedef unsigned Natural;
	Natural a, b;
	Area s;
	cin >> a >> b;
	cout << "s = " << a * b << endl;
	return 0;
}


这篇关于【C++】2. 简单程序设计的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程