C程序设计语言 第二版 课后答案第四章Word文档格式.docx
《C程序设计语言 第二版 课后答案第四章Word文档格式.docx》由会员分享,可在线阅读,更多相关《C程序设计语言 第二版 课后答案第四章Word文档格式.docx(34页珍藏版)》请在冰豆网上搜索。
=EOF&
c!
='
\n'
)
s[i++]=c;
if(c=='
s[i]='
\0'
;
returni;
intfind_the_situaion(chart[],chars[])
inti,j,k;
intb=count(s);
intc=count(t);
for(i=0;
s[i]!
i++)
{
for(j=i,k=0;
t[k]!
&
s[j]==t[k];
j++,k++)
;
if(k==b+1)
{
returnc-i-b;
}
}
return-1;
intcount(charv[])
inta=0;
if(v[a]!
='
)
a++;
returna;
Exercise4-2
Extendatoftohandlescientificnotationoftheform123.45e-6whereafloating-pointnumbermaybefollowedbyeorEandanoptionallysignedexponent.
Exercise4-3
Giventhebasicframework,it'
sstraightforwardtoextendthecalculator.Addthemodulus(%)operatorandprovisionsfornegativenumbers.
stdlib.h>
#include"
pch.h"
iostream>
#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]='
if(!
isdigit(c)&
=PERIOD&
-'
returnc;
next=getch();
if(!
isdigit(next)&
next!
=PERIOD)
returnc;
c=next;
else
c=getch();
while(isdigit(s[++i]=c))
if(c==PERIOD)
while(isdigit(s[++i]=c=getch()))
if(c!
ungetch(c);
returnNUMBER;
#defineMAXVAL100
intsp=0;
doubleval[MAXVAL];
doublepop(void)
{if(sp>
returnval[--sp];
else
printf("
error:
stackempty\n"
return0.0;
voidpush(doublef)
{
if(sp<
MAXVAL)
val[sp++]=f;
else
stackfull,can'
tpush%g\n"
f);
#defineBUFSIZE100
charbuf[BUFSIZE];
intbufp=0;
intgetch(void)
return(bufp>
0)?
buf[--bufp]:
getchar();
voidungetch(intc)
if(bufp>
=BUFSIZE)
ungetch:
toomanycharacters\n"
buf[bufp++]=c;
Exercise4-4
Addcommandstoprintthetopelementofthestackwithoutpopping,toduplicateit,andtoswapthetoptwoelements.Addacommandtoclearthestack.
#include<
ctype.h>
math.h>
#defineNUMBER0
#defineTRUE1
#defineFALSE0
intGetop(chars[]);
voidpush(doubleval);
voidshowTop(void);
voidduplicate(void);
voidswapItems(void);
voidclearStack();
caseNUMBER:
push(atof(s));
+'
push(pop()+pop());
*'
push(pop()*pop());
push(pop()-op2);
/'
push(pop()/op2);
\n出现错误!
?
'
showTop();
#'
duplicate();
~'
swapItems();
!
clearStack();
printf("
\n\t%.8g\n"
pop());
default:
不清楚的指令%s.\n"
s);
voidpush(doublef)
MAXVAL)
val[sp++]=f;
\n出现错误%g\n"
doublepop(void)
if(sp>
returnval[--sp];
\n出现错误\n"
return0.0;
voidshowTop(void)
Topofstackcontains:
%8g\n"
val[sp-1]);
Thestackisempty!
\n"
voidduplicate(void)
doubletemp=pop();
push(temp);
voidswapItems(void)
doubleitem1=pop();
doubleitem2=pop();
push(item1);
push(item2);
voidclearStack(void)
sp=0;
voidunGetch(int);
unGetch(c);
voidunGetch(intc)
=BUFSIZE)
buf[bufp++]=c;
Exercise4-5
Addaccesstolibraryfunctionslikesin,exp,andpow.See<
inAppendixB,Section4.
string.h>
#defineIDENTIFIER1
voiddealWithName(chars[]);
caseIDENTIFIER:
dealWithName(s);
divisionbyzero!
unknowncommand%s.\n"
stackfullcan'
voiddealWithName(chars[])
if(0==strcmp(s,"
sin"
))
push(sin(pop()));
elseif(0==strcmp(s,"
cos"
push(cos(pop()));
exp"
push(exp(pop()));
elseif(!
strcmp(s,"
pow"
op2=pop();
push(pow(pop(),op2));
%sisnotasupportedfunction.\n"
if(isalpha(c))
i=0;
while(isalpha(s[i++]=c))
c=getch();
s[i-1]='
if(c!
unGetch(c);
returnIDENTIFIER;
\nUnGetch:
Exercise4-6
Addcommandsforhandlingvariables.(It'
seasytoprovidetwenty-sixvariableswithsingle-letternames.)Addavariableforthemostrecentlyprintedvalue.
/*4-6thesearenewforthisexercise*/
#defineENDSTRING2
/*4-6endofnewstuff*/
#defineMAX_ID_LEN32
#defineMAXVARS30
structvarType{
charname[MAX_ID_LEN];
doubleval;
};
voidclearStacks(structvarTypevar[]);
voiddealWithName(chars[],structvarTypevar[]);
voiddealWithVar(chars[],str