C++入门(代码:学生管理系统;控制台界面输出坐标控制;向量)

一、学生管理系统`

/*学生成绩管理系统*/
#include<iostream>
#include<string>
#include<iomanip>//setprecision(1) << setiosflags(ios::fixed)【使输出保留1位小数】 的头文件
#include<stdlib.h>//用到system()
#include<algorithm>//需要用到排序算法

const short MAX_SIZE = 50;//决定student类数组的大小(可以容纳多少学生)

using namespace std;

class Student
{
    
    
private:
	char name[12];//姓名
	char id[10];//学号
	float math;//单科成绩
	float english;//单科成绩
	float computer;//单科成绩
public:
	//构造器 
	Student() {
    
    };
	Student(const char* n) {
    
     strcpy_s(name, n); }
	Student(const char* n, const char* i) {
    
     strcpy_s(name, n); strcpy_s(id, i); }

	void SetName(char* n) {
    
     strcpy_s(name, n); }
	void SetMath(float m) {
    
     math = m; }
	void SetEnglish(float e) {
    
     english = e; }
	void SetComputer(float c) {
    
     computer = c; }
	void SetAllScores(float m, float e, float c)
	{
    
    
		math = m; english = e; computer = c;
	}

	string GetName() {
    
     return name; }
	string GetID() {
    
     return id; }
	float GetSum() {
    
     return math + english + computer; }
	float GetMath() {
    
     return math;}
	float GetEnglish() {
    
     return english; }
	float GetComputer() {
    
     return computer; }

	bool Match(char* str, short flag)//flag为1,与姓名进行匹配,若flag为2,与学号进行匹配
	{
    
    
		return flag == 1 ? strcmp(name, str) == 0 : strcmp(id, str) == 0;
	}

	void print()
	{
    
    
		cout << name << '\t' << id << endl;
		cout << "数学" << setprecision(1) << setiosflags(ios::fixed) << math << '\t' << "英语:" << english << '\t' << "计算机" << computer << endl;
	}
};

//排序方式
bool Compare1(Student st1, Student st2)
{
    
    
	return st1.GetID() < st2.GetID();
}
bool Compare2(Student st1, Student st2)
{
    
    
	return st1.GetSum() > st2.GetSum();
}

class DataBase
{
    
    
private:
	Student stu[MAX_SIZE];
	short size;//记录当前学生数

public:
	DataBase() {
    
     size = 0; }//构造器

	bool Push(const char* n, const char* i, float m, float e, float c)
	{
    
    
		if (size == MAX_SIZE)
			return false;
		Student st(n, i);
		st.SetAllScores(m, e, c);
		stu[size++] = st;
		return true;
	}
	bool Push()//重载push函数
	{
    
    
		if (size == MAX_SIZE)
		{
    
    
			cout << "系统不能容纳更多学生。\n";
			system("pause");
			return false;
		}
		char n[12], i[10];
		cout << "请输入要添加的学生姓名:";
		cin >> n;
		int idx;//循环变量
		do
		{
    
    
			cout << "请输入要添加的学生的学号:";
			cin >> i;
			for (idx = 0; idx < size; idx++)
			{
    
    
				if (stu[idx].Match(i, 2))
				{
    
    
					cout << "该学号已存在,不能重复输入!\n";
					break;//跳出for循环
				}
			}
		} while (idx < size);
		Student stu_tmp(n, i);//创建学生实例
		float m, e, c;
		cout << "请输入该学生数学成绩:"; cin >> m;
		cout << "请输入该学生英语成绩:"; cin >> e;
		cout << "请输入该学生计算机成绩:"; cin >> c;
		stu_tmp.SetAllScores(m, e, c);
		stu[size++] = stu_tmp;
		cout << "添加成功!\n";
		system("pause");
		return true;
	}
	short AimedSearch(short start_id, char* str, short flag)//查找学生信息,不提供用户输入接口
	{
    
    
		for (short i = start_id; i < size; i++)
		{
    
    
			if (stu[i].Match(str, flag))
			{
    
    
				stu[i].print();
				return i;
			}
		}
		return -1;//返回-1表示没有找到
	}
	short Search()//实现查找功能,提供用户输入接口
	{
    
    
		short choice;//接受用户输入的字段选择
		do
		{
    
    
			cout << "请问你要按什么条件进行搜索?  1.姓名  2.学号  ";
			cin >> choice;
		} while (choice != 1 && choice != 2);
		char match[12];//用来接收需要查找的姓名或学号
		cout << "请输入你要找的" << (choice == 1 ? "姓名:" : "学号:");
		cin >> match;
		short result = 0;
		char nod;
		while (true)
		{
    
    
			result = AimedSearch(result, match, choice);
			if (result == -1)
			{
    
    
				cout << "未找到相关信息。\n";
				system("pause");
				return result;
			}
			cout << "这是你要找的人吗?(y/n)";
			cin >> nod;
			if (nod == 'y' || nod == 'Y')
				return result;
			else result++;//接着找
		}
	}

	bool Delete()
	{
    
    
		short result;
		result = Search();
		if (result == -1)return false;
		char choice;
		cout << "请问是否要删除这条信息?(y/n)";
		cin >> choice;
		short idx;
		if (choice == 'Y' || choice == 'y')
		{
    
    
			for (idx = result; idx < size - 1; idx++)
				stu[idx] = stu[idx + 1];
			size--;
			cout << "删除成功!\n";
			system("pause");
			return true;
		}
		cout << "删除失败。\n";
		system("pause");
		return false;
	}

	bool Alter()
	{
    
    
		short result;
		result = Search();
		if (result == -1)return false;
		char choice;//接受是否修改
		char subchoice;//接收修改哪个字段
		char rename[12];//接收新名字
		float rescore;//接受新成绩
		cout << "是否要修改这条信息?(y/n)";
		cin >> choice;
		if (choice == 'Y' || choice == 'y')
		{
    
    
			cout << "需要修改什么:1.名字  2.数学成绩  3.英语成绩  4.计算机成绩\n";
			cin >> subchoice;
			switch (subchoice) 
			{
    
    
			case '1':
				cout << "输入一个新名字:"; cin >> rename;
				stu[result].SetName(rename);
				cout << "修改成功!";
				system("pause");
				break;
			case '2':
				cout << "输入数学成绩:"; cin >> rescore;
				stu[result].SetMath(rescore);
				cout << "修改成功!";
				system("pause");
				break;
			case '3':
				cout << "输入英语成绩:"; cin >> rescore;
				stu[result].SetEnglish(rescore);
				cout << "修改成功!";
				system("pause");
				break;
			case '4':
				cout << "输入计算机成绩:"; cin >> rescore;
				stu[result].SetComputer(rescore);
				cout << "修改成功!";
				system("pause");
				break;
			default:
				cout << "信息未修改。\n";
				system("pause");
				return false;
				break;
			}
			cout << "信息未修改。\n";
			system("pause");
			return false;
		}
	}

	void Display()
	{
    
    
		cout << endl << setw(12) << setiosflags(ios::left) << "姓名" << setw(12) << "学号" << setw(8) << "数学" << setw(8)
			<< "英语" << setw(8) << "计算机" << "\n\n";
		cout << setprecision(1) << setiosflags(ios::fixed);//设置显示的小数位数
		for (int i = 0; i < size; i++)
		{
    
    
			cout << setw(12) << stu[i].GetName() << setw(12) << stu[i].GetID() << setw(8) 
				<< stu[i].GetMath() << setw(8) << stu[i].GetEnglish() << setw(8) << stu[i].GetComputer() << endl;
		}
		cout << resetiosflags(ios::left);
		system("pause");
	}

	void Sort()//排序
	{
    
    
		char choice;
		do
		{
    
    
			cout << "请输入排序方式: 1.学号升序  2.总成绩降序 ";
			cin >> choice;
		} while (choice!='1'&&choice!='2');
		if (choice == '1')
			sort(&stu[0], &stu[0] + size, Compare1);
		else
			sort(&stu[0], &stu[0] + size, Compare2);
		cout << "重新排序完成。\n";
		system("pause");
	}

	char ShowMenu()
	{
    
    
		char choice;
		do
		{
    
    
			system("cls");//清屏
			cout << "    ----------欢迎使用成绩管理系统----------\n\n";
			cout << "       1. 添加学生\n";
			cout << "       2. 查找学生\n";
			cout << "       3. 删除学生\n";
			cout << "       4. 重新排序\n";
			cout << "       5. 显示全部\n";
			cout << "       6. 修改信息\n";
			cout << "       7. 退出\n";
			cin >> choice;
		} while (choice < '1' || choice>'7');
		return choice;
	}

};

int main()
{
    
    

	DataBase db;
	bool quit = false;
	char choice;

	db.Push("张三", "00006", 70.0, 72.0, 76.0);
	db.Push("李四", "00003", 62, 60, 70);
	db.Push("王大勇", "00001", 61.5, 74, 68.5);
	while (!quit)
	{
    
    
		choice = db.ShowMenu();
		switch (choice)
		{
    
    
		case '1':db.Push(); break;
		case '2':db.Search(); break;
		case '3':db.Delete(); break;
		case '4':db.Sort(); break;
		case '5':db.Display(); break;
		case '6':db.Alter(); break;
		case '7':quit = true; break;
		}
	}
	
	return 0;
}

二、控制台界面输出坐标控制

1.输出坐标控制

#include<iostream>
#include<Windows.h>//设置控制台界面
#include<time.h>//调用和时间相关的函数
#include<conio.h>//定义_getch()函数

using namespace std;

void SetOutputPosition(int x, int y)//设置输出坐标
{
    
    
	HANDLE h;//接收控制台输出设备
	h = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD pos;//获取控制台坐标
	pos.X = x;
	pos.Y = y;
	SetConsoleCursorPosition(h, pos);//设置控制台光标位置
}
int main()
{
    
    
	srand(time(0));//用当前时间来设定rand函数所用的随机数产生演算法的种子值。
	int x = 0,y = 0;
	char c;
	while (true)
	{
    
    
		SetOutputPosition(x, y);
		cout << "                    ";//输出空字串是为了擦掉先前的输出内容和第24行代码搭配实用
		//上面两行可以用system("cls");代替,需要头文件#include<stdlib.h>
		x = rand() % 40;
		y = rand() % 18;
		SetOutputPosition(x, y);
		cout << "欢迎回家!";
		c = _getch();
		if (c == VK_SPACE)
			break;
	}
	return 0;

2.控制台界面颜色修改

/*
0 = 黑色, 1 = 蓝色, 2 = 绿色, 3 = 湖蓝色, 4 = 红色, 5 = 紫色, 6 = 黄色, 7 = 白色, 8 = 灰色,
9 = 淡蓝色, A = 淡绿色, B = 淡浅绿色, C = 淡红色, D = 淡紫色, E = 淡黄色, F = 亮白色
*/
/*
文字前景色:
FOREGROUND_RED 红
FOREGROUND_GREEN 绿
FOREGROUND_BLUE 蓝
FOREGROUND_RED|FOREGROUND_GREEN 黄
FOREGROUND_RED|FOREGROUND_BLUE 紫红
FOREGROUND_GREEN|FOREGROUND_BLUE 淡蓝
0 黑
FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE 白
文字背景色
BACKGROUND_...
*/

#include<iostream>
#include<windows.h>

using namespace std;

int main()
{
    
    
	system("color 14");//设置控制台界面颜色(1:背景色;4:前景色)针对于全局
	HANDLE h;
	h = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleTextAttribute(h, FOREGROUND_GREEN | BACKGROUND_RED);//设置文本的背景色和前景色
	cout << "欢迎回家!\n";
	SetConsoleTextAttribute(h, FOREGROUND_RED |FOREGROUND_GREEN| BACKGROUND_BLUE);
	cout << "哈哈哈哈哈哈!\n";
	return 0;
}

3.实现高亮显示方向键选取菜单项

#include<iostream>
#include<Windows.h>
#include<conio.h>//调用getch()函数

using namespace std;

void SetOutputPosition(unsigned short x, unsigned short y)//设置输出坐标
{
    
    
	HANDLE handle_out;//定义句柄用于获得输出设备句柄
	COORD crd;
	crd.X = x; crd.Y = y;
	handle_out = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleCursorPosition(handle_out, crd);
}

int main()
{
    
    
	SetOutputPosition(20, 5);
	HANDLE h;
	h = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleTextAttribute(h, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
	cout << "假如有妹子向你表白怎么办?\n\n";
	int choice = 1;
	char c = 0;//用来接收getch();
	while (1)
	{
    
    
		if (c == 77)//按下了右方向键
			choice = choice % 3 + 1;
		if (c == 75)//按下了左方向键
			choice = (choice == 1 ? 3 : choice - 1);
		if (c == VK_RETURN)//表明做出了选择,离开循环体
			break;
		SetOutputPosition(14, 7);
		if (choice == 1)
			SetConsoleTextAttribute(h, FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_BLUE | BACKGROUND_GREEN);
		cout << "接受她";
		SetConsoleTextAttribute(h, FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
		SetOutputPosition(24, 7);
		if (choice == 2)
			SetConsoleTextAttribute(h, FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_BLUE | BACKGROUND_GREEN);
		cout << "拒绝她";
		SetConsoleTextAttribute(h, FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
		SetOutputPosition(34, 7);
		if (choice == 3)
			SetConsoleTextAttribute(h, FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_BLUE | BACKGROUND_GREEN);
		cout << "观察一段时间";
		//SetConsoleTextAttribute(h, FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
		c = _getch();
		//if (c == 0) c = _getch();
	}
	SetConsoleTextAttribute(h, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
	cout << "\n\n你做出了正确的选择。\n\n";
	return 0;
}

结果:

4.控制台界面修改

#include<iostream>
#include<conio.h>
#include<Windows.h>

using namespace std;

void SetOutputPosition(unsigned short x, unsigned short y)/*设定输出坐标*/
{
    
    
	HANDLE handle_out;//定义句柄用于获得输出设备句柄
	COORD crd;
	crd.X = x; crd.Y = y;
	handle_out = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleCursorPosition(handle_out, crd);
}

void GetConsoleSize(short* cols, short* lines)
{
    
    
	HANDLE h;
	h = GetStdHandle(STD_OUTPUT_HANDLE);
	CONSOLE_SCREEN_BUFFER_INFO csbi;
	GetConsoleScreenBufferInfo(h, &csbi);
	*cols = csbi.dwSize.X;
	*lines = csbi.dwSize.Y;
}

int main()
{
    
    
	system("mode con cols=40 lines=20");//设置控制台界面大小,cols为控制台的宽度,lines则代表控制台的高度。
	short cols, lines;
	GetConsoleSize(&cols, &lines);
	cout << "列数:" << cols << "\t行数:" << lines << endl;
	char c;
	do
	{
    
    
		SetOutputPosition(rand() % cols, rand() % lines);
		cout << (char)('A' + rand() % 26);
		c = _getch();
	} while (c != 'q');
	return 0;
}

三、标准模板库的应用

1.vector(向量)

向量使用动态分配的数组,这意味着一旦达到极限,就必须重新分配向量。

#include<iostream>
#include<vector>
#include<algorithm>
#include<time.h>

using namespace std;

/*排序标准*/
bool PrimeOdd(int a, int b)
{
    
    
	int m = a % 2;
	int n = b % 2;

	if (m > n)	return true;//使得奇数排在偶数前面
	else
		if (m == n)return a < b;//同为偶数比大小,小的在前,大的在后;
		else return false;//m<n(a为偶数,b为奇数);
}

int main()
{
    
    
	vector<int> shuzi;
	srand(time(0));//取得随机种子
	for (int i = 0; i < 10; i++)
		shuzi.push_back(rand() % 20);
	for (int i = 0; i < shuzi.size(); i++)
		cout << shuzi[i] << '\t';
	cout << endl;
	/*迭代器*/
	vector<int>::iterator it;//建立一个迭代器
	cout << "用迭代器遍历访问\n";
	for (it = shuzi.begin(); it != shuzi.end(); it++)
		cout << *it << '\t';
	cout << endl;
	sort(shuzi.begin(), shuzi.end());
	cout << "排序后\n";
	for (it = shuzi.begin(); it != shuzi.end(); it++)
		cout << *it << '\t';
	cout << endl;
	cout << "先奇数后偶数排序\n";
	sort(shuzi.begin(), shuzi.end(), PrimeOdd);
	for (it = shuzi.begin(); it != shuzi.end(); it++)
		cout << *it << '\t';
	cout << endl;
	cout << "乱序处理\n";
	random_shuffle(shuzi.begin(), shuzi.end());//洗牌函数
	for (it = shuzi.begin(); it != shuzi.end(); it++)
		cout << *it << '\t';
	cout << endl;
	return 0;
}

2.向量插入

#include<iostream>
#include<vector>
#include<algorithm>
#include<time.h>

using namespace std;

void InsertToVector(vector<int>& v, int n)//普通插入,逐个查找
{
    
    
	vector<int>::iterator it;
	for (it = v.begin(); it != v.end(); it++)
	{
    
    
		if (*it > n)
		{
    
    
			v.insert(it, n);//在位置l插入n,后面的值依次后移
			break;
		}
	}
}

void InsertToVector2(vector<int>& v, int n)//二分插入
{
    
    
	vector<int>::iterator l = v.begin();
	vector<int>::iterator m = v.begin()+v.size()/2;
	vector<int>::iterator r = v.end()-1;
	while(l<r)
	{
    
    
		if (*m > n)
		{
    
    
			//r = (m == r ? r - 1 : m);
			r = m - 1;
			m = l + (r - l) / 2;
		}
		else
		{
    
    
			//l = (m == l ? l + 1 : m);;
			l = l + 1;
			m = l + (r - l) / 2;
		}
	}
	if (*l > n)	v.insert(l, n);//在位置l插入n,后面的值依次后移
	else v.insert(l + 1, n);
}

int main()
{
    
    
	vector<int> shuzi;
	srand(time(0));//取得随机种子
	for (int i = 0; i < 10; i++) shuzi.push_back(rand() % 20);//往尾部添加新元素
	/*迭代器*/
	vector<int>::iterator it;//建立一个迭代器
	cout << "用迭代器遍历访问\n";
	for (it = shuzi.begin(); it != shuzi.end(); it++)
		cout << *it << '\t';
	cout << endl;
	sort(shuzi.begin(), shuzi.end());
	cout << "排序后\n";
	for (it = shuzi.begin(); it != shuzi.end(); it++)
		cout << *it << '\t';
	cout << endl;
	cout << "添加新元素\n新输入一个值:";
	int m;
	cin >> m;
	InsertToVector(shuzi, m);
	for (it = shuzi.begin(); it != shuzi.end(); it++)
		cout << *it << '\t';
	cout << endl;
	cout << "添加新元素\n新输入一个值:";
	cin >> m;
	InsertToVector2(shuzi, m);
	for (it = shuzi.begin(); it != shuzi.end(); it++)
		cout << *it << '\t';
	cout << endl;
	return 0;
}

3.向量删除

#include<iostream>
#include<vector>
#include<time.h>

using namespace std;

int main()
{
    
    
	vector<int> shuzi;
	srand(time(0));//取得随机种子
	for (int i = 0; i < 10; i++) shuzi.push_back(rand() % 20);//往尾部添加新元素
	//迭代器
	vector<int>::iterator it;//建立一个迭代器
	cout << "用迭代器遍历访问\n";
	for (it = shuzi.begin(); it != shuzi.end(); it++)
		cout << *it << '\t';
	cout << endl;
	//shuzi.pop_back()删除最末尾的元素
	for (it = shuzi.begin(); it != shuzi.end();)//删除所有偶数
	{
    
    
		if (*it % 2 == 0)//判断是否为偶数
			it = shuzi.erase(it);//删除迭代器所指元素,其后面的元素依次先前补齐
		else
			it++;
	}
	cout << "删除所有偶数之后......\n";
	for (it = shuzi.begin(); it != shuzi.end(); it++)
		cout << *it << '\t';
	cout << endl;
	return 0;
}

4.向量全排列

/*对于next_permutation函数,其函数原型为:
#include < algorithm>
bool next_permutation(iterator start,iterator end)
当当前序列不存在下一个排列时,函数返回false,否则返回true
注意:当当前序列为升序时,才能遍历所有排列结果,最后一个为降序序列。
prev_permutation(int *begin, int *end)相反
同时,相对应的,上一个排列即为prev_permutation(int *begin, int *end)end为向量元素的下一个地址*/

#include<iostream>
#include<vector>
#include<algorithm>//调用next_permutation()函数
#include<time.h>


using namespace std;

int main()
{
    
    
	vector<int>shuzi;
	srand(time(0));//取得随机种子
	for (int i = 0; i < 5; i++)
		shuzi.push_back(rand() % 20);//往尾部添加新元素
	//迭代器
	vector<int>::iterator it;
	cout << "用迭代器遍历访问\n";
	for (it = shuzi.begin(); it != shuzi.end(); it++)
		cout << *it << '\t';
	cout << endl << endl;
	sort(shuzi.begin(), shuzi.end());//默认升序
	do
	{
    
    
		for (it = shuzi.begin(); it != shuzi.end(); it++)
			cout << *it << '\t';
		cout << endl;
	} while (next_permutation(shuzi.begin(), shuzi.end()));
	
	cout << "有甲,乙,丙,丁四个成员的犯罪团伙被警察抓获并盘问。\n\n";
	cout << "甲:丙是我们的头目,一切是他指挥。\n";
	cout << "乙:我不是头目。\n";
	cout << "丙:我不是头目。\n";
	cout << "丁:甲说得对。\n\n";
	cout << "如果四人中只有一个说了真话,请问谁是头目?\n";
	
	bool head[4] = {
    
     false,false,false,true };//谁为头目,谁为true,
	//注意:之所以这样初始化是为了能够得到所有的排列序列
	
	int correct;//记录真话的数量
	do
	{
    
    
		correct = 0;
		if (head[2]) correct += 2;
		if (!head[1]) correct++;
		if (!head[2])correct++;
		if (correct == 1)break;
	} while (next_permutation(head, head + 4));
	if (head[0])  cout << "甲是头目。\n";
	if (head[1])  cout << "乙是头目。\n";
	if (head[2])  cout << "丙是头目。\n";
	if (head[3])  cout << "丁是头目。\n";

	return 0;
}

栈(stack)

#include<iostream>
#include<stack>
#include<time.h>

using namespace std;

int main()
{
    
    
	int n;
	stack<int> st;
	srand(time(0));
	for (int i = 0; i < 8; i++)
	{
    
    
		if (i < 3 || rand() & 1)
		{
    
    
			cout << "请往栈内添加一个新的整数: ";
			cin >> n;
			st.push(n);
		}
		else
		{
    
    
			if (st.empty())
				cout << "此时栈为空。\n";
			else
			{
    
    
				cout << "弹出栈顶元素" << st.top() << endl;
				st.pop();
			}
		}
	}
	while (!st.empty())
	{
    
    
		cout << "弹出栈顶元素: " << st.top() << endl;//t.top()读取栈顶元素,并未弹出元素
		st.pop();
	}
	cout << "此时栈内为空。\n";
	return 0;
}

stack_Hanoi(汉诺塔,栈实现)

#include<iostream>
#include<stack>

using namespace std;

struct Hanoi
{
    
    
	short plates;//当前盘子个数
	char from;//移出的柱子
	char via;//中转柱
	char to;//要移往的柱子

	//构造函数
	Hanoi() {
    
    };
	Hanoi(short p, char f, char v, char t):plates(p), from(f), via(v), to(t) {
    
    };
};

int main()
{
    
    
	Hanoi hanoi;
	stack<Hanoi> st;
	st.push(Hanoi(3, 'A', 'B', 'C'));
	while (!st.empty())
	{
    
    
		hanoi = st.top();
		st.pop();
		if (hanoi.plates == 1)
			cout << "从" << hanoi.from << "柱子移动到一个盘子到" << hanoi.to << "柱子。\n";
		else
		{
    
    
			st.push(Hanoi(hanoi.plates - 1, hanoi.via, hanoi.from, hanoi.to));//3.将剩下的n-1个盘子从B移到C
			st.push(Hanoi(1, hanoi.from, hanoi.via, hanoi.to));//2.将一个盘中从A移到C
			st.push(Hanoi(hanoi.plates - 1, hanoi.from, hanoi.to, hanoi.via));//1.将n-1个盘子从A移到B
		}
	}
	return 0;
}

队列

#include<iostream>
#include<queue>
#include<time.h>
#include<string>

using namespace std;

int main()
{
    
    
	cout << "主妇又开始了新的一天,请你来为她安排工作任务吧。\n";
	string order;
	queue<string> task;
	srand(time(0));
	for (int i = 0; i < 8; i++)
	{
    
    
		if (i < 3 || rand() & 1)//rand() & 1判断奇偶
		{
    
    
			cout << "请为她安排一项新任务:";
			cin >> order;
			task.push(order);//为队列添加一个元素
		}
		else
		{
    
    
			if (task.empty())//判断队列是否为空
				cout << "目前没有任务。\n";
			else
			{
    
    
				cout << task.front() << "已完成。\n";
				task.pop();//取走队头的元素
			}
		}
	}
	while (!task.empty())
	{
    
    
		cout << task.front() << "已完成。\n";//task.front()读取队头元素
		task.pop();//取走队头的元素
	}
	cout << "目前没有任务。\n";
	return 0;
}

猜你喜欢

转载自blog.csdn.net/m0_60534571/article/details/130469819