Click here to Skip to main content
15,893,594 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
I have below code for division. I can't divide first output with second output. Please help.

first Result is = 1.5
Second Result is = 0.5

Total Result will be = 3 (first Result / Second Result)

What I have tried:

string mExp = "3/2/1/2";
           string[] strNum = (Regex.Split(mExp, @"\s*[+/*-]\s*"));


           List<string> stringList = mExp.Split('/').ToList();

           int i;
           for (i = 0; i < stringList.Count - 1;  i++)
           {
               string a = stringList[i];
               string b = stringList[i + 1];
               decimal c = int.Parse(a);
               decimal d = int.Parse(b);
               decimal e = c / d;
               Console.WriteLine(e);
               i++;
               Console.ReadLine();
           }
Posted
Updated 28-Feb-22 20:54pm
Comments
CPallini 1-Mar-22 2:34am    
Why 3/2/1/2 should produce 3? Shouldn't instead produce 0.75?
Do you mean (3/2)/(1/2)?
OriginalGriff 1-Mar-22 2:55am    
He's trying to create a fraction calculator, he's been on it for a week or so.
Trouble is, he doesn't really want to think too hard, or listen to what we tell him ... :sigh:
HelalCtg 1-Mar-22 4:33am    
Hi OriginalGriff, I am trying to implement that fraction in the way of string process. Sometimes getting wrong result. If ok in console application, that will be implemented in Windows Form. Thanks.
HelalCtg 1-Mar-22 2:49am    
Yes, it will be (3/2)/(1/2). Thanks.
Chris Copeland 1-Mar-22 5:01am    
Take a look at my Math Equation Parsing using Call Stacks article.

I explained all this to you 5 days ago: Fraction calculator C# windows form[^]

It hasn't changed. You need to implement operator precedence and brackets to get teh result you want.
3 / 2 / 1 / 2
is not the same thing as
3 / 2
_____
1 / 2
Or
(3 / 2) / (1 / 2)
Try it on a "regular calculator" and you'll see. Without the brackets, the divide operators have equal precedence, so so are evaluated like this:
((3 / 2) / 1) / 2
 
Share this answer
 
No need to declare extra variables and inside the loops. Try the following code to divide you first and second result:
string mExp = "3/2/1/2";
string[] strNum = (Regex.Split(mExp, @"\s*[+/*-]\s*"));
List<string> stringList = mExp.Split('/').ToList();
int i;
decimal c, d, e = 0;
for (i = 0; i < stringList.Count - 1; i++)
{
   c = int.Parse(stringList[i]);
   d = int.Parse(stringList[i + 1]);
   if (i == 0)
       e = c / d;
   else
   {
       e /= c / d;
   }
   i++;
}
Console.WriteLine(e);
Console.ReadLine();
 
Share this answer
 
Comments
HelalCtg 1-Mar-22 4:22am    
Hi Imran, it is working fine with above code. If there is no value in 4th position or so on (example "3/2/9" --> after 9 no value), how can i handle this with [i+1] ? Please..

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