您的位置:首页 > 其它

文章标题

2015-03-20 08:45 267 查看
代码转自 C程序设计语言

#include <stdio.h>
#include <stdlib.h>    /* 为了使用atof()函数 */

#define MAXOP 100    /* 操作数或运算符的最大长度 */
#define NUMBER '0'    /* 标识找到一个数 */

#define MAXLINE 100    /* 最大输入行长度 */

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

/* 逆波兰计算器 */
int reverse_poland_counter()
{
int type;
double op2;
char s[MAXOP];

while ((type = gettop(s)) != EOF) {
switch (type) {
case NUMBER:
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 != 0.0)
push(pop() / op2);
else
printf("error: zero divisor!\n");
break;
case '\n':
printf("\t%.8g\n", pop());
break;
default:
printf("error: unknown command %s \n", s);
break;
}
}
return 0;
}

#define MAXVAL 100    /* 栈val的最大深度 */

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

/* push函数:把f压入到值栈中 */
void push(double f)
{
if (sp < MAXVAL)
val[sp++] = f;
else
printf("error: stack full , can't push %g\n", f);
}

/* pop函数:弹出并返回栈顶的值 */
double pop(void)
{
if (sp > 0)
return val[--sp];
else {
printf("error: stack empty \n");
return 0.0;
}
}

#include <ctype.h>

int getch(void);
void ungetch(int);

/* getop函数:获取下一个运算符或数值操作数 */
int gettop(char s[])
{
int i, c;

while ((s[0] = c = getch()) == ' ' || 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)
ungetch(c);
return NUMBER;
}

#define BUFSIZE 100

char buf[BUFSIZE];    /* 用于ungetch函数的缓冲区 */
int bufp = 0;    /* buf中下一个空闲位置 */

/* 取一个字符(可能是压回的字符) */
int getch(void)
{
return (bufp > 0) ? buf[--bufp] : getchar();
}

/* 把字符压回到输入中 */
void ungetch(int c)
{
if (bufp >= BUFSIZE)
printf("ungetch: too many characters \n");
else
buf[bufp++] = c;
}

int main()
{
reverse_poland_counter();
return 0;
}

/* 逆波兰计算器 ending */

int getline(char line[], int max);
int strindex(char source[], char searchfor[]);
int strrindex_first(char source[], char searchfor[]);
int strrindex_second(char source[], char searchfor[]);

char pattern[] = "ould";    /* 待查找的模式 */

/* 找出所有与模式匹配的行 */
int find_line_match_mode(void)
{
char line[MAXLINE];
int found = 0;
int pos;

while (getline(line, MAXLINE))
if ((pos = strrindex_second(line, pattern)) >= 0) {
printf("%s", line);
printf("the pos is : %d\n\n", pos);
found++;
}
return found;
}

/* strindex函数: 返回t(=pattern)在s中的位置, 若未找到则返回-1 */
int strindex(char s[], char t[])
{
int i, j, k;

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')
return i;
}
return -1;
}

/* strrindex函数: 返回t(=pattern)在s中的位置(最右边出现的位置), 若未找到则返回-1 */
/* strrindex: returuns rightmost index of t in s, -1 if none */
int strrindex_first(char s[], char t[])
{
int i, j, k, pos;

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;
}

#include <string.h>

int strrindex_second(char s[], char t[])
{
int i, j, k;

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

#include <ctype.h>
/* atof函数: 把字符串s转换为相应的双精度浮点数 */
double atof(char s[])
{
double val, power;
int 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;
}
return sign * val / power;
}

//#define MAXLINE 100    /* 最大输入行长度 */

/* getline 函数: 将行保存到s中,并返回该行的长度 */
int getline(char s[], int lim)
{
int c, i;

i = 0;
while (--lim > 0 && (c=getchar()) != EOF && c!= '\n')
s[i++] = c;
if (c == '\n')
s[i++] = c;
s[i] = '\0';
return i;
}

double atof_modified(char s[])
{
double val, power;
int i, sign, exp;

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;    /* 此处已算出除指数外的数值 */

sign = 0;/* 以防e(E)后面出现+/-以外的字符 */
if ((s[i] == 'e' || s[i] == 'E')) {
if(s[i + 1] == '+' || s[i + 1] == '-')
sign = (s[++i] == '-') ? -1 : 1;
for (exp = 0, i++; isdigit(s[i]); i++)
exp = 10* exp + s[i] - '0';
if (sign == 1)
while (exp-- > 0)
val *= 10;
else if (sign == -1)
while (exp-- > 0)
val /= 10;
}
return val;
}

int main_copy()
{
char line[MAXLINE];
double sum;

sum = 0;
while (getline(line, MAXLINE) > 0)
printf("\t%g\n", atof_modified(line));
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: