还剩23页未读,继续阅读
本资源只提供10页预览,全部文档请下载后查看!喜欢就下载吧,查找使用更方便
文本内容:
实验报告第五题设计一种程序,用筛选法求出3~100之间的所有素数,规定每行输出5个素数判断一种整数与否为素数应设计一种函数来实现源程序清单#includeiostream.h#includeiomanip.hint funi ntnfor int i=2;in;i++if n%i==0return0;return1;void mainintcount=0;for inti=3;i100;i++if funi|cout«i«setw4;count++;if count%5==0cout«\n;cout«\n;}运行成果:357111317192329313741434753596167717379838997Press.any key1to continue第六题把二维数组a赋值给b,并把b中元素时值等于a中对应元素的平方源程序清单#includeiostream.h#includestr ing.hvoid mainint a
[3]
[4],b
[3]
[4],i,j;;;fori=0i3i++forj=0;j4;j++cin»a[i][j];fori=0;i3;i++for j=0;j4;j++b[i][j]=a[i][j]*a[i][j];fori=0;i3;i++for j=0;j4;j++cout«b[i][j]«f\f;cout«f\nf;运行成果:123423453456149164916259162536Press anykey tocontinue第七题下面的程序求出1000以内的挛生质数的对数改错题源程序清单#i ncIudei ostream.h#includeiomanip.h#includemath.hint isprimeintxint k,i;k=i ntsqrtx;fori=2;i=k;i++if x%i==0return0;return1;void maininta
[500],count,k=0;i ntb
[35]
[2];for inti=3;i1000;i+=2if isprimeia[k++]=i;count=0;fori=0;ik-1;i++if a[i+1]-a[i]==2b[count]
[0]=a[i];b[count++]
[1]=a[i+1];}}for i=0;icount;i++if i%5==0cout\n;«coutC setw⑶b[i]
[0],setw3b[i]
[1],,,\t;««««««««}cout end I,,count=,,count end I;«««}运行成果:,3,55711,1317,1929,3141,4359,6171,73101,103〉107,109137,139〈149,151179,181191,193〉197,1991227-229〉239,241〉269,271〉281,283〈311,313347,349419」421〉431,433461,463521,523569,511599,601617,
619641.643〉
659.661〉809,811821,823827,829857,859881-883〉:ount=35Jress anykey tocontinue.第八题有两个字符串,将主字符串中的所有子字符串删去并输出,例如,主字符串为This is a verygood book”,子字符串为book,成果为This isa verygood源程序清单#includeiostream.h#includestring.hint withstrchar*s1,char*subswhile*subs*s1==*subs{s1++;subs++;}return!*subs;}char*delsub char*s1,char*subschar*p1,*p2;int len=strIensubs;p2=p1=s1;whi Ie*p1if withstrp1,subsp1+=len;else*p2++=*p1++;}*p2=\0,;return s1;void mainchar str1
[80]=,,This isa verygood book;char str2
[40]=,,bookM;cout str1end I str2end I;««««cout deI substr1,str2end I;««运行成果This isa uery good bookbook ThisisauerygoodPress anykey tocontinue第九题设计一种通用函数查询一种带通配符”的字符串在另一种字符串中出现的次数,在主程序中进行测试源程序清单#includeiostream.hint funchar*str,char*sear,int n{n=0char*s,*f;for;*str;str++{if*str==*sear||*sear=={s=str;f=sear;s++;f++;i f*f=={if*f+D=*s!=\0n++;eIse{s++;f++;}while*s++==*f++if*f==\0,{n++;break;}}}return n;}void mainO{int n;char str
[81],s0
[3],int,int*,int*,分别记录3个字符串中所有的数字字符,英文字母及其他字符的数量,编写程序测试该函数源程序清单:#includeiostream.hvoid statchar*s
[3],intnum,int*aphlet,int*other{inti;char*p;num=*aphlet=*other=0;for i=0;i3;i++{P=S[i];cout,,s,,i+1,n pendl;«««««while*p{if*p=0*p=9num++;else if*p=,A,*p=,Z,||*p=,a,*p=,z,*aphlet++;else*other++;p++;}}void mainOchar*s
[3]={,,1Ab12c/#$Den,H tGkp9h7*cGHd+,,,,Tj^g587dd@fHD%f52nG=;fint n,a,o;stat s,n,a,o;cout,,num=,,n endI;««cout,,aphlet=n aendl;««cout,,other=n oendl;«««运行成果为r:---------------------------------------------*C:\Users\hp\Desktop\Debug\test.exe pl1Abl2c/#$De卜2tGkp9h7*cGHd+卜3Tj/vg587ddfHDzf52nG=nun=10aphlet=25other=12Press anykey tocontinue第十二题建立一种类SUM,求一种二维数组外围各元素之和,并输出数组中各元素所求之和详细规定如下:1私有数据组员int array
[5]
[5];〃二维整型数组int s;〃数组array各外围元素之和2公有组员函数SUM intd
[5]
[5]:构造函数,初始化组员数据void process:求二维数组外围元素之和svoid print:输出二维数组每行输出五个元素及其外围元素之和3在主程序中对该类进行测试使用如下测试数据外E指阴影部分的元素「2345678910|11121314151617181920[2122232425源程序清单#includeiostream.hclass SUM{int array
[5]
[5];int s;public:SUMint d
[5]
[5];void process;void print;;SUM::SUMint d
[5]
[5]{for inti=0;i5;i++for intj=0;j5;j++array[i][j]=d[i][j];;void SUM::process{int t=0,tl=0;for inti=0;i5;i++for intj=0;j5;j++t+=array[i][j];for i=l;i4;i++for intj=0;j5;j++tl+=array[i]Ej];s=t+tl;;void SUM::print{forint i=0;i5;i++{for intj=0;j5;j++cout«array[i][j]«,\t,;cout«,\n,;}cout«,zSUM=z«s«endl;}void main{int aa
[5]
[5];inta=l;for inti=0;i5;i++for intj=0;j5;j++aa[i][j]=a++;SUM ssaa;ss.process;ss.print;运行成果:C:\Users\hp\Desktop\Debug\qq.exe12345678910111213141516171819202122232425SUM=520Press anykey tocontinue老师辛劳!谢谢您!!=7*5=25=712899097ress anykey tocontinue第二题设计一种程序,求出之间的所有素数100~10000源程序清单#i ncIudei ostream.h#includeiomanip.hvoid maininti,n,count=0;for n=101;n1000;n++for i=2;i=n-1;i++ifn%i==0break;if in-1cout setw4n;««count++;if count%10==0cout endI;«}cout endI;«运行成果:1011031071091131271311371391491511571631671731791811911931971992112232272292332392412512572632692127728128329330731131331733133734734935335936737337938338939740140941942143143343944344945746146346747948749149950350952152354154755756356957157758759359960160761361761963164164364765365966167367768369170170971972773373974375175776176977378779780981182182382782983985385785986387788188388790911919929937941947953967971977983991997Press;an1J keqj tocontinue.第三题设计一种程序,输入一种任意整数(厂位),4求出各位数字之和,并记录出位数源程序清单#i ncIudei ostrearn.hvoid mainintn,k,s=0,t=0;cout,,n=|,endl;««c in n;»while nk=n%10;n=n/10;couts1\t1t endI;««««运行成果:n=4321104Press anykey tocontinue第四题设计一种程序,计算组合数C m,r=m!/r!*m-r!,其中m、r为正整数,且mr分别求出04,
2.C6,
4、C8,7的值规定用函数来实现求阶乘和组合数,在主函数中测试源程序清单#include iostream.h#include stdIib.hfl oatf intn{if n0{cout n应为正数!\n;}«float s;if n==11|n==0return1;elses=n*f n-1;return s;}float cint叫int r{if mr{cout nm不应不大于r!\nn;}«return f m/f r*fm-r;}void main{int m,r;cin mr;»»cout cm,yr=”《cm,r\n;}«««运行成果42c4,2=6Press anykey tocontinue46,4=15ress anykey tocontinue8,7=8ress anykey tocontinue。