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