版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
Chapter2
AnswerstoSelectedExercises
2.[was#21(a)Theprogramcontainsonedirective(#include)andfour
statements(threecallsofprintfandonereturn).
(b)
Parkinson'sLaw:
Workexpandssoastofillthetime
availableforitscompletion.
3.[was#4]
ttinclude<stdio.h>
intmain(void)
(
intheight=8,length=12,width=10,volume;
volume=height*length*width;
printf(^Dimensions:%dx%dx%d\n〃,length,width,height);
printf(Z/Volume(cubicinches):%d\nz,,volume);
printf(^Dimensionalweight(pounds):%d\n”,(volume+165)/166);
return0;
)
4.[was#6]Here'sonepossibleprogram:
ttinclude<stdio.h>
intmain(void)
(
inti,j,k;
floatx,y,z;
printf("Valueofi:%d\nz,,i);
printf("Valueofj:%d\n〃,j);
printf("Valueofk:%d\n〃,k);
printf("Valueofx:%g\nz,,x);
printf("Valueofy:y);
printf(^Valueofz:%g\n,/,z);
return0;
)
WhencompiledusingGCCandthenexecuted,thisprogramproducedthe
followingoutput:
Valueofi:5618848
Valueofj:0
Valueofk:6844404
Valueofx:3.98979e-34
Valueofy:9.59105e-39
Valueofz:9.59105e-39
Thevaluesprinteddependonmanyfactors,sothechancethatyou'11get
exactlythesenumbersissmall.
5.[was#10](a)isnotlegalbecause100_bottlesbeginswithadigit.
8.[was#12]Thereare14tokens:a,=,(,3,*,q,p,*,p,),/,3,
and
AnswerstoSelectedProgrammingProjects
4.[was#8;modified]
ftinclude<stdio.h>
intmain(void)
(
floatoriginal_amount,amount_withtax;
printf("Enteranamount:〃);
scanf(//%f&original_amount);
amount_with_tax=original_amount*1.05f;
printf("Withtaxadded:$%.2f\n〃,amount_with_tax);
return0;
)
Theamount_with_taxvariableisunnecessary.Ifweremoveit,theprogram
isslightlyshorter:
ttinclude<stdio.h>
intmain(void)
(
floatoriginal_amount;
printf("Enteranamount:〃);
scanf(〃%f”,&original_amount);
printf("Withtaxadded:$%.2f\n”,original_amount*1.05f);
return0;
)
Chapter3
AnswerstoSelectedExercises
2.[was#2]
(a)printf(/z%-8.le^,x);
(b)printf6e〃,x);
(c)printfC%-8.3f*,x);
(d)printf(〃%6.Of”,x);
5.[was#8]Thevaluesofx,i,andywillbe12.3,45,and.6,respectively.
AnswerstoSelectedProgrammingProjects
1.[was#4;modified]
ttinclude<stdio.h>
intmain(void)
(
intmonth,day,year;
printf("Enteradate(nun/dd/yyyy):〃);
scanf(〃%d/%d/%cT,&month,&day,&year);
printf(Z/Youenteredthedate%d%.2d%.2d\n,z,year,month,day);
return0;
)
3.[was#6;modified]
ttinclude<stdio.h>
intmain(void)
(
intprefix,group,publisher,item,check_digit;
printf("EnterISBN:〃);
scanf(/,%d-%d-%d-%d-%d,z,&prefix,&group,fcpublisher,&item,
&checkdigit);
printf(〃GS1prefix:%d\n/z,prefix);
printf("Groupidentifier:%d\n〃,group);
printf("Publishercode:%d\n〃,publisher);
printf("Itemnumber:%d\n/z,item);
printf("Checkdigit:%d\n〃,check_digit);
/*Thefiveprintfcallscanbecombinedasfollows:
printf(Z,GS1prefix:%d\nGroupidentifier:%d\nPublisher
code:%d\nltemnumber:%d\nCheckdigit:%d\n,z,
prefix,group,publisher,item,checkdigit);
*/
return0;
)
Chapter4
AnswerstoSelectedExercises
2.[was#2]NotinC89.Supposethatiis9andjis7.Thevalueof(~i)/j
couldbeeither-1or-2,dependingontheimplementation.Ontheother
hand,thevalueof-(i/j)isalways-1,regardlessoftheimplementation.
InC99,ontheotherhand,thevalueof(-i)/jmustbeequaltothevalue
of-(i/j).
9.[was#6]
(a)638
(b)321
(c)2-13
(d)000
13.[was#8]Theexpression++iisequivalentto(i+=1).Thevalueof
bothexpressionsisiaftertheincrementhasbeenperformed.
AnswerstoSelectedProgrammingProjects
2.[was#4]
ftinclude<stdio.h>
intmain(void)
(
intn;
printf("Enterathree-digitnumber:〃);
scanf&n);
printf(z,Thereversalis:%d%d%d\n,z,n%10,(n/10)%10,n/100);
return0;
)
Ch叩ter5
AnswerstoSelectedExercises
2.[was#21
/\
\(a/!1
/b1\
X(71
(/\
\c/)1
(/d\
X71
4.[was#4](i>j)-(i<j)
6.[was#12]Yes,thestatementislegal.Whennisequalto5,itdoes
nothing,since5isnotequalto-9.
10.[was#16]Theoutputis
onetwo
sincetherearenobreakstatementsafterthecases.
AnswerstoSelectedProgrammingProjects
2.[was#6]
ttinclude<stdio.h>
intmain(void)
(
inthours,minutes;
printf("Entera24-hourtime:〃);
scanf(〃%d:%d〃,&hours,&minutes);
printf(^Equivalent12-hourtime:〃);
if(hours==0)
printf("12:%.2dAM\n〃,minutes);
elseif(hours<12)
printf(/z%d:%.2dAM\nzz,hours,minutes);
elseif(hours==12)
printf(〃與d:%.2dPM\n〃,hours,minutes);
else
printf(z,%d:%.2dPM\n",hours-12,minutes);
return0;
)
4.[was#8;modified]
#include<stdio.h>
intmain(void)
(
intspeed;
printf("Enterawindspeedinknots:〃);
scanf(〃%d〃,&speed);
if(speed<1)
printf(〃Calm\n");
elseif(speed<=3)
printf(z,Lightair\n〃);
elseif(speed<=27)
printf("Breeze'n");
elseif(speed<=47)
printf("Gale\n〃);
elseif(speed<=63)
printf(z/Storm\nzz);
else
printf("Hurricane\n〃);
return0;
)
6.[was#10]
ttinclude<stdio.h>
intmain(void)
(
intcheck_digit,d,il,i2,i3,i4,i5,jl,j2,j3,j4,j5,
first_sum,second_sum,total;
printf("Enterthefirst(single)digit:");
scanf(,z%ldz,,&d);
printf("Enterfirstgroupoffivedigits:〃);
scanf&il,&i2,&i3,&i4,&i5);
printf("Entersecondgroupoffivedigits:〃);
scanf&jl,&j2,&j3,&j4,&j5);
printf("Enterthelast(single)digit:〃);
scanf(/z%Idz,,&check_digit);
first_sum=d+i2+i4+jl+j3+j5;
second_sum=il+i3+i5+j2+j4;
total=3*first_sum+second_sum;
if(check_digit=9-((total-1)%10))
printf("VALID'n");
else
printfCNOTVALTD\n3;
return0;
}
10.[was#14]
#include<stdio.h>
intmain(void)
(
intgrade;
printf("Enternumericalgrade:");
scanf&grade);
if(grade<0||grade>100){
printf("Illegalgrade\n〃);
return0;
)
switch(grade/10){
case10:
case9:printf("Lettergrade:A\n〃)
break;
case8:printf(''Lettergrade:B\n〃)
break;
case7:printf("Lettergrade:C\n〃)
break;
case6:printf(^Lettergrade:D\n〃)
break;
case5:
case4:
case3:
case2:
case1:
case0:printf(^Lettergrade:F\n〃);
break;
)
return0;
)
Ch叩ter6
AnswerstoSelectedExercises
4.[was#10](c)isnotequivalentto(a)and(b),becauseiisincremented
beforetheloopbodyisexecuted.
10.[was#12]Considerthefollowingwhileloop:
while(…){
continue;
Theequivalentcodeusinggotowouldhavethefollowingappearance:
while(…){
gotoloop_end;
???
loop_end:;/*nullstatement*/
)
12.[was#14]
for(d=2;d*d<=n;d-+)
if(n%d==0)
break;
Theifstatementthatfollowstheloopwillneedtobemodifiedaswell:
if(d*d<=n)
printf(zz%disdivisibleby%d\n〃,n,d);
else
printf(〃%disprime\n〃,n);
14.[was#16]Theproblemisthesemicolonattheendofthefirstline.
Ifweremoveit,thestatementisnowcorrect:
if(n%2二二0)
printf(^niseven\n〃);
AnswerstoSelectedProgrammingProjects
2.[was#2]
ffinclude<stdio.h>
intmain(void)
(
intm,n,remainder;
printf("Entertwointegers:〃);
scanf(〃%d%d”,&m,&n);
while(n!=0){
remainder=m%n;
m=n;
n二remainder;
)
printf(^Greatestcommondivisor:%d\nz,,m);
return0;
)
4.[was#41
ffinclude<stdio.h>
intmain(void)
(
floatcommission,value;
printf(Z/Entervalueoftrade:");
scanf(〃%f”,lvalue);
while(value!=0.Of){
if(value<2500.OOf)
commission二30.OOf+.017f*value;
elseif(value<6250.OOf)
commission二56.OOf+.0066f*value;
elseif(value<20000.OOf)
commission二76.OOf+.0034f*value;
elseif(value<50000.OOf)
commission=100.OOf+.0022f*value;
elseif(value<500000.OOf)
commission=155.OOf+.OOllf*value;
else
commission=255.OOf+.0009f*value;
if(commission<39.OOf)
commission=39.OOf:
printf(''Commission:$%.2f\n\n〃,commission)
printf(/zEntervalueoftrade:〃);
scanf(zz%fz/,&value);
return0;
)
6.[was#6]
^include<stdio.h>
intmain(void)
(
inti,n;
printf(^Enterlimitonmaximumsquare:〃);
scanf&n);
for(i=2;i*i<=n;i+=2)
printf(〃%d\n〃,i*i);
return0;
}
8.[was#8]
#include<stdio.h>
intmain(void)
(
inti,n,start_day;
printf("Enternumberofdaysinmonth:〃);
scanf&n);
printf("Enterstartingdayoftheweek(l=Sun,7=Sat):〃);
scanf(〃/d〃,&start_day);
/*printanyleading"blankdates"*/
for(i=1;i<start_day;i++)
printfC〃);
/*nowprintthecalendar*/
for(i=1;i<=n;i++){
printf("%3d〃,i);
if((start_day+i-1)%7—0)
printf(〃\n〃);
)
return0;
Chapter7
AnswerstoSelectedExercises
3.[was#4](b)isnotlegal.
4.[was#61(d)isillegal,sinceprintfrequiresastring,notacharacter,
asitsfirstargument.
10.[was#14]unsignedint,becausethe(int)castappliesonlytoj,not
j*k.
12.[was#16]Thevalueofiisconvertedtofloatandaddedtof,then
theresultisconvertedtodoubleandstoredind.
14.[was#18]No.Convertingftointwillfailifthevaluestoredin
fexceedsthelargestvalueoftypeint.
AnswerstoSelectedProgrammingProjects
1.[was#2]shortintvaluesareusuallystoredin16bits,causingfailure
at182.intandlongintvaluesareusuallystoredin32bits,withfailure
occurringat46341.
2.[was#8]
#include<stdio.h>
intmain(void)
(
inti,n;
charch;
printf("Thisprogramprintsatableofsquares.\n,z);
printf("Enternumberofentriesintable:〃);
scanf(/z%d/z,&n);
ch=getchar();
/*disposeofnew-linecharacterfollowingnumberofentries*/
/*couldsimplybegetchar();*/
for(i=1;i<=n;i++){
printf(,,%10d%10d\n,/,i,i*i);
if(i%24=0){
printf("PressEntertocontinue…〃);
ch=getchar();/*orsimplygetchar();*/
}
return0;
}
5.[was#10]
ttinclude<ctype.h>
itinclude<stdio.h>
intmain(void)
(
intsum=0;
charch;
printf("Enteraword:〃);
while((ch=getchar())!='\n')
switch(toupper(ch)){
case'D':case'G':
sum+=2;break;
case'B':case'C':case'M':case'P':
sum+=3;break;
case'F':case'H':case'V':case'W':case'Y':
sum+=4;break;
case'K':
sum+=5;break;
case'J,:case'X':
sum+=8;break;
case'Q':case'Z':
sum+=10;break;
default:
sum++;break;
)
printf(^Scrabblevalue:%d\n,z,sum);
return0;
}
6.[was#12]
#include<stdio.h>
intmain(void)
printf("Sizeofint:%d\n〃,(int)sizeof(int));
printf(,zSizeofshort:%d\n/z,(int)sizeof(short));
printf("Sizeoflong:%d\n,z,(int)sizeof(long));
printf("Sizeoffloat:%d\n/z,(int)sizeof(float));
printf("Sizeofdouble:%d\n〃,(int)sizeof(double));
printf("Sizeoflongdouble:刎\n〃,(int)sizeof(longdouble));
return0;
}
Sincethetypeofasizeofexpressionmayvaryfromoneimplementation
toanother,it'snecessaryinC89tocastsizeofexpressionstoaknown
typebeforeprintingthem.Thesizesofthebasictypesaresmallnumbers,
soit'ssafetocastthemtoint.(Ingeneral,however,it'sbesttocast
sizeofexpressionstounsignedlongandprintthemusing%lu.)InC99,
wecanavoidthecastbyusingthe%zuconversionspecification.
Chapter8
AnswerstoSelectedExercises
1.[was#4]Theproblemwithsizeof(a)/sizeof(t)isthatitcan'teasily
becheckedforcorrectnessbysomeonereadingtheprogram.(Thereader
wouldhavetolocatethedeclarationofaandmakesurethatitselements
havetypet.)
2.[was#8]Touseadigitd(incharacterform)asasubscriptintothe
arraya,wewouldwritea[d-,0*].Thisassumesthatdigitshave
consecutivecodesintheunderlyingcharacterset,whichistrueofASCII
andotherpopularcharactersets.
7.[was#1。]
constintsegments[10][7]:{{1,1,1,1,1,1),
(0,1,1),
{1,1,0,1,1,o,1},
{1,1,1,1,0,0,1),
{o,1,I,0,0,1,1),
{1,0,1,1,0,1,1},
{1,0,1,1,1,1,1},
{1,1,1),
(1,1,1,1,1,1,1),
{1,1,1,1,0,1,1))
AnswerstoSelectedProgrammingProjects
2.[was#21
#include<stdio.h>
intmain(void)
(
intdigit_count[10]={0};
intdigit;
longn;
printf(/zEnteranumber:〃);
scanf(〃%ld”,&n);
while(n>0){
digit=n%10;
digit_count[digit]++;
n/-10;
)
printf("Digit:〃);
for(digit=0;digit<=9;digit++)
printf(線3d”,digit);
printf(〃\nOccurrences:〃);
for(digit=0;digit<=9;digit++)
printf(,z%3dzz,digit_count[digit]);
printfCXn");
return0;
)
5.[was#6]
ttinclude<stdio.h>
ftdefineNUM_RATES((int)(sizeof(value)/sizeof(value[0])))
^defineINITIAL_BALANCE100.00
intmain(void)
(
inti,low_rate,month,num_years,year;
doublevalue[5];
printf("Enterinterestrate:");
scanf(,z%d/z,&low_rate);
printf("Enternumberofyears:〃);
scanf&num_years);
printf(〃\nYears");
for(i=0;i<NUM_RATES;i++){
printf(〃%6d%%〃,low_rate+i);
value[i]=INITIAL_BALANCE;
)
printfCAn");
for(year=1;year<=num_years;year++){
printf(z,%3d”,year);
for(i=0;i<NUM_RATES;i++){
for(month=1;month<=12;month++)
value[i]+=((double)(low_rate+i)/12)/100.0*value[i];
printf("%7.2f",value[i]);
)
printfCXn");
return0;
)
8.[was#12]
#include<stdio.h>
ftdefineNUM_QUIZZES5
#defineNUM_STUDENTS5
intmain(void)
(
intgrades[NUM_STUDENTS:[NUM_QUIZZES];
inthigh,low,quiz,student,total;
for(student=0;student<NUM_STUDENTS;student++){
printf(Z/Entergradesforstudent%d:〃,student+1);
for(quiz=0;quiz<NUM_QUIZZES;quiz++)
scanf&grades[student][quiz]);
printf(^XnStudentTotalAverage\n〃);
for(student=0;student<NUM_STUDENTS;student++){
printf(z/%4d”,student+1);
total=0;
for(quiz=0;quiz<NUM_QUIZZES;quiz++)
total+=grades[student][quiz];
printf(/z%3d%3d\n”,total,total/NUM_QUIZZES);
printf("\nQuizAverageHighLow\n〃);
for(quiz=0;quiz<NUM_QUIZZES;quiz++){
printf("%3d〃,quiz+1);
total=0;
high=0;
low=100;
for(student=0;student<NUM_STUDENTS;student++){
total+=grades[student][quiz];
if(grades[student][quiz]>high)
high=grades[student][quiz];
if(grades[student][quiz]<low)
low=grades[student][quiz];
)
printf("%3d%3d與3d\n〃,total/NUM_STUDENTS,high,low);
return0;
Ch叩ter9
AnswerstoSelectedExercises
2.[was#2]
intcheck(intx,inty,intn)
(
return(x>=0&&x<=n-1&&y>=0&&y<=n-1);
)
4.[was#4]
intday_of_year(intmonth,intday,intyear)
(
intnum_days[]={31,28,31,30,31,30,31,31,30,31,30,31};
intday_count=0,i;
for(i=1;i<month;i++)
day_count+=num_days[i-1];
/*adjustforleapyears,assumingtheyaredivisibleby4*/
if(year%4==0&&month>2)
day_count++;
returnday_count+day;
}
Usingtheexpressionyear%4==0totestforleapyearsisnotcompletely
correct.Centuriesarespecialcases:ifayearisamultipleof100,then
itmustalsobeamultipleof400inordertobealeapyear.Thecorrect
testis
year%4=0&&(year%100!二0||year%400==0)
6.[was#6;modified]
intdigit(intn,intk)
(
inti;
for(i=1;i<k;i++)
n/=10;
returnn%10;
)
8.[was#8](a)and(b)arevalidprototypes,(c)isillegal,sinceit
doesn,tspecifythetypeoftheparameter.(d)incorrectlyspecifiesthat
freturnsanintvalueinC89;inC99,omittingthereturntypeisillegal.
10.Lwas#10」
(a)
intlargest(inta[],intn)
(
inti,max=a[0];
for(i=1;i<n;i++)
if(a[i]>max)
max=a[i];
returnmax;
)
(b)
intaverage(inta[],intn)
(
inti,avg=0;
for(i=0;i<n;i++)
avg+=a[i];
returnavg/n;
}
(c)
intnum_positive(inta[],intn)
(
inti,count=0;
for(i=0;i<n;i++)
if(a[i]>0)
count++;
returncount;
)
15.[was#12;modified]
doublemedian(doublex,doubley,doublez)
(
doubleresult;
if(x<=y)
if(y<=z)result=y;
elseif(x<=z)result=z;
elseresult=x;
else{
if(z<=y)result二y;
elseif(x<=z)result=x;
elseresult=z;
}
returnresult;
)
17.[was#14]
intfact(intn)
(
inti,result=1;
for(i=2;i<=n;i++)
result*=i;
returnresult;
)
19.[was#161Thefollowingprogramteststhepbfunction:
ttinclude<stdio.h>
voidpb(intn);
intmain(void)
(
intn;
printf(^Enteranumber:〃);
scanf&n);
printf("Outputofpb:〃);
pb(n);
printf;
return0;
}
voidpb(intn)
(
if(n!=0){
pb(n/2);
putchar+n%2);
)
)
pbprintsthebinaryrepresentationoftheargumentn,assumingthatn
isgreaterthan0.(Wealsoassumethatdigitshaveconsecutivecodesin
theunderlyingcharacterset.)Forexample:
Enteranumber:53
Outputofpb:110101
Atraceofpb'sexecutionwouldlooklikethis:
pb(53)findsthat53isnotequalto0,soitcalls
pb(26),whichfindsthat26isnotequalto0,soitcalls
pb(13),whichfindsthat13isnotequalto0,soitcalls
pb(6),whichfindsthat6isnotequalto0,soitcalls
pb(3),whichfindsthat3isnotequalto0,soit
calls
pb(l),whichfindsthat1isnotequalto0,so
itcalls
pb(0),whichfindsthat0isequalto0,so
itreturns,causing
pb(l)toprint1andreturn,causing
pb(3)toprint1andreturn,causing
pb(6)toprint0andreturn,causing
pb(13)toprint1andreturn,causing
pb(26)toprint0andreturn,causing
pb(53)toprint1andreturn.
Ch叩ter10
AnswerstoSelectedExercises
1.[was#2](a)a,b,andcarevisible.
(b)a,anddarevisible.
(c)a,d,andearevisible.
(d)aandfarevisible.
AnswerstoSelectedProgrammingProjects
3.[was#4]
ttinclude<stdbool.h>/*C99only*/
#include<stdio.h>
ttinclude<stdlib.h>
^defineNUM_CARDS5
ttdefineRANK0
^defineSUIT1
/*externalvariables*/
inthand[NUM_CARDS][2];
/*01
0|_
1I—I—
2I_1_
3I1_
4I—I—
ranksuit
*/
boolstraight,flush,four,three;
intpairs;/*canbe0,1,or2*/
/*prototypes*/
voidread_cards(void);
voidanalyze_hand(void);
voidprint_result(void);
*main:Callsread_cards,analyze_hand,andprint_result*
*repeatedly.*
^|cj
intmain(void)
(
for(;;){
read_cards();
analyze_hand();
print_result();
}
)
[?)|^
*read_cards:Readsthecardsintotheexternalvariable*
*hand;checksforbadcardsandduplicate*
*cards.*
voidread_cards(void)
(
charch,rank_ch,suit_ch;
inti,rank,suit;
boolbad_card,duplicate_card;
intcards_read=0;
while(cards_read<NUM_CARDS)(
badcard=false;
printf(z,Enteracard:〃);
rank_ch=getchar();
switch(rank_ch){
case'O'exit(EXIT_SUCCESS)
case,2'rank=0break;
case'3'rank=1break;
case,4'rank二2break;
case,5'rank=3break;
case,6'rank二4break;
,7,
caserank=5break;
caserank=6break;
case,c,rank=7break;
case't':caseTrank=8break;
9j.
case:case'J'rank二9break;
,,
caseq:case'Q'rank=10;break;
case,k':case'K'rank二11;break;
,,
casea:case'A,rank=12;break;
default:bad_(:ard二true;
)
suit_ch二getchar();
switch(suit_ch){
casec:case'C'suit=0break;
case'd':case'D'suit=1break;
case'h':case'H'suit=2break;
,,
cases:case'S':suit二3break;
default:bad_(:ard=true;
while((ch=getchar())!='\n')
if(ch!=')bad_card=true;
if(bad_card){
printf(^Badcard;ignored.\n/z);
continue;
}
duplicate_card=false;
for(i=0;i<cards_read;i++)
if(handEi][RANK]=rank&&hand[i][SUIT]==suit)
printf(^Duplicatecard;ignored.Xn^);
duplicate_card=true;
break;
if(!duplicate_card){
hand[cards_read][RANK]=rank;
hand[cards_read][SUIT]=suit;
cards_read++;
)
}
*analyze_hand:Determineswhetherthehandcontainsa*
*straight,aflush,four-of-a-kind,*
*and/orthree-of-a-kind;determinesthe*
*numberofpairs;storestheresultsinto*
*theexternalvariablesstraight,flush,*
*four,three,andpairs.*
/
voidanalyze_hand(void)
(
intrank,suit,card,pass,run;
straight=true;
flush=true;
four=false;
three=false;
pairs=0;
/*sortcardsbyrank*/
for(pass-1;pass<NUM_CARDS;pass++)
for(card=0;card<NUM_CARDS-pass;card++)
rank=hand[card][RANK];
suit=hand[card][SUIT];
if(hand[card+l][RANK]<rank){
hand[card][RANK]=hand[card+1][RANK];
hand[card][SUIT]=hand[card+1][SUIT];
hand[card+1][RANK'=rank;
hand[card+1][SUIT=suit;
)
)
/*checkforflush*/
suit=hand[0][SUIT];
for(card=1;card<NUM_CARDS;card++)
if(hand[card][SUIT]!=suit)
flush=false;
/*checkforstraight*/
for(card=0;card<NUM_CARDS-1;card++)
if(hand[card][RANK]-1!=hand[card+1][RANK])
straight=false;
/*checkfor4-of-a-kind,3-of-a-kind,andpairsby
lookingfor"runs"ofcardswithidenticalranks*/
card=0;
while(card<NUM_CARDS){
rank=hand[card][RANK];
run=0;
do{
run+4-;
card++;
}while(card<NUM_CARDS&&hand[card][RANK]==rank);
switch(run){
case2:pairs++;break;
case3:three=true;break;
case4:four=true;break;
}
}
)
*print_result:Printstheclassificationofthehand,*
*basedonthevaluesoftheexternal*
*variablesstraight,flush,four,three,*
*andpairs.*
t
voidprint_result(void)
(
if(straight&&flush)printf(^Straightflush");
elseif(four)printf("Fourofakind");
elseif(three&&
pairs==1)printf("Fullhouse");
elseif(flush)printf("Flush");
elseif(straight)printf("Straight");
elseif(three)printf("Threeofakind")
elseif(pairs==2)printf(^Twopairs");
elseif(pairs二二1)printf("Pair");
elseprintf(^Highcard");
printfC\n\nO;
)
5.[was#6]
ttinclude<stdbool.h>/*C99only*/
ttinclude<stdio.h>
ttinclude<stdlib.h>
ttdefineNUM_RANKS13
^defineNUM_SUITS4
ttdefineNUM_CARDS5
/*externalvariables*/
intnum_in_rank[NUM_R
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 二零二五版商鋪購買意向定金合同模板3篇
- 2024年版校車租賃合同范本
- 二零二五版車輛行駛安全協(xié)議及事故責任認定3篇
- 二零二五年度甜品店經(jīng)營管理權(quán)及專利轉(zhuǎn)讓合同3篇
- 2024授權(quán)簽訂合同的委托書
- 二零二五年燈具及五金配件銷售合同3篇
- 二零二五年快餐外賣平臺加盟與合作協(xié)議3篇
- 瀟湘職業(yè)學院《用戶畫像與精準營銷》2023-2024學年第一學期期末試卷
- 西藏藏醫(yī)藥大學《鍋爐原理及設(shè)備》2023-2024學年第一學期期末試卷
- 溫州醫(yī)科大學《中醫(yī)護理技能》2023-2024學年第一學期期末試卷
- 三年級道德與法治教學工作總結(jié)
- 托卡馬克等離子體約束
- 各級各類護理人員崗位職責
- 狂犬病與破傷風的同意告知書
- FZ/T 81013-2016寵物狗服裝
- PPVT幼兒語言能力測試題附答案
- JB∕T 14089-2020 袋式除塵器 濾袋運行維護技術(shù)規(guī)范
- 陜西省寶雞市各縣區(qū)鄉(xiāng)鎮(zhèn)行政村村莊村名居民村民委員會明細及行政區(qū)劃代碼
- 中華人民共和國職業(yè)分類大典電子版
- 畢業(yè)設(shè)計小型液壓機主機結(jié)構(gòu)設(shè)計與計算
- 19XR開機運行維護說明書
評論
0/150
提交評論