C程序设计语言 第二版 课后答案第四章.docx
《C程序设计语言 第二版 课后答案第四章.docx》由会员分享,可在线阅读,更多相关《C程序设计语言 第二版 课后答案第四章.docx(34页珍藏版)》请在冰豆网上搜索。
![C程序设计语言 第二版 课后答案第四章.docx](https://file1.bdocx.com/fileroot1/2022-12/11/af783162-956b-46f5-a19f-dbf78ad50132/af783162-956b-46f5-a19f-dbf78ad501321.gif)
C程序设计语言第二版课后答案第四章
Exercise4-1
Writethefunctionstrrindex(s,t),whichreturnsthepositionoftherightmostoccurrenceoftins,or-1ifthereisnone.
#include
#defineMAXLINE1000
intgetline(charline[],intmax);
intfind_the_situaion(chart[],chars[]);
intcount(charv[]);
voidmain()
{
chartarget[MAXLINE];
charline[MAXLINE];
while(getline(line,MAXLINE)>0)
{
printf("%d",find_the_situaion(line,target));
}
}
intgetline(chars[],intlim){
intc,i;
i=0;
while(--lim>0&&(c=getchar())!
=EOF&&c!
='\n')
s[i++]=c;
if(c=='\n')
s[i++]=c;
s[i]='\0';
returni;
}
intfind_the_situaion(chart[],chars[])
{
inti,j,k;
intb=count(s);
intc=count(t);
for(i=0;s[i]!
='\0';i++)
{
for(j=i,k=0;t[k]!
='\0'&&s[j]==t[k];j++,k++)
;
if(k==b+1)
{
returnc-i-b;
}
}
return-1;
}
intcount(charv[])
{
inta=0;
if(v[a]!
='\0')
{
a++;
}
returna;
}
Exercise4-2
Extendatoftohandlescientificnotationoftheform123.45e-6whereafloating-pointnumbermaybefollowedbyeorEandanoptionallysignedexponent.
Exercise4-3
Giventhebasicframework,it'sstraightforwardtoextendthecalculator.Addthemodulus(%)operatorandprovisionsfornegativenumbers.
#include
#include"pch.h"
#include
#defineMAXOP100
#defineNUMBER'0'
#defineTRUE1;
intGetop(char[]);
intgetch(void);
doublepop(void);
voidpush(doublef);
voidungetch(intc);
intmain(void)
{
inttype;
doubleop2;
chars[MAXOP];
intflag=TRUE;
while((type=Getop(s))!
=EOF)
{
switch(type)
{
case'%':
op2=pop();
if(op2)
push(fmod(pop(),op2));
else
printf("\nError:
Divisionbyzero!
");
break;
}
}
returnEXIT_SUCCESS;
}
intGetop(chars[])
{
#definePERIOD'.'
inti=0;
intc;
intnext;
while((s[0]=c=getch())==''||c=='\t')
;
s[1]='\0';
if(!
isdigit(c)&&c!
=PERIOD&&c!
='-')
returnc;
if(c=='-')
{
next=getch();
if(!
isdigit(next)&&next!
=PERIOD)
{
returnc;
}
c=next;
}
else
{
c=getch();
}
while(isdigit(s[++i]=c))
c=getch();
if(c==PERIOD)
while(isdigit(s[++i]=c=getch()))
;
s[i]='\0';
if(c!
=EOF)
ungetch(c);
returnNUMBER;
}
#defineMAXVAL100
intsp=0;
doubleval[MAXVAL];
doublepop(void)
{if(sp>0)
returnval[--sp];
else
{
printf("error:
stackempty\n");
return0.0;
}
}
voidpush(doublef)
{
if(spval[sp++]=f;
else
printf("error:
stackfull,can'tpush%g\n",f);
}
#defineBUFSIZE100
charbuf[BUFSIZE];
intbufp=0;
intgetch(void)
{
return(bufp>0)?
buf[--bufp]:
getchar();
}
voidungetch(intc)
{
if(bufp>=BUFSIZE)
printf("ungetch:
toomanycharacters\n");
else
buf[bufp++]=c;
}
Exercise4-4
Addcommandstoprintthetopelementofthestackwithoutpopping,toduplicateit,andtoswapthetoptwoelements.Addacommandtoclearthestack.
#include"pch.h"
#include
#include
#include
#include
#defineMAXOP100
#defineNUMBER0
#defineTRUE1
#defineFALSE0
intGetop(chars[]);
voidpush(doubleval);
doublepop(void);
voidshowTop(void);
voidduplicate(void);
voidswapItems(void);
voidclearStack();
intmain(void)
{
inttype;
doubleop2;
chars[MAXOP];
intflag=TRUE;
while((type=Getop(s))!
=EOF)
{
switch(type)
{
caseNUMBER:
push(atof(s));
break;
case'+':
push(pop()+pop());
break;
case'*':
push(pop()*pop());
break;
case'-':
op2=pop();
push(pop()-op2);
break;
case'/':
op2=pop();
if(op2)
push(pop()/op2);
else
printf("\n出现错误!
");
break;
case'%':
op2=pop();
if(op2)
push(fmod(pop(),op2));
else
printf("\n出现错误!
");
break;
case'?
':
showTop();
break;
case'#':
duplicate();
break;
case'~':
swapItems();
break;
case'!
':
clearStack();
case'\n':
printf("\n\t%.8g\n",pop());
break;
default:
printf("\nError:
不清楚的指令%s.\n",s);
break;
}
}
returnEXIT_SUCCESS;
}
#defineMAXVAL100
intsp=0;
doubleval[MAXVAL];
voidpush(doublef)
{
if(spval[sp++]=f;
else
printf("\n出现错误%g\n",f);
}
doublepop(void)
{
if(sp>0)
returnval[--sp];
else
{
printf("\n出现错误\n");
return0.0;
}
}
voidshowTop(void)
{
if(sp>0)
printf("Topofstackcontains:
%8g\n",val[sp-1]);
else
printf("Thestackisempty!
\n");
}
voidduplicate(void)
{
doubletemp=pop();
push(temp);
push(temp);
}
voidswapItems(void)
{
doubleitem1=pop();
doubleitem2=pop();
push(item1);
push(item2);
}
voidclearStack(void)
{
sp=0;
}
intgetch(void);
voidunGetch(int);
intGetop(chars[])
{
inti=0;
intc;
intnext;
while((s[0]=c=getch())==''||c=='\t')
;
s[1]='\0';
if(!
isdigit(c)&&c!
='.'&&c!
='-')
returnc;
if(c=='-')
{
next=getch();
if(!
isdigit(next)&&next!
='.')
{
returnc;
}
c=next;
}
else
c=getch();
while(isdigit(s[++i]=c))
c=getch();
if(c=='.')
while(isdigit(s[++i]=c=getch()))
;
s[i]='\0';
if(c!
=EOF)
unGetch(c);
returnNUMBER;
}
#defineBUFSIZE100
charbuf[BUFSIZE];
intbufp=0;
intgetch(void)
{
return(bufp>0)?
buf[--bufp]:
getchar();
}
voidunGetch(intc)
{
if(bufp>=BUFSIZE)
printf("\n出现错误\n");
else
buf[bufp++]=c;
}
Exercise4-5
Addaccesstolibraryfunctionslikesin,exp,andpow.SeeinAppendixB,Section4.
#include
#include
#include
#include
#include
#defineMAXOP100
#defineNUMBER0
#defineIDENTIFIER1
#defineTRUE1
#defineFALSE0
intGetop(chars[]);
voidpush(doubleval);
doublepop(void);
voidshowTop(void);
voidduplicate(void);
voidswapItems(void);
voidclearStack();
voiddealWithName(chars[]);
intmain(void)
{
inttype;
doubleop2;
chars[MAXOP];
intflag=TRUE;
while((type=Getop(s))!
=EOF)
{
switch(type)
{
caseNUMBER:
push(atof(s));
break;
caseIDENTIFIER:
dealWithName(s);
break;
case'+':
push(pop()+pop());
break;
case'*':
push(pop()*pop());
break;
case'-':
op2=pop();
push(pop()-op2);
break;
case'/':
op2=pop();
if(op2)
push(pop()/op2);
else
printf("\nError:
divisionbyzero!
");
break;
case'%':
op2=pop();
if(op2)
push(fmod(pop(),op2));
else
printf("\nError:
divisionbyzero!
");
break;
case'?
':
showTop();
break;
case'#':
duplicate();
break;
case'~':
swapItems();
break;
case'!
':
clearStack();
case'\n':
printf("\n\t%.8g\n",pop());
break;
default:
printf("\nError:
unknowncommand%s.\n",s);
break;
}
}
returnEXIT_SUCCESS;
}
#defineMAXVAL100
intsp=0;
doubleval[MAXVAL];
voidpush(doublef)
{
if(spval[sp++]=f;
else
printf("\nError:
stackfullcan'tpush%g\n",f);
}
doublepop(void)
{
if(sp>0)
returnval[--sp];
else
{
printf("\nError:
stackempty\n");
return0.0;
}
}
voidshowTop(void)
{
if(sp>0)
printf("Topofstackcontains:
%8g\n",val[sp-1]);
else
printf("Thestackisempty!
\n");
}
voidduplicate(void)
{
doubletemp=pop();
push(temp);
push(temp);
}
voidswapItems(void)
{
doubleitem1=pop();
doubleitem2=pop();
push(item1);
push(item2);
}
voidclearStack(void)
{
sp=0;
}
voiddealWithName(chars[])
{
doubleop2;
if(0==strcmp(s,"sin"))
push(sin(pop()));
elseif(0==strcmp(s,"cos"))
push(cos(pop()));
elseif(0==strcmp(s,"exp"))
push(exp(pop()));
elseif(!
strcmp(s,"pow"))
{
op2=pop();
push(pow(pop(),op2));
}
else
printf("%sisnotasupportedfunction.\n",s);
}
intgetch(void);
voidunGetch(int);
intGetop(chars[])
{
inti=0;
intc;
intnext;
while((s[0]=c=getch())==''||c=='\t')
;
s[1]='\0';
if(isalpha(c))
{
i=0;
while(isalpha(s[i++]=c))
c=getch();
s[i-1]='\0';
if(c!
=EOF)
unGetch(c);
returnIDENTIFIER;
}
if(!
isdigit(c)&&c!
='.'&&c!
='-')
returnc;
if(c=='-')
{
next=getch();
if(!
isdigit(next)&&next!
='.')
{
returnc;
}
c=next;
}
else
c=getch();
while(isdigit(s[++i]=c))
c=getch();
if(c=='.')
while(isdigit(s[++i]=c=getch()))
;
s[i]='\0';
if(c!
=EOF)
unGetch(c);
returnNUMBER;
}
#defineBUFSIZE100
charbuf[BUFSIZE];
intbufp=0;
intgetch(void)
{
return(bufp>0)?
buf[--bufp]:
getchar();
}
voidunGetch(intc)
{
if(bufp>=BUFSIZE)
printf("\nUnGetch:
toomanycharacters\n");
else
buf[bufp++]=c;
}
Exercise4-6
Addcommandsforhandlingvariables.(It'seasytoprovidetwenty-sixvariableswithsingle-letternames.)Addavariableforthemostrecentlyprintedvalue.
#include
#include
#include
#include
#include
#defineMAXOP100
#defineNUMBER0
/*4-6thesearenewforthisexercise*/
#defineIDENTIFIER1
#defineENDSTRING2
/*4-6endofnewstuff*/
#defineTRUE1
#defineFALSE0
#defineMAX_ID_LEN32
#defineMAXVARS30
structvarType{
charname[MAX_ID_LEN];
doubleval;
};
intGetop(chars[]);
voidpush(doubleval);
doublepop(void);
voidshowTop(void);
voidduplicate(void);
voidswapItems(void);
voidclearStacks(structvarTypevar[]);
voiddealWithName(chars[],structvarTypevar[]);
voiddealWithVar(chars[],str