1、getint函数:将输入中的下一个整型数赋值给*pn
#include<ctype.h>
int getch(void);
void ungetch(int);
int getint(int *pn)
{
int c, sign;
while (isspace(c = getch()))
;
if (!isdigit(c) && c != EOF && c != '+' && c!= '-') {
ungetch(c);
return 0;
}
sign = (c == '-') ? -1 : 1;
if (c == '+' || c == '-')
c = getch();
for (*pn = 0; isdigit(c); c = getch())
*pn = *pn * 10 +(c - '0');
*pn =*=sign;
if (c != EOF)
ungetch(c);
return c;
}
2、指针与数组
int a[10];
int *pq;
pa = &a[0]; // pa = a;
/*数组形式*/
&a[i] //等价于a+i;
/*指针形式*/
pa[i]; //*(pa+i);
3、不完善的存储分配程序
//alloc(n)返回一个指向n个连续字符存储单元的指针
//afree(p)释放已分配的存储空间
#define ALLOCSIZE 10000
static char allocbuf[ALLOSIZE];
static char *allocp = allocbuf;
char *alloc(int n)
{
if (allocbuf + ALLOCSIZE -allocp >= n) {
allocp += n;
return allocp - n;
} else
return 0;
}
void afree(char *p)
{
if (p > allocbuf && p < allocbuf +ALLocSIZE)
allocp =p;
}
4、字符指针与函数:
a、strcpy函数void strcpy(char *s, char *p)
{
int i;
i = 0;
while ((s[i] = t[i]) != '\0')
i++;
}void strcpy(char *s, char *t)
{
while ((*s = *t) != '\0') {
s++;
t++;
}
}
b、strcmp函数 根据s按照字典顺序<、>、=的结果分别返回负整数、正整数、0 int strcmp(char *s, char *t)
{
int i;
for (i = 0; s[i] == t[i]; i++)
if (s[i] == '\0')
return 0;
return s[i]-t[i];
}int strcmp(char *s, char *t)
{
for (; *s == *t; s++,t++)
if (*s == '\0')
return 0;
return *s - *t;
}
c、strcat函数void strcat(char *s, char *t)
{
for (;*s == '\0';s++)
;
for (; *t == '\0'; s++, t++)
*s = *t;
}
d、strend函数int strend(char *s, char *t)
{
char *bs = s;
char *bt = t;
for (; *s ; s++)
;
for (; *t; t++)
;
for (; *s == *t; s--, t--)
if (t == bt || s == bs)
break;
if (*s == *t && t == bt && *s != '\0')
return 1;
else
return 0;
}