C程序设计语言 第二版 新版 第四章 自用

4-1
#include<stdio.h>
int main()
{
printf("yes");
return 0;
}
int strrindex(char s[],char t[])
{
int i,j, k,pos=-1;
for(i=0;s[i]!='\0';i++)
{
for(j=i,k=0;t[k]!='\0' && s[j]==t[k];j++,k++)
{
;
}
if(k>0&&t[k]=='\0')
{
pos=i;
}
}
return pos;

}
4-2
#include<stdio.h>
#include<ctype.h>
int main()
{
printf("yes");
return 0;
}
double atof(char s[])
{
double val,power;
int exp,i,sign;
for(i=0;isspace(s[i]);i++)
;
sign= (s[i]=='-')?-1:1;
if(s[i]=='+'||s[i]=='-')
i++;
for(val=0.0;isdigit(s[i]);i++)
val= 10.0*val+(s[i]-'0');
if(s[i]=='.') i++;
for(power=1.0;isdigit(s[i]);i++)
{ val=10.0*val+(s[i]-'0');
power*=10.0;
}
val=sign*val/power;
if(s[i]=='e'||s[i]=='E')
{
sign=(s[++i]=='-')?-1:1;
if(s[i]=='+'||s[i]=='-')
i++;
for(exp = 0;isdigit(s[i]);i++)
{
exp=exp*10+(s[i]-'0');
}
if(sign==1)
{
while(exp-->0)
{val*=10;}

}
if(sign==-1)
{
while(exp-->0)
{val/=10;}

}

}
return val;
}
4-3
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<ctype.h>
#define MAXOP 100
#define NUMBER '0'

int main()
{
int getop(char []);
void push(double);
double pop(void);
double stof(char s[]);
int type;
double op2;
char s[MAXOP];
while((type=getop(s))!=EOF)
{
switch (type){
case NUMBER:
push(stof(s));
break;
case '+':
push(pop()+pop());break;
case '-':
op2=pop();
push(pop()-op2);break;
case '*':
push(pop()*pop());break;
case '/':
op2=pop();
if(op2!=0.0) push(pop()/op2);
else printf("error");
break;
case '%':
op2=pop();
if(op2!=0.0) push(fmod(pop(),op2));
else printf("error");
break;
case '\n':
printf("%.8g",pop());break;
default:
printf("error:");
break;


}

}
return 0;
}
int getop(char s[])
{
int getch();
void ungetch(int);

int c,i;
while((s[0]=c=getch())==' '||c=='\t') ;
s[1]='\0';
i=0;
if(!isdigit(c)&&c!='.' &&c!='-')
{
return c;
}
if(c=='-')
{
if(isdigit(c=getch())||c=='.') s[++i]=c;
else {
if(c!=EOF) ungetch(c);
return '-';
}

}
if(isdigit(c))
{while ( isdigit( s[++i]=c=getch() ) )
;}
if(c=='.')
{while ( isdigit(s[++i]=c=getch()) )
;}
s[i]='\0';
if(c!=EOF)
ungetch(c);
return NUMBER;
}
void push(double s)
{}
double pop(void)
{}
double stof(char s[])
{

}
int getch(){}
void ungetch(int a){}
4-4
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<ctype.h>
#define MAXOP 100
#define NUMBER '0'
int sp=0;
int main()
{
void clear(void);
int getop(char []);
void push(double);
double pop(void);
double stof(char s[]);
int type;
double op2,op1;
char s[MAXOP];
while((type=getop(s))!=EOF)
{
switch (type){
case NUMBER:
push(stof(s));
break;
case '+':
push(pop()+pop());break;
case '-':
op2=pop();
push(pop()-op2);break;
case '*':
push(pop()*pop());break;
case '/':
op2=pop();
if(op2!=0.0) push(pop()/op2);
else printf("error");
break;
case '%':
op2=pop();
if(op2!=0.0) push(fmod(pop(),op2));
else printf("error");
break;
case '?':
op2=pop();
printf(".8g\n",op2);
push(op2);break;
case 'c':
clear();break;
case 'd':
op2=pop();
push(op2);push(op2);break;
case 's':
op1=pop();
op2=pop();
push(op1);
push(op2);break;
case '\n':
printf("%.8g",pop());break;
default:
printf("error:");
break;


}

}
return 0;
}
int getop(char s[])
{
int getch();
void ungetch(int);

int c,i;
while((s[0]=c=getch())==' '||c=='\t') ;
s[1]='\0';
i=0;
if(!isdigit(c)&&c!='.' &&c!='-')
{
return c;
}
if(c=='-')
{
if(isdigit(c=getch())||c=='.') s[++i]=c;
else {
if(c!=EOF) ungetch(c);
return '-';
}

}
if(isdigit(c))
{while ( isdigit( s[++i]=c=getch() ) )
;}
if(c=='.')
{while ( isdigit(s[++i]=c=getch()) )
;}
s[i]='\0';
if(c!=EOF)
ungetch(c);
return NUMBER;
}
void push(double s)
{}
double pop(void)
{}
double stof(char s[])
{

}
int getch(){}
void ungetch(int a){}
void clear(void)
{
sp=0;
}
4-5
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<ctype.h>
#define MAXOP 100
#define NUMBER '0'
#define NAME 'n'
int sp=0;
void clear(void);
int getop(char []);
void push(double);
double pop(void);
double stof(char s[]);
void mathfnc(char s[])
{
double op2;
if(strcmp(s,"sin")==0)
push(sin(pop()));
else if(strcmp(s,"cos")==0)
push(cos(pop()));
else if(strcmp(s,"exp")==0)
push(exp(pop()));
else if(strcmp(s,"pow")==0)
{
op2=pop();
push(pow(pop(),op2));
}
else
printf("error");
}
int main()
{

int type;
double op2,op1;
char s[MAXOP];
while((type=getop(s))!=EOF)
{
switch (type){
case NUMBER:
push(stof(s));
break;
case NAME:
mathfnc(s);break;
case '+':
push(pop()+pop());break;
case '-':
op2=pop();
push(pop()-op2);break;
case '*':
push(pop()*pop());break;
case '/':
op2=pop();
if(op2!=0.0) push(pop()/op2);
else printf("error");
break;
case '%':
op2=pop();
if(op2!=0.0) push(fmod(pop(),op2));
else printf("error");
break;
case '?':
op2=pop();
printf(".8g\n",op2);
push(op2);break;
case 'c':
clear();break;
case 'd':
op2=pop();
push(op2);push(op2);break;
case 's':
op1=pop();
op2=pop();
push(op1);
push(op2);break;
case '\n':
printf("%.8g",pop());break;
default:
printf("error:");
break;


}

}
return 0;
}
int getop(char s[])
{
int getch();
void ungetch(int);

int c,i;
while((s[0]=c=getch())==' '||c=='\t') ;
s[1]='\0';
i=0;
if(islower(c))
{
while(islower(s[++i]=c=getch()))
{
;
}
s[i]='\0';
if(c!=EOF)
ungetch(c);
if(strlen(s)>1)
return NAME;
else return c;

}
if(!isdigit(c)&&c!='.' &&c!='-')
{
return c;
}
if(c=='-')
{
if(isdigit(c=getch())||c=='.') s[++i]=c;
else {
if(c!=EOF) ungetch(c);
return '-';
}

}
if(isdigit(c))
{while ( isdigit( s[++i]=c=getch() ) )
;}
if(c=='.')
{while ( isdigit(s[++i]=c=getch()) )
;}
s[i]='\0';
if(c!=EOF)
ungetch(c);
return NUMBER;
}
void push(double s)
{}
double pop(void)
{}
double stof(char s[])
{

}
int getch(){}
void ungetch(int a){}
void clear(void)
{
sp=0;
}
4-6
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<ctype.h>
#define MAXOP 100
#define NUMBER '0'
#define NAME 'n'
int sp=0;
void clear(void);
int getop(char []);
void push(double);
double pop(void);
double stof(char s[]);
void mathfnc(char s[])
{
double op2;
if(strcmp(s,"sin")==0)
push(sin(pop()));
else if(strcmp(s,"cos")==0)
push(cos(pop()));
else if(strcmp(s,"exp")==0)
push(exp(pop()));
else if(strcmp(s,"pow")==0)
{
op2=pop();
push(pow(pop(),op2));
}
else
printf("error");
}
int main()
{

int type;int var=0;
double v;
double op2,op1;
char s[MAXOP];
double variable[26];
int i;
for(i=0;i<26;i++)
variable[i]=0.0;
while((type=getop(s))!=EOF)
{
switch (type){
case NUMBER:
push(stof(s));
break;
case NAME:
mathfnc(s);break;
case '+':
push(pop()+pop());break;
case '-':
op2=pop();
push(pop()-op2);break;
case '*':
push(pop()*pop());break;
case '/':
op2=pop();
if(op2!=0.0) push(pop()/op2);
else printf("error");
break;
case '=':
pop();
if(var>='A'&&var<='Z')
variable[var-'A']=pop();
else
printf("error");
break;
case '%':
op2=pop();
if(op2!=0.0) push(fmod(pop(),op2));
else printf("error");
break;
case '?':
op2=pop();
printf(".8g\n",op2);
push(op2);break;
case 'c':
clear();break;
case 'd':
op2=pop();
push(op2);push(op2);break;
case 's':
op1=pop();
op2=pop();
push(op1);
push(op2);break;
case '\n':
v=pop();
printf("%.8g",v);break;
default:
if(type>='A'&&type<='Z')
push(variable[type-'A']);
else if(type=='v')
push(v);
else
printf("error:");

break;


}
var=type;
}
return 0;
}
int getop(char s[])
{
int getch();
void ungetch(int);

int c,i;
while((s[0]=c=getch())==' '||c=='\t') ;
s[1]='\0';
i=0;
if(islower(c))
{
while(islower(s[++i]=c=getch()))
{
;
}
s[i]='\0';
if(c!=EOF)
ungetch(c);
if(strlen(s)>1)
return NAME;
else return c;

}
if(!isdigit(c)&&c!='.' &&c!='-')
{
return c;
}
if(c=='-')
{
if(isdigit(c=getch())||c=='.') s[++i]=c;
else {
if(c!=EOF) ungetch(c);
return '-';
}

}
if(isdigit(c))
{while ( isdigit( s[++i]=c=getch() ) )
;}
if(c=='.')
{while ( isdigit(s[++i]=c=getch()) )
;}
s[i]='\0';
if(c!=EOF)
ungetch(c);
return NUMBER;
}
void push(double s)
{}
double pop(void)
{}
double stof(char s[])
{

}
int getch(){}
void ungetch(int a){}
void clear(void)
{
sp=0;
}
4-7
#include<string.h>
void ungets(char s[])
{
int len =strlen(s);
void ungetch(int);
while(len>0)
ungetch(s[--len]);
}
4-8
#include<stdio.h>
char buf=0;
int getch(void)
{
int c;
if(buf!=0)
c=buf;
else c=getchar();
return c;
}
void ungetch()
{
if(buf!=0)
printf("too");
else
buf = c;
}
4-9
#include<stdio.h>
#define BUFSIZE 100
int buf[BUFSIZE];
int bufp=0;
int getch(void)
{
return (bufp>0)? buf[--bufp]:getchar();
}
void ungetch(int c)
{
if(bufp>=BUFSIZE)
printf("too many");
else
buf[bufp++]=c;
}
4-10
#include<stdio.h>
#include<ctype.h>
#define MAXLINE 100
#define NUMBER '0'
int getline(char line[],int limit);

int li=0;
char line[MAXLINE];
int getop(char s[])
{
int c,i;
if(line[li]=='\0')
if(getline(line,MAXLINE)==0)
return EOF;
else
li=0;
while((s[0]=c=line[li++])==' '|| c=='\t') ;
s[1]='\0';
if(!isdigit(c) &&c!='.') return c;
i=0;
if(isdigit(c))
while(isdigit(s[++i]=c=line[li++]))
;
if(c=='.')
while(isdigit(s[++i]=c=line[li++]))
;
s[i]='\0';
li--;
return NUMBER;
}
4-11
#include<stdio.h>
#include<ctype.h>
#define MAXLINE 100
#define NUMBER '0'
int getch();
int getop(char s[])
{
int c,i;
static int lastc=0;
if(lastc==0)
c=getch();
else
{
c=lastc;
lastc=0;
}
while((s[0]=c)==' '|| c=='\t') ;
s[1]='\0';
if(!isdigit(c) &&c!='.') return c;
i=0;
if(isdigit(c))
while(isdigit(s[++i]=c=getch()))
;
if(c=='.')
while(isdigit(s[++i]=c=getch()))
;
s[i]='\0';
if(c!=EOF)
lastc=c;
return NUMBER;
}
4-12
#include<math.h>
void itoa(int n,char s[])
{
static int i;
if(n/10)
itoa(n/10,s);
else
{
i=0;
if(n<0)
s[i++]='-';
}
s[i++]=abs(n)%10 +'0';
s[i]='\0';
}
4-13
#include<string.h>
void reverse(char s[])
{
void reverser(char s[],int i,int len);
reverser(s,0,strlen(s));
}
void reverser(char s[],int i,int len)
{
int c,j;
j=len-(i+1);
if(i<j)
{
c=s[i];s[i]=s[j];s[j]=c;
reverser(s,++i,len);
}
}
4-14
#define swap(t,x,y) {t _z;_z=y; y=x;x=_z; }

上一篇:Python函数


下一篇:mini dc(选做)