在计算器中使用字符串拆分 [英] In calculator use string split

查看:80
本文介绍了在计算器中使用字符串拆分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



如果我们通过键入1 + 2 * 8来输入文本,那么如何读取此字符串以及如何拆分opeartor和操作数。

给出代码用于功能的计算器+, - ,*,/



plz紧急发送代码










if we give input on text by typing 1+2*8 then how to read this string and how to split opeartor and operand.
give code for calculator for function +,-,*,/

plz send code urgently




using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;


namespace WindowsFormsApplication6
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

private void button1_Click(object sender, EventArgs e)
{
int counter = 0;
string inputText = Input.Text;
Stack stack = new Stack();
char[] chars = inputText.ToCharArray();
string lastNumber = string.Empty;



foreach (char c in chars)
{
if (char.IsDigit(c))
{
lastNumber = lastNumber + c.ToString();
}
else
{

stack.Push(lastNumber);
stack.Push(c);
lastNumber = string.Empty;
}
counter++;
}
stack.Push(lastNumber);


int prevNum, currNum=0;
string operand;
bool isEnd = true;

prevNum = int.Parse(stack.Pop().ToString());
operand = stack.Pop().ToString();


do {

operand = stack.Pop().ToString();
if (operand == "+")
{
currNum = int.Parse(stack.Pop().ToString());

prevNum = currNum + prevNum;
answer.Text = prevNum.ToString();
}

if (operand == "-")
{
currNum = int.Parse(stack.Pop().ToString());
prevNum = currNum - prevNum;
answer.Text = prevNum.ToString();
}

if (operand == "/")
{
currNum = int.Parse(stack.Pop().ToString());
prevNum = currNum / prevNum;
answer.Text = prevNum.ToString();
}

if (operand == "*")
{
currNum = int.Parse(stack.Pop().ToString());
prevNum = currNum * prevNum;
answer.Text = prevNum.ToString();
}
}while (operand!=" ");

}


}
}





I do these coding but have an error int ToString() method . any1 help to solve it.

推荐答案

听起来像是家庭作业 - 我们不做你的作业。



如果不是,请看看: Math Parser .NET C# [ ^ ]它可以做你想要的,还有更多。
Sounds like homework to me - and we don''t do your homework.

If it isn''t, then have a look at this: Math Parser .NET C#[^] it does what you want, and a whole lot more.


我想你在这样一个新主题上找不到任何东西,只需使用< a href =https://www.google.com/search?q=parsing+mathematical+expressions&aq=f&oq=parsing+mathematical+expressions&sourceid=chrome&ie=UTF-8> Google [ ^ ]。
I suppose you won''t find anything on such a new topic, just using Google[^] .


请检查:

数学表达式解析简单指南 [ ^ ]


这篇关于在计算器中使用字符串拆分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆