版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
謹慎#include<iostream.h>voidmain(void){cout?"***************\n”.cout?"Hello!\nH;cout?H************** ?實驗1-2#include<iostream.h>voidmain(void){inta,b,c,sum;cout?"Inputa,be”;cin?a?b?c;sum=a+b+c;cout?"Sum="?sum?endl;實驗1-3#include<iostream.h>voidmain(void){floata,b,l,s;cout?"Inputa,b:";cin?a?b;l=2*(a+b);s=a*b;cout?ML=u?l?endl;cout?"S="?s?endl;實驗1-4#include<iostream.h>voidmain(void){floatf,c;coutくぐ請輸入華氏溫度值:”;cin?f;c=(f-32)*5.O/9.O;coutvv”攝氏溫度值為:“vvcvvendl;實驗1-5#include<iostream.h>voidmain(void){intn,a,b,c,d,m;cout?"Inputn:M;cin?n;a=n/1000;b=(n-a*1000)/100;c=(n-a*1000-b*100)/10;d=n%10;m=d*l000+c*100+b*10+a;cout?"m="?m?endl;實驗2-1#include<iostream.h>voidmain(void){inta;cout?nInputa:";cin?a;if(a>0)couivvavv”為正數(shù)。\nH;elsecoutvvavv”為負數(shù)。\n";if(a%2==0)coutvvavv”為偶數(shù)。\n”;elsecouivvavv”為奇數(shù)。\nH;實驗2-2#include<iostream.h>#include<math.h>voidmain(void){doublex,y;cout?MInputx:H;cin?x;if(x<5)y=-x+3.5;elseif(x<10)y=20-3.5*(x+3)*(x+3);elsey=x/2.0-3.5+sin(x);cout?Hy=M?y?endl;實驗2-3#include<iostream.h>voidmain(void){inta,c;floatr,m,f;coutvv”請輸入獎金數(shù)額:";cin?a;if(a>=8OO)c=8;elsec=a/100;switch(c){case0:r=0;break;:r=0.02;break;case3:r=0.04;break;case7:r=0.08;break;case8:r=0.1;break;)m=a*r;f=a-m;cout?M稅率:"?r?endl;cout?"應(yīng)交稅款:"<vmv<endl;coutvv”實得獎金:"?f?endl;練習3-14#include<iostream.h>voidmain(void){inta,b,c;cout?nInputa,b,c:n;cin?a?b?c;if(a>b&&b>c)cout<<a?'\t'?b?\t,?c?endl;elseif(a>c&&c>b)cout?a?,\t'?c?At'<<b?endl;elseif(b>a&&a>c)cout<<b<<At,?a?,\t,?c?endl;elseif(b>c&&c>a)cout<<b?,\t,?c?At,?a?endl;elseif(c>a&&a>b)cout?c?'\t'?a?'\t'?b?endl;elsecout?c?,\t'?b?V?a?endl;練習3-16#include<iostream.h>voidmain(void){floatdataLdata2,result;charop;coutvv”輸入兩個實數(shù):";cin?dataI?data2;coutvv”輸入運算符:";cin?op;switch(op){case,+,:result=data1+data2;break;case'-'result二datal-data2;break;case'*':result=datal*data2;break;case7':result=datal/data2;break;cout?data1<<op<<data2?'=,?result?endl;練習3-21#include<iostream.h>voidmain(void){inti;floatt,sum;sum=0;for(i=1;i<=100;i++){t=1.0/i;sum=sum+t;)cout?,,Sum=,'?sum?endl;)#include<iostream.h>voidmain(void){inti;floatt,sum;sum=0;i=l;while(i<=100){t=1.0/i;sum=sum+t;i++;)cout?"Sum=H?sum?endl;)#include<iostream.h>voidmain(void){inti;floatt,sum;sum=0;i=l;do{t=1.0/i;sum=sum+t;i++;while(i<=100);cout?HSum=n?sum?endl;練習3-22#include<iostream.h>voidmain(void){floatx,t,y;coutvv”請輸入x的值(x>0):cin?x;t=1.0;y=0.0;while(t>=le-4){y=y+t;t=t/x;)cout?My="?y?endl;例題2-26 編寫能顯示平行四邊形圖形(圖形"ボ’組成)的程序。#include<iostream.h>voidmain(void){inti,j,k;for(i=l;i<=5;i++){for(k=l;k<=5-i;k++)cout?*for(j=l;j<=10;j++)cout?**';cout?endl;)練習3-24#include<iostream.h>voidmain(void){intn,i,j,k;floatc,sum,avg;i=0;j=0;k=0;sum=0.0;coutvv”請輸入十天的溫度值:";for(n=l;n<=!〇;n++){cin?c;if(c>0)i++;elseif(c==0)j++;elsek++;sum=sum+c;)avg=sum/10.0;cout?"0度以上天數(shù):"?i?endl;cout?"0度天數(shù):M?j?endl;cout?"0度以下天數(shù):u?k?endl;coutvv”平均溫度:"vvavgvvendl;練習3-25#include<iostream.h>voidmain(void){inti;longintt;floats;s=0;t=l;for(i=l;i<=10;i++){t=t*i;s=s+t;)cout?s?endl;練習3-26#include<iostream.h>voidmain(void){inti,j,k;for(i=l;i<=33;i++)for(j=1;j<=5O;j++){k=100-i-j;if((k%2==0)&&(i*3+j*2+k/2=100))cout?i?At,?j?,\t,?k?endl;練習3-27#include<iostream.h>voidmain(void){inti,j,k;for(i=l;i<=5;i++){for(k=l;k<=5-i;k++)cout?'for(j=l;j<=2*i-l;j++)cout?'*';cout?endl;)for(i=l;i<=4;i++){for(k=l;k<=i;k++)cout?'for(j=l;j<=9-2*i;j++)cout?**';cout?endl;實驗4-1#include<iostream.h>#defineN10voidmain(void){inti,a,b,c,d,e;floats[N];coutvv”請輸入成績:";for(i=0;i<N;i++)cin?s[i];a=0:b=0;c=0;d=0;e=0;for(i=0;i<N;i++){if(s[i]>=90&&s[i]<=100)a++;elseif(s[i]>=80&&s[i]<=89)b++;elseif(s[i]>=70&&s[i]<=79)C++;elseif(s[i]>=60&&s[i]<=69)d++;elsee++;)coutvv”各分數(shù)段人數(shù)為:H?endl;coutvv”優(yōu)(90-100):H?a?endl;coutvv"良(8〇?89):M?b?endl;cout?”中(70-79):"?c?endl;coutvv”及格(60-69):n?d?endl;coutvv"不及格(0-59):u?e?endl;實驗4-2a#include<iostream.h>#include<iomanip.h>#defineN10voidmain(void){inti,j,a[N],temp;coutvv”請輸入數(shù)據(jù):“;for(i=0;i<N;i++)cin?a[i];for(i=0;i<N-l;i++)for(j=O;j<N-l-i;j++)if(a[j]>a[j+l]){temp=a|j];a|j]=a[j+1];a[j+l]=temp;}for(i=0;i<N;i++)cout?setw(6)?a[i];cout?endl;實驗4-2b#include<iostream.h>#include<iomanip.h>#defineN10voidmain(void){intij,a[N],temp;coutcぐ,請輸入數(shù)據(jù):";for(i=0;ivN;i++)cin?a[i];for(i=0;i<N-l;i++)for(j=i+l;j<N;j++)if(a[i]>a[j]){temp=afi];a[i]=a[j];a[j]=temp;}for(i=0;i<N;i++)cout?setw(6)?a[i];cout?endl;實驗4-3#include<iostream.h>#include<iomanip.h>voidmain(void){inti,j,a[5][5];intmax,min,maxi,maxj,mini,minj,s;coutvv”請輸入數(shù)據(jù):“;for(i=0;i<5;i++)for(j=O;j<5;j++)cin?a[i][j];for(i=0;i<5;i++){for(j=O;j<5;j++)cout?setw(6)?a[i][j];cout?endl;}max=a[0][0];min=a[0][0];s=0;maxi=0;maxj=0;mini=0;minj=0;for(i=0;i<5;i++)for(j=O;j<5;j++){if(a[i][j]>max){max=a[i][j];maxi=i;maxj=j;}if(a[i][j]<min){min=a[i][j];mini=i;minj=j;)if(i==j)s=s+a[i][j];)cout?"max="?"a[,,?maxi?,'],'?',[,'?maxj?',]="?max?endl;cout?,,min=,,?na[,,?mini?,,J,,?"[,,?minj?',]=,,?min?endl;cout?"s=,,?s?endl;實驗4-4#include<iostream.h>#include<iomanip.h>#defineM3#defineN3voidmain(void){intij,a[M][N],b[M][N],c[M][N];coutvv”請輸入A矩陣數(shù)據(jù):";for(i=0;i<M;i++)for(j=O;jvN;j++)cin?a[i][j];couivv”請輸入B矩陣數(shù)據(jù):for(i=0;i<M;i++)for(j=O;jvN;j++)cin?b[i]|j];for(i=0;i<M;i++)for(j=O;jvN;j++)c[i]|j]=a[i]|j]+b[i][j];coutvv”C矩陣為:n?endl;for(i=0;ivM;i++){for(j=O;j<N;j++)cout?setw(6)?c[i][j];cout?endl;練習411#include<iostream.h>#include<iomanip.h>voidmain(void){inti,j,a[5][5];for(i=0;i<5;i++)for(j=O;j<5;j++){if(i>=j)a[i][j]=l;elsea[i][j]=j-i+l;)for(i=0;i<5;i++){for(j=0;j<5;j++)cout?setw(6)?a[i]fjl;cout?endl;練習4-18#include<iostream.h>#include<iomanip.h>#defineM6#defineN5voidmain(void){ints[M][N],ij,sum,max;coutvv”請輸入數(shù)據(jù)”;for(i=0;i<M-l;i++)for(j=O;j<N-l;j++)cin?s[i][j];for(i=0;i<M-l;i++){sum=0;for(j=l;j<N-l;j++)sum=sum4-s[i][j];s[i][4]=sum;)for(j=l;j<N-ly++){max=s[0][j];for(i=O;i<M-l;i++)if(s[i][j]>max)max=s[i][j];s[5][j]=max;)cout?H 學號“vv“ 數(shù)學”vv” 語文”?n 外語”<v”總成績”vvendl;cout?" \n”;for(i=0;i<M-l;i++){for(j=O;j<N;j++)cout?setw(8)?s[ij[j];cout?endl;)cout?H \n”;cout?n最高分”;for(j=l;j<N-l;j++)cout?setw(8)?s[5]|j];cout?endl;#include<iostream.h>#defineN10voidmain(void){inta[N],i,k;floatsum,ave;coutvv”請輸入數(shù)據(jù):“;for(i=0;i<=N-l;i++)cin?a[i];sum=0.0;k=0;for(i=0;i<=N-l;i++){sum=sum+a[i];if(a[i]<60)k++;)ave=sum/N;coutvv”平均分:n?ave?endl;cout?n不及格人數(shù):M?k?endl;練習4-8#include<iostream.h>voidmain(void){inta[20],i;a[0]=0;a[l]=0;a[2]=2;a[3]=5;for(i=4;i<=19;i++)a[ij=a[i-1J+a[i-2]+a[i-3j4-a[i-4];for(i=0;iv=19;i++){if(i%5==0)cout?endl;cout?a[i]?,\t,;)cout?endl;練習4-9a#include<iostream.h>#defineN10voidmain(void){inta[N],ij,temp;coutvv”請輸入數(shù)據(jù):";for(i=0;i<=N-l;i++)cin?a[i];for(i=0;i<=N-2;i++)for(j=0;j<=N-2-i;j++)if(aU]<aU+l]){temp=a[j];a[j]=a|j+l];a|j+1]=temp;)for(i=0;i<=N-l;i++)cout?a[i]?At,;cout?endl;練習4-9b#include<iostream.h>#defineN10voidmain(void){inta[N],i,j,temp;coutv<”請輸入數(shù)據(jù):“;for(i=0;ivN;i++)cin?a[i];for(i=0;i<=N-2;i++)for(j=i+1;j<=N-l;j++)if(a[i]<a[j]){temp=a[i];a[i]=a[j];a[j]=temp;}for(i=0;i<N;i++)cout?a[iJ?At,;cout?endl;#include<iostream.h>intsum(intn){ints;if(n=l)s=l;elses=n+sum(n-l);returns;}voidmain(void){intm,total;cout?"Inputm:n;cin?m;total=sum(m);cout?"Total="?total?endl;}練習5-24#include<iostream.h>floatpower(floatx,intn){floatp;if(n=O)p=l;elsep=x*power(x,n-l);returnp;}voidmain(void){floaty,t;intm;cout?MInputx,m:n;cin?y?m;t=power(y,m);cout?Ht=M?t?endl;實驗5-1#include<iostream.h>voidmain(void){inta[10],i,k,flag,top,bot,mid,loc;coutvv”輸入數(shù)組數(shù)據(jù):";for(i=0;i<10;i++)cin?a[i];coutvv”輸入要查找數(shù)據(jù):“;cin?k;flag=0;top=0;bot=9;while(flag==0&&top<=bot){mid=(top+bot)/2;if(k==a[mid]){flag=l;loc=mid;break;)elseif(k<a[mid])bot=mid-l;elsetop=mid+1;}if(flag==1)cout?loc;elsecoutvv”沒有找到.”;)實驗5-2a#include<iostream.h>#include<string.h>voidmain(void){charstr[8O],strl[40],str2[40];cout?"Input2string:";cin.getline(strl,40);cin.getline(str2,40);strcpy(str,strl);strcat(str,str2);cout?str?endl;實驗5-2b#include<iostream.h>#include<string.h>voidmain(void){charstr[80],strl[40],str2[40];inti,j;cout?"Input2string:*';cin.getline(str1,40);cin.getline(str2,40);i=0;while(strl[i]!=,\O,){str[i]=strl[i];i++;}j=0;while(str2[j]!=ヘ?!?{str[i]=str2[j];i++;j++;)str[i]=W;cout?str?endl;)實驗5-3#include<iostream.h>#include<iomanip.h>#defineN8voidmain(void){intc[N][N],m,n,k;charspace='';for(n=l;n<=N;n++){c[n][n]=l;c[n][l]=l;)for(m=3;mvN;m++)for(n=2;n<=m-l;n++)c[m][n]=c[m-1][n-1]+c[m-1][n];for(m=1;m<N;m++){for(k=l;k<=(7-m)*4;k++)cout?space;for(n=l;n<=m;n++)cout?setw(8)?c[m][n];cout?endl;實驗5-4#include<iostream.h>#include<iomanip.h>#defineM7#defineN5voidmain(void){floats[M][N],sum,ave,max,min,temp;inti,j,k;coutvv”InputData:H?endl;for(i=0;i<M-2;i++)for(j=O;j<N-l;j++)cin?s[i][j];for(i=0;i<M-2;i++){sum=0.0;for(j=l;j<N-l;j++)sum=sum+s[i]|j];ave=sum/(N-2);s[i][N-l]=ave;}for(j=l;j<N-l;j++){max=s[0J[j];min=s[O][j];for(i=0;i<M-2;i++){if(s[i]|j]>max)max=s[i]|j];if(s[i][j]<min)min=s[i][jj;)s[M-2J[j]=max;s[M-l][j]=min;}for(i=0;i<M-3;i++){k=i;for(j=i+l;jvM?2;j++)if(s[k][N-l]<sU][N-l])k=j;if(k!=j)for(j=O;j<N;j++){temp=s[i][j];s[ij|j]=s[k][j];s[kj|j]=temp;}cout?MNum.,,?nMath.Chin.Engl.Aveg.H?endl;cout?H n?encll;for(i=0;i<M-2;i++){for(j=O;j<N;j++)cout?setw(6)?s[i][j];cout?endl;}cout?Hmax";for(j=l;j<N-l;j++)cout?setw(6)?s[M-2][j];cout?endl;cout?"min";for(j=l;j<N-l;j++)cout?setw(6)?s[M-1][j];cout?endl;cout?" "?endl;練習5-18#include<iostream.h>floatftoc(floatf){floatc;c=(f-32)*5.0/9.0;returnc;)voidmain(void){floatfl,cl;cout?nInputf:H;cin?f1;cl=ftoc(fl);cout?,,C=,,?cl?endl;}練習5-19intprime(intk){intflag,i;flag=l;for(i=2;i<=k-l;i++)if(k%i==0){flag=O;
break;}returnflag;}#include<iostream.h>voidmain(void){intn;cout?MInputn:M;cin?n;if(prime(n)==l)coutvvnvv”是素數(shù)。H?endl;elsecout?nvv”不是素數(shù)。"vvendl;練習5-21#include<iostream.h>voidmain(void){floats;floatpfh(int);s=pfh(12)+pfh(15)*pfh(15);cout?Ms=n?s?endl;)floatpfh(intn){floatp,i;p=0;for(i=l;i<=n;i++)p=p+i*i;returnp;函數(shù)例子#include<iostream.h>#include<math.h>floatf(intn){floatt,i;t=l;for(i=l;i<=n;i++)t=t*i;returnt;)floatpow(floatx,intn){floaty;inti;y=l;for(i=l;iv=n;i++)y=y*x;returny;)voidmain(void){floata,s,p;inti;cout?"Inputa:";cin?a;s=0;i=l;do{p=pow(-l,i+1)*pow(a,2*i-1)/f(2*i-1);s=s+p;i++;}while(fabs(p)>=le-4);cout?,,sin(,,?a?M)=,,?s?endl;#defineN5voidmain(void){inta[N],ij,k,tl;floats[N],t2;coutvv”輸入運動員號碼和成績:”;for(i=0;i<N;i++){cin?a[i];cin?s[i];;)for(i=O;i<N-l;i++){k=i;for(j=i+l;j<N;j++)if(s[k]>s[j])k=j;if(k>i){tl=a[i];a[i]=a[k];a[k]=tl;t2=s[i];s[i]=s[k];s[k]=t2;for(i=0;ivN;i++){cout?i+l?'\t';cout?a[i]?,\t,;cout?s[i]?^1;cout?endl;練習4-20#include<iostream.h>defineM3defineN4defineK3voidmain(void){inta[M][N],b[N][K],c[M][K],i,j,q;coutvv”輸入矩陣A\n”;for(i=0;i<M;i++)for(j=O;j<N;j++)cin?a[i][j];cout?"輸入矩陣B\n";for(i=0;ivN;i++)for(j=O;j<K;j++)cin?b[i]0];for(i=0;i<M;i++)forG=O;j<K;j++){c[i][j]=O;for(q=0;q<N;q++)c[i]U]=c[i]U]+a[i][q]*b[q][j];)cout?"輸出矩陣C\n";for(i=0;i<M;i++){for(j=O;j<K;j++)cout?c[i][j]?M,;cout?endl;練習4-17B#include<iostream.h>#include<string.h>defineN5voidmain(void){chars[N][20],string[20];inti,j,k;floata[N],t;coutvv”輸入運動員號碼:\n";for(i=0;i<N;i++)cin.getline(s[i],20);coutvv”輸入運動員成績:\n”;for(i=0;ivN;i++){cout?s[i]?M:w;cin?a[i];}for(i=0;i<N-l;i++){k=i;for(j=i+l;j<N;j++)if(a[k]>a[j])k=j;if(k>i){t=a[i];a[i]=a[k];a[k]=t;strcpy(string,s[i]);strcpy(s[i],s[k]);strcpy(s[k],string);})coutvv”名次”vv't'vv"號碼“vvWvv”成績"vvendl;for(i=0;i<N;i++){coutv<i+l?'\t';cout?s[i]?,\t,;cout?a[i]?,\t,;cout?endl;實驗6-1#include<iostream.h>#include<iomanip.h>#defineN10voidsortl(inta[],intn){intij,temp;for(i=0;i<n-l;i++)for(j=O;j<n-l-i;j++)if(a[j]>a|j+l]){temp=a[j];a[j]=a|j+l];a|j+l]=temp;}Ivoidsort2(inta[],intn){inti,j,temp;for(i=0;i<n-l;i++)for(j=i+l;j<n;j++)if(a[i]<a[j]){temp=a[i];a[i]=a[j];a[j]=temp;})voidsort3(inta[],intn){inti,j,k,temp;for(i=0;i<n-l;i++){k=i;for(j=i+1;jvn;j++)if(a[k]>a[j])k=j;if(k>i){temp=a[i];a[i]=a[k];a[k]=temp;}))voidprint(inta[],intn){inti;for(i=0;i<n;i++)cout?setw(8)?a[i];cout?endl;}voidmain(void){inta[N],i;cout?"InputArray:n;for(i=0;i<N;i++)cin?a[i];sortl(a,N);print(a,N);sort2(a,N);print(a,N);sort3(a,N);print(a,N);實驗6-2#include<iostream.h>doublepx(doublex,intn){doublep;if(n=l)p=x;elsep=x-x*px(x,n-l);returnp;)voidmain(void){doubles,x,intn;cout?"Inputx:H;cin?x;cout?"Inputn:M;cin?n;s=px(x,n);cout?,,s=,'?s?endl;voidstr_cat(chars[],charsi[],chars2[]){strcpy(s,sl);strcat(s,s2);)voidmain(void){charstr[8O],strl[40],str2[20];cout?"Input2string:11;cin.getline(strl,40);cin.getline(str2,20);str_cat(str,str1,str2);cout?str?endl;)實驗6-3B#include<iostream.h>voidstr_cat(chars[],charsl[],chars2[]){inti,j;i=0;while(sl[i]!=ヘ〇'){s[i]=sl[i];i++;)j=0;while(s2[j]!=ヘ?!?{s[i]=s2[j];i++;j++;)s[i]=AO';)voidmain(void){chars[80],sl[40],s2[20];cout?"Input2string:**;cin.getline(sl,20);cin.getline(s2,40);str_cat(s,sl,s2);cout?s?endl;實驗6-4#include<iostream.h>#include<math.h>doublefsin(doublex){doubles,p,t,k,f;inti;i=l;s=0.0;k=x;t=1.0;f=l;do{p=f*k/t;s=s+p;i=i+2;k=k*x*x;t=t*i;f=(-l)*f;}while(fabs(p)>=le-10);returns;}voidmain(void){doublex,y;cout?MInputx:M;cin?x;y=fsin(x);cout?,,y=,'?y?endl;實驗6-5#include<iostream.h>#include<iomanip.h>#defineN10longf(intn){inti;longt;t=l;fbr(i=l;i<=n;i++)t=t*i;returnt;}intcnm(intnjntm){ints;s=f(n)/(f(m)*f(n-m));returns;}voidmain(void){inta[N][N],n,m;for(n=0;n<N;n++)for(m=0;mv=n;m++)a[n][m]=cnm(n,m);for(n=0;n<N;n++){for(m=0;mv=n;m++)cout?setw(6)?a[n][m];cout?endl;練習6-5:floatmax(floatx,floaty){floatz;if(x>=y)z=x;elsez=y;returnz;#include<iostream.h>#include"filemax.h"voidmain(void){floata,b,c,m;cout?"Input3float:";cin?a?b?c;m=max(max(a,b),c);cout?"max="?m?endl;練習6-6#include<iostream.h>#defineM(a,b)a>b?a:bvoidmain(void){intx,y,z,ml,m2;cout?"Inputx,y,z:";cin?x?y?z;ml=M(x,y);m2=M(ml,z);cout?"max="?m2?endl;#defineS(a,b)a*b#defineL(a,b)2*(a+b)voidmain(void){floatx,y,area,length;cout?"Inputx,y:";cin?x?y;area=S(x,y);length=L(x,y);cout?"area=H?area?endl;cout?"length="?length?endl;)練習6-7B#include<iostream.h>floatareaf(floata,floatb){floats;s=a*b;returns;}floatlengthf(floata,floatb){float1;l=2*(a+b);return1;}voidmain(void){floatx,y,area,length;cout?MInputx,y:H;cin?x?y;area=areaf(x,y);length=lengthf(x,y);cout?narea=H?area?endl;cout?,,length="?iength?endl;練習6-8#include<iostream.h>#include<math.h>#defineDEBUG#defineL(a,b,c)(a+b+c)#defineS(a,b,c)sqrt((b+c-a)/2*(a+c-b)/2*(a+b-c)/2*(a+b+c)/2)voidmain(void){floatx,y,z,length,area;cout?"Inputx,y,z:H;cin?x?y?z;length=L(x,y,z);#ifdefDEBUGcout<<nL=M?length?endl;#endifarea=S(x,y,z);cout?nLength=n?length?endl;cout?"Area="?area?endl;例7-61〃用數(shù)組指針的方法求一維數(shù)組的最大值#include<iostream.h>voidmain(void){inta[5]={1,6,3,8,0},max,i;int*p;p=a;max=a[0];for(i=0;i<5;i-H-)if(a[i]>max)max=a[i];coul?,rmax1=r,?max?endl;max=p[〇];for(i=0;i<5;i++)if(p[i]>max)max=p[i];cout?nmax2="?max?endl;max=*(a+0);for(i=0;i<5;i++)if(*(a+i)>max)max=*(a+i);cout?,,max3=,,?max?endl;max=*(p+0);for(i=0;i<5;i++)if(*(p+i)>max)max=*(p+i);cout?Hmax4=M?max?endl;max=?p;for(p=a;p<a+5;p++)if(*p>max)max二?p;cout?Mmax5=H?max?endl;voidmain(void){chars[40];inti;cout?MInputalinestring:n;cin.getline(s,40);i=0;while(s[i]!='()')i++;cout?"L="?i?endl;}例7-10-1#include<iostream.h>voidmain(void){chars[40],*p;intn;cout?"Inputalinestring:n;cin.getline(s,40);p=s;n=0;while(*p!=へ。'){n++;P++;)cout?s?endl;cout?nL=H?n?endl;例7-12#include<iostream.h>voidstr_cpy(char*p1,char*p2){while(*p2!=W){*pl=*p2;pl++;p2++;)*pl=\0,;)voidmain(void){charstrl[8O],str2[8O];char*ql=strl,*q2=str2;cout?"InputString2:";cin.getline(str2,80);str_cpy(ql,q2);cout<<"Stringl="?strl?endl;voidmain(void){inta[10],sum,i,*p;floatave;cout?"Inputarray:**;for(i=0;i<10;i++)cin?a[i];sum=0;for(i=0;i<10;i++)sum=sum+a[i];ave=sum/10.0;cout?"ave1=n?ave?endl;sum=0;for(i=0;i<10;i++)sum=sum4-*(a+i);ave=sum/10.0;cout<<"ave2=n?ave?endl;p=a;sum=0;for(i=0;i<10;i++)sum=sum+*(p+i);ave=sum/10.0;cout?"ave3=n?ave?endl;sum=0;for(i=0;i<10;i++)sum=sum+p[i];ave=sum/10.0;sum=0;cout?"ave4="?ave?endl;for(p=a;p<a+!〇;p++)sum=sum+*p;ave=sum/10.0;cout?Mave5=n?ave?endl;voidmain(void){floata[3][3],sum;inti,j;cout?"Inputarray:**;for(i=0;i<3;i++)for(j=O;j<3;j++)cin?a[i][j];sum=0;for(i=0;i<3;i++)sum=sum+*(a[i]4-i);for(i=0;i<3;i++)sum=sum+*(a[i]+2-i);cout?Msum="?sum?endl;實驗7-3#include<iostream.h>voidstr_cat(char*pl,char*p2){while(*pl!=,\0,)pl++;while(*p2!=\0,){*pl=*p2;pl++;p2++;}*pi=、(r;)intstr_cmp(char*pl,char*p2){intc;while(*pl==*p2&&*pl!=\0'&&*p2!='\0'){pl++;p2++;}c=*pl-*p2;returnc;intstr_len(char*p){intn;n=0;while(*p!=へ。'){n++;P++;)returnn;}voidmain(void){charstrl[8O],str2[8O];char*ql=strl,*q2=str2;intcmp,len;cout?"Input1string:";cin.getline(strl,8O);cout?"Input2string:";cin.getline(str2,8O);cmp=str_cmp(ql,q2);if(cmp==0)cout?"strl=str2"?endl;elseif(cmp>0)cout?"strl>str2"?endl;elsecout?"strl<str2"?endl;str_cat(ql,q2);cout?strl?endl;len=str_len(q1);cout?"len="?len?endl;)實驗74#include<iostream.h>voidsort1(inta[J,intn){inti,j,k,temp;for(i=0;i<n-l;i++){k=i;for(j=i+l;j<n;j++)if(a[k]<a|j])k=j;if(k>i){temp=a[i];a[i]=a[k];a[k]=temp;}voidsort2(int*p,intn){intij,k,temp;for(i=0;i<n-l;i++){k=i;for(j=i+l;j<n;j++)if(*(p+k)<*(p+j))k=j;if(k>i){temp=*(p+i);*(p+i)=*(p+k);*(p+k)=temp;}voidsort3(inta[],intn){inti,j,k,temp;for(i=0;i<n-l;i++){k=i;for(j=i+l;j<n;j++)if(*(a+k)<*(a+j))k=j;if(k>i){temp=*(a+i);*(a+i)=*(a+k);*(a+k)=temp;}voidsort4(int*p,intn){intij,k,temp;for(i=0;i<n-l;i++){k=i;for(j=i+l;j<n;j++)if(p[k]<p[j])k=j;if(k>i){temp=p[i];p[i]=p[k];p[k]=temp;}voidmain(void){inta[10],*q,i;cout?"Inputdata:*';for(i=0;i<10;i++)cin?a[i];sort1(aJO);for(i=0;i<10;i++)cout?a[i]?At,;cout?endl;sort2(a,10);for(i=0;i<10;i++)cout?a[i]?,\t,;cout?endl;q=a;sort3(q,10);for(i=0;i<10;i++)cout?a[i]?,\t,;cout?endl;q=a;sortl(q,10);for(i=0;i<10;i++)cout?a[i]?'\t,;cout?endl;練習3?23#include<iostream.h>voidmain(void){intm,n,ml,nl,r,p;coutvv”請輸入m,n:n;cin?m?n;ml=m;nl=n;r=m%n;while(r!=0){m=n;n=r;r=m%n;)coulvv”最大公約數(shù)為:"?n?endl;p=ml*nl/n;coutvv”最小公倍數(shù)為:“vvpvvendl;補充1ー雞兔同籠#include<iostream.h>voidmain(void){intx,y,h,f;cout?"Inputh,f:M;cin?h?f;x=(4*h-0/2;y=(f-2*h)/2;cout?,,x=',?x?endl;cout?My="?y?endl;)補充2一水仙花數(shù)#include<iostream.h>#include<iomanip.h>voidmain(void){inta,b,c,n;for(n=100;n<=999;n++){a=n/100;b=(n-a*100)/10;c=n%10;if(a*a*a+b*b*b+c*c*c==n)cout?setw(5)?n;;cout?endl;練習7-9#include<iostream.h>voidmain(void){inta[3][4],max,*p,*pl,i,j;cout?MInputArray:M;for(i=0;i<3;i++)for(j=0;j<4;j++)cin?a[i][j];p=&a[0][0];max=a[0][0];for(p1=p;p1<p+12;p1++){if(*pl>max)max=*pl;}cout?',Maxl=',?max?endl;max=a[0][0];for(i=0;iv3;i++)forO=0;j<4;j++)if(*(&a[i][O]+j)>max)max=*(&a[i][0]+j);cout?"Max2=H?max?endl;練習7-13#include<iostream.h>#include<iomanip.h>#defineN10sort1(floata[],intn){inti,j,k;floattemp;for(i=0;i<n-l;i++){k=i;for(j=i+l;j<n;j++)if(a[k]<a[j])k=j;if(k>i){temp=a[i];a[i]=a[k];a[k]=temp;})sort2(float*p,intn){intij;floattemp;for(i=0;i<n-l;i++)for(j=O;j<n-l-i;j++)if(*(p+j)<*(p+j+l)){temp=*(p+j);*(p+j)=*(p+j+l);*(p+j+l)=temp;})sort3(floata[],intn){inti,j;floattemp;for(i=0;i<n-l;i++)for(j=i+l;j<n;j++)if(*(a+i)<*(a+j)){temp=*(a+i);*(a+i)=*(a+j);*(a4j)=temp;})sort4(float*p,intn){intij;floattemp;for(i=0;i<n-l;i++)for(j=i+l;jvn;j++)if(p[i]<plj]){temp=p[i];p[i]=p[j];p[j]=temp;})print(floatal],intn){inti;floattemp;for(i=0;i<n;i++)cout?setw(5)?a[i];cout?endl;}voidmain(void){floatb[N],*pf;inti;cout?"Inputarray:n;for(i=0;i<N;i++)cin?b[i];sortl(b,N);print(b,N);sort2(b,N);print(b,N);pf=b;sort3(pf,N);print(b,N);pf-b;sort4(pf,N);print(b,N);例7-15#include<iostream.h>voidmain(void){inti;charc[3][4]={MABC',,,,DEF',MGHr,};char*pc[3];for(i=0;i<3;i++)cout?c[i]?endl;for(i=0;i<3;i++)pc[i]=c[i];for(i=0;i<3;i++)cout?pc[i]?endl;例7/6#include<iostream.h>#include<string.h>voidmain(void){charc[5][20];char*pc[5],*qc;inti,j;cout?"Input5string:,'?endl;for(i=0;iv5;i++)cin?c[i];for(i=0;i<5;i++)pc[i]=c[i];for(i=0;i<4;i++)for(j=i+l;j<5;j++)if(strcmp(pc[i],pc|j])>0){qc=pc[i];pc[i]=pc|j];pc[jj=qc;}for(i=0;i<5;i++)cout?pc[i]?'\t,;cout?endl;for(i=0;i<5;i++)cout?c[i]?,\t,;cout?endl;例7?17#include<iostream.h>voidmain(void){inti,j,sum;mta[3][3],(*p)[3];cout?"InputArray:n?endl;for(i=0;i<3;i++){p=a+i;for(j=O;j<3;j++)cin?(*p)[j];)for(p=a;p<a+3;p++){for(j=O;j<3;j++)cout?(*p)[j]?\t,;cout?endl;}sum=0;for(p=a;p<a+3;p++)for(j=O;j<3;j++)sum=sum+(*p)[j];cout?nsum=n?sum?endl;)例7-18#include<iostream.h>intsum(int(*p)[3],intm){inti,j,s=O;for(i=0;i<m;i++){for(j=O;jv3;j++)s=s+(*p)|j];P++;}returns;voidmain(void){inti,j,a[3][3];cout?"InputArray:11;for(i=0;i<3;i++)for(j=O;j<3y++)cin?ali][j];cout?"sum=u?sum(a,3)?endl;int*max(intx,inty){intz;z=x>y?x:y;return&z;)voidmain(void){inta=5,b=9,*p;p=max(a,b);cout?Hmax=n?*p?endl;)例7-20#include<iostream.h>char*str_cat(char*pl,char*p2){char*p3;p3=pl;while(*pl!=W)pl++;while(*p2!=ヘ0’){*pl=*p2;pl++;p2++;)*pl=へ?!?;returnp3;)voidmain(void){charsl[80],s2[40],*p;cout?nInput1string:";cin.getline(sl,80);cout?"Input2string:";cin.getline(s2,40);p=str_cat(sl,s2);cout?"Inputresultstring:";cout?p?endl;char*encrypt(char*ps){char*p;p=ps;while(*ps!=W){if(*ps>=48&&*ps<=57)*ps-=16;elseif(*ps>=65&&*ps<=90)*ps-=23;elseif(*ps>=97&&*ps<=122)*ps-=5;ps++;)returnp;}voidmain(void){chars[8O],*p;cin.getline(s,80);p=encrypt(s);cout?p?endl;)例7-22-1#include<iostream.h>floatf(floatx){floaty;y=x+1;returny;}voidmain(void){floatxl,yl;float(*pf)(float);cout?nInputxl:n;cin?x1;pf=f;yl=pf(xl);cout?"yl="?y1?endl;例7-22-2#include<iostream.h>#include<math.h>floatfl(floatx){return1+x;)floatf2(floatx){returnx/(l+x*x);}floatf3(floatx){return(x+x*x)/(l+cos(x)+x*x);}voidmain(void){floatxl,yl,y2,y3;float(*pf)(float);cout?"Inputxl:',;cin?xl;pf=fl;yl=pf(xl);cout?My1=H?y1?endl;pf=f2;y2=pf(xl);cout<<ny2=n?y2?endl;pf=f3;y3=pf(xl);cout?Hy3=n?y3?endl;例7?23#include<iostream.h>#include<math.h>floatfl(floatx){return1+x;)floatf2(floatx){returnx/(l+x*x);}floatf3(floatx){return(x+x*x)/(l+cos(x)+x*x);}floatintegral(float(*pf)(float),floata,floatb,intn){floaty,h,s;inti;y=(pf(a)+pf(b))/2;h=(b-a)/n;for(i=l;iv=n-l;i++)y+=pf(a+i*h);s=y*h;returns;)voidmain(void){floatsl,s2,s3;sl=integral(fl,1,4,1000);s2=integral(f2,0,1,1000);s3=integral(f3,l,3J000);cout?nsl=M?sl?M,?Ms2=M?s2?\t,?Hs3=M?s3?\t,;例7-24#include<iostream.h>voidmain(void){inti,n,sum=0;811tvv”輸入數(shù)組長度:“;cin?n;int*p=newint[n];coutvv"輸入n個元素的值:for(i=0;i<n;i++)cin?p[i];for(i=0;i<n;i++)sum+=p[i];cout?,'sum=,,?sum?endl;)例7-25#include<iostream.h>voidswap(int&rx,int&ry){inttemp;temp=rx;rx=ry;ry=temp;)voidmain(void){intx,y;cout?nInputx,y:*'?endl;cin?x?y;swap(x,y);cout?,'x=',?x?,\t'?,'y=',?y?endl;練習7ノ1#include<iostream.h>floataverage(int(*p)[6],intn){floatsum=0,ave;intj;for(j=0;j<6;j++)sum+=(*p)[j];p=p+n-1;for(j=0;j<6;j++)sum+=(*p)|j];ave=sum/12;returnave;voidmain(void){inta[6][6],i,j;cout?"InputArray:n?endl;for(i=0;i<6;i++)for(j=0;j<6;j++)cin?a[i][j];cout?average(a,6)?endl;練習7-12#include<iostream.h>#include<string.h>voidmain(void){charc[5][80],*s[5],*pc;inti,j,k;cout?"Input5string:,'?endl;for(i=0;i<5;i++)cin.getline(c[i],80);for(i=0;i<5;i++)s[i]=c[i];for(i=0;i<4;i++){k=i;for0=i+l;jv5;j++)if(strcmp(s[k],s|j])<0)k=j;if(k>i){pc=s[i];s[i]=s[k];s[k]=pc;})for(i=0;i<5;i++)cout?s[i]?,\t,;cout?endl;for(i=0;i<5;i++)cout?c[i]?^t;cout?endl;練習7-14#include<iostream.h>#include<string.h>char*str_nx(char*p){chare;intlen,i;len=strlen(p);for(i=0;i<len/2;i++){c=*(p+i);*(p+i)=*(p+len-1-i);*(p+len-1-i)=c;}returnpc;)voidmain(void){chars[80],*pq;cout?"Inputstring:',;cin.getline(s,80);pq=str_nx(s);cout?pq?endl;練習7-14(第2種解法)#include<iostream.h>#include<string.h>char*str_nx(charstr[]){char*pc,c;intlenj;pc=str;len=strlen(str);for(i=0;i<len/2;i++)13{c=str[i];str[i]=str[len-1-i];str[len-1-i]=c;}returnpc;)voidmain(void){chars[80],*pq;cout?"Inputstring:**;cin.getline(s,80);pq=str_nx(s);cout?pq?endl;練習7-18#include<iostream.h>#include<string.h>constiniN=5;voidinput(char*p[],intn){chars[50];inti;for(i=0;i<n;i++){cout?HInputstring["?i?M]";cin?s;p[i]=newchar[strlen(s)+l];strcpy(p[i],s);voidsort(char*p[],intn){intij;char*pc;for(i=0;i<n-l;i++)for(j=i+l;j<n;j++)if(strcmp(p[i],p[j])>0){pc=p[i];p[i]=p[j];p[j]=pc;})voidmain(void){char*p[N];inti;cout?"InputNstring:n?endl;input(p,N);sort(p,N);for(i=0;i<N;i++){cout?p[i]?endl;delete[Jp[iJ;)練習7-19#include<iostream.h>voidaverage(floata[],intn,float&ave,float&max,float&min){floatsum;inti;sum=0;max=a[0];min=a[0];for(i=0;i<n;i++){sum+=a[i];if(a[i]>max)max=a[i];if(a[i]<min)min=a[i];)ave=sum/n;)voidmain(void){floatb[10],aver,maxm,minm;inti;cout?"Inputdate:H?endl;for(i=0;i<10;i++)cin?b[i];average(b,10,aver,maxm,minm);cout?naverage=n?aver?endl;cout?"max="?maxm?endl;cout?,'min=,'?minm?endl;實驗8-1#include<iostream.h>#include<string.h>voidmain(void){chars[3][80],*p[3],*pc;inti,j,k;cout?MInput3string:"?endl;for(i=0;i<3;i++)cin.getline(s[i],80);for(i=0;i<3;i++)p[i]=s[i];for(i=0;i<2;i++){k=i;for(j=i+l;j<3;j++)if(strcmp(p[kl,p[jl)<0)k=j;if(k>i){pc=p[i];p[i]=p[k];p[k]=pc;})for(i=0;i<3;i++)cout?p[i]?'\t,;cout?endl;for(i=0;i<3;i++)cout?s[i]?,\t,;cout?endl;)實驗8-2#include<iostream.h>voidamn(float(*p)[3],intm){intij;floatsum,ave,max,min;sum=0;max=(*p)[0];min=(*p)[0J;for(i=0;i<m;i++){for(j=O;j<3;j++){sum+=(*p)[j];if((*p)[j>max)max=(*p)[jj;if((*p)U]<min)min=(*p)[j];)P++;ave=sum/(m*3);cout?"Average=,'?ave?endl;cout?"Max=M?max?endl;cout?"Min=H?min?endl;)voidmain(void){floata[3][3];inti,j;cout?"InputArray:'1;for(i=0;i<3;i++)for(j=O;j<3;j++)cin?a[i]|j];amn(a,3);實驗8-3#include<iostream.h>intadd(intx,inty){returnx+y;}intsub(intx,inty){returnx-y;}intmul(intx,inty){returnx*y;}intdiv(intx,inty){returnx/y;}intmod(intx,inty){returnx%y;}voidmain(void){intsl,s2,s3,s4,s5;int(*pf)(int,int);pf=add;sl=pf(10,20);pf=sub;s2=pf(10,5);pf=mul;s3=pf(10,15);pf=div;s4=pf(10,2);pf=mod;s5=pf(10,3);cout?sl?,\t'?s2?At,?s3?'\t,?s4?V,?s5?endl;實驗8-4#include<iostream.h>#include<math.h>floatfl(floatx){return(1+log(x)+x*x*x);}floatf2(floatx){return(l/(l+x*x));}floatf3(floatx){return(x+exp(x))/(l+sin(x)+x*x);}floatintegral(float(*f)(float),floata,floatb,intn){floaty,h;inti;y=(f(a)+f(b))/2;h=(b-a)/n;fbr(i=l;ivn;i++)y+=f(a+i*h);return(y*h);)voidmain(void){floatsl,s2,s3;sl=integral(fl,l,2,l000);s2=integral(f2,-l,4,l000);s3=integral(f3,1,3,1000);cout?nsl=M?sl?'\t,?,,s2=M?s2?\t,?Ms3=M?s3?endl;實驗8-2b#include<iostream.h>intfl(int(*p)[3],intm){inti,j,sum=O;for(i=O;i<m;i++){for(j=O;j<3;j++)sum+=(*p)[
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 學生社團財務(wù)審批與監(jiān)督管理制度
- 實驗室管理心得體會
- 初中畢業(yè)演講稿匯編五篇
- 合同管理員的工作總結(jié)
- 競聘演講稿結(jié)尾怎么寫四篇
- 迎新活動策劃方案集合15篇
- 單位年終個人工作總結(jié)報告
- 2022年監(jiān)理公司安全工作計劃
- 2025商業(yè)攝影合同書范文
- 監(jiān)控員年度考核個人總結(jié)總結(jié)計劃
- 全力以赴備戰(zhàn)期末-2024-2025學年上學期備戰(zhàn)期末考試主題班會課件
- 2024年保密協(xié)議書(政府機關(guān))3篇
- 《視頻拍攝與制作:短視頻?商品視頻?直播視頻(第2版)》-課程標準
- 研發(fā)部年終總結(jié)和規(guī)劃
- 石油開采技術(shù)服務(wù)支持合同
- 山東省煙臺市2024屆高三上學期期末考試英語試題 含解析
- 《汽車專業(yè)英語》期末試卷附答案第1套
- 2024年廉潔經(jīng)營承諾書2篇
- 《如何培養(yǎng)良好心態(tài)》課件
- 《中醫(yī)養(yǎng)生腎》課件
- 鄉(xiāng)鎮(zhèn)(街道)和村(社區(qū))應(yīng)急預(yù)案編制管理百問百答
評論
0/150
提交評論