c语言逆波兰计算器程序,逆波兰式计算器

#include

#include

#include

#include

#include

#define  MAX     100                                           /*stac的容量*/

#define  NUM     '0'                                           /*数字类型标识符*/

#define  SMAX     200                                          /*缓冲池的容量*/

#define  AGAIN    -2                                           /*接着再接受输入的标识符*/

#define  SIN      -3                                           /*sin函数的标识符*/

#define  COS      -4                                           /*cos函数的标识符*/

#define  EXP      -5                                           /*exp函数的标识符*/

#define  POW      -6                                           /*pow函数的标识符*/

double pop(void);

void   push(double);

int    getst();

int    get(void);

void   unget(int);

double stac[MAX];

char   suf[SMAX];

char   string[SMAX];

char   s[MAX];

int    point,poi;

void main()

{

int type;

double  mid,midans;

point=poi=0;

printf("Welcome to my calculator!\n ");

printf("Illustration:\n");

printf("Inverse Poland type calculator:\n");

printf("This calculator includes the 'sin','cos','exp','pow':");

printf("Please input in the right order!\n");

printf("input 'flash'meaning flash the stack:\n");

while((type=getst())!=EOF)                                /*循环判断输入的情况*/

{           switch(type)

{

case     SIN:                          /*求正弦*/

midans=sin(pop());

push(midans);          /*回收结果*/

printf("%f\n",midans);

getch();

break;

case    COS:

midans=cos(pop());

push(midans);

printf("%f\n",midans);

getch();

break;

case     EXP:

midans=exp(pop());

push(midans);

printf("%f\n",midans);

getch();

break;

case     POW:

mid=pop();

midans=pow(mid,pop());

push(midans);

printf("%f\n",midans);

getch();

break;

case     NUM:

push(atof(s));

break;

case     '+':   midans=pop()+pop();

push(midans);

printf("%f\n",midans);

getch();

break;

case     '*':

midans=pop()*pop();

push(midans);

printf("%f\n",midans);

getch();

break;

case     '-':

mid=pop();

midans=pop()-mid;

push(midans);

printf("%f\n",midans);

getch();

break;

case     '/':

mid=pop();

if(mid==0)

{

printf("ERROR!the location of the 0 is wrong!\n");

getch();

break;

}

else

{

midans=pop()/mid;

push(midans);

printf("%f\n",midans);

getch();

}

break;

case     '%' :

mid=pop();

if(mid==0)

{

printf("ERROR!the location of the 0 is wrong!\n");

getch();

break;

}

else

{

midans=(int)pop()%(int)mid;

push(midans);

printf("%d\n",midans);

getch();

}

break;

case     '\n':

printf("%f\n",pop());

printf("Please input :\n");

getch();

break;

case    AGAIN :

break;

default      :

printf("%d\n",type);

printf("ERROR!The get is wrong !\n");

break;

}

}

getch();

}

double pop(void)                                               /*出栈*/

{

if(point<=0)

{

printf("The array is empty!\n");

return    0;

}

else

return   stac[--point];

}

void  push(double f)

{                                                              /*入栈*/

if(point

stac[point++]=f;

else

{

printf("The memory is limited!\n");

}

}

int getst()

{                                                              /*获得输入的类型,并返回类型*/

int type,i,c,k,length,m;

i=1;

k=0;

if(poi<1)

{

aa:     gets(string);

if(!strcmp(string,""))

{

return  '\n';

}

if(!strcmp(string,"flash"))

{

while(k

{

stac[k]=0;

k++;

}

return AGAIN;

}

if(!strcmp(string,"sin"))

return     SIN;

if(!strcmp(string,"cos"))

return     COS;

if(!strcmp(string,"exp"))

return     EXP;

if(!strcmp(string,"pow"))

return     POW;

poi=length=strlen(string);

if(length>SMAX-1)

{

printf("the memory of the suf is limited!\n");

printf("Please input again!\n");

goto  aa;

}

for(m=0,length--;length>=0;length--,m++)         /*倒着装入缓冲池*/

suf[m]=string[length];

suf[m]='\0';

}

for(;(s[0]=c=get())==' '||c=='\t';)

;

s[1]='\0';

if(!isdigit(c)&&c!='.'&&c!='-')

return  c;

if(isdigit(c)||'-'==c)

{

while(isdigit(s[i++]=c=get()))

;

if(!isdigit(c)&&2==i&&s[0]=='-')

{

unget(c);

return  '-';

}

if(c=='.')

while(isdigit(s[i++]=c=get()))

;

if(!(poi==0))

{

unget(c);

s[--i]='\0';

}

return  NUM;

}

else if(c=='\n')

return   '\n';

else

{

printf("ERROR!");

printf("The char cant't be recoganized!\n");

}

}

int get()                                                        /*得到一个字符*/

{

if(poi>0)

return  suf[--poi];

else

if(poi==0)

return       AGAIN;

}

void unget(int c)                                                /*若字符类型不符合,再放进缓冲池*/

{

if(poi>=SMAX)

{

printf("ERROR!The memory is limited !\n");

getch();

exit(0);

}

else

suf[poi++]=c;

}

6f83fddf9cb9ff6843fffd45e1807199.gif

/*************我就是程序;程序就是我!******************/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用以下步骤来实现基于C#的简单逆波兰式计算器: 1. 创建一个接受用户输入表达式的文本框和一个按钮来触发计算。 2. 当用户按下按钮时,获取文本框中的表达式。 3. 将表达式转换为逆波兰式(后缀表达式)。 4. 使用堆栈来计算逆波兰式表达式。 5. 显示计算结果。 以下是一个基本的C#代码示例: ```csharp using System; using System.Collections.Generic; namespace Calculator { class Program { static void Main(string[] args) { Console.WriteLine("Enter an expression in reverse Polish notation:"); string input = Console.ReadLine(); // Convert the input expression to a list of tokens List<string> tokens = new List<string>(input.Split()); // Create a stack to store operands Stack<double> stack = new Stack<double>(); foreach (string token in tokens) { double operand; if (double.TryParse(token, out operand)) { // If the token is a number, push it onto the stack stack.Push(operand); } else { // If the token is an operator, pop two operands from the stack and apply the operator double operand2 = stack.Pop(); double operand1 = stack.Pop(); double result = 0; switch (token) { case "+": result = operand1 + operand2; break; case "-": result = operand1 - operand2; break; case "*": result = operand1 * operand2; break; case "/": result = operand1 / operand2; break; default: Console.WriteLine("Invalid operator: " + token); return; } // Push the result back onto the stack stack.Push(result); } } // The final result is on the top of the stack Console.WriteLine("Result: " + stack.Pop()); } } } ``` 这个示例程序将用户输入的表达式转换为逆波兰式,然后使用堆栈来计算表达式的值。你可以根据自己的需要进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值