C語言上機(jī)練習(xí)參考答案說課講解_第1頁
C語言上機(jī)練習(xí)參考答案說課講解_第2頁
C語言上機(jī)練習(xí)參考答案說課講解_第3頁
C語言上機(jī)練習(xí)參考答案說課講解_第4頁
C語言上機(jī)練習(xí)參考答案說課講解_第5頁
已閱讀5頁,還剩112頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、此文檔來源于網(wǎng)絡(luò),如有侵權(quán)請(qǐng)聯(lián)系網(wǎng)站刪除只供學(xué)習(xí)交流用第1章C語言概述1-1編寫程序,在屏幕上顯示一個(gè)如下輸出:Programming in C is fun!I love C Ian guage.Program#include mai n() printf( -n);printf(Programming in C is fun!n”);prin tf(I love C Ian guage.n); printf(n);1-2編寫程序,在屏幕上顯示一個(gè)如下圖案:Program (1)#include mai n()prin tf(*n);printf( *n);prin tf(*n);print

2、f(*n);Program (2)#include mai n()printf(%c%4c%4c%4cn, *,*, *, *); printf(%3c%4c%4cn, *,*,*);prin tf(%5c%4cn, *, *);此文檔來源于網(wǎng)絡(luò),如有侵權(quán)請(qǐng)聯(lián)系網(wǎng)站刪除只供學(xué)習(xí)交流用x的值,其中xprintf(%7cn , *);1-3已知某個(gè)圓的半徑,編寫一個(gè)程序,用來計(jì)算并顯示面積 要求:將n定義為符號(hào)常量,并假設(shè)一個(gè)恰當(dāng)?shù)陌霃街?。Program#i nclude #defi ne PI 3.14 mai n() float r=5, s;s = Pl*r*r;prin tf(The ar

3、ea of circle is: %.2fn, s); OutputThe area of circle is: 78.501-4已知兩個(gè)整數(shù)20和10,編寫程序,自定義函數(shù)add()將這兩個(gè)數(shù)相加,自定義 函數(shù)sub()計(jì)算這兩個(gè)數(shù)的差,并按照下面形式顯示計(jì)算結(jié)果:20+10=3020-10=10Program#include int add(i nt a, int b) return (a+b);int sub(i nt a, int b) return (a-b);mai n() int a=20, b=10;prin tf(%d + %d = %dn, a, b, add(a, b);

4、 prin tf(%d - %d = %dn, a, b, sub(a, b);Output20 + 10 = 3020 -10 = 101-5已知變量a、b和c的值,編寫程序,用來計(jì)算并顯示請(qǐng)分別用以下數(shù)值運(yùn)行該程序此文檔來源于網(wǎng)絡(luò),如有侵權(quán)請(qǐng)聯(lián)系網(wǎng)站刪除只供學(xué)習(xí)交流用(1)a=250, b=85, c=25(2)a=300, b=70, c=80Program (1)#i nclude mai n() int a=250, b=85, c=25; float x;x=1.0*a/(b-c); prin tf(x = %.2fn, x);Output (1) _x =_4.7Program

5、(2)#include mai n() int a=300, b=70, c=80;float x;x=1.0*a/(b-c); /*試寫成x=a/(b-c);得到什么運(yùn)行結(jié)果?為什么?*/prin tf(x = %.2fn, x);Output (2)x = -30.00第2章常量、變量及數(shù)據(jù)類型 &第3章運(yùn)算符和表達(dá)式3-1編寫程序,求華氏溫度100F對(duì)應(yīng)的攝氏溫度。計(jì)算公式如下:5 (f 32)c9式中:c表示攝氏溫度,f表示華氏溫度。(c定義為實(shí)型,f定義為整型)Program#i nclude mai n() int f=100; float c;c=5.0*(f-32)/9; /*

6、如果是c=5*(f-32)/9;會(huì)是什么結(jié)果?為什么? */ printf( Celsius 此文檔來源于網(wǎng)絡(luò),如有侵權(quán)請(qǐng)聯(lián)系網(wǎng)站刪除只供學(xué)習(xí)交流用degree (corresponding to %d Fahrenheit) is : %.2f.n, f, c);OutputCelsius degree (corresp onding to 100 Fahre nheit) is: 37.78.3-2 一個(gè)物體從100m的高空自由落下,編寫程序,求它在前3s內(nèi)下落的垂直距離 設(shè)重力加速度為10m/s2。要求,將重力加速度定義為符號(hào)常量,嘗試將其改為9.8 m/s2,看結(jié)果有何不同?Progr

7、am#include #define G 10mai n() int t=3;float s;s=1.0/2*G*t*t; /*如果是s=1/2*G*t*t;會(huì)是什么結(jié)果?為什么? */prin tf(The falli ng vertical dista nce (in %d sec on ds) is: %.2f.n, t, s);OutputThe falli ng vertical dista nce (in 3 sec on ds) is:45.00.3-3將球的半徑R定義為符號(hào)常量,計(jì)算球的表面積(4TR2)和體積(4/3* nR3)Program#i nclude #define

8、 R 5.2#define PI 3.14mai n() float s, v;s=4*p|*R*R;v=4.0/3*PI*R*R*R;prin tf(The surface area of the ball (radius is %.2f) is: %.2f, and the volume is: %.2f.n, R, s, v);OutputThe surface area of the ball (radius is 5.20) is: 339.62, and the volume588.68. _3-4給定x、y和z的值,編寫程序,使x等于y的值,y等于z的值,z等于x的 值。此文檔來

9、源于網(wǎng)絡(luò),如有侵權(quán)請(qǐng)聯(lián)系網(wǎng)站刪除只供學(xué)習(xí)交流用Program#include mai n() int x=1, y=2, z=3, t;prin tf(Before swap: x=%d, y=%d, z=%d.n, x, y, z); t=x;x=y;y=z;z=t;/*變量t的作用是什么? */prin tf(After swap: x=%d, y=%d, z=%d.n, x, y, z);OutputBefore swap: x=1, y=2, z=3.After swap: x=2, y=3, z=1.3-5編寫一個(gè)程序,給定一個(gè)浮點(diǎn)數(shù)(例如 456.78),顯示該數(shù)的十位數(shù)字與個(gè)位數(shù)

10、 字之和(例如5+6=11)Program (1)#i nclude mai n() float f=456.78;int n, a, b;n=f;a = n%10;/*賦值后,a是什么值? */b = n/10%10;/*賦值后,b是什么值? */printf(The sum of the tens digit and units digit of %.2f is: %d + %d =%d.n, f, b, a, a+b);Program (2)#include mai n() float f=456.78;int n, a, b;n=f;a = n %10;b = n%100/10;/*該

11、語句與上面程序不同,看看有何區(qū)別?*/此文檔來源于網(wǎng)絡(luò),如有侵權(quán)請(qǐng)聯(lián)系網(wǎng)站刪除只供學(xué)習(xí)交流用printf(The sum of the tens digit and units digit of %.2f is: %d + %d =%d.n, f, b, a, a+b);OutputThe sum of the tens digit and units digit of 456.78 is: 5+ 6 = 11.3-6某種物品每年折舊費(fèi)的計(jì)算方法如下:折舊費(fèi)購買價(jià)格廢品價(jià)值費(fèi)使用年限編寫一個(gè)程序,當(dāng)給定某物品的購買價(jià)格、使用年限和每年的折舊費(fèi)時(shí),計(jì)算 出其廢品價(jià)值。Program#includ

12、e mai n() float price=120.65, year=3, depreciati on=10.2, value;此文檔來源于網(wǎng)絡(luò),如有侵權(quán)請(qǐng)聯(lián)系網(wǎng)站刪除只供學(xué)習(xí)交流用而最優(yōu)的定購時(shí)間間隔TBO由下面等式給定:TBOvalue = price - year*depreciatio n; prin tf(The scrap value is %.2f.n, value);OutputThe scrap value is 90.05.3-7在庫存管理中,某單個(gè)物品的經(jīng)濟(jì)定購數(shù) EOQ由下面等式給定:2需求率生產(chǎn)成本單位時(shí)間內(nèi)每種物品的 儲(chǔ)備成本2生產(chǎn)成本 _需求率 單位時(shí)間內(nèi)每種物品

13、的 儲(chǔ)備成本編寫程序,給定需求率(單位時(shí)間內(nèi)的物品數(shù))、生產(chǎn)成本(每個(gè)定購)和儲(chǔ)備 成本(單位時(shí)間內(nèi)每種物品),計(jì)算EOQ和TBO。Program#include #in clude mai n() int dema nd=1000;float product_cost=3.5, storage_cost=0.63, eoq, tbo; eoq=sqrt(2*dema nd*product_cost/storage_cost);tbo=sqrt(2*product_cost/dema nd/storage_cost);printf(EOQ is %.2f, and TBO is %.2f.n,

14、 eoq, tbo);Output _EOQ is 105.41, _anQ TBO _is 0.11.此文檔來源于網(wǎng)絡(luò),如有侵權(quán)請(qǐng)聯(lián)系網(wǎng)站刪除只供學(xué)習(xí)交流用/* Blue is in put */* Blue is in put */第4章輸入輸出操作管理4-1輸入兩個(gè)數(shù),將它們交換后輸出Program#i nclude mai n() int x, y, t;prin tf(Please in put 2 nu mbers:);sca nf(%d%d, &x, & y);prin tf(Before swap, the 2 nu mbers are: %d, %dn, x, y); t=x

15、;x=y;y=t;prin tf(After swap, the 2 nu mbers are: %d, %dn, x, y);OutputPlease in put 2 nu mbers: 3 5/* Blue is in put */Before swap, the 2 nu mbers are: 3, 5:After swap, the 2 nu mbers are: 5, 3 4-2輸入一個(gè)十進(jìn)制數(shù),輸出對(duì)應(yīng)的八進(jìn)制數(shù)和十六進(jìn)制數(shù)。Program#include mai n() int n;prin tf(Please in put a decimal nu mber:);sca nf

16、(%d , &n);printf(The octal is %o, and the hexadecimal is %x.n, n, n); OutputPlease in put a decimal nu mber: 10The . octal_is . 12, and the _ hexadecimal. j.s_ a._ _考慮:如何得到下面的輸出?Please in put a decimal nu mber: 10The octal is 012, and the hexadecimal is 0 xa.4-3編寫程序,輸入3個(gè)整數(shù),計(jì)算并輸出它們的平均值。Program#i nclu

17、de mai n()此文檔來源于網(wǎng)絡(luò),如有侵權(quán)請(qǐng)聯(lián)系網(wǎng)站刪除只供學(xué)習(xí)交流用/* Blue is in put */ int a, b, c;prin tf(Please in put 3 in tegers:); sca nf(%d%d%d, &a, &b, &c);printf(The average is %.2f.n, (a+b+c)/3.0); OutputPlease in put 3 in tegers: 47-19/* Blue is input */The average is -2.67.4-4編寫一個(gè)程序,讀取x和y的值,顯示下面表達(dá)式的值:(1)x y(2)32(3)x

18、y x yProgram#include mai n() float x, y;prin tf(Please in put x and y:);sca nf(%f%f, &x, &y);prin tf(1) (x+y)/(x-y) = %.2fn, (x+y)/(x-y);prin tf(2) (x+y)/2 = %.2fn, (x+y)/2);prin tf(3) (x+y)(x-y) = %.2fn, (x+y)*(x-y); OutputPlease input x and y: 3.54.1(1) (x+y)/(x-y) = -12.67(2) (x+y)/2 = 3.80(3) (x

19、+y)(x-y) = -4.564-5計(jì)算銀行存款的本息。編寫程序,輸入存款金額money、存期year和年利率rate, 根據(jù)下列公式計(jì)算存款到期時(shí)的本息合計(jì) sum (稅前),輸出時(shí)保留兩位小數(shù)。sum mon ey(1 rate)yearProgram#i nclude #in elude 此文檔來源于網(wǎng)絡(luò),如有侵權(quán)請(qǐng)聯(lián)系網(wǎng)站刪除只供學(xué)習(xí)交流用mai n() float mon ey, rate, sum;int year;prin tf(Please in put the deposit mon ey:); sea nf(%f, &mon ey);prin tf(Please in p

20、ut the deposit period:);seanf(%d, &year);prin tf(Please in put the annual in terest rate:);sea nf(%f, & rate);sum=mon ey*pow(1+rate, year);prin tf(The total prin cipal and in terest is: %.2fn, sum); OutputPlease in put the deposit mon ey: 2500/* Blue isin put*/Please in put the deposit period: 3/* B

21、lue isin put*/Please in put the annual in terest rate: 0.023/* Blue isin put*/The total principal and in terest is: 2676.50_ _4-6輸入圓柱的高h(yuǎn)和半徑r,求圓柱體積volume=nr2*h。Program#include #define PI 3.14mai n() float volume, r, h;prin tf(Please in put the radius of the cyli nder:); sca nf(%f, &r);prin tf(Please

22、in put the height of the cyli nder:); sca nf(%f, & h);volume=PI*r*r*h;printf(The volume of the cylinder is: %.2fn, volume); 此文檔來源于網(wǎng)絡(luò),如有侵權(quán)請(qǐng)聯(lián)系網(wǎng)站刪除只供學(xué)習(xí)交流用/* Blue is in put */ /* Blue is in put */* Blue is in put */* Blue is in put */OutputPlease in put the radius of the cyli nder: 3.5IPlease in put the

23、 height of the cyli nder: 12.7iThe volume of the cyli nder is: 488.514-7編寫一個(gè)程序,讀取一個(gè)實(shí)數(shù)f,將其四舍五入的值賦值給整型變量n,輸出n的值。(嘗試不用if語句完成本程序,并考慮負(fù)數(shù)是否適用)Program (1)#include mai n() float f;int n, m;prin tf(Please in put a real nu mber:);sca nf(%f, &f);m=f*10;m=m%10/5;/*m 是什么值? */n=f;n=n+m;/*m有何作用? */prin tf(The roun

24、ded in teger is: %dn, n);Program (2)如果不明白if語句,可以在學(xué)完第5章后再看#include mai n() float f;int n, m;prin tf(Please in put a real nu mber:);sca nf(%f, &f);n=f+0.5;/*是否理解該語句? */prin tf(The roun ded in teger is: %dn, n);Output (1)Please in put a real nu mber: 3.6.Ihe. _roynged_in teger. . isi 4-Output Please in

25、 put a real nu mber: -13.2The roun ded.i nteger is: -13 _此文檔來源于網(wǎng)絡(luò),如有侵權(quán)請(qǐng)聯(lián)系網(wǎng)站刪除只供學(xué)習(xí)交流用4-8編寫程序,讀入兩個(gè)兩位數(shù)字的整數(shù),并按照如下形式輸出這兩個(gè)整數(shù)的乘積4 5*3 73 1 51 3 51 6 6 5提示:注意格式!Program#i nclude mai n() int x, y, m, n;prin tf(Please in put 2 in tegers:);sca nf(%d%d, &x, & y);prin tf(%5dn *%3dn -n, x, y);m=y%10;n=y/10;printf

26、(%5dn%4dn -n%5dn, x*m, x*n, x*y);此文檔來源于網(wǎng)絡(luò),如有侵權(quán)請(qǐng)聯(lián)系網(wǎng)站刪除只供學(xué)習(xí)交流用第5章判斷與分支5-1輸入一個(gè)字符ch,根據(jù)不同情況進(jìn)行輸出:(1)ch為小寫字母,輸出對(duì)應(yīng)的大寫字母;(2)ch為大寫字母,按照原樣輸出;(3)ch為數(shù)字,輸出對(duì)應(yīng)的數(shù)字;(4)ch 為其他字符,輸出“ Other character.Program#i nclude mai n() char ch;prin tf(Please in put a character:);ch=getchar();if (ch=a & ch=A & ch=0 & ch=9)prin tf(%

27、dn, ch-O);elseprin tf(Other character.n);Output (1)Please in put a character: A/* Blue is in put */A _ _Output (2)Please in put a character: b/* Blue is in put */Output (3)Please in put a character: 3/* Blue is in put */Output (4)Please in put a character: /* Blue is in put */Other character.此文檔來源于網(wǎng)

28、絡(luò),如有侵權(quán)請(qǐng)聯(lián)系網(wǎng)站刪除只供學(xué)習(xí)交流用y f(x)4x3x 155-2為鼓勵(lì)居民節(jié)約用水,自來水公司采用按月用水量分段計(jì)費(fèi)的辦法,居民應(yīng)交水費(fèi)y元與月用水量x噸的函數(shù)關(guān)系式如下(設(shè)x 0)2.5x 10.5 x 15編寫程序,輸入用戶的月用水量x噸,計(jì)算并輸出該用戶應(yīng)支付的水費(fèi) y元(保留兩位小數(shù))Program#i nclude mai n() float x, y;prin tf(Please in put the water con sumpti on:);sca nf(%f, &x);if(x=15)y=4*x/3;/*是否可以寫成y=4/3*x; ?區(qū)別在哪里? */elsey=2

29、.5*x-10.5;prin tf(The water price is: %.2f. n, y);OutputPlease in put the water con sumpti on: 27.9/* Blue is in put */The water price is: 59.255-3輸入一個(gè)年份year,判斷year是否為閏年。year若為閏年,需要滿足下列條件 之一:(1)能被4整除,但不能被100整除(如2004年是閏年,2100年不是閏年)(2)能被400整除(如2000年是閏年)Program#include mai n() int year;prin tf(Please i

30、n put the year:); sea nf(%d, &year);此文檔來源于網(wǎng)絡(luò),如有侵權(quán)請(qǐng)聯(lián)系網(wǎng)站刪除只供學(xué)習(xí)交流用if (year%4=0&year%100!=0)|(year%400=0) prin tf(%d is a leap year! n, year);elseprintf (%d is not a leap year! n, year);OutputPlease in put the year: 1900/* Blue is input */1900 is not a leap year!5-4輸入3個(gè)實(shí)數(shù),將其按照降序輸出(較大數(shù)在前),保留3位小數(shù)Program (

31、1)#inelude mai n() float x, y, z;prin tf(Please in put 3 real nu mbers:);sea nf(%f%f%f, &x, &y, &z);if (xy)if(yz)prin tf(%.3f, %.3f, %.3fn, x, y, z); else if(xz)printf(%.3f, %.3f, %.3fn, x, z, y); elseprintf(%.3f, %.3f, %.3fn, z, x, y);else if(xz)printf(%.3f, %.3f, %.3f, y, x, z);else if(yz)prin tf(

32、%.3f, %.3f, %.3fn, y,乙 x); elseprin tf(%.3f, %.3f, %.3fn,乙 y, x); /*程序結(jié)束時(shí),x, y, z的值是否發(fā)生了變化?*/Program (2)#inelude mai n() float x, y, z,t;prin tf(Please in put 3 real nu mbers:);sea nf(%f%f%f, &x, &y, &z);if (xy)此文檔來源于網(wǎng)絡(luò),如有侵權(quán)請(qǐng)聯(lián)系網(wǎng)站刪除只供學(xué)習(xí)交流用t=x; x=y; y=t;if (xz)t=x; x=z; z=t;if (yz) t=y; y=z; z=t; /*此程

33、序與上面的程序有何不同?*/prin tf(%.3f, %.3f, %.3fn, x, y, z); /*程序結(jié)束時(shí),x, y, z的值是否發(fā)生了變化?*/OutputPlease in put 3 real numbers: 7.8 5 10/* Blue is input */J0.00Q, 7800, 5.000_5-5輸入五級(jí)制成績(jī)(AE),輸出相應(yīng)的百分制成績(jī)(0100)區(qū)間。五級(jí)制成績(jī) 對(duì)應(yīng)百分制程序區(qū)間為: A( 90100)、B( 8089)、C( 7079)、D( 6069) 和E( 059)。例如:輸入B,輸出(8089)。(1)用switch語句實(shí)現(xiàn);(2)用if語句實(shí)現(xiàn)

34、。Program (1)#inelude mai n() char grade;prin tf(Please in put the grade:); seanf(%e, &grade);switch (grade) ease A:prin tf(90100n);break;ease B:prin tf(8089n);break;ease C:prin tf(7079n);break;ease D:prin tf(6069n);break;ease E:prin tf(059n);break;default:prin tf(Wro ng grade! n);Program (2)#i nclud

35、e mai n() char grade;prin tf(Please in put the grade:);sca nf(%c, &grade);此文檔來源于網(wǎng)絡(luò),如有侵權(quán)請(qǐng)聯(lián)系網(wǎng)站刪除只供學(xué)習(xí)交流用if (grade=A)prin tf(90100n);else if (grade=B)prin tf(8089n);elseif (grade=C)prin tf(7079n);else if (grade=D)prin tf(6069n);else if (grade=E)prin tf(059n);elseprin tf(Wro ng grade! n); OutputPlease in

36、 put the grade: B/* Blue is in put */80895-6用switch語句實(shí)現(xiàn),輸入數(shù)字17,輸出對(duì)應(yīng)的英文單詞 Mon day, Tuesday,Sun day。Program#include mai n() int day;prin tf(Please in put the day in a week:);sca nf(%d, & day);switch(day)case 1:prin tf(Mo ndayn);break;case 2:prin tf(Tuesdayn);break;case 3:prin tf(Wed nesdayn); break;ca

37、se 4:prin tf(Thursdayn);break;case 5:prin tf(Fridayn);break;case 6:prin tf(Saturdayn);break;case 7:prin tf(Su ndayn);break;default:prin tf(Wro ng day! n);OutputPlease in put the day in a week: 4/* Blue is in put */Thursday 5-7某高校的某專業(yè)錄取研究生的條件如下:此文檔來源于網(wǎng)絡(luò),如有侵權(quán)請(qǐng)聯(lián)系網(wǎng)站刪除只供學(xué)習(xí)交流用(1) 英語成績(jī)55(2) 政治成績(jī)60(3) 專業(yè)課成績(jī)

38、60(4) 以上三科的總數(shù)學(xué)成績(jī)200,或英語和專業(yè)課的總成績(jī)130。編寫程序,讀入一個(gè)學(xué)生的成績(jī)(英語、政治和專業(yè)課),判斷是否可以被錄取。Program#include mai n() float en glish, politics, professi on al;prin tf(Please in put score of En glish:);sca nf(%f, &en glish);prin tf(Please in put score of Politics:);sca nf(%f, & politics);prin tf(Please in put score of the

39、professi onal course:); sca nf(%f, &professi on al);if(en glish=55 & politics=60 & professio nal=60 &(en glish+politics+professi on al=200 | en glish+professi on al=130) printf(Congratulations! You have been admitted! n);else prin tf(Sorry, you have bee n rejected! n);I _IOutput _i Please in put sco

40、re of En glish: 60/* Blue is in put */Please in put score of Politics: 70/* Blue is in put */Please in put score of professi onal course: 60/* Blue is in put */Sorry, you have bee n rejected!5-8編寫程序,計(jì)算下面二元方程的實(shí)數(shù)根。ax2 bx c 0應(yīng)用如下規(guī)則:(1)如果a和b的值為零,則沒有解;(2)如果a的值為零,則只有一個(gè)解(x=-c/b);(3)如果b2-4ac為負(fù)數(shù),則沒有實(shí)數(shù)根;(4)否則

41、,有兩個(gè)實(shí)數(shù)根: 2bb 4acf 2b .b 4acXi2aX22a此文檔來源于網(wǎng)絡(luò),如有侵權(quán)請(qǐng)聯(lián)系網(wǎng)站刪除只供學(xué)習(xí)交流用輸入a, b和c的值,輸出求根的情況,如有實(shí)數(shù)根,貝U輸出實(shí)數(shù)根的值。Program#i nclude #in clude mai n () float a, b, c, f;printf ( Please in put the value of a, b and c in the quadratic equatio n:n ax*x+ bx + c = 0n);printf ( a = );scanf ( %f, &a );printf ( b = );scanf (

42、%f, &b );printf ( c = );scanf ( %f, &c );printf ( About %.2fx*x + %.2fx + %.2f = 0: , a, b, c );if ( a = 0 & b = 0 )prin tf(There is no soluti on!n ”);else if ( a = 0 )prin tf(There is on ly one root: %.2f.n,- c/b);else f = b * b - 4 * a * c;if ( f 0 )prin tf(There are no real root!n);elseprin tf(Th

43、ere are 2 real roots: %.2f, %.2f.n,(-b + sqrt (f) )/2/a, (-b - sqrt( f)/2/a );_OutputPlease in put the value of a, b and c in the quadratic equati on:ax*x + bx + c = 0a = -3/*Blue is input */b = 4/*Blue is input */c = 5/*Blue is input */About -3.00 x*x + 4.00 x+5.00 = 0: There are 2 real roots: -0.7

44、9, 2.12.5-9編寫程序,讀取x的值,求下面函數(shù)的值1for x0y 0for x01for x0(1)用else if語句實(shí)現(xiàn)(2)用嵌套if語句實(shí)現(xiàn)(3)用條件運(yùn)算符?:實(shí)現(xiàn)此文檔來源于網(wǎng)絡(luò),如有侵權(quán)請(qǐng)聯(lián)系網(wǎng)站刪除只供學(xué)習(xí)交流用Program (1)#include mai n () float x;printf ( Please in put the value of x:); scanf ( %f, &x );if ( x 0 )printf ( y = 1n);else if ( x = 0 )printf ( y = 0n);else printf ( y = -1n);Pr

45、ogram (2)#include mai n () float x;printf ( Please in put the value of x:); scanf ( %f, &x );if ( x != 0 )/*寫成if(x)是否正確? */ if ( x 0 )/*寫成if(x)是否正確? */printf ( y = 1n);elseprintf ( y = -1n); else printf ( y = 0n);Program (3)#include mai n () float x;printf ( Please in put the value of x:); scanf ( %

46、f, &x );printf ( y = %dn, x 0 ? 1 : x 0 ? -1 : 0 ); OutputPlease in put the value of x: 9/* Blue is in put */_y =丄_ _ _ _ _ _ _ _ _ _ _ _ _ _此文檔來源于網(wǎng)絡(luò),如有侵權(quán)請(qǐng)聯(lián)系網(wǎng)站刪除只供學(xué)習(xí)交流用5-10某個(gè)服裝展示廳宣布以下所賣物品季節(jié)性打折:折扣購買總額機(jī)加工制品手工制品0100無折扣5%1012005%7.5%2013007.5%10.0%大于30010.0%15.0%編寫程序,計(jì)算某顧客應(yīng)付的款項(xiàng)。要求:同時(shí)用switch和if語句實(shí)現(xiàn)Progra

47、m#include mai n () float price, disco unt;此文檔來源于網(wǎng)絡(luò),如有侵權(quán)請(qǐng)聯(lián)系網(wǎng)站刪除只供學(xué)習(xí)交流用OutputCategory (m/h): h/* Blue is input */iPrice: 250.60/* Blue is in put */The net amount to be paid is 225.545-11x5 2x 丄x 0y f(x)x廠CXx 0提示:請(qǐng)調(diào)用 sqrt()函數(shù)求平方根,調(diào)用pow()函數(shù)求冪。Program#include #in clude mai n() float x, y;char category;pr

48、intf ( Category (m/h): );sea nf ( %c, & category );printf ( Price: );sea nf ( %f, &price );if ( category = m)switch ( (i nt) (price-1)/100)case 0: disco unt =:0;break;case 1: disco unt =:0.05;break;case 2: disco unt =:0.075;break;default: disco unt =:0.1; elseswitch ( (int) (price-1)/100)case 0: dis

49、co unt =:0.05;break;case 1: disco unt =:0.075;break;case 2: disco unt =:0.1;break;default: disco unt =:0.15;printf (The net amount to be paid is %.2fn, price*(1-disco un t); prin tf(Please in put the value of x:); sea nf(%f, &x);f(x)的值(保留2位小數(shù))編寫程序,輸入x,計(jì)算并輸出下列分段函數(shù)此文檔來源于網(wǎng)絡(luò),如有侵權(quán)請(qǐng)聯(lián)系網(wǎng)站刪除只供學(xué)習(xí)交流用/* Blue is

50、 in put */if(x0)y=pow(x,5)+2*x+1/x;else y=sqrt(x);prin tf(The value of y is: %.2f. n, y); OutputPlease in put the value of x: -2The value of y is: -36.505-12編寫程序,輸入一個(gè)實(shí)數(shù)(例如 456.78),求不小于該數(shù)的最小整數(shù)和不大于該數(shù)的最大整數(shù)。(提示:注意負(fù)數(shù))。Program#in elude mai n() float f;int n;prin tf(Please in put a real nu mber: ); sea nf(

51、%f, &f);n=f;if(f=0) printf(The largest integer (not larger than %.2f) is: %dn, f, n);if (f-n0)prin tf(The smallest in teger (not less than %.2f) is: %dn, f, n+1); elseprin tf(The smallest in teger (not less tha n %.2f) is: %dn, f, n);else if (f-*0)printf(The largest integer (not larger than %.2f) is

52、: %dn, f, n-1); elseprintf(The largest integer (not larger than %.2f) is: %dn, f, n); prin tf(The smallest in teger (not less tha n %.2f) is: %dn, f, n);bOutput Please in put a real nu mber: 6.78/* Blue is in put */IThe largest in teger (not larger tha n 6.78) is 6iThe smallest in teger (not less th

53、a n 6.78) is 7Output Please in put a real nu mber: 6/* Blue is in put */1The largest in teger (not larger tha n 6.00) is 6此文檔來源于網(wǎng)絡(luò),如有侵權(quán)請(qǐng)聯(lián)系網(wǎng)站刪除只供學(xué)習(xí)交流用The smallest in teger (not less tha n 6.00) is 6Output (3)Please in put a real nu mber: -6.78/* Blue is in put */The largest in teger (not larger tha n

54、 -6.78) is -7:The smallest in teger (not less tha n -6.78) is -65-13 輸入一個(gè)整數(shù)(考慮負(fù)數(shù)),判斷它是奇數(shù)還是偶數(shù)(認(rèn)為 0是偶數(shù)),并 輸出它的絕對(duì)值(不要使用labs()函數(shù))。(1) 使用if-else語句;(2) 使用if語句,但不使用else語句;(3) 使用switch語句。Program (1)#i nclude mai n() intnu mber, m;prin tf(Please in put a nu mber:);sca nf(%d, &n umber);m= nu mber0?-1* nu mber

55、: nu mber;if (m%2=0)/*如果條件是 if(number%2=0),是否正確? */ prin tf(%d is an eve n nu mber, and the absolute nu mber is %d.n, number, m); else prin tf(%d is an odd nu mber, and the absolute nu mber is %d.n,number, m);/*如果條件改為if(m%2),if語句是否需要調(diào)整? */一 一 一丨Program (2)#i nclude mai n() intnu mber, m;prin tf(Plea

56、se in put a nu mber:);scanf(%d, &number);m=nu mber;if (nu mber0) m*=-1;此文檔來源于網(wǎng)絡(luò),如有侵權(quán)請(qǐng)聯(lián)系網(wǎng)站刪除只供學(xué)習(xí)交流用運(yùn)算符操作數(shù)”的if (nu mber%2=0)printf(%d is an even number, and the absolute number is %d.n, nu mber, m);if (nu mber%2=1| nu mber%2=-1)printf(%d is an odd number, and the absolute number is %d.n, nu mber, m);P

57、rogram (3)#include mai n() intnu mber;prin tf(Please in put a nu mber:);scanf(%d, &number);switch (nu mber%2) case 1:printf(%d is an odd number, and the absolutenu mber is %d.n, nu mber, nu mber);break;case -1:printf(%d is an odd number, and the absolutenu mber is %d.n, nu mber, -1* nu mber);break;d

58、efault:printf(%d is an even number, and the absolutenumber is %d.n, number, number* (number+1)%2);OutputPlease in put a nu mber: -9/* Blue is in put */二9 is an odd number, and _thejabsolute_number is 9._ _5-14求解簡(jiǎn)單的二元運(yùn)算表達(dá)式。輸入一個(gè)形如“操作數(shù)四則運(yùn)算表達(dá)式(如:2 + 3或4 * 2.1),輸出運(yùn)算結(jié)果Program#i nclude mai n() float x, y;c

59、har op;prin tf(Please in put the expressi on:); scanf(%f%c%f, &x, &op, &y);此文檔來源于網(wǎng)絡(luò),如有侵權(quán)請(qǐng)聯(lián)系網(wǎng)站刪除只供學(xué)習(xí)交流用switch(op) case +: printf(The result is: %.2f.n, x+y);break;case -:printf(The result is: %.2f.n, x-y);break;case *: printf(The result is: %.2f.n, x*y);break;case /:prin tf(Theresult is: %.2f.n, x/y)

60、;break;default:prin tf(The expressi on is wron g.n);OutputPlease in put the expressi on: 4*2.1/* Blue is in put */The result is: 8.40.5-15某城市普通出租車收費(fèi)標(biāo)準(zhǔn)如下:“起步里程3公里,起步費(fèi)10元;超過起步里程后10公里內(nèi),每公里租費(fèi)2元;超過10公里以上的部分加收50%的 空回補(bǔ)貼費(fèi),即每公里3元。營(yíng)運(yùn)過程中,因路阻及乘客要求臨時(shí)停車的,每 5分鐘按1公里租費(fèi)計(jì)收。運(yùn)價(jià)計(jì)費(fèi)尾數(shù)四舍五入,保留到元?!本帉懗绦颍斎胄旭偫锍蹋ü铮┡c等待時(shí)間(分鐘),并計(jì)算

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(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ǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論