cad sunny

cad sunny

  • NA
  • 138
  • 23k

textbox content sum

Dec 10 2015 7:33 AM
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);
} 
 
 

Answers (4)