求一个迷你计算器的C语言程序(需要注释),拜托在线的好友们

迷你计算器
功能要求:
(1)计算任意一个算术表达式的加法、减法、除法以及乘法。
(2)运算数为整数和小数
(3)人机界面自定义,要友好。如用户输入错误在计算之前可以修改。
较高要求:实现图形化操作界面,扩充运算符集合

/*** 一共包含四个文件 *************
| Symbol.h
| Stack.h
| Expression.h
| Expression.c
**********************************/
// --Expression.c
#include "Expression.h"

extern EXPRESSION_DEBUG;

STATUS EvaluateExpression(float*,char*);
void Usage(char *);
int HandleOptions(int,char **);

int main(int argc,char*argv[ ])
{
char strLine[30]={0};
float fResult=0;

/* handle the program options */
HandleOptions(argc,argv);
fprintf(stderr,"Input cls to clear the screen\n");
fprintf(stderr," debug to show stack change\n");
fprintf(stderr," nodebug to show no stack change\n");
fprintf(stderr," end to exit\n");
while(TRUE)
{
printf("Input:\n");
gets(strLine);
if(!strcmp(strLine,"end"))
break;
if(!strcmp(strLine,"cls")) {
system("cls");
continue;
}
if(!strcmp(strLine,"debug")){
EXPRESSION_DEBUG=TRUE;
continue;
}
if(!strcmp(strLine,"nodebug")){
EXPRESSION_DEBUG=FALSE;
continue;
}
EvaluateExpression(&fResult,strLine);
printf("Ans=%f\n",fResult);
}
return OK;

}

void Usage(char *programName)
{
fprintf(stderr,"%s usage:%s [-d][-h/?]\n",programName,programName);
fprintf(stderr,"-d Test program, calculate expression and\n");
fprintf(stderr," see changes in the stack at the same time.\n");
exit(OK);
}

/* returns the index of the first argument that is not an option; i.e.
does not start with a dash or a slash
*/
int HandleOptions(int argc,char *argv[])
{
int i,firstnonoption=0;

for (i=1; i< argc;i++) {
if (argv[i][0] == '/' || argv[i][0] == '-') {
switch (argv[i][1]) {
/* An argument -? means help is requested */
case '?':
case 'h':
case 'H':
Usage(argv[0]);
break;
case 'd':
case 'D':
EXPRESSION_DEBUG=TRUE;
break;
default:
fprintf(stderr,"unknown option %s\n",argv[i]);
break;
}
}
else {
firstnonoption = i;
break;
}
}
return firstnonoption;
}

//--Symbol.h
//--symbol.h - definitions/declarations for symbols used by other C Header files

#ifndef SYMBOL_H
#define SYMBOL_H

#define STACK_H
#define TRUE 1
#define OK 1
#define YES 1
#define FALSE 0
#define ERROR 0
#define NO 0
#define OVERFLOW -1
#ifndef NULL
#define NULL 0
#endif

typedef unsigned int UINT;
typedef int STATUS;
typedef int BOOL;

#endif /*SYMBOL_H*/

//--stack.h - definitions/declarations for stack operation

#ifndef STACK_H
#define STACK_H

#include "Symbol.h"
#include <malloc.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>

#define S_CHAR 1
#define S_SHORT 2
#define S_INT 3
#define S_FLOAT 4
#define S_DOUBLE 5

//-- Stack.h
typedef struct tagNode
{
void*pData;
struct tagNode*pNext;
}Node,*PNode;
typedef struct tagStack
{
UINT uType;
/*1 char
*2 short/short int
*3 int
*4 float
*5 double
*/
UINT uLength;
struct tagNode*pFirst;
struct tagNode*pTop;
}Stack,*PStack;

STATUS InitStack(PStack pStack,UINT uType)
{
pStack->uType=uType;
pStack->uLength=0;
pStack->pFirst=pStack->pTop=NULL;
return OK;
}

STATUS ShowStack(PStack pStack)
{
PNode pNode=pStack->pFirst;
while(pNode)
{
switch(pStack->uType)
{
case S_CHAR: // char
printf("%c ",*(char*)(pNode->pData));break;
case S_FLOAT: // float
printf("%-4.1f ",*(float*)(pNode->pData));
}
pNode=pNode->pNext;
}
putchar(10);
return OK;
}

STATUS Push(PStack pStack,void*pData)
{
PNode pNode=(PNode)malloc(sizeof(Node));
if(!pNode)
{
printf("\nmalloc error!\n");
fflush(stdin);
getch();
exit(ERROR);
}
if(pStack->uType==1)
{
pNode->pData=(char*)malloc(sizeof(char));
*(char*)(pNode->pData)=*(char*)pData;
}
else if(pStack->uType==3)
{
pNode->pData=(int*)malloc(sizeof(int));
pNode->pData=(int*)malloc(sizeof(int));
*(int*)(pNode->pData)=*(int*)pData;
}
else if(pStack->uType==4)
{
pNode->pData=(float*)malloc(sizeof(float));
pNode->pData=(float*)malloc(sizeof(float));
*(float*)(pNode->pData)=*(float*)pData;
}
else if(pStack->uType==5)
{
pNode->pData=(double*)malloc(sizeof(double));
pNode->pData=(double*)malloc(sizeof(double));
*(double*)(pNode->pData)=*(double*)pData;
}
pNode->pNext=NULL;
if(!pStack->pTop)
pStack->pTop=pStack->pFirst=pNode;
else
{
pStack->pTop->pNext=pNode;
pStack->pTop=pNode;
}
pStack->uLength++;
return OK;
}

STATUS Pop(PStack pStack,void*pData)
{
PNode pPre=pStack->pFirst;
if(pStack->pTop!=pStack->pFirst)
while(pPre->pNext!=pStack->pTop)
pPre=pPre->pNext;
else
pPre=NULL;
if(pStack->uType==1)
*(char*)(pData)=*(char*)(pStack->pTop->pData);
else if(pStack->uType==3)
*(int*)(pData)=*(int*)(pStack->pTop->pData);
else if(pStack->uType==4)
*(float*)(pData)=*(float*)(pStack->pTop->pData);
else if(pStack->uType==5)
*(double*)(pData)=*(double*)(pStack->pTop->pData);
free(pStack->pTop->pData);
free(pStack->pTop);
pStack->pTop=pPre;
if(pPre)
pStack->pTop->pNext=NULL;
else
pStack->pFirst=NULL;
pStack->uLength--;
return OK;
}

STATUS GetTop(PStack pStack,void*pData)
{
if(pStack->uType==1)
*(char*)(pData)=*(char*)(pStack->pTop->pData);
else if(pStack->uType==3)
*(int*)(pData)=*(int*)(pStack->pTop->pData);
else if(pStack->uType==4)
*(float*)(pData)=*(float*)(pStack->pTop->pData);
else if(pStack->uType==5)
*(double*)(pData)=*(double*)(pStack->pTop->pData);
return OK;
}
STATUS DestroyStack(PStack pStack)
{
PNode pPre1,pPre2;
pPre1=pPre2=pStack->pFirst;
while(pPre1)
{
pPre1=pPre1->pNext;
free(pPre2->pData);
free(pPre2);
pPre2=pPre1;
}
pStack->pFirst=pStack->pTop=NULL;
pStack->uLength=0;
return OK;
}

#endif /* STACK_H */

//--Expresson.h

#ifndef EXPRESSION_H
#define EXPRESSION_H

#include "Stack.h"

typedef struct tagOptr
{
char cOptr;
UINT uPriority;
}Optr,*POptr;

BOOL EXPRESSION_DEBUG=FALSE;
Optr pOptr[8]={{0,7},{')',1},{'*',2},{'/',2},{'+',3},{'-',3},{'(',4},{'#',4}};
STATUS Operate(float*fTemp3,float fTemp1,char theta,float fTemp2)
{
switch(theta)
{
case '+':*fTemp3=fTemp1+fTemp2;break;
case '-':*fTemp3=fTemp1-fTemp2;break;
case '*':*fTemp3=fTemp1*fTemp2;break;
case '/':
if(fTemp2!=0)
*fTemp3=fTemp1/fTemp2;
else
{
printf("\n0 can not be divisor!\n\nPress any key to continue...\n");
fflush(stdin);
getch();
exit(ERROR);
}// else
break;
}
return OK;
}

int Precede(char cOptrTop,char cChar)
{
UINT i,j;
if(cOptrTop=='#'&&cChar=='#')
return 0;
if(cChar=='(')
return -1;
if(cChar==')')
if(cOptrTop=='(')
return 0;
else
return 1;
for(i=1;i<=pOptr[0].uPriority;i++)
if(pOptr[i].cOptr==cOptrTop)
{
i=pOptr[i].uPriority;
break;
}
for(j=1;j<=pOptr[0].uPriority;j++)
if(pOptr[j].cOptr==cChar)
{
j=pOptr[j].uPriority;
break;
}
if(i<=j)
return 1;
else
return -1;
return -2;
}

STATUS IsIn(char cChar)
{
if(cChar>='0'&&cChar<='9'||cChar=='.')
return YES;
return NO;
}

STATUS Debug(PStack stackOptr,PStack stackOpnd,char*strExpression,int i)
{
// --debug
if(EXPRESSION_DEBUG)
{
printf("-------------------------------\n");
printf("%s\n",strExpression);
printf("Optr:");
ShowStack(stackOptr);
printf("Opnd:");
ShowStack(stackOpnd);
}
return OK;
}
STATUS EvaluateExpression(float*fResult,char*strExpression)
{
char cChar='#',cOptrTop=0,theta=0;
float fTemp1,fTemp2,fTemp3,fTemp4;
int i=0,iTemp;
Stack stackOptr,stackOpnd;
InitStack(&stackOptr,S_CHAR);
InitStack(&stackOpnd,S_FLOAT);
Push(&stackOptr,&cChar);
GetTop(&stackOptr,&cOptrTop);
strcat(strExpression,"#");
if(strExpression[0]=='-'){
fTemp1=0;
Push(&stackOpnd,&fTemp1);
}
cChar=strExpression[0];
while(cChar!='#'||cOptrTop!='#')
{
Debug(&stackOptr,&stackOpnd,strExpression,i);
if(IsIn(cChar))
{
fTemp1=0;
fTemp3=10;
fTemp4=1;
while(IsIn(cChar))
{
if(cChar=='.'){
fTemp3=1;
cChar=strExpression[++i];
continue;
}
fTemp2=(float)(cChar-'0');
if(fTemp3==1)
{
iTemp=i;
while(IsIn(strExpression[i])){
fTemp2/=10;
i++;
}
i=iTemp;
}
fTemp1=fTemp1*fTemp3+fTemp2;
cChar=strExpression[++i];
}
Push(&stackOpnd,&fTemp1);
}
else
{
switch(Precede(cOptrTop,cChar))
{
case -1:Push(&stackOptr,&cChar);cChar=strExpression[++i];break;
case 0:Pop(&stackOptr,&cChar);cChar=strExpression[++i];break;
case 1:
Pop(&stackOptr,&theta);
Pop(&stackOpnd,&fTemp2);Pop(&stackOpnd,&fTemp1);
Operate(&fTemp3,fTemp1,theta,fTemp2);
Push(&stackOpnd,&fTemp3);
break;
}// switch
GetTop(&stackOptr,&cOptrTop);
}// else
}// while
Pop(&stackOptr,&cChar);
Debug(&stackOptr,&stackOpnd,strExpression,i);
GetTop(&stackOpnd,fResult);
DestroyStack(&stackOptr);
DestroyStack(&stackOpnd);
return OK;
}// EvaluateExpression

#endif // EXPRESSION_H
温馨提示:内容为网友见解,仅供参考
无其他回答

用c语言编一个简单的能计算加减乘除的小计算器,要是能把思路附上最好...
printf("\\b%c",ch);count=1;num1=ch-'0';while(ch=getch(),ch>='0' && ch<='9'){ putchar(ch);num1=num1*10+ch-'0';count++;} while(1){ op=ch;\/\/符号 ch=getch();while(ch<'0' || ch>'9')\/\/输入的不是数字,重新输入 ch=getch();for(i=0;i<count;i++)\/\/...

如何用c语言编写一个简易计算器??
for(i=0;i<26;i++)printf("%c ",i+'a');return 0;}

一个计算器的C语言代码我不知道什么意思求每一句代码的注释
main(void){\/\/主函数,返回值是int整型,参数为空 int choice;\/\/选择 double a,result;\/\/a用来存放输入的值,result存放计算结果 while (1){\/\/一个死循环,为了让程序可以进行多次运算 result = 0.0;\/\/存放结果变量的初始化 printf("(1.加 2.减 3.乘 4.除 5.退出)\\n输入你的选择:");...

用c语言程序设计一个简单计算器,求其源代码
char str1[] = "1230.456+-789*\/知消扒Qc=^%";char cnum[5], str2[20] = "", c;int x, y, x0, y0, i, j, v, m, n, act, flag = 1;\/* Function prototypes *\/ void drawboder(void);void initialize(void);void computer(void);void changetextstyle(int font, int di...

C语言简单计算器,支持加减乘除乘方运算,每步要有注释,求助C语言高手解决...
private char lastOperator; \/\/ 表示上一运算符 private JTextField operation; \/\/ 结果栏 private JButton dot, plus, minus, multi, div, sqrt, equal, changePN, clear; \/\/ 运算符 private JButton[] numbers; \/\/ 数字 \/\/ 构造者 public Calculator3() { setTitle("Calculator");\/\/ 初始...

用C语言编一个简单的计算器小程序
int add(int x,int y) {return x+y;} int sub(int x,int y) {return x-y;} int mul(int x,int y) {return x*y;} int div(int x,int y) {return x\/y;} int (*func[])()={add,sub,mul,div};int num,curch;char chtbl[]="+-*\/()=";char corch[]="+-*\/...

用c语言编写一个简单计算器程序
#include<stdio.h>//计算器 voidmenu()//自定义的菜单界面 { printf("---\n");printf("请输入你的选择\n");printf("1.+\n");printf("2.-\n");printf("3.*\n");printf("4./\n");printf("---\n");} intmain(){ int...

如何用C语言实现一个计算器?
1、首先打开DEV C++软件,点击“新建源代码”,在编辑页面输入以下代码。2、因为题目要求我们先输入一个整数,所以在定义变量时,就应该将其定义为整数型,注意,在输入,输出函数中,整数型对应的是“%d”。3、接下来就要对输入的整数进行判断,在C语言中,if是判断语句,所以用它来对整数进行判断。if...

用C语言编写一个简单的计算器1
case '+':ret = num1 + num2;break;case '-':ret = num1 - num2;break;case '*':ret = num1 * num2;break;case '\/':ret = num1 \/ num2;break;default:break;} printf( "结果:%.2lf\\n", ret ); return 0;} 写个简单易懂的,你操作计算器的步骤就是编写程序的思路呀 ...

用c语言设计一个简单的加减乘除计算器
1、打开visual C++ 6.0-文件-新建-文件-C++ Source File。2、输入预处理命令和主函数:#include \/*函数头:输入输出头文件*\/,void main()\/*空类型:主函数*\/。3、定义变量:int a,b,d; \/*定义变量的数据类型为整型*\/,char c;\/*定义变量的数据类型为字符型*\/。4、输入四则运算式:...

相似回答