sticking strictly to your example you would want to add a new method to your class:
private double DoMath(int val1, int val2, string oper)
{
switch (oper)
{
case "+":
return (val1 + val2);
case "-":
return (val1 - val2);
case "*":
return (val1 * val2);
case "/":
return (val1 / val2);
}
return 0;
}
Then in your main code you will want to ask the user for the additional input:
now here is the easiest method to understand:
using System;
class Maths
{
public static void Main()
{
Maths m = new Maths();
m.ShowDemo();
}
void ShowDemo()
{
Console.Write("Please enter the first value:");
var num1 = int.Parse(Console.ReadLine());
Console.Write("Please enter the second value:");
var num2 = int.Parse(Console.ReadLine());
Console.Write("Please enter the desired function (+,-,*,/)");
var oper= Console.ReadLine();
var result = DoMath(num1, num2, oper);
Console.WriteLine("The result to your equation is: " + result);
}
private double DoMath(int val1, int val2, string oper)
{
switch (oper)
{
case "+":
return (val1 + val2);
case "-":
return (val1 - val2);
case "*":
return (val1 * val2);
case "/":
return (val1 / val2);
}
return 0;
}
}
However if you wanted to make this a little easier for the user you might want to use a regular expression to parse a plain text expression:
Console.WriteLine("Please enter your equation:");
var equation = Console.ReadLine();
var data = Regex.Match(equation, "(?<val1>\\d+)\\s*(?<sign>[+\\-*/])\\s*(?<val2>\\d+)");
var val1 = int.Parse(data.Groups["val1"].Value);
var val2 = int.Parse(data.Groups["val2"].Value);
var operator = data.Groups["sign"].Value;
var result = DoMath(val1, val2, operator);
Console.WriteLine("Your answer is " + result);
To explain some of the stuff in there -
int.Parse - is a way of converting a 'string' to an 'int'
A regular expression matches a string to a pattern and allows you to pull information from it - please see
this page for more info
></\/~Psightoplasm`~