C PRIMER PLUS 第七八正确答案Word文档下载推荐.docx
《C PRIMER PLUS 第七八正确答案Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《C PRIMER PLUS 第七八正确答案Word文档下载推荐.docx(22页珍藏版)》请在冰豆网上搜索。
\n"
return0;
}
PE7-2
intcount=0;
inuputastring,andthisprogramwilloutitandASCII\n(#toquit)...\n"
if(ch=='
){
printf("
\ninputanotherstring(#toquit):
\n"
continue;
}
while(count==8){
count=0;
printf("
%c,%d"
ch,ch);
count++;
\nBye!
PE7-3
intmain(void)
intn;
doublesumeven=0.0;
intct_even=0;
doublesumodd=0.0;
intct_odd=0;
while(scanf("
%d"
&
n)==1&
&
n!
=0)
if(n%2==1)
sumodd+=n;
++ct_odd;
else
sumeven+=n;
++ct_even;
Numberofevens:
%d"
ct_even);
if(ct_even>
0)
average:
%g"
sumeven/ct_even);
putchar('
Numberofodds:
ct_odd);
if(ct_odd>
sumodd/ct_odd);
\ndone\n"
PE7-4
intex_c,pe_c;
ex_c=pe_c=0;
inuputastring,andthisprogramwillswitch.to!
and!
to!
!
\n(#toquit)...\n"
.'
"
pe_c++;
elseif(ch=='
'
ex_c++;
%c"
ch);
\nthereare%dpeswitchingand%dexswitching"
pe_c,ex_c);
PE7-5
intct1=0;
intct2=0;
switch(ch)
case'
:
++ct1;
break;
++ct2;
default:
putchar(ch);
%dreplacementsof.with!
ct1);
%dreplacementsof!
with!
ct2);
PE7-6
intmain(void){
inte_c,i_c,st_c;
e_c=i_c=st_c=0;
inputyourstringandthisprogramwilloutputthecount\n(#toquit):
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'
st_c=10;
i_c=1;
}
i_c=10;
if(st_c==i_c){//防止eii的bug
e_c++;
st_c=1;
\nthereis%deiinyourstring!
\nbye!
e_c);
PE7-7
#defineBASEPAY10/*$10perhour*/
#defineBASEHRS40/*hoursatbasepay*/
#defineOVERTIME1.5/*1.5time*/
#defineAMT1300/*1stratetier*/
#defineAMT2150/*2stratetier*/
#defineRATE10.15/*ratefor1sttier*/
#defineRATE20.20/*ratefor2ndtier*/
#defineRATE30.25/*ratefor3rdtier*/
doublehours;
doublegross;
doublenet;
doubletaxes;
Enterthenumberofhoursworkedthisweek:
"
scanf("
%lf"
hours);
if(hours<
=BASEHRS)
gross=hours*BASEPAY;
gross=BASEHRS*BASEPAY+(hours-BASEHRS)*BASEPAY*OVERTIME;
if(gross<
=AMT1)
taxes=gross*RATE1;
elseif(gross<
=AMT1+AMT2)
taxes=AMT1*RATE1+(gross-AMT1)*RATE2;
taxes=AMT1*RATE1+AMT2*RATE2+(gross-AMT1-AMT2)*RATE3;
net=gross-taxes;
gross:
$%.2f;
taxes:
net:
$%.2f\n"
gross,taxes,net);
PE7-8
ctype.h>
intmain(void){
intchoose;
inttest=1;
*****************************************************************\n"
Enterthenumbercorrespondingtothedesiredpayrateoraction:
1)$8.75/hr\t\t2)$9.33/hr\n3)$10.00/hr\t\t4)$11.20/hr\n5)quit\n"
while((scanf("
&
choose))==1){
switch(choose){
case1:
hours=8.75;
break;
case2:
hours=9.33;
case3:
hours=10.00;
case4:
hours=11.20;
case5:
test=0;
default:
test=2;
printf("
error!
tryagain:
if(test==2){
test=1;
if(test==1){
if(hours<
gross=hours*BASEPAY;
else
gross=BASEHRS*BASEPAY+(hours-BASEHRS)*BASEPAY*OVERTIME;
if(gross<
taxes=gross*RATE1;
elseif(gross<
taxes=AMT1*RATE1+(gross-AMT1)*RATE2;
taxes=AMT1*RATE1+AMT2*RATE2+(gross-AMT1-AMT2)*RATE3;
net=gross-taxes;
$%.2f\nnext:
}
elseif(test==0){
break;
}
return0;
PE7-9
#defineNO0
#defineYES1
longnum;
/*valuetobechecked*/
longdiv;
/*potentialdivisors*/
longlim;
/*limittovalues*/
intprime;
Pleaseenterlimittovaluestobechecked;
Enterqtoquit.\n"
%ld"
lim)==1&
lim>
for(num=2;
num<
=lim;
num++)
{
for(div=2,prime=YES;
(div*div)<
=num;
div++)
if(num%div==0)
prime=NO;
/*numberisnotprime*/
if(prime==YES)
%ldisprime.\n"
num);
Pleaseenteranotherlimit;
PE7-11
constdoubleprice_artichokes=1.25;
constdoubleprice_beets=0.65;
constdoubleprice_carrots=0.89;
constdoubleDISCOUNT_RATE=0.05;
doublelb_artichokes;
doublelb_beets;
doublelb_carrots;
doublelb_total;
doublecost_artichokes;
doublecost_beets;
doublecost_carrots;
doublecost_total;
doublefinal_total;
doublediscount;
doubleshipping;
Enteratobuyartichokes,bforbeets,"
cforcarrots,qtoquit:
q'
&
ch!
Q'
if(ch=='
continue;
while(getchar()!
ch=tolower(ch);
switch(ch)
a'
Enterpoundsofartichokes:
lb_artichokes);
b'
Enterpoundsofbeets:
lb_beets);
c'
Enterpoundsofcarrots:
lb_carrots);
%cisnotavalidchoice.\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;
elseif(lb_total<
5.0)
shipping=3.50;
20)
shipping=10.0;
shipping=8.0+0.1*lb_total;
if(cost_total>
100.0)
discount=DISCOUNT_RATE*cost_total;
discount=0.0;
final_total=cost_total+shipping-discount;
Yourorder:
%.2flbsofartichokesat$%.2fperpound:
$%.2f\n"
lb_artichokes,price_artichokes,cost_artichokes);
%.2flbsofbeetsat$%.2fperpound:
lb_beets,price_beets,cost_beets);
%.2flbsofcarrotsat$%.2fperpound:
lb_carrots,price_carrots,cost_carrots);
Totalcostofvegetables:
cost_total);
100)
Volumediscount:
discount);
Shipping:
shipping);
Totalcharges:
final_total);
Chapter8
PE8-1
//Ctrl+z结束文件
intch;
intct=0;
=EOF)
ct++;
%dcharactersread\n"
ct);
PE8-3
/*ProgrammingExercise8-3*/
/*Usingctype.heliminatesneedtoassumeASCIIcoding*/
intuct=0;
intlct=0;
if(isupper(ch))
uct++;
elseif(islower(ch))
lct++;
%duppercasecharactersread\n"
uct);
%dlowercasecharactersread\n"
lct);
/*
oryoucoulduse
if(ch>
A'
ch<
Z'
elseif(ch>
z'
*/
PE8-5
/*ProgrammingExercise8-5*/
/*binaryguess.c--animprovednumber-guesser*/
inthigh=100;
intlow=1;
intguess=(high+low)/2;
charresponse;
Pickanintegerfrom1to100.Iwilltrytoguess"
it.\nRespondwithayifmyguessisright,with"
\nahifitishigh,andwithanlifitislow.\n"
Uh...isyournumber