using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace cal_test
{
class Program
{
static void Main()
{
Console.WriteLine($"계산할 값들을 입력하세요.\nex) 3+5*2-3");
string str = Console.ReadLine();
str = str.Replace(" ", "");
try
{
double result = EvaluateExpression(str);
Console.WriteLine($"{str} = {result}");
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
}
static double EvaluateExpression(string expression)
{
var values = new Stack<double>();
var operators = new Stack<char>();
int i = 0;
while (i < expression.Length)
{
if (char.IsDigit(expression[i]))
{
double value = 0;
while (i < expression.Length && char.IsDigit(expression[i]))
{
value = value * 10 + (expression[i] - '0');
i++;
}
values.Push(value);
}
else if (expression[i] == '+' || expression[i] == '-' || expression[i] == '*' || expression[i] == '/')
{
while (operators.Count > 0 && HasPrecedence(expression[i], operators.Peek()))
{
values.Push(ApplyOperation(operators.Pop(), values.Pop(), values.Pop()));
}
operators.Push(expression[i]);
i++;
}
else
{
throw new ArgumentException($"Invalid character in expression: {expression[i]}");
}
}
while (operators.Count > 0)
{
values.Push(ApplyOperation(operators.Pop(), values.Pop(), values.Pop()));
}
return values.Pop();
}
static bool HasPrecedence(char op1, char op2)
{
if ((op1 == '*' || op1 == '/') && (op2 == '+' || op2 == '-'))
{
return false;
}
return true;
}
static double ApplyOperation(char op, double b, double a)
{
switch (op)
{
case '+':
return a + b;
case '-':
return a - b;
case '*':
return a * b;
case '/':
if (b == 0)
{
throw new DivideByZeroException("Division by zero.");
}
return a / b;
default:
throw new ArgumentException($"Invalid operator: {op}");
}
}
}
}
728x90
'Embedded > 3. C, C#, Python' 카테고리의 다른 글
C# - WinForm 연습 (Form, Button, CheckBox, ComboBox, GroupBox, Label, ListBox, TextBox, Timer... 구현 ) (0) | 2024.06.10 |
---|---|
C# - WinForm 기본 화면 구성 (0) | 2024.06.10 |
C - 포인터 (0) | 2024.05.13 |
C# 컬렉션, 제네릭, 널(null), LINQ (0) | 2024.04.29 |
C# - 문자열 / 예외 처리 (0) | 2024.04.22 |