逆波兰式算法(加减乘除,小数)

#include<iostream> 
#include<string>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#define max 100
using namespace std;
long double str2num(const char *s)
{
 long double num;
 sscanf(s, "%lf", &num);
 return num;
}
char ex[max]; /*存储后缀表达式*/
void trans() /*将算术表达式转化为后缀表达式*/
{
 char str[max]; /*存储原算术表达式*/
 char stack[max]; /*作为栈使用*/
 char ch;
 int sum,i,j,t,top=0;
 i=0; /*获取用户输入的表达式*/
 do
 {
  i++;
  cin>>str[i];/*此步我用的是C++ C语言的话在后面 之所以用这个有一点区别 都*/
  //scanf("%c",&str[i]);
 } while(str[i]!='#' && i!=max);
 sum=i;
 t=1;i=1;
 ch=str[i];i++;
 while(ch!='#'){
  switch(ch){
  case '(': /*判定为左括号*/
   stack[++top]=ch;
   break;
  case ')': /*判定为右括号*/
   while(stack[top]!='(')
    ex[t++]=stack[top--];
   top--;
   break;
  case '+': /*判定为加减号*/
  case '-': 
   while(top!=0&&stack[top]!='(')
    ex[t++]=stack[top--];
   stack[++top]=ch;
   break;
  case '*': /*判定为乘除号*/
  case '/':
   while(stack[top]=='*'||stack[top]=='/')
    ex[t++]=stack[top--];
   stack[++top]=ch;
   break;
  case '#':
   break;
  default:
   while(ch>='0'&&ch<='9'||ch=='.') /*判定为数字*/
   {
    ex[t++]=ch;
    ch=str[i++];
    
   }
   i--;
   ex[t++]=' ';
  }
  ch=str[i];i++;
 }
 while(top!=0)
  ex[t++]=stack[top--];
 ex[t]=' ';
 for(j=1;j<t;j++)
  printf("%c",ex[j]);
 printf("\n");
}
void compvalue(){ /*计算后缀表达式的值*/
 float stack[max],d; /*作为栈使用*/
 char ch;
 int t=1,top=0; /*t为ex下标,top为stack下标*/
 ch=ex[t++];
 string digit;
 while(ch!=' '){
  switch(ch){
  case '+':
   stack[top-1]=stack[top-1]+stack[top];
   top--;
   break;
  case '-':
   stack[top-1]=stack[top-1]-stack[top];
   top--;
   break;
  case '*':
   stack[top-1]=stack[top-1]*stack[top];
   top--;
   break;
  case '/':
   if(stack[top]!=0) stack[top-1]=stack[top-1]/stack[top];
   else{
    printf("除零错误!\n");
    exit(0); /*异常退出*/
   }
   top--;
   break;
  case '0':
  case '1':
  case '2':
  case '3':
  case '4':
  case '5':
  case '6':
  case '7':
  case '8':
  case '9':
  case '.':
   digit = "";
   while(ch>='0'&&ch<='9'||ch=='.'){ /*将数字字符转化为对应的数值*/
    digit += ch;
    ch=ex[t];t++;
   }
   d=str2num(digit.c_str());
   top++;
   stack[top]=d;
   break;
  default:
   top++;
   break;
  }
  ch=ex[t];t++;
 }
 printf("%f",stack[top]);
}
int main(){
 trans();
 compvalue();
 system("pause");
 return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值