设为首页
收藏本站
用户名
Email
自动登录
找回密码
密码
登录
注册
快捷导航
平台
Portal
论坛
BBS
文库
项目
群组
Group
我的博客
Space
搜索
搜索
热搜:
shell
linux
mysql
本版
用户
LinuxSir.cn,穿越时空的Linuxsir!
»
论坛
›
编程开发讨论区 —— LinuxSir.cn
›
Linux 程序设计专题讨论
›
只匹配数字的程序?
返回列表
查看:
669
|
回复:
1
只匹配数字的程序?
[复制链接]
777
777
当前离线
积分
990
IP卡
狗仔卡
发表于 2004-3-12 20:59:01
|
显示全部楼层
|
阅读模式
/* this program forms the basis on which exercises 4-3 through 4-10 build */
#include <stdio.h>
#include <stdlib.h> /* for atof() - in K&R, math.h is referenced - this is an anachronism */
#define MAXOP 100 /* max size of operand or operator */
#define NUMBER '0' /* signal that a number was found */
int getop(char []);
void push(double);
double pop(void);
/* reverse Polish calculator */
int main(void)
{
int type;
double op2;
char s[MAXOP];
while((type = getop(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 /* maximum depth of val stack */
int sp = 0; /* next free stack position */
double val[MAXVAL]; /* value stack */
/* push: push f onto value stack */
void push(double f)
{
if(sp < MAXVAL)
val[sp++] = f;
else
printf("error: stack full, can't push %g\n", f);
}
/* pop: pop and return top value from stack */
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: get next operator or numeric operand */
int getop(char s[])
{
int i, c;
while((s[0] = c = getch()) == ' ' || c == '\t')
;
s[1] = '\0';
if(!isdigit(c) && c != '.')
return c; /* not a number */
i = 0;
if(isdigit(c)) /* collect integer part */
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]; /* buffer for ungetch */
int bufp = 0; /* next free position in buf */
int getch(void) /* get a (possibly pushed back) character */
{
return (bufp > 0) ? buf[--bufp] : getchar();
}
void ungetch(int c) /* push character back on input */
{
if(bufp >= BUFSIZE)
printf("ungetch: too many characters\n");
else
buf[bufp++] = c;
}
复制代码
不知道这个程序是干嘛的?
回复
使用道具
举报
提升卡
置顶卡
沉默卡
喧嚣卡
变色卡
显身卡
kj501
kj501
当前离线
积分
5317
IP卡
狗仔卡
发表于 2004-3-12 21:41:09
|
显示全部楼层
如果学过编译原理就清楚啦。这就是利用逆波兰表达式实现计算器。如果表达式为2+3*5,转化成逆波兰表达式输入就是3 5 * 2 +,再利用堆栈,先压入3和5,碰到*时弹出3和5计算乘积得15并压入堆栈得15,再压入2,碰到+时再弹出15和2,得出其和为17。
回复
支持
反对
使用道具
举报
显身卡
返回列表
高级模式
B
Color
Image
Link
Quote
Code
Smilies
您需要登录后才可以回帖
登录
|
注册
本版积分规则
发表回复
回帖后跳转到最后一页
浏览过的版块
LFS(LinuxfromScratch)
硬件设备 Linux 驱动
Redhat/Fedora/CentOS Linux
Copyright © 2002-2023
LinuxSir.cn
(http://www.linuxsir.cn/) 版权所有 All Rights Reserved.
Powered by
RedflagLinux!
技术支持:
中科红旗
|
京ICP备19024520号
快速回复
返回顶部
返回列表