A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© ?﹪滾妳媽丶 中级黑马   /  2014-4-7 16:42  /  1415 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 ?﹪滾妳媽丶 于 2014-4-8 09:44 编辑

之前做了一个简单的计算器,就是提示用户输入数字A运算符和数字B
假如是1+1+1-1+1/1*1这样的例子,就是一次性输成这样,然后按回车直接有结果。
我用+-*/这四个符号分隔出来,然后把每一个分隔出来的转成int类型数组。完全没思路,想到的也是错的。有没有人指点下。。

评分

参与人数 1技术分 +1 收起 理由
czwanglei + 1

查看全部评分

5 个回复

倒序浏览
没有小括号的话应该还好做,有括号就有点麻烦...
回复 使用道具 举报

吧你输入的全部装进一个数组。用switch语句判断符号 最后输出

评分

参与人数 1技术分 +1 收起 理由
czwanglei + 1

查看全部评分

回复 使用道具 举报
我觉得判断是否输入合法是个问题。。
回复 使用道具 举报
本帖最后由 mdb 于 2014-4-8 01:51 编辑

最后手痒还是写了下,思路很简单,先算乘除法,最后算加减,算到最后就是一连串的加减法了,这样就很好处理了,先把整理个字符串给分成只有加减法的列表,然后把列表里有乘除法的项目先算出来再把结果的值存进去,最后整个列表就都是数字和运算符了,这样直接循环进行判断运算就可以了,简单写了个类,不考虑大小括号的运算符,明天再加个功能让它支持括号的表达式。

  1.         public class yunshuanshi
  2.         {
  3.             private List<string> all { get; set; }
  4.             public yunshuanshi(string s)
  5.             {
  6.                 string[] numarr = s.Split(new char[] { '+', '-' });
  7.                 List<string> la = Regex.Split(s.Replace("*", "").Replace("/", ""), @"\d+").Where(r => !string.IsNullOrEmpty(r)).ToList();
  8.                 List<string> ls = new List<string>();
  9.                 for (int i = 0; i < numarr.Length; i++)
  10.                 {
  11.                     ls.Add(numarr[i]);
  12.                     if (i < la.Count)
  13.                         ls.Add(la[i]);
  14.                 }
  15.                 all = ls;
  16.             }
  17.             public string yunshuan()
  18.             {
  19.                 for (int i = 0; i < all.Count; i++) // 先乘除
  20.                 {
  21.                     if (all[i].Contains("*") || all[i].Contains("/"))
  22.                         all[i] = chengchu(all[i]).ToString();
  23.                 }
  24.                 yunshuanall();// 后加减
  25.                 return all[0]; // 结果
  26.             }
  27.             private void yunshuanall()
  28.             {
  29.                 double sum = 0;
  30.                 while (all.Count != 1)
  31.                 {
  32.                     switch (all[1])
  33.                     {
  34.                         case "+":
  35.                             sum = double.Parse(all[0]) + double.Parse(all[2]);
  36.                             break;
  37.                         case "-":
  38.                             sum = double.Parse(all[0]) - double.Parse(all[2]);
  39.                             break;
  40.                     }
  41.                     all.RemoveRange(0, 3);
  42.                     all.Insert(0, sum.ToString());
  43.                 }
  44.             }
  45.             private double chengchu(string s)
  46.             {
  47.                 List<string> cx = s.Split(new char[] { '*', '/' }).ToList();
  48.                 List<string> fh = Regex.Split(s, @"\d+").Where(r => !string.IsNullOrEmpty(r)).ToList();
  49.                 List<string> la = new List<string>();
  50.                 for (int i = 0; i < cx.Count; i++)
  51.                 {
  52.                     la.Add(cx[i]);
  53.                     if (i < fh.Count)
  54.                         la.Add(fh[i]);
  55.                 }
  56.                 cx = la;
  57.                 double sum = 0;
  58.                 while (cx.Count != 1)
  59.                 {
  60.                     switch (cx[1])
  61.                     {
  62.                         case "*":
  63.                             sum = double.Parse(cx[0]) * double.Parse(cx[2]);
  64.                             break;
  65.                         case "/":
  66.                             sum = double.Parse(cx[0]) / double.Parse(cx[2].Trim() == "0" ? "1" : cx[2].Trim());
  67.                             break;
  68.                     }
  69.                     cx.RemoveRange(0, 3);
  70.                     cx.Insert(0, sum.ToString());
  71.                 }
  72.                 return sum;
  73.             }
  74.         }
复制代码

调用

  1. yunshuanshi y = new yunshuanshi("2*40*5/2+10-5+4*16/8-5*18/3/2");
  2. Console.WriteLine("结果是:" + y.yunshuan());
复制代码

简单试算了几个结果都对,不知道还有没有什么BUG
回复 使用道具 举报
同上,手痒。思路是这个样子的,使用正则匹配,然后依次将含有/*+-的部分替换成运算结果。最后就可以得到结果。
为了简便,我只贴运算部分的代码。如果还是不明白可以回复我。
  1. public static string operation(string str, string op)//str为输入的字符串,默认已经经过验证,op为运算符
  2.         {
  3.             string opstring = string.Format(@"((\d+)([\{0}](\d+)))", op);//组合字符串
  4.             do
  5.             {
  6.                 Match m = Regex.Match(str, opstring);//匹配项
  7.                 if (!string.IsNullOrEmpty(m.Groups[1].Value))
  8.                 {
  9.                     double a = Convert.ToDouble(m.Groups[2].Value);
  10.                     double b = Convert.ToDouble(m.Groups[4].Value);
  11.                     switch (op)//根据运算符运算
  12.                     {
  13.                         case "+": str = str.Replace(m.Groups[1].Value, (a + b).ToString()); break;
  14.                         case "-": str = str.Replace(m.Groups[1].Value, (a - b).ToString()); break;
  15.                         case "*": str = str.Replace(m.Groups[1].Value, (a * b).ToString()); break;
  16.                         case "/": str = str.Replace(m.Groups[1].Value, (a / b).ToString()); break;
  17.                         default: str = null; break;
  18.                     }
  19.                 }
  20.             } while (str.IndexOf(op) != -1);
  21.             return str;
  22.         }
复制代码


回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马