本文介绍了在计算器中使用字符串拆分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 如果我们通过键入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 urgentlyusing 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. 推荐答案 这篇关于在计算器中使用字符串拆分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-20 15:13