C PRIMER PLUS 第七八正确答案Word文档下载推荐.docx

上传人:b****7 文档编号:22100334 上传时间:2023-02-02 格式:DOCX 页数:22 大小:18.47KB
下载 相关 举报
C PRIMER PLUS 第七八正确答案Word文档下载推荐.docx_第1页
第1页 / 共22页
C PRIMER PLUS 第七八正确答案Word文档下载推荐.docx_第2页
第2页 / 共22页
C PRIMER PLUS 第七八正确答案Word文档下载推荐.docx_第3页
第3页 / 共22页
C PRIMER PLUS 第七八正确答案Word文档下载推荐.docx_第4页
第4页 / 共22页
C PRIMER PLUS 第七八正确答案Word文档下载推荐.docx_第5页
第5页 / 共22页
点击查看更多>>
下载资源
资源描述

C PRIMER PLUS 第七八正确答案Word文档下载推荐.docx

《C PRIMER PLUS 第七八正确答案Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《C PRIMER PLUS 第七八正确答案Word文档下载推荐.docx(22页珍藏版)》请在冰豆网上搜索。

C PRIMER PLUS 第七八正确答案Word文档下载推荐.docx

\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

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 解决方案 > 学习计划

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1