Click here to Skip to main content
16,015,976 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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.

C#
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);
}
Posted
Comments
F-ES Sitecore 10-Dec-15 8:32am    
You haven't explained the logic of your program or why those inputs give those results, it's unlikely anyone will be able to help you with this.
cadsolution 11-Dec-15 0:46am    
if iam using in textbox
[12-A
12-B]9 this is caluculate sum = 216 ok but if iam using more than 9 like 99 or 999 but sum not calucate.this is the problem.

1 solution

"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!
 
Share this answer
 
Comments
cadsolution 13-Jan-16 7:39am    
stack empty error shown when iam using morethan 1-9.
tempStr = stack.Pop();

please solve this problem.i could not solve this my side.
OriginalGriff 13-Jan-16 10:01am    
And? What did the debugger show you was happening?
cadsolution 14-Jan-16 4:09am    
here is my error please download below links for better idea.
http://www.mediafire.com/view/4gneoo4uuzb25gb/FIGURE_1%282%29.jpg http://www.mediafire.com/view/regk1u36oyrik37/FIGURE_2%282%29.jpg

here is my code
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;

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

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';
OriginalGriff 14-Jan-16 4:23am    
And what do you see when you use the debugger?
cadsolution 14-Jan-16 4:27am    
in this my code iam using timing is one to nine then its ok.if i want to use moretha nine then stack.pop() stack empty error shown .please see images.also note red color comments in figure1.and figure2 is after debugging error shown like

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900