




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、1 整型、實(shí)型1.1 實(shí)型四舍五入 請編一個(gè)函數(shù)fun,函數(shù)的功能是使實(shí)型數(shù)保留2位小數(shù),并對第三位進(jìn)行四舍五入 (規(guī)定實(shí)型數(shù)為正數(shù))。 例如:實(shí)型數(shù)為 1234.567, 則函數(shù)返回 1234.57; 實(shí)型數(shù)為 1234.564, 則函數(shù)返回 1234.56。 注意: 部分源程序存在文件PROG1.C文件中。 請勿改動(dòng)主函數(shù)main和其它函數(shù)中的任何內(nèi)容,僅在函數(shù)fun的花括號中填入你編寫的若干語句。 #include <stdio.h>int NONO(void);float fun ( float h ) int main(void) float a; printf (&qu
2、ot;Enter a: "); scanf ( "%f", &a ); printf ( "The original data is : " ); printf ( "%f nn", a ); printf ( "The result : %fn", fun ( a ) );NONO( ); return 1;int NONO(void) /* 請?jiān)诖撕瘮?shù)內(nèi)打開文件,輸入測試數(shù)據(jù),調(diào)用 fun 函數(shù), 輸出數(shù)據(jù),關(guān)閉文件。 */ int i ; float a ; FILE *rf, *wf ;
3、rf = fopen("./05/in.dat","r") ; wf = fopen("./05/out.dat","w") ; for(i = 0 ; i < 20 ; i+) fscanf(rf, "%f", &a) ; fprintf(wf, "%fn", fun(a) ; fclose(rf) ; fclose(wf) ; return 1; 答案1:long i=h*1000;if(i%10<5) return (i/10)/100.0;else
4、 return (i/10+1)/100.0;答案2:long temp=a*1000,temp2; temp2=temp/10; temp=temp%10; float result; /被除數(shù)定義成float if(temp>4) result=temp2+1;elseresult=temp2;return result/100;1.2 根據(jù)公式求圓周率#include <stdio.h> #include <math.h> double fun ( double eps) main( ) double x; printf("Input eps:&q
5、uot;) ; scanf("%lf",&x); printf("neps = %lf, PI=%lfn", x, fun(x); NONO(); NONO ( ) /* 本函數(shù)用于打開文件,輸入數(shù)據(jù),調(diào)用函數(shù),輸出數(shù)據(jù),關(guān)閉文件。 */ FILE *fp, *wf ; int i ; double x ; fp = fopen("c:testin.dat","r") ; wf = fopen("c:testout.dat","w") ; for(i = 0 ; i
6、< 10 ; i+) fscanf(fp, "%lf", &x) ; fprintf(wf, "%lfn", fun(x) ; fclose(fp) ; fclose(wf) ; 答案1:double i=1,a=1,b=1,c=1,s=0; while(c>=eps) s+=c; a*=i; b*=2*i+1; c=a/b; i+; return s*2;答案2:double n=1.0,m,s1,s2; m=2*n+1; s1=n; s2=m; double temp=s1/s2,sum=0.0; while(temp>=e
7、ps) sum+=temp; n+=1; m+=2; s1*=n; s2*=m; temp=s1/s2; return (sum+1)*2;1.3 求階乘#include <stdio.h> float fun(int m, int n) int main(void) /* 主函數(shù) */ printf("P=%fn", fun (12,8); /NONO(); return 1;int NONO (void) /* 本函數(shù)用于打開文件,輸入數(shù)據(jù),調(diào)用函數(shù),輸出數(shù)據(jù),關(guān)閉文件。 */ FILE *fp, *wf ; int i, m, n ; float s; f
8、p = fopen("c:testin.dat","r") ; wf = fopen("c:testout.dat","w") ; for(i = 0 ; i < 10 ; i+) fscanf(fp, "%d,%d", &m, &n) ; s = fun(m, n) ; fprintf(wf, "%fn", s) ; fclose(fp) ; fclose(wf) ; return 1; 答案:float result; int x,y,z,o=1,p
9、=1,q=1; for(x=1;x<=m;x+) o=o*x; for(y=1;y<=n;y+) p=p*y; for(z=1;z<=m-n;z+) q=q*z; result=o/(p*q); return result;嘗試建立一個(gè)專門求階乘的函數(shù)。1.4 求公式#include <stdio.h> double fun( int n ) int main(void) /* 主函數(shù) */ printf("%fn", fun(10); /NONO(); return 1;int NONO (void) /* 本函數(shù)用于打開文件,輸入數(shù)據(jù),調(diào)用
10、函數(shù),輸出數(shù)據(jù),關(guān)閉文件。 */ FILE *fp, *wf ; int i, n ; double s ; fp = fopen("c:testin.dat","r") ; wf = fopen("c:testout.dat","w") ; for(i = 0 ; i < 10 ; i+) fscanf(fp, "%d", &n) ; s = fun(n) ; fprintf(wf, "%fn", s) ; fclose(fp) ; fclose(wf) ;
11、return 1; 答案:int i; double m,sum=0.0; for(i=1;i<=n;i+) m=i*(i+1); sum+=1/m; return sum;1.5 整數(shù)分解函數(shù)fun的功能是:將a、b中的兩個(gè)兩位正整數(shù)合并形成一個(gè)新的整數(shù)放在c中。合并的方式是:將a中的十位和個(gè)位數(shù)依次放在變量c的十位和千位上,b中的十位和個(gè)位數(shù)依次放在變量c的個(gè)位和百位上。 例如,當(dāng)a45,b=12。調(diào)用該函數(shù)后,c=5241。 注意: 部分源程序存在文件PROG1.C中。數(shù)據(jù)文件IN.DAT中的數(shù)據(jù)不得修改。 請勿改動(dòng)主函數(shù)main和其它函數(shù)中的任何內(nèi)容,僅在函數(shù)fun的花括號中填入
12、你編寫的若干語句。#include <stdio.h>void NONO(void);long fun(int a, int b) int main(void) int a,b; long c; printf("Input a, b:"); scanf("%d%d", &a, &b); c=fun(a, b); printf("The result is: %ldn", c); NONO(); return 0; void NONO (void) /* 本函數(shù)用于打開文件,輸入數(shù)據(jù),調(diào)用函數(shù),輸出數(shù)據(jù),關(guān)閉
13、文件。 */ FILE *rf, *wf ; int i, a,b ; long c ; rf = fopen("./44/in.dat","r") ; wf = fopen("./44/out.dat","w") ; for(i = 0 ; i < 10 ; i+) fscanf(rf, "%d,%d", &a, &b) ; fun(a, b, &c) ; fprintf(wf, "a=%d,b=%d,c=%ldn", a, b, c) ; f
14、close(rf) ; fclose(wf) ;答案: long i1=(a%10)*1000; printf("aa=%d,bb=%d",b%10,b/10); long i2=(b%10)*100; long i3=a/10*10; long i4=b/10; /return a%10*1000+b%10*100+a/10*10+b/10; return i1+i2+i3+i4;1.6 數(shù)學(xué)公式#include <stdio.h> /*found*/ fun(int m)double y = 0, d; int i ; /*found*/ for( i =
15、100;i < m;i += 100 ) d = (double)i * (double)i ; y += 1.0 / d ; return (y);main( ) int n = 2000 ; printf( "nThe result is %lfn", fun ( n ) ) ; 答案:double fun ( int m ) for( i = 100;i <=m;i += 100 ) 1.7 數(shù)學(xué)公式修正:當(dāng)k=1時(shí),第一項(xiàng)為4/1*3#include <stdio.h>#include <math.h>float fun(int
16、k)main ( ) printf("%fn", fun (10);答案1:int n;float s, w, p, q;n = 1;s = 1.0;while ( n <= k ) w = 2.0 * n;p = w - 1.0;q = w + 1.0;s = s * w *w/p/q; n+;return s;改錯(cuò):int i; float sum=1.0; for(i=2;i<=k;i+) sum*=(2*i * 2*i)/(2*i+1)*(2*i-1); /注意數(shù)據(jù)類型 return sum;1.8 最小公倍數(shù)給定程序MODI1.C中函數(shù)fun的功能是:
17、求三個(gè)數(shù)的最小公倍數(shù)。 例如,給主函數(shù)中的變量x1、x2、x3分別輸入15 11 2, 則輸出結(jié)果應(yīng)當(dāng)是:330。請改正程序中的錯(cuò)誤,使它能得出正確結(jié)果。 注意:不要改動(dòng)main函數(shù),不得增行或刪行,也不得更改程序的結(jié)構(gòu)。#include <stdio.h>/*found*/fun(int x, y, z ) int j,t ,n ,m;j = 1 ;t=j%x;m=j%y ;n=j%z;while(t!=0|m!=0|n!=0) j = j+1;t=j%x;m=j%y;n=j%z;/*found*/return i;main( ) int x1,x2,x3,j ;printf(&
18、quot;Input x1 x2 x3: ");scanf("%d%d%d",&x1,&x2,&x3);printf("x1=%d, x2=%d, x3=%d n",x1,x2,x3);j=fun(x1,x2,x3);printf("The minimal common multiple is : %dn",j);答案:int fun(int x,int y,int z)return j;編程:#include <stdio.h>/求三個(gè)數(shù)的最大數(shù)int max(int x,int y,i
19、nt z) /求三個(gè)數(shù)最小公倍數(shù)int fun(int x, int y, int z ) main( ) int x1,x2,x3,j ;printf("Input x1 x2 x3: ");scanf("%d%d%d",&x1,&x2,&x3);printf("x1=%d, x2=%d, x3=%d n",x1,x2,x3);j=fun(x1,x2,x3);printf("The minimal common multiple is : %dn",j);答案:int temp; if (
20、x>y) temp=x; else temp=y; if(temp >z) return temp; else return z;int temp=max(x,y,z); int result=temp; while(1) if(result%x=0 && result%y=0 && result%z=0) return result; result=result+temp; 1.9 n!改錯(cuò)給定程序MODI1.C中函數(shù) fun 的功能是:計(jì)算n!。 例如,給n輸入5,則輸出120.000000。 請改正程序中的錯(cuò)誤,使程序能輸出正確的結(jié)果。 注意:
21、不要改動(dòng)main函數(shù),不得增行或刪行,也不得更改程序的結(jié)構(gòu)!#include <stdio.h>double fun ( int n ) double result = 1.0 ;/*found*/if n = = 0 return 1.0 ;while( n >1 && n < 170 )/*found*/result *= n- return result ;main ( ) int n ;printf("Input N:") ;scanf("%d", &n) ;printf("nn%d! =
22、%lfnn", n, fun(n) ;答案:if(n=0) return 1.0;result*=n-;1.10 數(shù)學(xué)公式#include <stdio.h>double fun(int n)main( ) int n = 5;printf( "nThe value of function is: %lfn", fun ( n ) );答案:int a, b, c, k;double s;s = 0.0;a = 2;b = 1;for ( k = 1; k <= n; k+ ) s=s+(double)a/b;c = a;a = a + b;b
23、= c;return s;1.11 數(shù)列已知一個(gè)數(shù)列從第0項(xiàng)開始的前三項(xiàng)分別為0,0,1,以后的各項(xiàng)都是其相鄰的前三項(xiàng)之和。給定程序MODI1.C中函數(shù)fun的功能是:計(jì)算并輸出該數(shù)列前n項(xiàng)的平方根之和。n的值通過形參傳入。 例如,當(dāng)n10時(shí),程序的輸出結(jié)果應(yīng)為:23.197745。 請改正程序中的錯(cuò)誤,使程序能輸出正確的結(jié)果。 注意:不要改動(dòng)main函數(shù),不得增行或刪行,也不得更改程序的結(jié)構(gòu)!#include <stdio.h>#include <math.h>double fun(int n)main ( ) int n;printf("Input N=&
24、quot;);scanf("%d", &n);printf("%fn", fun(n) );答案:double sum, s0, s1, s2, s;int k;sum = 1.0;if (n <= 2) sum = 0.0;s0 = 0.0;s1 = 0.0;s2 = 1.0;for (k = 4; k <= n; k+) s = s0 + s1 + s2;sum += sqrt(s);s0 = s1;s1 = s2;s2 = s;return sum;1.12 數(shù)學(xué)公式#include <stdio.h>/*found
25、*/int fun ( int n ) float A=1;int i;/*found*/for (i=2; i<n; i+) A = 1.0/(1+A);return A ;main( ) int n ;printf("nPlease enter n: ") ;scanf("%d", &n ) ;printf("A%d=%fn", n, fun(n) ) ;答案:float fun(int n)if(2<=n) return 1.0/(1+fun(n-1);/遞歸或:for (i=2; i<=n; i+)
26、A=1.0/(1+A);/另一種1.13 數(shù)學(xué)公式給定程序MODI1.C中函數(shù)fun的功能是:根據(jù)整型形參m的值,計(jì)算如下公式的值。#include <stdio.h>double fun ( int m ) double y = 1.0 ;int i ;/*found*/for(i = 2 ; i < m ; i+)/*found*/y -= 1 /(i * i) ;return( y ) ;main( ) int n = 5 ;printf( "nThe result is %lfn", fun ( n ) ) ;答案:for(i=2;i<=m;
27、i+)y -=1.0/(i*i);1.14 數(shù)學(xué)公式#include <math.h>#include <stdio.h>double fun(double eps) double s,t;int n=1;s=0.0;/*found*/t=0;while( t>eps) s+=t;t=t * n/(2*n+1);n+;/*found*/return (s);main() double x;printf("nPlease enter a precision: ");scanf("%lf",&x);printf(&quo
28、t;neps=%lf, Pi=%lfnn",x,fun(x);答案:t=1.0;return(s*2);1.15 素?cái)?shù)給定程序MODI1.C中函數(shù)fun的功能是:找出一個(gè)大于形參m且緊隨m的素?cái)?shù),并作為函數(shù)值返回。 請改正程序中的錯(cuò)誤,使它能得出正確的結(jié)果。 注意:不要改動(dòng)main函數(shù),不得增行或刪行,也不得更改程序的結(jié)構(gòu)! #include <stdio.h>int fun(int m) int i, k ;for (i = m + 1 ; ; i+) for (k = 2 ; k < i ; k+)/*found*/if (i % k != 0) break ; /*found*/if (k < i) return(i); void main() int n ;n = fun(20) ;printf("n=%d
溫馨提示
- 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)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 業(yè)主答謝活動(dòng)方案
- 大暑中班活動(dòng)方案
- 大唐不夜城美食活動(dòng)方案
- 大班拜年文案活動(dòng)方案
- 大學(xué)廉潔宣傳活動(dòng)方案
- 圣誕節(jié)公司適合活動(dòng)方案
- 地產(chǎn)調(diào)酒活動(dòng)方案
- 天然氣公司安全活動(dòng)方案
- 大班畢業(yè)賀卡活動(dòng)方案
- 夏天戶外活動(dòng)方案
- 《膽管炎的護(hù)理》課件
- 中國概況(英文版)課件
- 2025年中國orc低溫余熱發(fā)電系統(tǒng)行業(yè)分析及發(fā)展趨勢預(yù)測
- 中醫(yī)護(hù)理疑難病例討論
- 2025年江蘇啟東市勞務(wù)技術(shù)經(jīng)濟(jì)開發(fā)有限公司招聘筆試參考題庫含答案解析
- 房屋市政工程施工現(xiàn)場安全風(fēng)險(xiǎn)分級管控與防范措施清單
- 山西焦煤招聘筆試題庫2025
- DB50-T 1808-2025“一表通”智能報(bào)表市級業(yè)務(wù)數(shù)據(jù)規(guī)范
- 房屋市政工程生產(chǎn)安全重大事故隱患判定檢查表(2024版)
- 高企研發(fā)費(fèi)用培訓(xùn)
- 飼料公司銷售管理制度
評論
0/150
提交評論