实验一 c++简单程序设计

时间:2023-03-09 19:35:44
实验一 c++简单程序设计

一、实验内容

1、ex 2_28

(1) 用if...else判断

 #include<iostream>
using namespace std;
int main()
{
char i;
cout<<"Menu: A(dd) D(elect) S(ort) Q(uit), Select one:";
while(cin>>i)
{
if(i=='A')
cout<<"Data has been added."<<endl;
else if(i=='D')
cout<<"Data has been delected."<<endl;
else if(i=='S')
cout<<"Data has been sorted."<<endl;
else if(i=='Q') exit();
else
cout<<"Have no choice,please select another one."<<endl;
cout<<"Menu: A(dd) D(elect) S(ort) Q(uit), Select one:";
}
return ;
}

运行结果如下:

实验一 c++简单程序设计

(2)用switch语句

 #include<iostream>
using namespace std;
int main()
{
char i;
cout<<"Menu: A(dd) D(elect) S(ort) Q(uit), Select one:";
while(cin>>i)
{
switch(i)
{
case 'A':cout<<"Data has been added."<<endl;break;
case 'D':cout<<"Data has been delected."<<endl;break;
case 'S':cout<<"Data has been sorted."<<endl;break;
case 'Q':exit();
default:cout<<"have no choice,please select another one."<<endl;
}
cout<<"Menu: A(dd) D(elect) S(ort) Q(uit), Select one:";
}
return ;
}

结果同上。

2、ex 2_29

(1)while循环

 #include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int i=,j,k=;
while(i<=)
{
j=;
while(j<=i)
{
if(i%j==) break;
j++;
}
if(i==j)
{
cout<<setw()<<i;
k++;
}
i++;
if(k%==) cout<<endl;
}
return ;
}

结果如下:

实验一 c++简单程序设计

(2)do...while循环

 #include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int i=,j,k=;
do
{
j=;
do
{
if(i%j==) break;
j++;
}while(j<=i);
if(i==j)
{
cout<<setw()<<i;
k++;
}
i++;
if(k%==) cout<<endl;
}while(i<=);
return ;
}

(3)for循环

 #include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int i,j,k=;
for(i=;i<=;i++)
{
for(j=;j<=i;j++)
if(i%j==) break;
if(i==j)
{
cout<<setw()<<i;
k++;
}
if(k%==) cout<<endl;
}
return ;
}

3、ex 2_32

(1)while循环

 #include<iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
srand(time());
int m=rand()%+,n; cout<<"Please write down the number you guess:";
while(cin>>n)
{
if(m<n)
cout<<"What you guess is bigger than the number."<<endl;
if(m>n)
cout<<"What you guess is smaller than the number."<<endl;
if(m==n)
{cout<<"Congratulation!"<<endl;break;}
cout<<"Please write down the number you guess:";
}
return ;
}

(2)do...while循环

 #include<iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
srand(time());
int m=rand()%+,n; cout<<"Please write down the number you guess:";
do
{
cin>>n;
if(m<n)
cout<<"What you guess is bigger than the number."<<endl;
if(m>n)
cout<<"What you guess is smaller than the number."<<endl;
if(m==n)
{cout<<"Congratulation!"<<endl;break;}
cout<<"Please write down the number you guess:";
}while(m!=n);
return ;
}

效果如下:

实验一 c++简单程序设计

 4、ex 2_34

 #include<iostream>
#include<iomanip>
using namespace std;
enum Balls{red,yellow,blue,white,black};
void Print(int a)
{
switch(a)
{
case :cout<<setw()<<"red";break;
case :cout<<setw()<<"yellow";break;
case :cout<<setw()<<"blue";break;
case :cout<<setw()<<"white";break;
case :cout<<setw()<<"black";break;
}
}
int main()
{
int n=,i,j,k;
for(i=red;i<=blue;i++)
{
for(j=i+;j<=white;j++)
{ for(k=j+;k<=black;k++)
{
Print(i);Print(j);Print(k);
cout<<endl;
n++;
}
}
}
cout<<"There are "<<n<<" ways in all."<<endl;
return ;
}

效果如下:

实验一 c++简单程序设计

二、实验反思

1、注意"A"与'A'的区别,"A"表示字符串,'A'表示字符,比较的是ASCII码。

2、三种循环各具特点,用前应思考其作用,明显看出29题用for循环简洁。

3、生成随机数时,注意头文件cstdlib,ctime;设置域宽时,注意头文件iomanip。

4、使用枚举类型,返回球颜色时,我使用一个了函数将数字与颜色对应。

三、实验小评

https://www.cnblogs.com/fifi1224/p/10555549.html

https://www.cnblogs.com/yinyinzuinihai/p/10556280.html#4213197

https://www.cnblogs.com/sora5934/p/10562100.html