C#练习实现四则运算(可以输入多位数和小数)
代码语言:javascript
复制using System;
using System.Linq;
using System.Data;
namespace day15test03
{
class Program
{
/// <summary>
/// 四则运算计算器
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
//Console.WriteLine("输入要计算的式子(等号结尾即可,类似 1 21*6-8/4 4-55*6 4/5=):");
//String str = Console.ReadLine();
Suan("1/2 4 5/6=");
}
//运算,调用加减和乘除运算
public static void Suan(String sss)
{
String ss = sss.TrimStart('0'), s1 = "", s = "";
DataTable dt = new DataTable();
if (ss.IndexOf("*") != -1 && ss.IndexOf("/") != -1) { s1 = yunSuan(yunSuan(ss, "*"), "/"); s = dt.Compute(s1.Replace("=", ""), "").ToString(); }
else if(ss.IndexOf("*") != -1 && ss.IndexOf("/") == -1) { s1 = yunSuan(ss, "*"); s = dt.Compute(s1.Replace("=", ""), "").ToString(); }
else if(ss.IndexOf("*") == -1 && ss.IndexOf("/") != -1) { s1 = yunSuan(ss, "/"); s = dt.Compute(s1.Replace("=", ""), "").ToString(); }
else { s = dt.Compute(ss.Replace("=", ""), "").ToString(); }
Console.WriteLine(ss s);
}
//运算
private static String yunSuan(String ss, String tt)
{
float[] nums = new float[10];
int count1 = 0;
String ss1 = "";
String[] s1 = new String[10];
if(ss == null) { return null; }
if (ss.IndexOf(tt) != -1)
{
s1[0] = ss;
int[] nn = getFuhao(ss, tt);
for (int x = 0; x < nn.Length; x )
{
if (nn[x] > 0) { count1 ; }
}
for (int i = 0; i < count1; i )
{
{ nums[i] = getCheng(s1[i], getFuhao(s1[i], tt)[1], tt, ref ss1); s1[i 1] = s1[i].Replace(ss1, nums[i].ToString()); }
}
}
return s1[count1];
}
返回运算符的位置
private static int[] getFuhao(String ss, String stt)
{
int[] num = new int[5];
for (int i = 1; i < 5; i )
{
if (ss.IndexOf(stt, num[i - 1] 1) != -1) { num[i] = ss.IndexOf(stt, num[i - 1] 1); }
else { break; }
}
return num;
}
//乘除运算
private static float getCheng(String ss, int n, String t, ref String ing)
{
int[] left = new int[ss.Length];
int[] right = new int[ss.Length];
int c = 0, d = 0;
for (int i = 1; i < ss.Length; i )
{
if (n >= i && c == 0 && int.TryParse(ss[n - i].ToString(), out left[i - 1])) { }
else if (n >= i && ss[n - i] == '.' && c == 0) { left[i - 1] = 46; }
else { c ; left[i - 1] = -1; }
if (d == 0 && int.TryParse(ss[n i].ToString(), out right[i - 1])) { }
else if (d == 0 && ss[n i] == '.') { right[i - 1] = 46; }
else { d ; right[i - 1] = -1; }
if (c != 0 && d != 0) { break; }
}
float a = get2(left, 1);
float b = get2(right, 2);
ing = a.ToString() t b.ToString();
if (t == "*") { return (float)Math.Round(a * b, 2); }
else if (t == " ") { return (float)Math.Round(a b, 2); }
else if (t == "-") { return (float)Math.Round(a - b, 2); }
else
{
try { return (float)Math.Round(a / b, 2); }
catch { Console.WriteLine("除数不能为零!"); return -1; }
}
}
//返回取出的运算符的两边的数
private static float get2(int[] right, int a)
{
int count = 0, num = 0;
float sum;
for (int j = 1; j < right.Length-1; j )
{
if (right[j] == 46) { num = j; }
if (right[j] != -1 && right[j 1] != 0 || right[j] != -1 && right[j - 1] != 0 || right[j] != -1 && right[j 1] == 0 && right[j - 1] == 0)
{ count ; }
else { break; }
}
count ;
int[] nums = new int[count];
for (int i = 0; i < count; i ) { nums[i] = right[i]; }
if (num == 0)
{
if (a == 1) { sum = int.Parse(String.Concat(String.Join("", nums).ToCharArray().Reverse())); }
else { sum = int.Parse(String.Concat(String.Join("", nums))); }
}
else
{
if (a == 1) { sum = float.Parse(String.Concat(String.Join("", nums).Replace("46", ".").ToCharArray().Reverse())); }
else { sum = float.Parse(String.Join("", nums).Replace("46", ".")); }
}
return (float)Math.Round(sum, 2);
}
}
}