//文中有格式错误请无视

//这个编辑器一言难尽

实验目的


1. 掌握c++中类c部分的编程知识: 数据类型,常量,变量,运算符,表达式,分支结构,循环结构

2. 掌握C++中数据输入和输出的基本方法

3. 熟练使用c++程序开发环境,掌握c++程序编写、编译、运行、调试的方法

实验准备


1. 简单的c++程序结构 学习/复习教材「2.1.3 C++程序实例」

2. c++中数据输入输出的基本方法 学习/复习教材2.3节,学习C++中I/O流、预定义的插入符<<和提取符>>的基本用法。

3. if语句、switch语句、while语句、do…while语句的用法 学习/复习教材2.4节,通过示例理解背后简单算法及c++分支语句、循环语句的用法

4. 自定义数据类型: typedef,枚举类型用法 学习/复习教材2.5节,结合示例理解枚举类型和int型在类型转换时的注意事项

实验内容


Part2: 编程练习

教材第2章习题2-28 简单的菜单程序

教材第2章习题2-29 穷举法求质数

教材第2章习题2-32 猜数

教材第2章习题2-34排列组合

实验结论


2-28简单的菜单程序(if-else

Code:

 #include<iostream>
using namespace std;
int main()
{
cout<<"Menu:A(dd) D(elete) S(ort) Q(uit)";
cout<<"Select one:";
char k;
while(cin>>k)
{
if(k=='A')
cout<<"Data has added.\n"<<"Menu:A(dd) D(elete) S(ort) Q(uit)"<<"Select one:";
else if(k=='D')
cout<<"Data has deleted.\n"<<"Menu:A(dd) D(elete) S(ort) Q(uit)"<<"Select one:";
else if(k=='S')
cout<<"Data has sorted.\n"<<"Menu:A(dd) D(elete) S(ort) Q(uit)"<<"Select one:";
else if(k=='Q')
break;
else
cout<<"No such choice,please select again.\n"<<"Menu:A(dd) D(elete) S(ort) Q(uit)"<<"Select one:";
}
return ;
}

Screenshot:

实验1:c++简单程序设计(1)-LMLPHP

(switch

Code:

 #include<iostream>
using namespace std;
int main()
{
cout<<"Menu:A(dd) D(elete) S(ort) Q(uit)";
cout<<"Select one:";
char k;
while(cin>>k&&k!='Q')
{
switch(k)
{
case 'A':
cout<<"Data has added.\n"<<"Menu:A(dd) D(elete) S(ort) Q(uit)"<<"Select one:";
continue;
case 'D':
cout<<"Data has deleted.\n"<<"Menu:A(dd) D(elete) S(ort) Q(uit)"<<"Select one:";
continue;
case 'S':
cout<<"Data has sorted.\n"<<"Menu:A(dd) D(elete) S(ort) Q(uit)"<<"Select one:";
continue;
default:cout<<"No such choice,please select again.\n"<<"Menu:A(dd) D(elete) S(ort) Q(uit)"<<"Select one:";
}
}
return ;
}

Screenshop:

实验1:c++简单程序设计(1)-LMLPHP

2-29 穷举法求质数(while

Code:

 #include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
int main()
{
cout<<"1~100间的质数为:"<<endl;
int x=;
int a,b,c=;
while(x<=)
{
a=sqrt(x);
if(x!=)
{
b=;
while(x%b!=&&b<=a)
b++;
if(b>a)
{
c++;
cout<<setw()<<x;
if(c%==)
cout<<"\n";
}
}
x++;
}
return ;
}

Screenshop:

实验1:c++简单程序设计(1)-LMLPHP

(for

Code:

 #include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
int main()
{
cout<<"1~100间的质数为:"<<endl;
int x;
int a,b,c=;
for(x=;x<=;x++)
{
a=sqrt(x);
if(x==)
continue;
else
{
for(b=;b<=a;b++)
if(x%b==)
break;
if(b>a)
{
c++;
cout<<setw()<<x;
if(c%==)
cout<<"\n";
}
}
}
return ;
}

Screenshop:

实验1:c++简单程序设计(1)-LMLPHP

(do-while

Code:

 #include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
int main()
{
cout<<"1~100间的质数为:"<<endl;
int x=;
int a,b,c=;
do
{
x++;
a=sqrt(x);
if(x!=)
{
b=;
do
{
b++;
}while(x%b!=&&b<=a);
if(b>a)
{
c++;
cout<<setw()<<x;
if(c%==)
cout<<"\n";
}
}
}while(x<);
return ;
}
 

Screenshop:

实验1:c++简单程序设计(1)-LMLPHP

2-32 猜数(while

Code:

 #include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;
int main()
{
int x;
int a;
srand(time(NULL));
x=+rand()%(-+);
cout<<"Your guess number is(1~100): ";
cin>>a;
while(a!=x)
{
if(a<x)
{
cout<<"Bigger than the number."<<endl;
cout<<"Your guess number is(1~100): ";
cin>>a;
}
else
{
cout<<"Lower than the number. "<<endl;
cout<<"Your guess number is(1~100): ";
cin>>a;
}
}
cout<<"Congretulations.You guessed it.~";
return ;
}

Screenshop:

实验1:c++简单程序设计(1)-LMLPHP

(do-while

Code:

 #include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;
int main()
{
int x;
int a;
srand(time(NULL));
x=+rand()%(-+);
cout<<"Your guess number is(1~100): ";
do
{
cin>>a;
if(a<x)
{
cout<<"Bigger than the number."<<endl;
cout<<"Your guess number is(1~100): ";
}
else if(a>x)
{
cout<<"Lower than the number. "<<endl;
cout<<"Your guess number is(1~100): ";
}
}while(a!=x);
cout<<"Congretulations.You guessed it.~";
return ;
}

Screenshop:

实验1:c++简单程序设计(1)-LMLPHP

//暴力循环

2-34排列组合(排列

Code:

 #include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int x[],k=,i;
for(x[]=;x[]<=;x[]++)
{
for(x[]=;x[]<=;x[]++)
{
if(x[]==x[])
continue;
else
{
for(x[]=;x[]<=;x[]++)
{
if(x[]==x[]||x[]==x[])
continue;
else
{
k++;
for(i=;i<=;i++)
{
if(i!=)
{
switch(x[i])
{
case :
cout<<"red ";
continue;
case :
cout<<"yellow ";
continue;
case :
cout<<"blue ";
continue;
case :
cout<<"white ";
continue;
case :
cout<<"black ";
}
}
else
{
switch(x[i])
{
case :
cout<<"red\n";
continue;
case :
cout<<"yellow\n";
continue;
case :
cout<<"blue\n";
continue;
case :
cout<<"white\n";
continue;
case :
cout<<"black\n";
}
}
}
}
}
}
}
}
cout<<"Total: "<<k;
return ;
}

Screenshop:

实验1:c++简单程序设计(1)-LMLPHP

(组合

Code:

 #include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int x[],k=,i;
for(x[]=;x[]<=;x[]++)
for(x[]=x[]+;x[]<=;x[]++)
for(x[]=x[]+;x[]<=;x[]++)
{
k++;
for(i=;i<=;i++)
{
if(i!=)
{
switch(x[i])
{
case :
cout<<"red ";
continue;
case :
cout<<"yellow ";
continue;
case :
cout<<"blue ";
continue;
case :
cout<<"white ";
continue;
case :
cout<<"black ";
}
}
else
{
switch(x[i])
{
case :
cout<<"red\n";
continue;
case :
cout<<"yellow\n";
continue;
case :
cout<<"blue\n";
continue;
case :
cout<<"white\n";
continue;
case :
cout<<"black\n";
}
}
}
}
cout<<"Total: "<<k;
return ;
}

Screenshop:

实验1:c++简单程序设计(1)-LMLPHP

实验总结


这次试验主要是类C的部分,CPP的特性我还没有完全体会到(从I/O流已经可以看出些特点的程度)。

在进行多次判断时还是用switch更方便。

多数时候for循环较为好用。

写程序时尽量避免嵌套过多层循环,严重拖慢编译和运行速度。

srand(time(NULL));

x=1+rand()%(100-1+1);

以此来取一定范围内的随机数。

枚举数据类型不同于整型或字符型。

04-28 11:38