#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#define N 100
double numStack[N]={0};//操作数栈
int numTop;
char opStack[N];//运算符栈
int opTop;
void print_num(double str1[],int n)
{
int i;
printf("\n操作数栈:\n");
for(i=0;i<n;i++)
printf("%g ",str1[i]);
}
void print_op(char str2[],int m)
{
int j;
printf("\n运算符栈:\n");
for(j=0;j<m;j++)
printf("%c ",str2[j]);
}
int op(char ch)//判断运算符优先级
{
if(ch=='+'||ch=='-') return 2;
if(ch=='*'||ch=='/') return 3;
if(ch=='(') return -1;
return 0;
}
double result(double num1,char op,double num2)//计算
{
if(op=='+') return num1+num2;
if(op=='-') return num1-num2;
if(op=='*') return num1*num2;
if(op=='/') return num1/num2;
return 0;
}
int compute(char str[])
{
double num=0;
int i=0,j=1,k=1;
numTop=opTop=0;
while(str[i]!='\0'||opTop>0)
{
if(str[i]>='0'&&str[i]<='9')
num=num*10+str[i]-'0';
else if( k==1&&str[i]=='-'&&(i==0||op(str[i-1])) )
k=-1;
else
{
if(i>0&&!op(str[i-1])&&str[i]!='('&&str[i-1]!=')')
{
numStack[numTop++]=num*k;
if(opTop!=0&&numTop!=0)
print_num(numStack,numTop);
num=0; j=1; k=1;
}
if(opTop==0||str[i]=='(')
{opStack[opTop++]=str[i];print_op(opStack,opTop);}
&nb

这是一个用C语言编写的计算器程序,它实现了基本的数学运算,包括加、减、乘、除。程序通过操作数栈和运算符栈来处理运算优先级,并能够处理括号内的表达式计算。用户可以通过菜单选项进行输入、计算或退出操作。
5万+

被折叠的 条评论
为什么被折叠?



