Предмет: Другие предметы, автор: LightBloodMoon

Как заменить 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;
}

}
}

}
}
}​

Ответы

Автор ответа: никита2008н
0

Ответ:

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 _Aaaa

{

// Главная форма

public partial class Form1:Form

{

// Запуск, останов  

 public Form1() { InitializeComponent();  }

// Метод, запускаемый при нажатии кнопки

 private void button1_Click ( object sender,EventArgs e )

 {

  CreateConfiguration ();

 }

 public void CreateConfiguration ()  

 {

// Чтение текста из текст-боксов и передача вызываемому методу

  MessageContent ( textBox1.Text, textBox2.Text);

 }

 public void MessageContent ( string Title, string Content )  

 {

  MessageBox.Show ( Title + "\n" + Content );

 }

}

}

ПОПРОБУЙ ТАК

Интересные вопросы