//2-28-(1)
#include <iostream>
using namespace std;
int main()
{
char x;
cout<<"Menu: A(dd) D(elete) S(ort) Q(uit),Select one:"<<endl;
cin>>x;
while (x!=‘\\0‘){
if (x==‘A‘){
cout<<"数据已经增加"<<endl;
cin>>x;
continue;}
else if (x==‘D‘){
cout<<"数据已经删除"<<endl;
cin>>x;
continue;}
else if (x==‘S‘){
cout<<"数据已经排序"<<endl;
cin>>x;
continue;}
else {
cout<<"退出"<<endl;
break; }}
return 0;
}
//2-28-(2)
#include <iostream>
using namespace std;
int main()
{
char x;
cout<<"Menu: A(dd) D(elete) S(ort) Q(uit),Select one:"<<endl;
cin>>x;
switch(x){
case ‘A‘:
cout<<"数据已经增加"<<endl;
continue;
case ‘D‘:
cout<<"数据已经删除"<<endl;
continue;
case ‘S‘:
cout<<"数据已经排序"<<endl;
continue;
default:
cout<<"退出"<<endl;
break;
}
return 0;
}
\\\\2-29-1
#include<iostream>
using namespace std;
int main()
{
int i,n;
for(i=2;i<=100;i++)
{
for(n=2;n<i;n++)
{
if(i%n==0)
break;
}
if(i==n)
cout<<i<<endl;
}
return 0;
}
\\\\2-29-2
#include<iostream>
using namespace std;
int main()
{
int i,n;
i=2;
while(i<=100)
{
n=2;
while(n<=i)
{
if(i%n==0)
break;
n++;
}
if(i==n)
cout<<i<<endl;
i++;
}
return 0;
}
//2-32-1
#include<iostream>
#include<cstdlib>
using namespace std;
int main(){
cout<<" try to guess my number between 1 to 100"<<endl;
cout<<" i will tell you if your number is bigger or smaller"<<endl;
int x,y;
x=rand()%100;
cin>>y;
while (y!=x) {
if (y>x){
cout<<"lower"<<endl;
cin>>y;
}else{
cout<<"higher"<<endl;
cin>>y;}}
cout<<"you are right"<<endl;
return 0;
}
//2-32-2
#include<iostream>
#include<cstdlib>
using namespace std;
int main(){
cout<<" try to guess my number between 1 to 100"<<endl;
cout<<" i will tell you if your number is bigger or smaller"<<endl;
int x,y;
x=rand()%100;
do {
cin>>y;
if (y>x){
cout<<"lower"<<endl;
}else{
cout<<"higher"<<endl;}}
while (y!=x) ;
cout<<"you are right"<<endl;
return 0;
}
//2-34
#include<iostream>
using namespace std;
int main(){
enum color{red,yellow,blue,whilt,black};
color p;
int x,y,z,n=0;
for(x=red;x<=black;x++)
for(y=x+1;y<=black;y++)
for(z=y+1;z<=black;z++)
if(x!=y&&x!=z&&y!=z)
n++;
cout<<"totlly "<<n<<" kinds of diffrent conditions"<<endl;
return 0;
}
实验总结:
本章节的学习是面向过程的编程,大部分语法与c语言相同,for /while/do while/switch/。同一个题目可由不同算法与不同语法来完成,要找到最高效简洁的方法。本次编程练习中,在输出质数一环节出现卡顿,尤其在使用do while语法时,未成功运行。而在随机数一环节,由于使用的是伪随机数函数,虽然成功运行,但失去了程序的乐趣性。