⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 calculate.c

📁 运用栈的原理设计并实现一位数输入的加、减、乘、除、乘方计算器
💻 C
字号:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

#define MAXOP    100
#define NUMBER   '0'    // signal that a number was found
#define EMPTY    0

int getop(char s[])
{
	int i, c;
	while ((s[0] = c = getchar()) == ' ' || c == '\t')
    ;
	s[1] = '\0';
	if (!isdigit(c) && c != '.')
		return c; 
	
	i = 0;
	if (isdigit(c)) 
		while (isdigit(s[++i] = c = getchar())) ;
	
	if (c == '.') 
		while (isdigit(s[++i] = c = getchar()))  ;
	
	s[i] = '\0';
	if (c != EOF)  ungetc(c, stdin);
	return NUMBER;
}

int push(int s[],int top, int x){
	top++;
	s[top]=x;
	return(top);
}

int pop(int s[],int top,int *x){
	*x=s[top]; 
	top--;  
	return(top); 
}

int cal(int x, int p, int y){
	switch(p){
	case '+': return ((x-48)+(y-48)+48);break;
	case '-': return ((x-48)-(y-48)+48);break;
	case '*': return ((x-48)*(y-48)+48);break;
	case '/': return ((x-48)/(y-48)+48);break;
	}
}

int main()
{
	int c;
	char s[MAXOP];
	int ns[MAXOP];
	int os[MAXOP];
	int ntop,otop,x,y,p;
	int *m;
	int re;
	ntop=-1;
	otop=-1;


	while ((c = getop(s)) != EOF) 
	{
		switch (c) 
		{
			case NUMBER:
				//////////////////////////////////////////////////////////////////////////
				push(ns,ntop,c);
				//////////////////////////////////////////////////////////////////////////
				printf("%d\n", atoi(s));
				break;
			case '+':        // + 和 - 优先级最低,把除了 '(' 以外的全部操作符弹出后入栈
			case '-':
				//////////////////////////////////////////////////////////////////////////
				{while(os[otop]!='('&&otop!=0){
					ntop=pop(ns,ntop,m);x=*m;
					ntop=pop(ns,ntop,m);y=*m;
					otop=pop(os,otop,p);p=*m;
					putchar(x);
					putchar(y);
					putchar(p);
				    re=cal(x,p,y);
					printf("result=%c",re);
					ntop=push(ns,ntop,re);
				}
				otop=push(os,otop,c);
				//////////////////////////////////////////////////////////////////////////
					printf("%c\n", c);}
				break;
			case '*':              // 乘除优先级最高,把相同优先级的乘和除弹出后入栈
			case '/':
				//////////////////////////////////////////////////////////////////////////
				{while(os[otop]=='*'||os[otop]=='/'){
					ntop=pop(ns,ntop,m);x=*m;
					ntop=pop(ns,ntop,m);y=*m;
					otop=pop(os,otop,m);p=*m;
					putchar(x);
					putchar(y);
					putchar(p);
				    re=cal(x,p,y);
					printf("result=%c",re);
					ntop=push(ns,ntop,re);}
				otop=push(os,otop,c);
				//////////////////////////////////////////////////////////////////////////
				printf("%c\n", c);}
				break;
			case '(':        // '(' 不作运算,直接放入optor栈。
				//////////////////////////////////////////////////////////////////////////
				{otop=push(os,otop,c);
				//////////////////////////////////////////////////////////////////////////
				printf("%c\n", c);}
				break;
			case ')':        // 把 '(' 之前的全部操作符弹出,计算后把 '(' 也弹出
				//////////////////////////////////////////////////////////////////////////
				{while(os[otop]!='('){
					ntop=pop(ns,ntop,m);x=*m;
					ntop=pop(ns,ntop,m);y=*m;
					otop=pop(os,otop,m);p=*m;
					putchar(x);
					putchar(y);
					putchar(p);
				    re=cal(x,p,y);
					printf("result=%c",re);
					ntop=push(ns,ntop,re);}
				if(os[otop]=='(')
					otop=pop(ns,otop,m);
				//////////////////////////////////////////////////////////////////////////
				printf("%c\n", c);}
				break;
			case '\n':        // 把余下的操作符全部弹出,计算后输出结果
				//////////////////////////////////////////////////////////////////////////
				{while(otop>=0){
					ntop=pop(ns,ntop,m);x=*m;
					ntop=pop(ns,ntop,m);y=*m;
					otop=pop(os,otop,m);p=*m;
					putchar(x);
					putchar(y);
					putchar(p);
				    re=cal(x,p,y);
					printf("result=%c",re);
					ntop=push(ns,ntop,re);}

				//////////////////////////////////////////////////////////////////////////
				printf("%c\n", c);}
				break;
			default:
				printf("error: unknown command %s\n", s);
				break;
		}
	}
	return 0;
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -