Dear All,
I am Trying the code below.
input in textbox
[12-A
12-B]3 =======> IF I WANT TO CHANGE TWO DIGITS OR THREE DIGITS IN PLACE OF 3 (ONE DIGIT ) THEN NOT GET CORRECT SUM.
---------
72
Below is my code
C#
private void btnWpApply_Click(object sender, EventArgs e)
{
try
{
int combinedSum = 0;
for (char letter = 'A'; letter ();
int stackSum = 0;
foreach (string line in txtWpPattern.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 = '')
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;
}
}
}
}
combinedSum += sum;
}
txtWpResult.Text = String.Format(combinedSum + " = E/R".ToString());
label1.Text = String.Format(combinedSum.ToString());
}
catch (Exception ex)
{
MessageBox.Show("Please Enter Correct Inpit Strng Like 2-A");
}
}Please help i want correct sum of textbox input methord wherre i can change multi timing with two/three digits.like between 10-999
Varta

Christophe BOULAS (Firstruner)7 reputationPosted Sep 26, 2024, 4:13 AM EDT
Hi,
First you need to clarify your code, and specify variable type for missed.
First, specify "sum" type, I put int in code bellow because I think you try to do calcul between Hexa value.
For help you, you can do that in first time :
> Specify type
int sum = 0; // (); int stackSum = 0;> Change IFssss by Switch
4 if (if else if else if...) = 4 processors time, 1 switch = 1 processor time
Warn, you do not check other char, user can type @ or : or , etc...
switch (bracket) { case '}': openChar = '{'; break; case ']': openChar = '['; break; case ')': openChar = '('; break; case '>': openChar = '<'; break; }> Transform string directly into char array like
char[] temp = line .Replace(".", "") .Replace(" ", "") .ToCharArray(); <== HEREand change length test into
if (temp.Count() == 0) continue; // ignore blank line> Get First and Last Char with Linq
char first = temp.First(); char last = temp.Last();Another information
If you have more than one process, do a function for, exemple :
From
if (bracket == '}') openChar = '{'; else if (bracket == ']') openChar = '['; else if (bracket == ')') openChar = '('; else if (bracket == '>') openChar = '<';To
private char bracketControl(char bracket) { switch (bracket) { case '}': return '{'; case ']': return '['; case ')': return '('; case '>': return '<'; default: return bracket; } }and in code, call the function