使用C++编写程序,运行时显示“Menu: A(dd) D(elete) S(ort) Q(uit), Select one:”提示 用户输入,A 表示增加,D 表示删除,S 表示排序,Q 表示退出,输入为 A、 D、S 时分别提示“ 数据已经增加、删除、排序。“输入为 Q 时程序结束。按照上述功能需求写两个程序,分别使用if分支语句和switch分支语句实现: 程序1要求:使用 if ...else 语句进行判断,用 break、continue 控制程序流程。程序2要求:使用 Switch 语句实现。(2)构思过程(可用文字、流程图等方式表达) 在一个大的循环中可以无限次地输入、输出,将输入的内容去初始化一个新的字符,再对该字符进行对应操作: 程序1.用if进行分类,逐步套用 程序2.用case进行分类,逐步套用
时间: 2023-12-03 12:44:21 浏览: 72
程序1:
```c++
#include <iostream>
using namespace std;
int main() {
char option;
while (true) {
cout << "Menu: A(dd) D(elete) S(ort) Q(uit), Select one: ";
cin >> option;
if (option == 'A' || option == 'a') {
cout << "Data has been added." << endl;
// do something to add data
} else if (option == 'D' || option == 'd') {
cout << "Data has been deleted." << endl;
// do something to delete data
} else if (option == 'S' || option == 's') {
cout << "Data has been sorted." << endl;
// do something to sort data
} else if (option == 'Q' || option == 'q') {
cout << "Program terminated." << endl;
break; // exit the loop
} else {
cout << "Invalid option. Please try again." << endl;
continue; // go back to the start of the loop
}
}
return 0;
}
```
程序2:
```c++
#include <iostream>
using namespace std;
int main() {
char option;
while (true) {
cout << "Menu: A(dd) D(elete) S(ort) Q(uit), Select one: ";
cin >> option;
switch (option) {
case 'A':
case 'a':
cout << "Data has been added." << endl;
// do something to add data
break;
case 'D':
case 'd':
cout << "Data has been deleted." << endl;
// do something to delete data
break;
case 'S':
case 's':
cout << "Data has been sorted." << endl;
// do something to sort data
break;
case 'Q':
case 'q':
cout << "Program terminated." << endl;
return 0; // exit the program
default:
cout << "Invalid option. Please try again." << endl;
break;
}
}
}
```
阅读全文