需要我的第一个计算器的帮助 [英] Need assistance with my first calculator
问题描述
所以我刚开始学习编码,大约花了1个星期.我想尝试制作一个具有+和-的计算器,但无法弄清楚如何让用户选择他想使用的东西,有没有人可以帮助我呢?这是代码.
so i just started learning coding, been going at it for about 1 week. i wanted to try and make a calculator that does + and - but cant figure out how to let the user choose what he wants to use, is there anyone that can help me with that? here is the code.
int x;
int y;
Console.WriteLine("Welcome to my calculator program!");
Console.WriteLine("This calculator for now can only do + and -");
Console.WriteLine("If x is highter than y it will do - and if x is lower than y it will do +");
Console.WriteLine("pls write in you x");
x = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("pls write in your y");
y = Convert.ToInt32(Console.ReadLine());
if (x > y)
{
Console.WriteLine("you chose to use minus");
int sum = x - y;
Console.WriteLine("result: {0}", sum);
Console.WriteLine("Press a key to exit");
Console.ReadLine();
}
else
{
Console.WriteLine("you chose to use plus");
int sum1 = x + y;
Console.WriteLine("result: {0}", sum1);
Console.WriteLine("Press a key to exit");
Console.ReadLine();
}
}
}
}如您所见,如果x i小于y则使用+,如果x大于y则使用-.我知道如何使其工作的唯一方法.
} as you can see is just used + if the x i lower than y and - if the x is greater than y. the only way i knew how to make it work.
推荐答案
已经回答了幼稚的方法(询问用户).但是,您实际上可能想要实现的是编写一个功能强大的计算器,用户在其中输入 2 + 3
之类的字符串,然后程序会简单地给出答案.您希望编写一个解释器.
The naive approach (ask the user) has been answered. But what you probably actually want to achieve is write a full-fledged calculator where the user enters a string like 2+3
and the program simply gives the answer. You're aspiring to write an interpretor.
考虑一下,您将必须执行三个任务:
If you think about it, you will have to perform three tasks:
- 扫描用户输入,并将其转换为一系列标记.这是将字符串"2 + 3"转换为令牌系列值2的整数",加运算符",值3的整数"
- 解释令牌以计算结果
您在输入上施加的约束越多(例如,如您所说,仅允许+和-开头),代码将越简单(如果无法根据您设定的规则解释输入,则失败)).
The more constraints you place on the input (e.g. as you said, only allow + and - to start with), the simpler the code will be (just fail if the input can not be interpreted according to the rules you set out).
如果您想更深入地研究,可以在此主题上找到许多资源.我很喜欢阅读这一系列博客文章(作者使用Python,但是原理保持不变)
If you want to dig deeper, there's numerous resources available on this subject. I enjoyed reading this series of blog posts (the author uses Python, but the principles remain the same).
最后,您会发现您想做的许多事情已经实现.有可用的库已经解析和评估数学表达式(示例: NCalc ,
In the end you will discover that much of what you are trying to do has already been achieved. There are libraries available that already parse and evaluate mathematical expressions (examples: NCalc, Mathos).
最后,Roslyn允许您轻松解析任何C#表达式,因此它也可以用于构建一个简单的计算器,如下所示:
On a final note, Roslyn allows you to parse any C# expression easily, so it can also be used to build a simple calculator, like this:
class Program
{
static void Main(string[] args)
{
CSharpReplAsync().Wait();
}
private static async Task CSharpReplAsync()
{
Console.WriteLine("C# Math REPL");
Console.WriteLine("You can use any method from System.Math");
var options = ScriptOptions.Default
.AddImports("System", "System.Console", "System.Math");
var state = await CSharpScript.RunAsync("WriteLine(\"Hello from Roslyn\")", options);
while (true)
{
Console.Write("> ");
string expression = Console.ReadLine();
if (string.IsNullOrEmpty(expression))
break;
try
{
state = await state.ContinueWithAsync(expression, options);
if (state.ReturnValue != null)
Console.WriteLine(state.ReturnValue);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}
这篇关于需要我的第一个计算器的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!