-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculatorFromString.cs
More file actions
59 lines (52 loc) · 1.72 KB
/
Copy pathcalculatorFromString.cs
File metadata and controls
59 lines (52 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace calculatorFromString
{
class Program
{
static void Main(string[] args)
{
Console.Write("Please enter your two numbers with either '*, /, -, +': ");
string input = Console.ReadLine();
string operand;
if (input.IndexOf("*", 0) != -1)
{
operand = input.Substring(input.IndexOf("*", 0), 1);
}
else if (input.IndexOf("/", 0) != -1)
{
operand = input.Substring(input.IndexOf("/", 0), 1);
}
else if (input.IndexOf("-", 0) != -1)
{
operand = input.Substring(input.IndexOf("-", 0), 1);
}
else
{
operand = input.Substring(input.IndexOf("+", 0), 1);
}
string[] number = new string[2];
number[0] = input.Substring(0, input.IndexOf(operand, 0));
number[1] = input.Substring(input.IndexOf(operand) + 1);
if (operand == "*")
{
Console.WriteLine(Convert.ToDouble(number[0]) * Convert.ToDouble(number[1]));
}
else if (operand == "/")
{
Console.WriteLine(Convert.ToDecimal(number[0]) / Convert.ToDecimal(number[1]));
}
else if (operand == "+")
{
Console.WriteLine(Convert.ToDouble(number[0]) + Convert.ToDouble(number[1]));
}
else
{
Console.WriteLine(Convert.ToDouble(number[0]) - Convert.ToDouble(number[1]));
}
}
}
}