




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、實(shí)驗(yàn)一熟悉Microsft Visual C+ 6.0開發(fā)環(huán)境四思考題#include void main() int a,b,c; coutab; /輸入兩個(gè)數(shù)據(jù)到變量a、b中 c=a*b; /求乘積存入c couta*b=cendl; /輸出結(jié)果 實(shí)驗(yàn)二輸入/輸出與順序結(jié)構(gòu)三 實(shí)驗(yàn)內(nèi)容1. 閱讀程序,寫出運(yùn)行結(jié)果。 i+j=15i*j=50 a=9 b=5 2. 程序填空 k=i+j i+j= a=c charctASCII=a 3. 程序改錯(cuò)#include void main() double r,s,l; coutr; s=3.1416*r*r; l=2.0*3.1416*r; co
2、utS=sendl; coutL=lendl; 4. 編程題 輸入華氏溫度F,計(jì)算輸出對應(yīng)的攝氏溫度。由華氏溫度F求攝氏溫度c的公式為:#include void main() double f,c; coutf; c=(f-32)*5/9; /或c=5.0/9*(f-32); coutC=cendl; 輸入學(xué)生的語文、數(shù)學(xué)、英語、物理4門課程的成績,計(jì)算該學(xué)生的總成績和平均成績并輸出。#include void main() double eng, chin,math,phy,sum,aver; coutengchinmathphy; /輸入成績 sum=eng+chin+math+phy;
3、 /計(jì)算總成績 aver=sum/4; /計(jì)算平均分 coutSum=sumendltAverage=averendl; /輸出 編寫程序,從鍵盤輸入一個(gè)大寫英文字母,輸出對應(yīng)的小寫字母。#include void main()char c1,c2;coutc1;c2=c1+32;coutc1=c1tc2=c2endl; 實(shí)驗(yàn)三選擇結(jié)構(gòu)程序設(shè)計(jì)三 實(shí)驗(yàn)內(nèi)容1.選擇題 C B D C C C D 2. -4 4 5 99 2,1 1 3. 編程題 由鍵盤輸入三個(gè)字符,輸出其中的最大者。【源程序】#include void main() char x,max; coutx; max=x; cinx
4、; if (xmax) max=x; cinx; if (xmax) max=x; coutmax = maxendl; 輸入三角形三邊的長,求三角形的面積。若輸入的三個(gè)邊能構(gòu)成三角形,則計(jì)算其面積并輸出;否則輸出提示信息。【源程序】/參見教材P44例3.15#include#include void main() double a,b,c,s,area; coutabc; if (a+b=c|a+c=b|b+c=a)coutcant be a triangle!n;else s=(a+b+c)/2; area=sqrt(s*(s-a)*(s-b)*(s-c);coutarea=area0)
5、-1 (x0) 【源程序】#include void main() int x,y; coutx; if (x0) y=1; else if (x=0) y=0; else y=-1; couty=yendl; 計(jì)算獎(jiǎng)金。設(shè)企業(yè)利潤為L,當(dāng)企業(yè)利潤L不超過5000元時(shí),獎(jiǎng)金為利潤的1.5%,當(dāng)5000L10000元時(shí),超過5000元部分獎(jiǎng)金為2%(5000元以下仍按1.5%);當(dāng)10000L20000元,除10000以下的按上述方法計(jì)算外,超過10000元部分按2.5%計(jì)算獎(jiǎng)金;如果20000L50000元,超過20000元部分按3%計(jì)算獎(jiǎng)金;當(dāng)50000L100000元時(shí),超過50000元部
6、分按3.5%計(jì)算獎(jiǎng)金;當(dāng)L超過100000元時(shí),超過100000元部分按4%計(jì)算獎(jiǎng)金。由鍵盤輸入L的值,編程計(jì)算相應(yīng)的獎(jiǎng)金并輸出。【源程序】#includevoid main() double L,S; coutL; if(L5000) S=L*0.015; else if(L10000) S=75+(L-5000)*0.02; else if(L20000) S=175+(L-10000)*0.025; else if(L50000) S=175+250+(L-20000)*0.03; else if(L100000) S=175+250+900+(L-50000)*0.035; else
7、S=175+250+900+1750+(L-100000)*0.04; coutS=Sendl; 輸入年齡,輸出所處人群:9歲以下為兒童,輸出A;1019為少年,輸出B;2029為青年,輸出C;3049為中年,輸出D;50以上為老年,輸出E。【源程序】#include void main() int age; coutage; switch(age/10) case 0:coutA-兒童n;break; case 1:coutB-少年n;break; case 2:coutC-青年n;break; case 3: case 4:coutD-中年n;break; default:coutE-老年
8、n;break; 有如下函數(shù):0t11t22t33t4 由鍵盤輸入t值,計(jì)算S的值。【源程序】/方法一#include void main()double t,s;coutt; if(t0&t4)if(t1)s=t*t;else if(t2) s=t*t-1;else if(t3)s=t*t-2*t+1;else s=t*t+4*t-17;couts=sendl;elsecoutError! t cant be less than 0 or more than 4!n; /方法二#include void main()double t;coutt; if(t=0)couterror, t ca
9、nt be less than 0!n;else if(t1)couts=t*tendl;else if(t2) couts=t*t-1endl;else if(t3)couts=t*t-2*t+1endl;else if(t4)couts=t*t+4*t-17endl;elsecouterror, t cant be more than 4!n; /方法三#include void main()double t,s;coutt; if(t=0)couterror, t cant be less than 0!n;else if(t1)s=t*t;couts=sendl;else if(t2)
10、 s=t*t-1;couts=sendl;else if(t3)s=t*t-2*t+1;couts=sendl;else if(t4)s=t*t+4*t-17;couts=sendl;elsecouterror, t cant be more than 4!n; 實(shí)驗(yàn)四 循環(huán)結(jié)構(gòu)程序設(shè)計(jì) 三 實(shí)驗(yàn)內(nèi)容1選擇題 下面程序的運(yùn)行結(jié)果是_ D _。#include 2閱讀程序,寫出運(yùn)行結(jié)果。下面程序的運(yùn)行結(jié)果是_0918273645_。 下面程序的運(yùn)行結(jié)果是_a=4_。 下面程序的運(yùn)行結(jié)果是_n=4_。3程序填空 下面程序的功能是依次顯示100,80,60,40,20這5個(gè)數(shù),請?zhí)羁铡?includ
11、e void main() int i; for(i=100;_i=20_;_i-=20_) coutit; coutendl; 下面程序的功能是計(jì)算xn,請?zhí)羁铡?include void main() int n,x; coutxn; double y=1; for(int i=0;i_n_;i+) _ y*=x;_; coutyendl; 下面程序的功能是計(jì)算1-3+5-7+-99+101的值,請?zhí)羁铡?include void main() int i,t,s=0,sign=1; for(i=1;i=101;i+=2) _t=i*sign_; s+=t; sign=-sign_; co
12、uts=sendl; * * * 下面程序的功能是輸出以下形式的金字塔圖案:#include void main() for(int i=1;i=4;i+) for(int j=1;j=_4-i_;j+) cout ; for(j=1;j=_2*i-1_;j+) cout*; coutendl; 4編程題 輸入n,求1+2+3+n的和。 #includevoid main() int i,n; double sum=0; cinn; for(i=1;i=n;i+) sum+=i; cout1+2+3+.+n=sumendl; 輸入若干個(gè)整數(shù),求它們的和,遇到-999時(shí)結(jié)束輸入。 #includ
13、evoid main() int n; double sum=0; cinn; for(;n!=-999;) /while(n!=-999) sum+=n; cinn; coutsum=sumendl; 輸入一整數(shù),輸出各位數(shù)字之和。如輸入6228,則輸出6+2+2+8的和為18。#includevoid main() double s=0; int n; cinn; while(n!=0) s+=n%10; n=n/10; couts=sendl; 輸入一實(shí)數(shù)x和一整數(shù)n,求x+x2+x3+xn的值。#include void main() int i,j,n; double x,sum=0
14、,p; cinnx; for(i=1;i=n;i+) p=1.; for(j=1;j=i;j+) p*=x; sum+=p; coutx=xtn=nendl; coutsum=sumendl; 求2!+4!+6!+16!。#includevoid main() int i,j; double sum=0,p; for(i=2;i=16;i+=2) p=1; for(j=1;j=i;j+) p*=j; sum+=p; cout2!+4!+6!+.+16!=sumendl; * * * 輸入兩個(gè)整數(shù)n和m,打印n行星號(hào),每行m個(gè)星號(hào)。如果輸入的n和m的值為4 7,則輸出為:#include voi
15、d main() int i,j,n,m; cinnm; for(i=0;in;i+) for(j=0;ji;j+) cout ; for(j=0;jm;j+) cout*; coutendl; 求1n+3n+5n+7n+(2m-1)n,其中m和n的值從鍵盤輸入。#include void main() int i,j,n,m,sum=0,p; cinnm; for(i=1;i=2*m-1;i+=2) p=1.; for(j=1;j=n;j+) p*=i; sum+=p; coutm=mtn=nendl; coutsum=sumendl; 1程序填空 以下程序的功能是計(jì)算:s=1+12+123
16、+1234+12345。請?zhí)羁铡?t=10*t+i s=s+t 下面程序的功能是輸出符合條件的三位整數(shù):它是完全平方數(shù),又有兩位數(shù)字相同,并且統(tǒng)計(jì)個(gè)數(shù),請?zhí)羁铡?i-n1*100)/10或(i/10)%10num+j+2編程(1)輸入10個(gè)字符,輸出其中的最大者。#includevoid main() char ch,maxchar; coutch; maxchar=ch; for(int i=1;ich; if(chmaxchar)maxchar=ch; coutmaxchar=maxcharendl;(2)一個(gè)球從100m高度自由落下,每次落地后反彈回原來高度的一半,再落下,再反彈。求它在
17、第10次落地時(shí),共經(jīng)過多少米?第10次反彈多高?分析:共經(jīng)過: 100*(1+1/2+1/4+1/8-+1/1024) 米第10次:100/1024米#includevoid main() double s=1,t=1,sum,t10; int i; for(i=1;i=10;i+)t=2*t;s=s+1/t;t10=100/t;sum=100*s;coutsum=sumtt10=t10endl; (3)用下列泰勒級(jí)數(shù)求sinx的近似值,x的值從鍵盤輸入,精度要求為10-6。 #include#includevoid main() int sign=1,n=1; double x=3.0,fe
18、nzi=x,fenmu=1.0,equo=1,sum=x; while(fabs(equo)=1e-6) fenzi*=x*x; fenmu*=(2*n)*(2*n+1); sign*=-1; equo=sign*fenzi/fenmu; sum+=equo; n+; coutsin x=sumendl;(4)編寫程序,對輸入的一批整數(shù)統(tǒng)計(jì)出正數(shù)的個(gè)數(shù)、負(fù)數(shù)的個(gè)數(shù)、奇數(shù)的個(gè)數(shù)、偶數(shù)的個(gè)數(shù),要求所統(tǒng)計(jì)的整數(shù)由鍵盤輸入,以0作為輸入數(shù)據(jù)結(jié)束的標(biāo)志。#includevoid main() int a=0,b=0,c=0,d=0,x; cinx; while(x!=0) if(x0)a+=1; if(
19、xx; cout正數(shù)個(gè)數(shù)=aendl; cout負(fù)數(shù)個(gè)數(shù)=bendl; cout奇數(shù)個(gè)數(shù)=cendl; cout偶數(shù)個(gè)數(shù)=dendl;(5)用牛頓迭代法求方程2x3-4x2+3x6=0在1.5附近的根,精度要求為10-6。#include#includevoid main() double x1,x2=1.5,y1,y2; cout x1 x2 yn; do x1=x2; y1=2*x1*x1*x1-4*x1*x1+3*x1-6; y2=6*x1*x1-8*x1+3; x2=x1-y1/y2; couttx1tx2ty11e-6); coutx=x1endl;(6)輸出這樣的三位整數(shù):這些三位
20、數(shù)的個(gè)、十、百位上的數(shù)字均不相同,并且能被11整除。#includevoid main() int i,j,k,n=0; for(i=1;i10;i+) for(j=0;j10;j+) for(k=0;k10;k+) if(i=j|j=k|k=i) continue; else if(i*100+j*10+k)%11=0) cout+n; cout:i*100+j*10+kt; (7)輸入兩個(gè)正整數(shù)m和n,求其最大公約數(shù)和最小公倍數(shù)。#includevoid main() int a,b,num1,num2,temp; coutnum1num2; if(num1num2) temp=num1;
21、 num1=num2; num2=temp; a=num1,b=num2; while(b!=0) temp=a%b; a=b; b=temp; cout最大公約數(shù)為:aendl; cout最小公倍數(shù)為:num1*num2/aendl;實(shí)驗(yàn)六/* * 輸入10個(gè)學(xué)生的成績,求其平均值,輸出最高成績,并統(tǒng)計(jì)低于平均值的人數(shù)。 * 注: LT是小于的意思,less than */#include const double LOWER = - 10000;void main()double score10;double highestScore = LOWER;double average = 0;
22、double numLTaverage = 0;/輸入、求最高分、求總分for (int i = 0; i 10 ; + i)cout Please input the score ( i + 1 scorei;if ( highestScore scorei ) highestScore = scorei;average += scorei;average /= 10;/求成績低于平均分的人數(shù)for ( i = 0; i 10; + i)if ( scorei average ) + numLTaverage ;cout The average score is average endl;c
23、out The highest score is highestScore endl;cout The number of LT average is numLTaverage endl;/* * 分別用冒泡法和選擇法對輸入的10個(gè)整數(shù)按由大到小排序。 * 冒泡法參見課本第四章課后作業(yè)第四題 */#include void main()int myArray10;int outer, inner;int imax;for ( int i = 0; i 10 ; + i )cout myArrayi;for ( outer = 0 ; outer 10 ; + outer)/在下標(biāo)位outer到
24、9之間的元素中尋找最大值imax = outer ;for ( inner = outer + 1; inner 10 ; + inner)if ( myArrayimax myArrayinner ) imax = inner;/將最大值與下標(biāo)為outer的元素交換int temp = myArrayouter;myArrayouter = myArrayimax;myArrayimax = temp;/輸出for ( i = 0; i 10 ; + i )cout myArrayi ;cout endl;/* * 使用折半查找法,在給定的數(shù)組中查找某個(gè)數(shù)據(jù)。 */#include cons
25、t int N = 10;void main()int myArrayN = 1, 5, 8, 13, 16, 34, 67, 78, 90, 100;int iSearch;cout iSearch;int low = 0;int high = N - 1;int mid;/開始二分查找while ( low = high )mid = ( low + high ) / 2;if ( myArraymid = iSearch ) break;else if ( myArraymid iSearch )low = mid + 1;elsehigh = mid - 1;/判斷是否找到if ( m
26、yArraymid = iSearch )cout We have found the number. endl;elsecout We havnt found the number. endl;/* * 按楊輝三角形的規(guī)律打印以下的數(shù)據(jù)(要求只打印出10行)。 * 1 * 1 1 * 1 2 1 * 1 3 3 1 * 1 4 6 4 1 * 1 5 10 10 5 1 * */#include void main()int yangHui1010;yangHui00 = yangHui10 = yangHui11 = 1;for ( int i = 2; i 10 ; + i )yangH
27、uii0 = yangHuiii = 1;for ( int j = 1 ; j i ; + j )yangHuiij = yangHuii - 1j + yangHuii - 1j - 1;for ( i = 0 ; i 10 ; + i )for (int j = 0; j = i ; + j )cout yangHuiij t;cout endl;/* * 編寫程序統(tǒng)計(jì)某班英語、語文、數(shù)學(xué)3門課程的成績, * 學(xué)生人數(shù)與成績由鍵盤輸入,要求統(tǒng)計(jì)出每門課程全班 * 的總成績和平均成績以及每個(gè)學(xué)生三門課程的總成績和 * 平均成績。 */#include void main()/由用戶輸入學(xué)生
28、人數(shù)int numStu;cout numStu;/根據(jù)用戶輸入的人數(shù)建立數(shù)組,其中0-2列為三門課成績,最后一列是總分。/最后添加一行用來存儲(chǔ)總分。int (*p)4;p = new intnumStu + 14;/將總分置0pnumStu0 = pnumStu1 = pnumStu2 = 0;for ( int i = 0 ; i numStu ; + i )/將每個(gè)人的總分置0pi3 = 0;cout ID is i + 1 :n;/輸入英語成績cout pi0;pi3 += pi0;pnumStu0 += pi0;/輸入物理成績cout pi1;pi3 += pi1;pnumStu1
29、+= pi1;/輸入英語成績cout pi2;pi3 += pi2;pnumStu2 += pi2;/輸出for ( i = 0 ; i numStu ; + i )cout ID i + 1 :n;cout The sum is pi3 ;cout .tThe average is pi3/3.0 ;cout endl;cout Eng: sum= pnumStu0 ,average= pnumStu0/double(numStu);cout nPhy: sum= pnumStu1 ,average= pnumStu1/double(numStu);cout nMat: sum= pnumS
30、tu2 ,average= pnumStu2/double(numStu);delete p;/* * 編寫程序求對矩陣進(jìn)行轉(zhuǎn)置,即將元素的行列位置交換。 */#include void main()int myMatrix44;/輸入for ( int i = 0; i 4; + i )for ( int j = 0; j 4; + j)cout ( i + 1 , j + 1 myMatrixij;/輸出轉(zhuǎn)置前的數(shù)組cout Before exchange:n;for ( i = 0; i 4; + i )for ( int j = 0; j 4; + j)cout myMatrixij
31、t;cout endl;/轉(zhuǎn)置for ( i = 0 ; i 4 ; + i)for ( int j = 0 ; j i ; + j)int temp = myMatrixij;myMatrixij = myMatrixji;myMatrixji = temp;/輸出轉(zhuǎn)置后的數(shù)組cout After exchange:n;for ( i = 0; i 4; + i )for ( int j = 0; j 4; + j)cout myMatrixij t;cout endl;/* * 編寫程序求兩個(gè)矩陣的乘積,若矩陣ANM與BMK相乘, *則得到矩陣C,其行列數(shù)為NK。注意A的列數(shù)與B的行數(shù)相同
32、 *,才可以進(jìn)行乘法操作。 */#include const int N = 3;const int M = 4;const int K = 5;void main()/定義數(shù)組int aNM,bMK,cNK;/輸入數(shù)組Acout Matrix A:n;for ( int i = 0; i N; + i)for ( int j = 0; j M; + j)cout ( i + 1 , j + 1 )/;cout ( N , M aij;/輸入數(shù)組Bcout Matrix B:n;for ( i = 0; i M; + i)for ( int j = 0; j K; + j)cout ( i +
33、 1 , j + 1 )/;cout ( M , K bij;/計(jì)算C,并輸出cout Matrix C=AXB:n;for ( i = 0; i N; + i)for ( int j = 0; j K; + j)cij = 0;for (int k = 0 ; k M ; + k)cij += aik * bkj;cout cij t;cout endl; 實(shí)驗(yàn)七 數(shù)組與指針(一) 三 實(shí)驗(yàn)內(nèi)容1 選擇題B D D D D C 2分析以下程序,寫出程序的運(yùn)行結(jié)果,并上機(jī)調(diào)試驗(yàn)證。 下面程序的運(yùn)行結(jié)果為_ C+_ 下面程序的運(yùn)行結(jié)果為_C+ Program _ 下面程序的運(yùn)行結(jié)果為 下面程序的
34、運(yùn)行結(jié)果為_i=10_ 下面程序的運(yùn)行結(jié)果為*p=1*(p+3)=12_ 下面程序的運(yùn)行結(jié)果為min=-8max=100_ 下面程序的運(yùn)行結(jié)果為_xbcdBCD_3程序填空 下面程序的功能是輸出: p+ p str1i!=0 str2j!=0 str1i=str2j; t=pp=qq=t4編寫程序并上機(jī)調(diào)試運(yùn)行 編寫程序,輸入5個(gè)字符串,輸出其中最大者。要求使用二維字符數(shù)組及字符串函數(shù)。#include #includeconst int N=3;void main() char aN20; int i,max=0; for(i=0;iN;i+) cin.getline(ai,20); for
35、(i=1;i0) strcpy(amax,ai); coutamaxendl; 編寫程序,將一個(gè)字符串中的數(shù)字字符都刪除。#include void main() char a20; int i=0,j=0; cout”Please input the characters: ”; cin.getline(a,20); for(i=0;ai!=0;i+) if(ai9) aj+=ai; aj=0; coutaendl; 編寫程序,輸入一行字符,統(tǒng)計(jì)其中有多少個(gè)單詞,單詞之間用一個(gè)或多個(gè)空格分隔。#include void main() char a100; int i,num=0; coutP
36、lease input the characters:n; cin.getline(a,100); for(i=0;ai!=0;i+) while(ai= ) i+; num+; while(ai!= ) i+; coutThe number of the words are: num; 四 問題討論1字符數(shù)組的輸入、輸出有幾種方法?2字符串與字符數(shù)組有何相同與不同之處?3數(shù)組元素及其地址有幾種表示方法? 實(shí)驗(yàn)八 數(shù)組與指針(二) 三實(shí)驗(yàn)內(nèi)容1選擇題B C A A A2分析以下程序,寫出程序的運(yùn)行結(jié)果,并上機(jī)調(diào)試驗(yàn)證結(jié)果 下面程序的運(yùn)行結(jié)果為_ 12_ 下面程序的運(yùn)行結(jié)果為 hellogood
37、 morninghow are you 下面程序的運(yùn)行結(jié)果為 1 2 3 4 5 下面程序的運(yùn)行結(jié)果為_c=b 下面程序的運(yùn)行結(jié)果為 a=10,ref=10 a=100,ref=1003程序填空 pi=chi p=new charn; delete p; a k+1 0 strk4編寫程序并上機(jī)調(diào)試運(yùn)行 輸入若干個(gè)字符串,實(shí)現(xiàn)用英文字典排列順序由大到小排列。要求使用指針數(shù)組,字符串由鍵盤輸入。#includeiostream.h#includestring.hconst int N=5;const int M=20; void main() char sNM, *pN, *t = NULL;
38、int i, j; for(i = 0; i N; i+) pi = si; for(i = 0; i N; i+) cin.getline(pi, M); for(i = 0; i N - 1; i+) for(j = 0; j N - 1 - i; j+) if(strcmp(pj, pj + 1) 0) t = pj; pj = pj + 1; pj + 1 = t; for(i = 0; i N; i+) coutpiendl; 用二級(jí)指針對任一二維整型數(shù)組的元素求和。#includeiostream.hconst int M = 2;const int N = 3; void mai
39、n() int aMN, *pM, *pp=p; int i, j, sum = 0; for(i = 0; i M; i+) pi = ai; for(i = 0; i M; i+) for(j = 0; j ppij; sum += ppij; coutnsum = sum; 編寫程序,當(dāng)輸入17(表示星期幾)時(shí),顯示相應(yīng)的星期的英文名稱,輸入其它整數(shù)時(shí)則顯示錯(cuò)誤信息。#includeiostream.hconst int M = 2;const int N = 3; void main() char *p7 = Monday, Tuesday, Wednesday, Thirsday,
40、Friday, Saturday, Sunday; char num; for(;) coutnum; if(num 7) coutError!; break; coutpnum - 49endl; 使用new、delete運(yùn)算符為一任意長度的整型數(shù)組分配內(nèi)存空間,對數(shù)組逆序輸出。#includeiostream.h#includestdlib.h void main() int n; int *p; coutn; if(p = new int n) = 0) coutThere is not full memory!n; exit(1); for(int i = 0; i pi; for(i
41、 = n - 1; i = 0; i-) coutpiendl; delete p; 四 問題討論1對多個(gè)字符串排序使用何種方法較簡便?2引用本身有無地址?3使用動(dòng)態(tài)內(nèi)存分配技術(shù)時(shí),如果分配不成功,應(yīng)如何處理?實(shí)驗(yàn)九 函數(shù)及其調(diào)用 三 實(shí)驗(yàn)內(nèi)容1. 選擇題(1) B (2) A (3) C (4) D (5) B (6) B (7) D (8) A2. 編程題 編寫一個(gè)判斷素?cái)?shù)的函數(shù),在主函數(shù)中由鍵盤輸入整數(shù)的范圍,并給出在該范圍內(nèi)的所有素?cái)?shù)。源程序?yàn)?#includeint fun(int n); void main() int m1,m2; coutm1m2; for(int i=m1;i
42、m2;i+) if(fun(i) couti t; int fun(int n) int i; for(i=2;in;i+) if(n%i=0) return 0; return n; 編寫一個(gè)函數(shù),根據(jù)給定的年、月、日輸出該日是該年的第幾天。在主函數(shù)中調(diào)用該函數(shù)并輸出結(jié)果,從鍵盤輸入年、月、日的值。#includeint fun(int,int,int); void main() int year,month,day; coutyearmonthday; while(1) if(year0&month=1&month=1&day=31) coutyear-month-day is the f
43、un(year,month,day)th day of the year!endl; break; else coutyearmonthday; int fun(int year,int month,int day) int ds=day; switch(month-1) case 11:ds+=30; case 10:ds+=31; case 9:ds+=30; case 8:ds+=31; case 7:ds+=31; case 6:ds+=30; case 5:ds+=31; case 4:ds+=30; case 3:ds+=31; case 2: if(year%4=0&year%1
44、00!=0|year%400=0) ds+=29; else ds+=28; case 1:ds+=31; return ds; 編寫兩個(gè)函數(shù)分別求2n,n!,在主函數(shù)中調(diào)用這兩個(gè)函數(shù)計(jì)算211!+ 222!+2nn!(n10),并在主函數(shù)中輸入n的值,輸出結(jié)果。#includedouble fun1(int n);double fun2(int n); void main() int n,i; double s=0; coutn; for(i=1;i=n;i+) s+=fun1(i)*fun2(i); couts=sendl; double fun1(int n) double m=1; f
45、or(int i=1;i=n;i+) m*=2; return m; double fun2(int n) double m=1; for(int i=1;i=n;i+) m*=i; return m; 編寫函數(shù)求sin(x),求sin(x)的近似公式為: 在主函數(shù)中輸入x的值并調(diào)用該函數(shù),輸出結(jié)果。#include#includedouble fun(double); void main() double x,sum; coutx; sum=fun(x); coutsum= sumendl; coutsinx=sin(x)1e-6;i+=2) s+=t; t=-t*x*x/(i+1)/(i+
46、2); return s;實(shí)驗(yàn)十 函數(shù)與指針 三 實(shí)驗(yàn)內(nèi)容1. 選擇題 下列程序執(zhí)行后的輸出結(jié)果是_A_。2. 編程題 編寫一個(gè)程序,求方程ax2+bx+c=0的根,用三個(gè)函數(shù)分別求出當(dāng)b2-4ac大于0,小于0和等于0時(shí)的根。要求從主函數(shù)輸入a,b,c的值并輸出結(jié)果。源程序?yàn)?方法1:#include#includevoid root1(double,double,double,double *,double *);double root2(double,double);void root3(double,double,double,double *,double *); void main
47、() double a,b,c,d,x,x1,x2; coutabc; if(a) d=b*b-4*a*c; if(d0) root1(a,b,d,&x1,&x2); coutx1=x1nx2=x2endl; else if(d=0) x=root2(a,b); coutx=xendl; else root3(a,b,d,&x1,&x2); coutx1=x1+x2iendl; coutx2=x1-x2iendl; else if(b) coutx=-c/bn; else if(c) cout方程無根,輸入的a,b,c值有誤!n; else cout方程有無窮多解。n; void root1(
48、double a, double b, double d, double * x1, double * x2) *x1=(-b+sqrt(d)/(2*a); *x2=(-b-sqrt(d)/(2*a); double root2(double a, double b) double x=-b/(2*a); return x; void root3(double a, double b, double d, double * x1, double * x2) *x1=-b/(2*a); *x2=sqrt(-d)/(2*a); 方法2:#include#include void root1(dou
49、ble,double,double,double);void root2(double,double,double);void root3(double,double,double,double);double x1,x2;void main() double a,b,c,t; coutabc; if(a) t=b*b-4*a*c; if (t0) root1(a,b,c,t); coutx1=x1nx2=x2n; else if(t=0) root2(a,b,c); coutx1=x2=x1n; else root3(a,b,c,t); coutx1=x1+x2inx2=x1-x2in; e
50、lse if(b) coutx=-c/bn; else if(c) cout方程無根,輸入的a,b,c值有誤!n; else cout方程有無窮多解。n;void root1(double a,double b,double c,double t) t=sqrt(t); x1=(-b+t)/(2*a); x2=(-b-t)/(2*a); void root2(double a,double b,double c) x1=x2=-b/(2*a);void root3(double a,double b,double c,double t) t=sqrt(-t); x1=-b/(2*a); x2=t/(2*a); 編寫將一個(gè)整數(shù)n轉(zhuǎn)換成字符串的函數(shù)。在主函數(shù)中調(diào)用該函數(shù)并輸出結(jié)果,從鍵盤輸入n的值。例如輸入123,則輸出字符串“123”。n的位數(shù)可以任意。#includevoid convert(int n); void main() int number; coutnumber; coutthe output is:endl; if(number0) cout-; number=-number; convert(number); coutendl; void convert(int n) int i; char c; if(i=n/10)!=0)
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2024年全球及中國治療藥物監(jiān)測耗材行業(yè)頭部企業(yè)市場占有率及排名調(diào)研報(bào)告
- 光伏水泵項(xiàng)目可行性研究報(bào)告
- 2025-2030年中國螺旋藻提取液項(xiàng)目投資可行性研究分析報(bào)告
- 2025版含重金屬廢氣處理裝置項(xiàng)目可行性研究報(bào)告編制機(jī)構(gòu)服務(wù)流程及案例
- 2025-2030年中國平直鋼板行業(yè)深度研究分析報(bào)告
- 2025-2030年中國電子稱重床行業(yè)深度研究分析報(bào)告
- 農(nóng)藥學(xué)實(shí)驗(yàn)報(bào)告
- 電腦表格行業(yè)深度研究分析報(bào)告(2024-2030版)
- 2025-2030年快剪服務(wù)行業(yè)市場調(diào)研及前景趨勢預(yù)測報(bào)告
- 防盜門金屬門窗加工項(xiàng)目可行性研究報(bào)告建議書
- SZDB-Z 173-2016 物業(yè)綠化養(yǎng)護(hù)管理規(guī)范
- 藝考全真樂理試題及答案
- 鋼廠供銷合同協(xié)議
- 急救知識(shí)培訓(xùn)課件下載
- 小學(xué)教學(xué)科學(xué)試題及答案
- 集團(tuán)物業(yè)后勤管理制度
- 鍋爐安裝安全管理制度
- 企業(yè)中層管理培訓(xùn)課件
- 2025年北京市海淀區(qū)高三一模政治試卷(含答案)
- 貴州省2025年4月高三年級(jí)適應(yīng)性考試英語試卷(含答案)
- 液壓安全知識(shí)培訓(xùn)課件
評論
0/150
提交評論