Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- namespace Solve_expression
- {
- class Program
- {
- public static char[] operators = new char[] { '+', '-', '*', '/' };
- static void Main(string[] args)
- {
- string input = Console.ReadLine();
- Console.WriteLine(CalculateExpression(input));
- }
- static int CalculateExpression(string input)
- {
- char expressionOperator = input.Where(x => operators.Contains(x)).FirstOrDefault();
- string[] numbers = input.Split(expressionOperator, 2);
- if (!(input.Contains('(') && input.Contains(')')))
- {
- if (numbers.Length == 1)
- {
- return int.Parse(numbers[0]);
- }
- return Evaluate(int.Parse(numbers[0]), int.Parse(numbers[1]), expressionOperator);
- }
- return Evaluate(int.Parse(numbers[0]), CalculateExpression(numbers[1].Substring(1, numbers[1].Length - 2)), expressionOperator);
- }
- static int Evaluate (int a, int b, char Operator)
- {
- switch (Operator)
- {
- case '+':
- return a + b;
- case '-':
- return a - b;
- case '*':
- return a * b;
- case '/':
- return a / b;
- default: return 0;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement