字符串数学输入和解决方案 [英] String mathematical input and solution
本文介绍了字符串数学输入和解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我必须接受来自用户的字符串格式的数学表达式,例如 2 * 5 或 9/5 + 9 或 91-10 + 82/3 等等。它可以是任何长度。它可以工作,如果我有单个长度的操作数,如 3 + 9-5 * 4/2 通过我的下面的代码中的一些变化它工作正常。但如果我给它多一个长度操作数,如 38 + 12 / 2-7 + 69 或 325-69 + 584 等等,则会失败,等等C#。
我尝试了什么:
suppose I have to accept an mathematical expression in a string format from the user, like e.g. 2*5 or 9/5+9 or 91-10+82/3 and so on. It can be of any length. It works if I have single length operand like 3+9-5*4/2 by some change in my below code it works fine. but fails if i give it more then one length operand like 38+12/2-7+69 or 325-69+584 and so on C#.
What I have tried:
Below code works fine for single length operand.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplicationExpressionFirstDay
{
class Program
{
static void Main(string[] args)
{
char[] arrOne = new char[100];
int[] arrTwo = new int[2];
string opr="+";
string input;
int j = 0;
int i = 0;
input = Console.ReadLine();
arrOne = input.ToCharArray();
for (i = 0; i < arrOne.Length; i++)
{
if (arrOne[i] >= 48 && arrOne[i] <= 57)
{
arrTwo[j] = Convert.ToInt32(char.ConvertFromUtf32(arrOne[i]));
j++;
}
else
{
opr =char.ConvertFromUtf32(arrOne[i]);
}
if (j == 2){
switch (opr)
{
case "+":
arrTwo[0] = arrTwo[0] + arrTwo[1];
j = 1;
break;
case "-":
arrTwo[0] = arrTwo[0] - arrTwo[1];
j = 1;
break;
case "*":
arrTwo[0] = arrTwo[0] * arrTwo[1];
j = 1;
break;
case "/":
arrTwo[0] = arrTwo[0] / arrTwo[1];
j = 1;
break;
default:
j = 0;
break;
}
}
}
Console.WriteLine("Result : " + arrTwo[0]);
Console.ReadLine();
}
}
}
But this code fails for more than one length operands
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplicationExpressionFirstDay
{
class Program
{
static void Main(string[] args)
{
char[] arrOne = new char[100];
int[] arrTwo = new int[2];
string opr="+";
string input;
int j = 0;<pre lang="c#">int concat = 0;
int i = 0;
input = Console.ReadLine();
arrOne = input.ToCharArray();
for (i = 0; i < arrOne.Length; i++)
{
if (arrOne[i] >= 48 && arrOne[i] <= 57)
{
if (concat == 0)
{
arrTwo[j] = Convert.ToInt32(char.ConvertFromUtf32(arrOne[i]));
j++;
concat++;
}
else
{
j = 1;
arrTwo[j] = Convert.ToInt32(Convert.ToString(arrTwo[1])+char.ConvertFromUtf32(arrOne[i]));
j++;
concat = 0;
}
}
else
{
opr =char.ConvertFromUtf32(arrOne[i]);
concat = 0;
}
if (j == 2&&concat==0){
switch (opr)
{
case "+":
arrTwo[0] = arrTwo[0] + arrTwo[1];
j = 1;
break;
case "-":
arrTwo[0] = arrTwo[0] - arrTwo[1];
j = 1;
break;
case "*":
arrTwo[0] = arrTwo[0] * arrTwo[1];
j = 1;
break;
case "/":
arrTwo[0] = arrTwo[0] / arrTwo[1];
j = 1;
break;
default:
j = 0;
break;
}
}
}
Console.WriteLine("Result : " + arrTwo[0]);
Console.ReadLine();
}
}
}
推荐答案
Google是你的朋友:好看并经常拜访他。他可以比在这里发布问题更快地回答问题...
一个非常快速的搜索会找到你的大量例子。 c#评估表达式 - Google搜索 [ ^ ]为您提供88,000个示例...
这是一个很好的例子:最新技术表达评估 [ ^ ]
将来,请尝试自己做至少基础研究,不要浪费你的时间或者我们的。
Google is your friend: Be nice and visit him often. He can answer questions a lot more quickly than posting them here...
A very quick search would have found you loads of examples. c# evaluate expression - Google Search[^] gives you 88,000 examples ...
Here's a good example: State of the Art Expression Evaluation[^]
In future, please try to do at least basic research yourself, and not waste your time or ours.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplicationExpressionUsingWhileLoop
{
class Program
{
static void Main(string[] args)
{
char[] arrOne = new char[100];
int[] arrTwo = new int[2];
string opr = " ";
string input;
int j = 0;
int i = 0;
input = Console.ReadLine();
arrOne = input.ToCharArray();
for (i = 0; i < arrOne.Length; i++)
{
while(arrOne[i] >= 48 && arrOne[i] <= 57)
{
arrTwo[j] = Convert.ToInt32(Convert.ToString(arrTwo[j]) + char.ConvertFromUtf32(arrOne[i]));
if (i == (arrOne.Length)-1)
{
break;
}
else
{
i++;
}
}
j++;
if (j == 2)
{
switch (opr)
{
case "+":
arrTwo[0] = arrTwo[0] + arrTwo[1];
j = 1;
arrTwo[1] = 0;
break;
case "-":
arrTwo[0] = arrTwo[0] - arrTwo[1];
j = 1;
arrTwo[1] = 0;
break;
case "*":
arrTwo[0] = arrTwo[0] * arrTwo[1];
j = 1;
arrTwo[1] = 0;
break;
case "/":
arrTwo[0] = arrTwo[0] / arrTwo[1];
j = 1;
arrTwo[1] = 0;
break;
default:
j = 0;
break;
}
}
opr = char.ConvertFromUtf32(arrOne[i]);
}
Console.WriteLine("Result : " + arrTwo[0]);
Console.ReadLine();
}
}
}
这篇关于字符串数学输入和解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文