文本框内容与多个时间相加 [英] textbox content sum with multi timing

查看:66
本文介绍了文本框内容与多个时间相加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





iam使用下面的代码,我在这个平台上得到解决方案,但我卡在了下面。



iam在下面的文本框中使用

[12-a

12-b] 6

结果是144

这是完美的工作。



如果iam使用如下如下

{12-a

12 -b

[6-a

6-b] 3} 3

结果是60

这是也工作



如果iam使用如下以下

[12-a

12-b] 99



[12-a

12-b] 999





[12-a

12-b] 9999



这不行,请看下面的代码并解决这个问题。



Hi,

iam using below code which i get solution in this platform but i stuck below .

iam using in textbox like below
[12-a
12-b]6
result is 144
this is work perfectly.

if iam using like below
{12-a
12-b
[6-a
6-b]3}3
result is 60
this is also work

if iam using like below
[12-a
12-b]99
or
[12-a
12-b]999

or
[12-a
12-b]9999

this is not work please see below code and solve this problem.

private void button1_Click(object sender, EventArgs e)
{
int combinedSum = 0;
for (char letter = 'A'; letter <= 'D'; letter++)
{
int sum = 0;
var stack = new Stack<string>();
int stackSum = 0;
foreach (string line in textBox1.Lines)
{
string temp = line.Replace(".", "").Replace(" ", ""); // remove dots and spaces
if (temp == "") continue; // ignore blank line
bool containsLetter = (line.IndexOf(letter) > -1);
char first = temp[0];
char last = temp[temp.Length - 1];
if (Char.IsDigit(first))
{
string numStr = temp.Split('-')[0];
if (!containsLetter) numStr = "0";
if (stack.Count == 0)
{
sum += int.Parse(numStr);
}
else
{
stack.Push(numStr);
}
if (Char.IsDigit(last))
{
int multiplier = last - 48;
char bracket = temp[temp.Length - 2];
int total = 0;
char openChar = '\0';
string tempStr;
if (bracket == '}')
openChar = '{';
else if (bracket == ']')
openChar = '[';
else if (bracket == ')')
openChar = '(';
else if (bracket == '>')
openChar = '<';
do
{
tempStr = stack.Pop();
int num;
if (int.TryParse(tempStr, out num)) total += num;
}
while (tempStr[0] != openChar);
stackSum = (stackSum + total) * multiplier;
if (stack.Count == 0)
{
sum += stackSum;
stackSum = 0;
}
}
}
else
{
stack.Push(first.ToString());
int index = 1;
while (true)
{
if (Char.IsDigit(temp[index])) break;
stack.Push(temp[index].ToString());
index++;
}
string numStr2 = temp.Substring(index).Split('-')[0];
if (!containsLetter) numStr2 = "0";
stack.Push(numStr2);
if (Char.IsDigit(last))
{
int multiplier = last - 48;
char bracket = temp[temp.Length - 2];
int total = 0;
char openChar = '\0';
string tempStr;
if (bracket == '}')
openChar = '{';
else if (bracket == ']')
openChar = '[';
else if (bracket == ')')
openChar = '(';
else if (bracket == '>')
openChar = '<';
do
{
tempStr = stack.Pop();
int num;
if (int.TryParse(tempStr, out num)) total += num;
}
while (tempStr[0] != openChar);
stackSum += (stackSum + total) * multiplier;
if (stack.Count == 0)
{
sum += stackSum;
stackSum = 0;
}
}
}
}
switch (letter)
{
case 'A':
textBox3.Text = String.Format("A = {0}", sum);
break;
case 'B':
textBox4.Text = String.Format("B = {0}", sum);
break;
case 'C':
textBox5.Text = String.Format("C = {0}", sum);
break;
case 'D':
textBox6.Text = String.Format("D = {0}", sum);
break;
}
combinedSum += sum;
}
textBox2.Text = String.Format("Sum = {0}", combinedSum);
}

推荐答案

这不是工作甚至没有什么帮助 - 特别是考虑到我们不知道你的是什么代码应该生成!



所以,它取决于你。

在函数的第一行放一个断点,并通过调试器运行您的代码。然后查看您的代码,并查看您的数据并找出手动应该发生的事情。然后单步执行每一行检查您预期发生的情况正是如此。如果不是,那就是当你遇到问题时,你可以回溯(或者再次运行并仔细观察)以找出原因。


对不起,但我们不能为你做到这一点 - 时间让你学习一门新的(非常非常有用的)技能:调试!
"this is not work" is not even slightly helpful - especially given that we have no idea what your code is supposed to generate!

So, its going to be up to you.
Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!


这篇关于文本框内容与多个时间相加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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