1、n return 0;PE 7-2 int count = 0;inuput a string,and this program will out it and ASCII n(# to quit).n if(ch = ) printf(ninput another string(# to quit): n continue; while (count = 8) count = 0; printf(%c,%d ,ch,ch); count+;nBye!PE 7-3int main(void) int n; double sumeven = 0.0; int ct_even = 0; doubl
2、e sumodd = 0.0; int ct_odd = 0; while (scanf(%d, &n) = 1 & n != 0) if (n % 2 = 1) sumodd += n; +ct_odd; else sumeven += n; +ct_even;Number of evens: %d, ct_even); if (ct_even 0) average: %g, sumeven / ct_even); putchar(Number of odds:, ct_odd); if (ct_odd , sumodd / ct_odd);ndonenPE 7-4 int ex_c,pe_
3、c; ex_c = pe_c = 0;inuput a string,and this program will switch . to ! and ! to ! n(# to quit).n. pe_c+; else if (ch = ex_c+;%c,ch);nthere are %d pe switching and %d ex switching,pe_c,ex_c);PE 7-5 int ct1 = 0; int ct2 = 0; switch(ch) case : +ct1; break; +ct2; default : putchar(ch);%d replacements of
4、 . with !, ct1);%d replacements of ! with !, ct2);PE 7-6int main (void) int e_c,i_c,st_c; e_c = i_c = st_c = 0;input your string and this program will output the countn(# to quit): while(ch = getchar() !) /当输入ei 时 st_c =10,i_c = 10 if(ch !i) /当输入ie时st_c =10,i_c =1,输入其他字符st_c = 2 st_c = 2; if(ch = e
5、st_c = 10; i_c = 1; i_c = 10; if(st_c = i_c) /防止eii的bug e_c+; st_c = 1;nthere is %d ei in your string!nbye!,e_c);PE 7-7#define BASEPAY 10 /* $10 per hour */#define BASEHRS 40 /* hours at basepay */#define OVERTIME 1.5 /* 1.5 time */#define AMT1 300 /* 1st rate tier */#define AMT2 150 /* 2st rate tie
6、r */#define RATE1 0.15 /* rate for 1st tier */#define RATE2 0.20 /* rate for 2nd tier */#define RATE3 0.25 /* rate for 3rd tier */ double hours; double gross; double net; double taxes;Enter the number of hours worked this week: scanf(%lfhours); if (hours = BASEHRS) gross = hours * BASEPAY; gross = B
7、ASEHRS * BASEPAY + (hours - BASEHRS) * BASEPAY * OVERTIME; if (gross = AMT1) taxes = gross * RATE1; else if (gross int main(void) int choose; int test = 1;*nEnter the number corresponding to the desired pay rate or action:1) $8.75/hrtt2) $9.33/hrn3) $10.00/hrtt4) $11.20/hrn5) quitn while( (scanf(,&c
8、hoose) = 1) switch(choose) case 1: hours = 8.75; break; case 2: hours = 9.33; case 3: hours = 10.00; case 4: hours = 11.20; case 5: test = 0; default: test = 2; printf(error! try again: if(test = 2) test = 1; if(test = 1 ) if (hours gross = hours * BASEPAY; else gross = BASEHRS * BASEPAY + (hours -
9、BASEHRS) * BASEPAY * OVERTIME; if (gross taxes = gross * RATE1; else if (gross for (num = 2; num = lim; num+) for (div = 2, prime = YES; (div * div) = num; div+) if (num % div = 0) prime = NO; /* number is not prime */ if (prime = YES)%ld is prime.n, num);Please enter another limit;PE 7-11 const dou
10、ble price_artichokes = 1.25; const double price_beets = 0.65; const double price_carrots = 0.89; const double DISCOUNT_RATE = 0.05; double lb_artichokes; double lb_beets; double lb_carrots; double lb_total; double cost_artichokes; double cost_beets; double cost_carrots; double cost_total; double fin
11、al_total; double discount; double shipping;Enter a to buy artichokes, b for beets, c for carrots, q to quit:q & ch !Q if (ch = continue; while (getchar() ! ch = tolower(ch); switch (ch)aEnter pounds of artichokes:lb_artichokes);bEnter pounds of beets:lb_beets);cEnter pounds of carrots:lb_carrots);%c
12、 is not a valid choice.n cost_artichokes = price_artichokes * lb_artichokes; cost_beets = price_beets * lb_beets; cost_carrots = price_carrots * lb_carrots; cost_total = cost_artichokes + cost_beets + cost_carrots; lb_total = lb_artichokes + lb_beets + lb_carrots; if (lb_total shipping = 0.0; else i
13、f (lb_total 100.0) discount = DISCOUNT_RATE * cost_total; discount = 0.0; final_total = cost_total + shipping - discount;Your order:%.2f lbs of artichokes at $%.2f per pound:$ %.2fn, lb_artichokes, price_artichokes, cost_artichokes);%.2f lbs of beets at $%.2f per pound: lb_beets, price_beets, cost_b
14、eets);%.2f lbs of carrots at $%.2f per pound: lb_carrots, price_carrots, cost_carrots);Total cost of vegetables:, cost_total); 100)Volume discount:, discount);Shipping:, shipping);Total charges:, final_total);Chapter 8PE 8-1/Ctrl + z 结束文件 int ch; int ct = 0;= EOF) ct+;%d characters readn, ct);PE 8-3
15、/* Programming Exercise 8-3 */* Using ctype.h eliminates need to assume ASCII coding */ int uct = 0; int lct = 0; if (isupper(ch) uct+; else if (islower(ch) lct+;%d uppercase characters readn, uct);%d lowercase characters readn, lct);/* or you could use if (ch A ch z*/PE 8-5/* Programming Exercise 8-5 */* binaryguess.c - an improved number-guesser */ int high = 100; int low = 1; int guess = (high + low) / 2; char response;Pick an integer from 1 to 100. I will try to guess it.nRespond with a y if my guess is right, withna h if it is high, and with an l if it is low.nUh.is your number
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1