ImageVerifierCode 换一换
格式:DOCX , 页数:17 ,大小:19.28KB ,
资源ID:8626857      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/8626857.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(C++实验报告2.docx)为本站会员(b****6)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

C++实验报告2.docx

1、C+实验报告2C+实验报告11. 实验内容:了解基本编程语句,if语句,switch语句,for循环,输入输出语句。2. 实验目的:读懂教材f0203-f0212.cpp几个程序。3. 主要仪器:计算机一台,VS-2008。4. 实验步骤:1. 创建空白新工程,向新工程中添加空文件2. 编写代码3. 编译、调试并运行。5附录:/ f0203.cpp/ 拆成两个函数/=#includeusing namespace std;/-void sphere();/-int main() sphere();/-void sphere() double radius; coutradius; if(rad

2、ius 0) return; coutThe result is radius*radius*3.14*4n;/ f0204.cpp/ 字符直角三角形/=#includeusing namespace std;/-int main() for(int i=1; i=10; +i) for(int j=1; j=i; +j) coutM; coutendl;调试结果:M字符的直角三角形。/ f0205.cpp/ 字符倒三角形/=#includeusing namespace std;/-int main() for(int i=1; i=10; +i) for(int j=1; j=i-1; +

3、j) cout ; for(int k=1; k=21-2*i; +k) coutM; coutendl;调试结果:M字符的倒三角形。/=/ f0208.cpp/ 判断一个整数是否素数的稍微优化版/=#include#include / sqrt()using namespace std;/-int main() coutm; double sqrtm=sqrt(m*1.0); for(int i=2; i=sqrtm; +i) if(m%i=0) coutm isnt a prime.n; return 1; coutm is a prime.n;/=/=/ f0209.cpp/ 倒三角形流

4、状态设置版/=#include#includeusing namespace std;/-int main() for(int n=1; n=10; +n) coutsetfill( )setw(n) setfill(M)setw(21-2*n)Mendl;/=/=/ f0212.cpp/ 用筛法判断素数/=#include#include#includeusing namespace std;/-int main() vector prime(10000,1); for(int i=2; i100; +i) / 构造素数集合 if(primei) for(int j=i; i*ja & a1

5、 & a10000; ) / 判断素数 couta is(primea ? : not) a prime.n; getchar();/=调试结果:3 is a prime.12 is not a prime101 is a prime.131 is a prime.6 is a not prime.实验总结:1. 对于字符图形,一般用两重循环,外循环遍历所有行,内循环遍历行中每个字符。2. 上面程序中的%是取余的意思。3. 用筛法判断素数:从2开始的某个连续整数集合,留下2,除去所有2的倍数,留下3,除去3的所有倍数,留下5,除去5 的所有倍数,如此等等,留下某个最先遇到的素数,将其所有的倍数

6、从该数集中去掉,最后留下的全是素数了。C+实验报告21.实验内容:关于goto语句的运用,以及while循环语句的使用。2.实验目的:读懂教材f0213.cppf0220.cpp几个程序。3.主要仪器:计算机一台,VS-2008。4.实验步骤:1. 创建空白新工程,向新工程中添加空文件2. 编写代码3. 编译、调试并运行。附录:几个C+程序如下:213#include using namespace std;int main()int a;goto Init;Forward: a=a+1;Print: coutaendl; goto Down;Init: a=1; goto Print;Dow

7、n: if(a100) goto Forward;214.#include using namespace std;int main()for(int cock=1; cock=13; +cock )for(int hen=1; hen=18; +hen) for(int chick=1; chick =96; +chick) if(7*cock+5*hen+chick/3-100) continue; if(cock+hen+chick-100) continue; if(chick%3) continue; coutCock:cock, Hens:hen, Chicks:100-cock-

8、henendl;调试结果:Cock:3,Hens:10,chick:87215.#include using namespace std;int main()for(int cock=1; cock=13; +cock) for(int hen=1; hen=18; +hen) if(7*cock+5*hen+(100-cock-hen)/3-100) continue; if(100-cock-hen)%3) continue; coutcock:cock, hen:hen, chick:100-cock-henendl; 调试结果:Cock:3,Hens:10,chick:87216#in

9、clude using namespace std;int main() for(int cock=1; cock=13; +cock ) for(int hen=1; hen=18; +hen) if(100-cock-hen)%3=0 & 7*cock+5*hen+(100-cock-hen)/3=100) coutcock:cock, hen:hen, chick:100-cock-henendl; 调试结果:Cock:3,Hens:10,chick:87218.#include#include#includeusing namespace std;int main() double s

10、um=0,item=1; for(int n=1;fabs(item)1e-6;+n) item *= (-1.0)*(2*n-3)/(2*n-1); sum +=item; coutPi =fixedsum*4endl;调试结果:Pi=3.141595219.#include#includeusing namespace std;int main() double sum=0,item=1; for(int denom=1, sign=1; fabs(item)1e-6; denom +=2, sign *=-1) item =sign/double(denom); sum += item;

11、 coutPi =fixedsum*4endl;调试结果:Pi=3.141595220.#include#includeusing namespace std;int main() double sum=0,item=1; int denom=1, sign=1; while(fabs(item)1e-6) denom +=2; sign *=1; item =sign*1.0/denom; sum +=item; coutPi =fixedsum*4endl;调试结果:Pi=26.171751实验总结:1认真读懂程序的每一句,了解程序输出的结果。2.goto语句太灵活,初学者使用起来有一定的

12、难度。3.当用while循环写的程序很难看懂时,偶尔用do-while循环。4.输程序时要做到快而准,不出小错误。C+实验报告31.实验内容: 数据类型的学习,二进制的初步了解与学习,C-串与string.2.实验目的:读懂教材关于以上内容的几个程序。3. 主要仪器:计算机一台,VS-2008。4. 实验步骤: 1. 创建空白新工程,向新工程中添加空文件2. 编写代码3. 编译、调试并运行。5.附录:/=/ f0304.cpp/ C串操作/=#includeusing namespace std;/-int main() char* s1 = Hello ; char* s2 = 123; c

13、har a20; strcpy(a, s1); / copy cout(strcmp(a,s1)=0 ? : not)equaln; / compare coutstrcat(a, s2)endl; / concatenate coutstrrev(a)endl; / modify coutstrset(a, c)endl; / set cout(strstr(s1, ell) ? : not )foundn; / find string cout(strchr(s1,c) ? : not )foundn; / find char/=调试结果:EqualHello 123321 olleHcc

14、cccccccfoundnot found/=/ f0305.cpp/=#include#includeusing namespace std;/-int main() string a, s1 = Hello ; string s2 = 123; a = s1; / copy cout(a=s1 ? : not)equaln; / compare cout(a+=s2)endl; / concatenate reverse(a.begin(), a.end(); / reverse coutaendl; couta.replace(0,9,9,c)endl; / set cout(s1.fi

15、nd(ell)!= -1 ? : not )foundn;/ find string cout(s1.find(c)!= -1 ? : not )foundn; / find char/=调试结果与304相同。/=/ f0306.cpp/ 整行读入再分解读入/=#include#include#includeusing namespace std;/-int main() ifstream in(aaa.txt); for(string s; getline(in, s); ) int a, sum=0; for(istringstream sin(s); sina; sum += a); c

16、outsumendl; /=调试结果:1443231383524实验总结:1. 库函数的操作,默认在string.h的头文件中。由于BCB编译器的iostream头文件嵌套包含了它,所以程序中省略了#include指令。2. Istringstream是输入string流,它在sstream资源中说明。3. Getline函数返回的是流状态,通过其可以判断文件是否还有数据可读。C+实验报告41.实验内容:数组的认识与学习,向量的学习,指针与引用。2.实验目的:通过调试程序完成学习内容。3.实验器材:计算机一台,VS-2008系统。4.实验步骤:1. 创建空白新工程,向新工程中添加空文件2. 编

17、写代码3. 编译、调试并运行。附录:/=/ f0308.cpp/ 探测数组初值/=#includeusing namespace std;/-int array15=1,2,3; / 有初始化int array25; / 无初始化/-int main() int array35=2; / 有初始化 int array45; / 无初始化 coutarray1: ; for(int i=0; i5; +i) coutarray1i ; coutnarray2: ; for(int i=0; i5; +i) coutarray2i ; coutnarray3: ; for(int i=0; i5;

18、 +i) coutarray3i ; coutnarray4: ; for(int i=0; i5; +i) coutarray4i ; coutn;/=调试结果:array1: 1 2 3 0 0array2: 0 0 0 0 0array3: 2 0 0 0 0array4: 1245072 845597673 0 0 4198406 /=/ f0310.cpp/ 向量操作/=#include#include#includeusing namespace std;/-int main() ifstream in(aaa.txt); vector s; for(int a; ina; ) s

19、.push_back(a); int pare=0; for(int i=0; is.size()-1; +i) for(int j=i+1; js.size(); +j) if(si=sj) pare+; coutparen;/=调试结果: 4 /=/ f0311.cpp/ 若干个向量按长短排序/=#include#include#include#includeusing namespace std;/-typedef vectorvector Mat;Mat input();void mySort(Mat& a);void print(const Mat& a);/-int main()

20、Mat a = input(); mySort(a); print(a);/-Mat input() ifstream in(aaa.txt); Mat a; for(string s; getline(in, s); ) vector b; istringstream sin(s); for(int ia; sinia; ) b.push_back(ia); a.push_back(b); return a;/-void mySort(Mat& a) for(int pass=1; passa.size(); +pass) for(int i=0; ia.size()-pass; +i) if(ai+1.size()ai.size() ai.swap(ai+1);/-void print(const Mat& a) for(int i=0; ia.size(); +i) for(int j=0; jai.size(); +j) coutaij ; coutendl; /=调试结果:812 11212 231256 232 12 2312 3 45 67 8 9/=

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

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