i'm trying to create a simple natural language calculate. this is what i am trying to achieve, If i input the string "five plus six" the output should be answer: 11.
below i tried to create a Enum of numbers but for operators it does not accept strings,i use this so i can fetch and compare to the array input and covert respectively,could you please suggest what i can maybe use to store operators as words so? or suggest an article maybe.
code:
public class EnumSample
{
enum Numbers: int
{
zero = 0,
obne = 1,
two = 2,
three =3,
four =4,
five =5,
six =6,
seven=7,
eight = 8,
nine=9,
};
enum Operator
{
Add = "+",
plus ="+",
minus ="-",
subtract ="-"
};
public static void Main()
{
int[] arr = new int[200];
Console.Write("Get input");
for (int i = 0; i < 200; i++)
{
Console.Write("element - {0} : ", i);
arr[i] = Convert.ToInt32(Console.ReadLine());
}
foreach (int item in Enum.GetValues(typeof (Numbers)))
{
String name = Enum.GetName(typeof(Numbers), item);
Console.WriteLine(name+item);
}
Console.Read();
}
}
I prefer use Dictionary instead Enum. Here is solution only for 2 value but you can figure out something by looking at this solution;
public class EnumSample
{
private static int Solve(string operation, int first, int second)
{
switch (operation)
{
case "plus":
{
return second + first;
}
case "add":
{
return second + first;
}
default:
return second-first;
}
}
public static void Main()
{
var numbersDic = new Dictionary<string, int>()
{
{"zero", 0},
{"one", 1},
{"two", 2},
{"three", 3},
{"four", 4},
{"five", 5},
{"six", 6},
{"seven", 7},
{"eight", 8},
{"nine", 9}
};
Console.WriteLine("Get input");
string[] word = Console.ReadLine()?.Split(' '); // spliting words {"five","plus","six"}
if (word != null)
Console.Write(Solve(word[1], numbersDic[word[0]], numbersDic[word[2]]));
// plus , 5 , 6
Console.Read();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.