Как заменить case на if на C# :(
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp12
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
}

private void textBox1_TextChanged(object sender, EventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{

string arg = textBox1.Text;
string[] splitedArg = arg.Split(' ');
Stack<double> st = new Stack<double>();

double num;

foreach (var itemArg in splitedArg)
{
bool isNum = double.TryParse(arg, out num);
if (isNum)
st.Push(num);
else
{
double op2;
switch (itemArg)
{
case "+":
st.Push(st.Pop() + st.Pop());
break;
case "*":
st.Push(st.Pop() * st.Pop());
break;
case "-":
op2 = st.Pop();
st.Push(st.Pop() - op2);
break;
case "/":
op2 = st.Pop();
if (op2 != 0.0)
st.Push(st.Pop() / op2);
else
label1.Text = ("Ошибка. Деление на ноль");
break;
case "calc":
label1.Text = ("Результат: " + st.Pop());
break;
default:
label1.Text = ("Ошибка. Неизвестная команда");
break;
}

}
}

}
}
}​
Please enter comments
Please enter your name.
Please enter the correct email address.
You must agree before submitting.

Answers & Comments


Copyright © 2024 SCHOLAR.TIPS - All rights reserved.