C++实验报告2.docx

上传人:b****6 文档编号:8626857 上传时间:2023-02-01 格式:DOCX 页数:17 大小:19.28KB
下载 相关 举报
C++实验报告2.docx_第1页
第1页 / 共17页
C++实验报告2.docx_第2页
第2页 / 共17页
C++实验报告2.docx_第3页
第3页 / 共17页
C++实验报告2.docx_第4页
第4页 / 共17页
C++实验报告2.docx_第5页
第5页 / 共17页
点击查看更多>>
下载资源
资源描述

C++实验报告2.docx

《C++实验报告2.docx》由会员分享,可在线阅读,更多相关《C++实验报告2.docx(17页珍藏版)》请在冰豆网上搜索。

C++实验报告2.docx

C++实验报告2

C++实验报告1

1.实验内容:

了解基本编程语句,if语句,switch语句,for循环,输入输出语句。

2.实验目的:

读懂教材f0203-f0212.cpp几个程序。

3.主要仪器:

计算机一台,VS-2008。

4.实验步骤:

1.创建空白新工程,向新工程中添加空文件

2.编写代码

3.编译、调试并运行。

5.附录:

//f0203.cpp

//拆成两个函数

//=====================================

#include

usingnamespacestd;

//-------------------------------------

voidsphere();

//-------------------------------------

intmain(){

sphere();

}//------------------------------------

voidsphere(){

doubleradius;

cout<<"Pleaseinputradius:

";

cin>>radius;

if(radius<0)return;

cout<<"Theresultis"<

}

//f0204.cpp

//字符直角三角形

//=====================================

#include

usingnamespacestd;

//-------------------------------------

intmain(){

for(inti=1;i<=10;++i){

for(intj=1;j<=i;++j)

cout<<"M";

cout<

 

调试结果:

M字符的直角三角形。

//f0205.cpp

//字符倒三角形

//=====================================

#include

usingnamespacestd;

//-------------------------------------

intmain(){

for(inti=1;i<=10;++i){

for(intj=1;j<=i-1;++j)

cout<<"";

for(intk=1;k<=21-2*i;++k)

cout<<"M";

cout<

调试结果:

M字符的倒三角形。

 

//=====================================

//f0208.cpp

//判断一个整数是否素数的稍微优化版

//=====================================

#include

#include//sqrt()

usingnamespacestd;

//-------------------------------------

intmain(){

cout<<"pleaseinputanumber:

\n";

intm;

cin>>m;

doublesqrtm=sqrt(m*1.0);

for(inti=2;i<=sqrtm;++i)

if(m%i==0){

cout<

return1;

}

cout<

}//====================================

 

//=====================================

//f0209.cpp

//倒三角形流状态设置版

//=====================================

#include

#include

usingnamespacestd;

//-------------------------------------

intmain(){

for(intn=1;n<=10;++n)

cout<

<

}//====================================

//=====================================

//f0212.cpp

//用筛法判断素数

//=====================================

#include

#include

#include

usingnamespacestd;

//-------------------------------------

intmain(){

vectorprime(10000,1);

for(inti=2;i<100;++i)//构造素数集合

if(prime[i])

for(intj=i;i*j<10000;++j)

prime[i*j]=0;

ifstreamin("a.txt");

for(inta;in>>a&&a>1&&a<10000;)//判断素数

cout<

"":

"not")<<"aprime.\n";

getchar();

}//====================================

调试结果:

3isaprime.

12isnotaprime

101isaprime.

131isaprime.

6isanotprime..

实验总结:

1.对于字符图形,一般用两重循环,外循环遍历所有行,内循环遍历行中每个字符。

2.上面程序中的%是取余的意思。

3.用筛法判断素数:

从2开始的某个连续整数集合,留下2,除去所有2的倍数,留下3,除去3的所有倍数,留下5,除去5的所有倍数,如此等等,留下某个最先遇到的素数,将其所有的倍数从该数集中去掉,最后留下的全是素数了。

C++实验报告2

1.实验内容:

关于goto语句的运用,以及while循环语句的使用。

2.实验目的:

读懂教材f0213.cpp—f0220.cpp几个程序。

3.主要仪器:

计算机一台,VS-2008。

4.实验步骤:

1.创建空白新工程,向新工程中添加空文件

2.编写代码

3.编译、调试并运行。

附录:

几个C++程序如下:

213.

#include

usingnamespacestd;

intmain()

{inta;

gotoInit;

Forward:

a=a+1;

Print:

cout<

gotoDown;

Init:

a=1;

gotoPrint;

Down:

if(a<100)gotoForward;

 

}

214.

#include

usingnamespacestd;

intmain()

{

for(intcock=1;cock<=13;++cock)

for(inthen=1;hen<=18;++hen)

for(intchick=1;chick<=96;++chick){

if(7*cock+5*hen+chick/3-100)continue;

if(cock+hen+chick-100)continue;

if(chick%3)continue;

cout<<"Cock:

"<

"<

"<<100-cock-hen<

}

调试结果:

Cock:

3,Hens:

10,chick:

87

 

215.

#include

usingnamespacestd;

intmain()

{for(intcock=1;cock<=13;++cock)

for(inthen=1;hen<=18;++hen){

if(7*cock+5*hen+(100-cock-hen)/3-100)continue;

if((100-cock-hen)%3)continue;

cout<<"cock:

"<

"<

"<<100-cock-hen<

}

调试结果:

Cock:

3,Hens:

10,chick:

87

216.

#include

usingnamespacestd;

intmain()

{for(intcock=1;cock<=13;++cock)

for(inthen=1;hen<=18;++hen)

if((100-cock-hen)%3==0&&7*cock+5*hen+(100-cock-hen)/3==100)

cout<<"cock:

"<

"<

"<<100-cock-hen<

}

调试结果:

Cock:

3,Hens:

10,chick:

87

 

218.

#include

#include

#include

usingnamespacestd;

intmain(){

doublesum=0,item=1;

for(intn=1;fabs(item)>1e-6;++n){

item*=(-1.0)*(2*n-3)/(2*n-1);

sum+=item;

}

cout<<"Pi="<

}

调试结果:

Pi=3.141595

219.

#include

#include

usingnamespacestd;

intmain(){

doublesum=0,item=1;

for(intdenom=1,sign=1;fabs(item)>1e-6;denom+=2,sign*=-1){

item=sign/double(denom);

sum+=item;

}

cout<<"Pi="<

}

调试结果:

Pi=3.141595

220.

#include

#include

usingnamespacestd;

intmain(){

doublesum=0,item=1;

intdenom=1,sign=1;

while(fabs(item)>1e-6){

denom+=2;

sign*=1;

item=sign*1.0/denom;

sum+=item;

}

cout<<"Pi="<

}

调试结果:

Pi=26.171751

 

实验总结:

1认真读懂程序的每一句,了解程序输出的结果。

2.goto语句太灵活,初学者使用起来有一定的难度。

3.当用while循环写的程序很难看懂时,偶尔用do-while循环。

4.输程序时要做到快而准,不出小错误。

 

C++实验报告3

1.实验内容:

数据类型的学习,二进制的初步了解与学习,C-串与string.

2.实验目的:

读懂教材关于以上内容的几个程序。

3.主要仪器:

计算机一台,VS-2008。

4.实验步骤:

1.创建空白新工程,向新工程中添加空文件

2.编写代码

3.编译、调试并运行。

5.附录:

//=====================================

//f0304.cpp

//C串操作

//=====================================

#include

usingnamespacestd;

//-------------------------------------

intmain(){

char*s1="Hello";

char*s2="123";

chara[20];

strcpy(a,s1);//copy

cout<<(strcmp(a,s1)==0?

"":

"not")<<"equal\n";//compare

cout<

cout<

cout<

cout<<(strstr(s1,"ell")?

"":

"not")<<"found\n";//findstring

cout<<(strchr(s1,'c')?

"":

"not")<<"found\n";//findchar

}//====================================

调试结果:

Equal

Hello123

321olleH

ccccccccc

found

notfound

//=====================================

//f0305.cpp

//=====================================

#include

#include

usingnamespacestd;

//-------------------------------------

intmain(){

stringa,s1="Hello";

strings2="123";

a=s1;//copy

cout<<(a==s1?

"":

"not")<<"equal\n";//compare

cout<<(a+=s2)<

reverse(a.begin(),a.end());//reverse

cout<

cout<

cout<<(s1.find("ell")!

=-1?

"":

"not")<<"found\n";//findstring

cout<<(s1.find('c')!

=-1?

"":

"not")<<"found\n";//findchar

}//====================================

调试结果与304相同。

//=====================================

//f0306.cpp

//整行读入再分解读入

//=====================================

#include

#include

#include

usingnamespacestd;

//-------------------------------------

intmain(){

ifstreamin("aaa.txt");

for(strings;getline(in,s);){

inta,sum=0;

for(istringstreamsin(s);sin>>a;sum+=a);

cout<

}

}//====================================

调试结果:

144

323

13

8

3524

实验总结:

1.库函数的操作,默认在string.h的头文件中。

由于BCB编译器的iostream头文件嵌套包含了它,所以程序中省略了#include指令。

2.Istringstream是输入string流,它在sstream资源中说明。

3.Getline函数返回的是流状态,通过其可以判断文件是否还有数据可读。

 

C++实验报告4

1.实验内容:

数组的认识与学习,向量的学习,指针与引用。

2.实验目的:

通过调试程序完成学习内容。

3.实验器材:

计算机一台,VS-2008系统。

4.实验步骤:

1.创建空白新工程,向新工程中添加空文件

2.编写代码

3.编译、调试并运行。

 

附录:

//=====================================

//f0308.cpp

//探测数组初值

//=====================================

#include

usingnamespacestd;

//-------------------------------------

intarray1[5]={1,2,3};//有初始化

intarray2[5];//无初始化

//-------------------------------------

intmain(){

intarray3[5]={2};//有初始化

intarray4[5];//无初始化

cout<<"array1:

";

for(inti=0;i<5;++i)

cout<

cout<<"\narray2:

";

for(inti=0;i<5;++i)

cout<

cout<<"\narray3:

";

for(inti=0;i<5;++i)

cout<

cout<<"\narray4:

";

for(inti=0;i<5;++i)

cout<

cout<<"\n";

}//====================================

调试结果:

array1:

12300

array2:

00000

array3:

20000

array4:

1245072845597673004198406

 

//=====================================

//f0310.cpp

//向量操作

//=====================================

#include

#include

#include

usingnamespacestd;

//-------------------------------------

intmain(){

ifstreamin("aaa.txt");

vectors;

for(inta;in>>a;)

s.push_back(a);

intpare=0;

for(inti=0;i

for(intj=i+1;j

if(s[i]==s[j])pare++;

cout<

}//====================================

调试结果:

4

//=====================================

//f0311.cpp

//若干个向量按长短排序

//=====================================

#include

#include

#include

#include

usingnamespacestd;

//-------------------------------------

typedefvector>Mat;

Matinput();

voidmySort(Mat&a);

voidprint(constMat&a);

//-------------------------------------

intmain(){

Mata=input();

mySort(a);

print(a);

}//------------------------------------

Matinput(){

ifstreamin("aaa.txt");

Mata;

for(strings;getline(in,s);){

vectorb;

istringstreamsin(s);

for(intia;sin>>ia;)

b.push_back(ia);

a.push_back(b);

}

returna;

}//------------------------------------

voidmySort(Mat&a){

for(intpass=1;pass

for(inti=0;i

if(a[i+1].size()

}//------------------------------------

voidprint(constMat&a){

for(inti=0;i

for(intj=0;j

cout<

cout<

}

}//====================================

调试结果:

8

121

12122312

562321223

123456789

 

//=====================

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高等教育 > 农学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1