简单计算器的编写

来源:百度知道 编辑:UC知道 时间:2024/05/17 07:02:21
我们现在学的C语言都是编写一段程序...

我想知道,比如就想系统自带的计算器是用什么软件编写的呢?
我们能有什么办法编写一个类似的软件吗?
我的意思是不只是要能计算,而且拥有计算器一样的用户界面?
抄袭的就别拿来了

这是个支持表达式的计算器,代码很短。
可以输入类似(3+3*2)*5+6这样的表达式,自己看吧。
不懂其中的原理来找我,或者自助一下,学点编译原理的知识,一定要懂EBNF,EBNF不知道是什么东西?那就google一下吧。
/*
simple integer arithmetic calculator according to the EBNF
<exp> -> <term>{<addop><term>}
<addop>->+|-
<term>-><factor>{<mulop><factor>}
<mulop> -> *
<factor> -> ( <exp> )| Number
Input a line of text from stdin
Outputs "Error" or the result.
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

char token;/*global token variable*/
/*function prototypes for recursive calls*/
int exp(void);
int term(void);
int factor(void);

void error(void)
{
fprintf(stderr,"Error\n");
exit(1);
}

void match(char expectedToken)
{
if(token==expectedToken)token=getchar();
else error();
}