2-25 编写一个完整的程序,实现功能:向用户提问“现在正在下雨吗?”,提示用户输入Y 或N。若输入为Y,显示“现在正在下雨。”;若输入为N,显示“现在没有下雨。”;否则继续提问“现在正在下雨吗?”。
#include
using namespace std;
int main()
{
char i;
while(1){
cout<<"现在正在下雨吗?"<>i;
if(i=='Y'){
cout<<"现在正在下雨。"<
2-26 编写一个完整的程序,运行时向用户提问“你考试考了多少分?(0~100)”,接收输入后判断其等级显示出来。规则如下:
#include
using namespace std;
int main()
{
int score;
cout<<"你考试考了多少分?(0 ~ 100)"<>score;
if(score>=90&&score<=100)
cout<<"优"<=80&&score<90)
cout<<"良"<=60&&score<80)
cout<<"中"<=0&&score<60)
cout<<"差"<
2-27 实现一个简单的菜单程序,运行时显示“M enu:A (dd) D(elete) S(ort) Q (uit), Select one:”提示用户输入。A 表示增加,D 表示删除,S 表示排序,Q 表示退出。输入为 A、D、S时分别提示“数据已经增加、删除、排序。”,输入为Q 时程序结束。
(1)要求使用 if…else 语句进行判断,用 break,continue 控制程序流程。
(2)要求使用 switch 语句。
//(1) if-else 语句
#include
using namespace std;
int main()
{
char c;
while(1){
cout<<"Menu:A(dd) D(elete) S(ort) Q(uit),Select one:";
cin>>c;
if(c=='A'){
cout<<"数据已经增加"<
//(2)switch 语句
#include
using namespace std;
int main()
{
char c;
while(1){
cout<<"Menu:A(dd) D(elete) S(ort) Q(uit),Select one:";
cin>>c;
switch(c){
case 'A':
cout<<"数据已经增加"<
2-28 用穷举法找出 1~100 间的质数并显示出来。分别使用 while,do…while,for循环语句实现。
//while
#include
#include
using namespace std;
int main()
{
int i=2,j,k,flag;
while(i<=100){
flag=1;
j=2;
k=sqrt(i);
while(j<=k){
if(i%j==0){
flag=0;
break;
}
j++;
}
if(flag)
cout<
//do-while
#include
#include
using namespace std;
int main()
{
int i=2,j,k,flag;
do{
flag=1;
j=2;
k=sqrt(i);
while(j<=k){
if(i%j==0){
flag=0;
break;
}
j++;
}
if(flag)
cout<
//for
#include
#include
using namespace std;
int main()
{
for(int i=2;i<=100;i++){
int flag=1;
for(int j=2;j<=sqrt(i);j++){
if(i%j==0){
flag=0;
break;
}
}
if(flag)
cout<
2-30 声明一个表示时间的结构体,可以精确表示年、月、日、小时、分、秒;提示用户输入年、月、日、小时、分、秒的值,然后完整地显示出来。
#include
using namespace std;
struct shijian{
int year,mouth,day,hour,min,second;
};
int main()
{
shijian s;
cout<<"请输入年、月、日、小时、分、秒的值:";
cin>>s.year>>s.mouth>>s.day>>s.hour>>s.min>>s.second;
cout<
2-31 在程序中定义一个整型变量,赋予 1~100 的值。要求用户猜这个数,比较两个数的大小,把结果提示给用户,直到猜对为止。分别使用 while,do…while语句实现循环。
//while
#include
#include
#include
using namespace std;
int main()
{
int x;
srand(time(0)); //设置随机种子
int num=rand()%100+1; //随机生成一个1~100之间的数
cout<<"请猜一下这个数为多少: ";
while(cin>>x)
{
if(x!=num)
{
if(x
//do-while
#include
#include
#include
using namespace std;
int main()
{
int x;
srand(time(0)); //设置随机种子
int num=rand()%100+1; //随机生成一个1~100之间的数
cout<<"请猜一下这个数为多少: ";
cin>>x;
do{
if(x!=num)
{
if(x>x);
return 0;
}
2-32 口袋中有红、黄、蓝、白、黑 5 种颜色的球若干个。每次从口袋中取出 3 个不同颜色的球,问有多少种取法?
#include
#include //这个头文件是来设置输出格式的,比如设置输出占几个字符宽度的setw()
using namespace std;
enum color{red,yellow,blue,white,black}; //枚举类型
void print(int n)
{
switch(n)
{
//left表示左对齐
case red: cout<
好了,今天的分享就到这。
本专栏为本人大二C++课程的习题作业和一些学习经验的分享,供大家参考学习。如有侵权请立即与我联系,我将及时处理。
参考书籍为:C++语言程序设计 第五版 -清华大学出版社- 郑莉,董渊、C++语言程序设计 第五版 -清华大学出版社- 郑莉,董渊(学生用书)
编译环境:Visual Studio 2019、Dev-C++
2-26 编写一个完整的程序,运行时向用户提问“你考试考了多少分?(0~100)”,接收输入后判断其等级显示出来。规则如下:
#includeusing namespace std; int main() { int score; cout<<"你考试考了多少分?(0 ~ 100)"< >score; if(score>=90&&score<=100) cout<<"优"< =80&&score<90) cout<<"良"< =60&&score<80) cout<<"中"< =0&&score<60) cout<<"差"< 2-27 实现一个简单的菜单程序,运行时显示“M enu:A (dd) D(elete) S(ort) Q (uit), Select one:”提示用户输入。A 表示增加,D 表示删除,S 表示排序,Q 表示退出。输入为 A、D、S时分别提示“数据已经增加、删除、排序。”,输入为Q 时程序结束。
(1)要求使用 if…else 语句进行判断,用 break,continue 控制程序流程。
(2)要求使用 switch 语句。
//(1) if-else 语句 #includeusing namespace std; int main() { char c; while(1){ cout<<"Menu:A(dd) D(elete) S(ort) Q(uit),Select one:"; cin>>c; if(c=='A'){ cout<<"数据已经增加"< //(2)switch 语句 #includeusing namespace std; int main() { char c; while(1){ cout<<"Menu:A(dd) D(elete) S(ort) Q(uit),Select one:"; cin>>c; switch(c){ case 'A': cout<<"数据已经增加"< 2-28 用穷举法找出 1~100 间的质数并显示出来。分别使用 while,do…while,for循环语句实现。
//while #include#include using namespace std; int main() { int i=2,j,k,flag; while(i<=100){ flag=1; j=2; k=sqrt(i); while(j<=k){ if(i%j==0){ flag=0; break; } j++; } if(flag) cout< //do-while #include#include using namespace std; int main() { int i=2,j,k,flag; do{ flag=1; j=2; k=sqrt(i); while(j<=k){ if(i%j==0){ flag=0; break; } j++; } if(flag) cout< //for #include#include using namespace std; int main() { for(int i=2;i<=100;i++){ int flag=1; for(int j=2;j<=sqrt(i);j++){ if(i%j==0){ flag=0; break; } } if(flag) cout< 2-30 声明一个表示时间的结构体,可以精确表示年、月、日、小时、分、秒;提示用户输入年、月、日、小时、分、秒的值,然后完整地显示出来。
#includeusing namespace std; struct shijian{ int year,mouth,day,hour,min,second; }; int main() { shijian s; cout<<"请输入年、月、日、小时、分、秒的值:"; cin>>s.year>>s.mouth>>s.day>>s.hour>>s.min>>s.second; cout< 2-31 在程序中定义一个整型变量,赋予 1~100 的值。要求用户猜这个数,比较两个数的大小,把结果提示给用户,直到猜对为止。分别使用 while,do…while语句实现循环。
//while #include#include #include using namespace std; int main() { int x; srand(time(0)); //设置随机种子 int num=rand()%100+1; //随机生成一个1~100之间的数 cout<<"请猜一下这个数为多少: "; while(cin>>x) { if(x!=num) { if(x //do-while #include#include #include using namespace std; int main() { int x; srand(time(0)); //设置随机种子 int num=rand()%100+1; //随机生成一个1~100之间的数 cout<<"请猜一下这个数为多少: "; cin>>x; do{ if(x!=num) { if(x >x); return 0; } 2-32 口袋中有红、黄、蓝、白、黑 5 种颜色的球若干个。每次从口袋中取出 3 个不同颜色的球,问有多少种取法?
#include#include //这个头文件是来设置输出格式的,比如设置输出占几个字符宽度的setw() using namespace std; enum color{red,yellow,blue,white,black}; //枚举类型 void print(int n) { switch(n) { //left表示左对齐 case red: cout< 好了,今天的分享就到这。
本专栏为本人大二C++课程的习题作业和一些学习经验的分享,供大家参考学习。如有侵权请立即与我联系,我将及时处理。
参考书籍为:C++语言程序设计 第五版 -清华大学出版社- 郑莉,董渊、C++语言程序设计 第五版 -清华大学出版社- 郑莉,董渊(学生用书)
编译环境:Visual Studio 2019、Dev-C++



