学生管理系统主菜单的操作优化,该优化后的区别在于可以通过键盘上的w和s键来控制程序中的<<的移动。
其中_getch()函数功能为获取键盘输入,不同于cin的是cin只能输入一次而_getch()能够输入多次。若键盘没有输入则没有返回值,其头文件为conio.h。
#include<iostream>
#include <conio.h>
using namespace std;
void system_menu(int choice)
{
cout << "* * * * * * * * * * * * * * * * *" << endl;
cout << "*\t 学生管理系统 \t*" << endl;
cout << "*\t\t\t\t*" << endl;
cout << "*\t 主菜单\t\t*" << endl;
cout << "*\t\t\t\t*" << endl;
cout << "*\t 添加学生信息 ";
if (choice == 1)cout << " <<\t*" << endl;
else cout << " \t*" << endl;
cout << "*\t\t\t\t*" << endl;
cout << "*\t 删除学生信息 ";
if (choice == 2)cout << " <<\t*" << endl;
else cout << " \t*" << endl;
cout << "*\t\t\t\t*" << endl;
cout << "*\t 修改学生信息 ";
if (choice == 3)cout << " <<\t*" << endl;
else cout << " \t*" << endl;
cout << "*\t\t\t\t*" << endl;
cout << "*\t 查找学生信息 ";
if (choice == 4)cout << " <<\t*" << endl;
else cout << " \t*" << endl;
cout << "*\t\t\t\t*" << endl;
cout << "*\t 显示学生信息 ";
if (choice == 5)cout << " <<\t*" << endl;
else cout << " \t*" << endl;
cout << "*\t\t\t\t*" << endl;
cout << "*\t 退出系统 ";
if (choice == 6)cout << " <<\t*" << endl;
else cout << " \t*" << endl;
cout << "*\t\t\t\t*" << endl;
cout << "*\t\t\t\t*" << endl;
cout << "*\t系统版本:1.0\t\t*" << endl;
cout << "*\t\t\t\t*" << endl;
cout << "* * * * * * * * * * * * * * * * *" << endl;
}
int main()
{
int choice = 1;
while (1)
{
system("cls");//清屏
system_menu(choice);
int move = _getch();
if (move != 13)
{
switch (move)//判断上下移动
{
case 'w':choice -= 1; if (choice < 1)choice = 6; break;
case 's':choice += 1; if (choice > 6)choice = 1; break;
case 'W':choice -= 1; if (choice < 1)choice = 6; break;
case 'S':choice += 1; if (choice > 6)choice = 1; break;
default:break;
}
continue;
}
//按了回车确定模式后进行以下内容
if (choice == 1)
{
system("cls");//清屏
cout << "暂无此功能" << endl;
system("pause");
}
else if (choice == 2)
{
system("cls");//清屏
cout << "暂无此功能" << endl;
system("pause");
}
else if (choice == 3)
{
system("cls");//清屏
cout << "暂无此功能" << endl;
system("pause");
}
else if (choice == 4)
{
system("cls");//清屏
cout << "暂无此功能" << endl;
system("pause");
}
else if (choice == 5)
{
system("cls");//清屏
cout << "暂无此功能" << endl;
system("pause");
}
else if (choice == 6)
{
exit(0);
}
}
return 0;
}
以下是程序运行效果图: