您的位置:首页 > 编程语言

第四章代码

2014-01-16 15:36 232 查看
练习4-1 (60页)

int strrindex(char s[], char t[])
{
int i, j, k;
for (i = strlen(s) - strlen(t); i >= 0; i--)
{
for (j = i, k = 0; t[k] = s[j] && t[k] != '\0'; j++, k++)
;
if (k > 0 && t[k] != '\0')
return i;
}
return -1;
}

练习4-2(62页)

#include <stdio.h>
#include <ctype.h>

double atof(char[]);

int main()
{
double b = atof("1.2e+2");
printf("%f\n", b);
return 0;
}

double atof(char s[])
{
double val, power;
int sign, k = 0, j, i= 0;
sign = (s[i] == '-') ? -1 : 1;
if (s[i]== '-' || s[i] == '+')
i++;
for(val = 0.0; isdigit(s[i]); i++) {
val = val * 10 + s[i] - '0';
}
if (s[i] == '.')
i++;
for(power = 1.0; isdigit(s[i]); i++) {
val = val * 10 + s[i] - '0';
power *= 10;
}
if (s[i] == 'e' || s[i] == 'E') {
i++;
if (s[i] == '-') {
i++;
for(; isdigit(s[i]); i++) {
k = k * 10 + s[i] - '0';
}
for (j = 0; j < k; j++) {
power *= 10;
}
} else if (s[i] == '+') {
i++;
for (; isdigit(s[i]); i++) {
k = k * 10 + s[i] - '0';
}
for (j = 0; j < k; j++){
power /= 10;
}
} else {
for (; isdigit(s[i]); i++) {
k = k * 10 + s[i] - '0';
}
for (j = 0; j < k; j++){
power /= 10;
}
}
}
return sign * val / power;
}

习题4-3、4-4、4-5(还是有一些细节问题要考虑)

代码155行左右,使用getchar()函数而没有使用程序本身实现的函数getch()。

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <string.h>

#define MAXLEN  100 /* 读取字符串s的最大长度 */
#define NUMBER '0'  /* 标识找到的是数字      */
#define FUNCTION 'f'/* 标识找到的是函数      */
#define MAXVAL  100 /* 栈的深度              */
#define BUFSIZ 100

int sp = 0;         /* 栈中下一个空闲位置 */
double val[MAXVAL];

int getop(char []);
void push(double);
double pop(void);

void clear_stack(void);  /* 清空栈 */
void print_op(void);/* 打印栈顶元素 */
void copy_op(void); /* 复制栈顶元素 */
void swap_op(void); /* 交换栈顶两个元素 */
void mathfunction(char[]);
void ungetch(int);
int getch(void);

int main()
{
int tpye;
char s[MAXLEN];
double op2;
while ((tpye = getop(s)) != EOF) {  /* 处理各种类型数据 */
switch (tpye) {
case FUNCTION:
mathfunction(s);
break;
case NUMBER:
push(atof(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)
printf("error: 除数为0\n");
else
push(pop() / op2);
break;
case '\n':
printf("\t%.8g\n", pop());
break;
case '%':
op2 = pop();
if (op2 != 0.0)
push(fmod(pop(), op2));
else
printf("error: 0不能做除数\n");
case 'c':       /* 清空栈 */
clear_stack();
break;
case 'p':       /* 复制栈顶元素 */
copy_op();
break;
case '?':       /* 打印栈顶元素 */
print_op();
break;
case ']':       /* 交换栈顶的两个元素 */
swap_op();
break;
default:
printf("error: 不能处理\n");
break;
}
}
return 0;
}

/* 判断函数类型 */
void mathfunction(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: function %s is not find\n", s);

}

void push(double f) /* 入栈 */
{
if (sp < MAXVAL) {
val[sp++] = f;
}

else
printf("error: stack full, can't push %g\n", f);
}

double pop()        /* 出栈 */
{
if (sp > 0)
return val[--sp];
else {
printf("error: stack empty, can't pop");
return 0.0;
}
}

void print_op()     /* 打印栈顶元素 */
{
int i = sp - 1;
printf("\t%.g\n", val[i]);
}

void copy_op()    /* 复制栈顶元素 */
{
int i = sp - 1;
push(val[i]);
}

void swap_op()      /* 交换栈顶两个元素 */
{
int i = sp -1;
double f = val[i];
val[i] = val[i - 1];
val[i - 1] = f;
}

void clear_stack()  /* 清空栈 */
{
sp = 0;
}

int getop(char s[])    /* 获取一个输入,将数值以字符串形式存到数组s中 */
{
int c, i= 0;
while ((c = getchar()) == ' ' || c == '\t')  /* 跳过空格和制表符 */
;
if (islower(c)) {   /* 如果是小写字母,以函数名存入字符串s中 */
s[i] = c;
while (islower(s[++i] = c = getchar()))
;
s[i] = '\0';
if (c != EOF)
ungetch(c);
if (strlen(s) > 1)
return FUNCTION;
else
return c;
}

if ( c != '.' && !isdigit(c) && c != '-')
return c;

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

if (isdigit(c)) {   /* 遇到数字,整数部分 */
for (; isdigit(c); i++) {
s[i] = c;
c = getchar();
}
}
if (c == '.') {     /* 遇到小数点,然后处理小数部分*/
s[i++] = '.';
if (isdigit((c = getchar())))
for (; isdigit(c); i++) {
s[i] = c;
c = getchar();
}
}
s[i] = '\0';
if (c != EOF)
ungetch(c);   /* 放到输入缓冲中 */
return NUMBER;
}

char buf[BUFSIZ];    /* 输入缓冲区大小 */
int bufp = 0;        /* 输入缓冲区指针 */
int getch(void)          /* 获取输入       */
{
return (bufp > 0) ? buf[--bufp] : getchar();
}

void ungetch(int c)  /* 将字符放到缓冲 */
{
if (bufp < BUFSIZ)
buf[bufp++] = c;
else
printf("ungetch: too many characters\n");
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: