I co zrobić, żeby pusty nie był.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace postc
{
public partial class Form1 : Form
{
PFCalculator oblicz;
public Form1()
{
InitializeComponent();
}
public class PFCalculator
{
private string expr;
public PFCalculator()
{
setExpr("");
}
public PFCalculator(string expr)
{
setExpr(expr);
}
public void setExpr(string expr)
{
this.expr = expr;
}
public string getResult()
{
Stack<string> stk = new Stack<string>();
parse(expr.Split(' '), stk);
return stk.Pop();
}
private void parse(string[] input, Stack<string> stack)
{
for (int i = 0; i < input.Length; i++)
{
if (new Regex("^([0-9]+)?([.][0-9]+)?$").IsMatch(input[i]))
stack.Push(input[i]);
else
stack.Push(performOperatiopn(stack.Pop(), stack.Pop(), input[i][0]));<<<<tutaj wyskakuje ten wyjatek
}
}
private string performOperatiopn(string operand1, string operand2, char operation)
{
double val1 = double.Parse(operand1);
double val2 = double.Parse(operand2);
switch (operation)
{
case '+':
return (val2 + val1) + "";
case '-':
return (val2 - val1) + "";
case '*':
return (val2 * val1) + "";
case '/':
return (val2 / val1) + "";
case '%':
return (val2 % val1) + "";
}
return "";
}
}
private void button1_Click(object sender, EventArgs e)
{
String wyrazenie = "23+";
oblicz = new PFCalculator(wyrazenie);
label1.Text = oblicz.getResult();
}
}
}