---恢复内容开始---
实验结论
2-28
(1)if else语句
#include <iostream>
using namespace std;
int main()
{
char n;
cout<<"Menu: A(dd) D(elete) S(ort) Q(uit),Select one: "<<endl;
cin>>n;
while(n!='Q')
{
if(n=='A')
{
cout<<"数据已经增加"<<endl;
}
if(n=='D')
{
cout<<"数据已经删除"<<endl;
}
if(n=='S')
{
cout<<"数据已经排序"<<endl;
}
cin>>n;
}
return 0;
}

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

2-29
(1)while循环
#include<iostream>
using namespace std;
int main()
{
int count=0,i=2;
while(i<=100)
{
count=0;
for(int j=1;j<=i;j++)
{
if(i%j==0)
{
count++;
}
}
if(count==2)
{
cout<<i<<" ";
}
i++;
}
return 0;
}
(2)for循环
#include<iostream>
using namespace std;
int main()
{
int count=0;
for(int i=2;i<=100;i++)
{
count=0;
for(int j=1;j<=i;j++)
{
if(i%j==0)
{
count++;
}
}
if(count==2)
{
cout<<i<<" ";
}
}
return 0;
}
(3)do while语句
#include<iostream>
using namespace std;
int main()
{
int count=0,i=2;
do
{
count=0;
for(int j=1;j<=i;j++)
{
if(i%j==0)
{
count++;
}
}
if(count==2)
{
cout<<i<<" ";
}
i++;
}
while(i<=100);
return 0;
}

2-32
(1)while语句
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
int main()
{
int x;
srand(time(0));
int number=rand()%100+1;
cout<<"猜一下这个数: ";
cin>>x;
while(1)
{
if(x!=number)
{
if(x<number)
{
cout<<"小了"<<endl;
}
else
{
cout<<"大了"<<endl;
}
}
else
{
cout<<"恭喜猜对了!"<<endl;break;
}
cin>>x;
}
return 0;
}

(2)do while 语句
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
int main()
{
int x;
srand(time(0));
int number=rand()%100+1;
cout<<"猜一下这个数: "<<endl;
cin>>x;
do
{
if(x!=number)
{
if(x<number)
{
cout<<"小了"<<endl;
}
else
{
cout<<"大了"<<endl;
}
}
else
{
cout<<"恭喜猜对了!"<<endl;break;
}
cin>>x;
}while(1);
return 0;
}

2-34
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
enum color{red,yellow,blue,white,black};
color pri;
int i,j,k,n=0,loop;
for(i=red;i<=black;i++)
{
for(j=red;j<=black;j++)
{
if(i!=j)
{
for(k=red;k<=black;k++)
{
if(k!=i&&k!=j)
{
n=n+1;
cout<<setw(3)<<n;
for(loop=1;loop<=3;loop++)
{
switch(loop)
{
case 1:pri=color(i);break;
case 2:pri=color(j);break;
case 3:pri=color(k);break;
default :break;
}
switch(pri)
{
case red:cout<<setw(8)<<"红";break;
case yellow:cout<<setw(8)<<"黄";break;
case blue:cout<<setw(8)<<"蓝";break;
case white:cout<<setw(8)<<"白";break;
case black:cout<<setw(8)<<"黑";break;
default :break;
}
}
cout<<endl;
}
}
}
}
}
cout<<"total:"<<n<<endl;
return 0;
}

实验总结与体会
for语句中的两个分号是不能省略的
while语句不能省略结束循环的语句
do…while循环语句中,在while括号后,要加分号
---恢复内容结束---
实验结论
2-28
(1)if else语句
#include <iostream>
using namespace std;
int main()
{
char n;
cout<<"Menu: A(dd) D(elete) S(ort) Q(uit),Select one: "<<endl;
cin>>n;
while(n!='Q')
{
if(n=='A')
{
cout<<"数据已经增加"<<endl;
}
if(n=='D')
{
cout<<"数据已经删除"<<endl;
}
if(n=='S')
{
cout<<"数据已经排序"<<endl;
}
cin>>n;
}
return 0;
}

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

2-29
(1)while循环
#include<iostream>
using namespace std;
int main()
{
int count=0,i=2;
while(i<=100)
{
count=0;
for(int j=1;j<=i;j++)
{
if(i%j==0)
{
count++;
}
}
if(count==2)
{
cout<<i<<" ";
}
i++;
}
return 0;
}
(2)for循环
#include<iostream>
using namespace std;
int main()
{
int count=0;
for(int i=2;i<=100;i++)
{
count=0;
for(int j=1;j<=i;j++)
{
if(i%j==0)
{
count++;
}
}
if(count==2)
{
cout<<i<<" ";
}
}
return 0;
}
(3)do while语句
#include<iostream>
using namespace std;
int main()
{
int count=0,i=2;
do
{
count=0;
for(int j=1;j<=i;j++)
{
if(i%j==0)
{
count++;
}
}
if(count==2)
{
cout<<i<<" ";
}
i++;
}
while(i<=100);
return 0;
}

2-32
(1)while语句
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
int main()
{
int x;
srand(time(0));
int number=rand()%100+1;
cout<<"猜一下这个数: ";
cin>>x;
while(1)
{
if(x!=number)
{
if(x<number)
{
cout<<"小了"<<endl;
}
else
{
cout<<"大了"<<endl;
}
}
else
{
cout<<"恭喜猜对了!"<<endl;break;
}
cin>>x;
}
return 0;
}

(2)do while 语句
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
int main()
{
int x;
srand(time(0));
int number=rand()%100+1;
cout<<"猜一下这个数: "<<endl;
cin>>x;
do
{
if(x!=number)
{
if(x<number)
{
cout<<"小了"<<endl;
}
else
{
cout<<"大了"<<endl;
}
}
else
{
cout<<"恭喜猜对了!"<<endl;break;
}
cin>>x;
}while(1);
return 0;
}

2-34
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
enum color{red,yellow,blue,white,black};
color pri;
int i,j,k,n=0,loop;
for(i=red;i<=black;i++)
{
for(j=red;j<=black;j++)
{
if(i!=j)
{
for(k=red;k<=black;k++)
{
if(k!=i&&k!=j)
{
n=n+1;
cout<<setw(3)<<n;
for(loop=1;loop<=3;loop++)
{
switch(loop)
{
case 1:pri=color(i);break;
case 2:pri=color(j);break;
case 3:pri=color(k);break;
default :break;
}
switch(pri)
{
case red:cout<<setw(8)<<"红";break;
case yellow:cout<<setw(8)<<"黄";break;
case blue:cout<<setw(8)<<"蓝";break;
case white:cout<<setw(8)<<"白";break;
case black:cout<<setw(8)<<"黑";break;
default :break;
}
}
cout<<endl;
}
}
}
}
}
cout<<"total:"<<n<<endl;
return 0;
}

实验总结与体会
for语句中的两个分号是不能省略的
while语句不能省略结束循环的语句
do…while循环语句中,在while括号后,要加分号
浙公网安备 33010602011771号