C++语言程序设计    郑莉 董渊 何江舟 编著        清华大学出版社

2-28.代码：

#include <iostream>
using namespace std;
int main()
{
cout << "Menu: A(dd) D(elete) S(ort) Q(uit), Select one:" << endl;
char cv;
do
{
cin>>cv;
if(cv=='Q')break;
else
{
switch(cv)
{
case 'A':cout<< "  数据已经增加。 " <<endl;continue;
case 'D':cout<< "  数据已经删除。 " <<endl;continue;
case 'S':cout<< "  数据已经排序。 " <<endl;continue;
}
}
}while(cv!='Q');
return 0;

}

2-29.代码：

for语句实现：

#include <iostream>
#include <math.h>
using namespace std;
bool ccv(int n)//找出质数：for 语句
{
int p=1;
for( int i=2;i<=sqrt(n);i++)
{
if(n%i==0)p=0;
}
return p;
}
int main()
{
int t=100;
cin>>t;
cout << "The prime number range 1 to "<< t <<" is " <<endl;
for(int m=2;m<=t;++m)
{
if(ccv(m))cout<< "   " <<  m <<endl;
}
return 0;

}

while语句实现：

#include <iostream>
#include <math.h>
using namespace std;
bool ccv(int n)//找出质数：while 语句
{
int p=1;
int i=2;
while(i<=sqrt(n))
{
if(n%i==0)p=0;
i++;
}
return p;
}
int main()
{
int t=100;
cin>>t;
cout << "The prime number range 1 to "<< t <<" is " <<endl;
int m=2;
while(m<=t)
{
if(ccv(m))cout<< "   " <<  m <<endl;
++m;
}
return 0;

}

do_while语句实现：

#include <iostream>
#include <math.h>
using namespace std;
bool ccv(int n)//找出质数：do_while 语句
{
int p=1;
int i=2;
do
{
if(n%i==0&&n!=2)p=0;
i++;
}while(i<=sqrt(n));
return p;
}
int main()
{
int t=100;
cin>>t;
cout << "The prime number range 1 to "<< t <<" is " <<endl;
int m=2;
do
{
if(ccv(m))cout<< "   " <<  m <<endl;
m++;
}while(m<=t);
return 0;

}

2-32.代码：这个是1~10000之间的，因为1~100太好猜了~~~

while语句实现：

#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
srand((unsigned)time(nullptr));
int i=rand()%10000;
int m=0;
cout<<        " The computer just set a number.  "
<<"Now,please guess what the number the computer set."<<endl;
cin>>m;
while(m!=i)
{
else   cout<<"Oh,your number is bigger." <<endl;
cin>>m;
}
if(m==i)
cout << "Congratulation! You get the number." << endl;
return 0;

}

do_while语句实现：

#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
srand((unsigned)time(nullptr));
int i=rand()%10000;
int m;
cout<<      " The computer just set a number.  "
<<"Now,please guess what the number the computer set."<<endl;
do
{
cin>>m;
}while(m!=i);
if(m==i)
cout << "Congratulation! You get the number." << endl;
return 0;

}

2-34.代码：这个主要取决于对题目的理解，理解得简单，程序也简单；往复杂里想，就实实在在的一道排列组合题了，还是超难的那种。。。下面这个算是中等吧

#include <iostream>
#include <iomanip>
using namespace std;
enum color{red,yellow,blue,white,black};
int main()
{
int k=0;
color sdx;
for(int i=red;i<=black;i++)
{
for(int j=red;j<=black;j++)
{
if(i!=j)
{
for(int p=red;p<=black;p++)
{
if(i!=p&&j!=p)
{
k++;
cout<<"  Number: "<<k;
for(int cx=0;cx<3;cx++)
{
switch(cx)
{
case 0:sdx=color(i);break;
case 1:sdx=color(j);break;
case 2:sdx=color(p);break;
}
switch(sdx)
{
case 0:cout<<setw(12)<<"   red";continue;
case 1:cout<<setw(12)<<"   yellow";continue;
case 2:cout<<setw(12)<<"   blue";continue;
case 3:cout<<setw(12)<<"   white";continue;
case 4:cout<<setw(12)<<"   black";continue;
}
}
cout<<endl;
}
}
}
}
}
cout<<"  Total:  "<<k<<endl;
return 0;

}

PS:本人学生一枚，初学C++,若有不足之处，还请各位大佬指正。

//不知道为什么，之前上传的图片全都不见了，只好重新截屏。。。