C# Console Programming
|
Erm i kinda have a problem with my C# Programming.. I know this is kind of a weird place to ask this question but.. I hope someone can help me though.. T_T Well aniwae i have this assignment im suppose to do.. The original program was this: //////////\\\\\\\\\\ Console.WriteLine("Enter a line arithmetic expression"); string str = Console.ReadLine().Trim(); char[] chA = new char[] {' '}; char op; string[] strA = str.Split(chA); double num = Convert.ToDouble(strA[0].Trim()); int i = 1; while (i < strA.Length) { op = Convert.ToChar(strA[i].Trim()); num = num + Convert.ToDouble(strA[i+1].Trim()); i+=2; } Console.WriteLine("Result is {0:f3}", num); \\\\\\\\\\////////// Im suppose to modify this program so that these expressions might work: (Basically the need for exactly one space in between the numerals is no longer needed) -> 1.2+2.3 - 4.5 -> +1.2+ 2.3 - 4.5 -> -1.2 +2.3 - 4.5 So far i've done this: //////////\\\\\\\\\\ Console.WriteLine("Enter a line arithmetic expression"); string str = Console.ReadLine().Trim((Char)43); char[] chA = new char[] {'+'}; char op; string[] strA = str.Split(chA); double num = Convert.ToDouble(strA[0].Trim()); int i = 1; while (i < strA.Length) { num = num + Convert.ToDouble(strA[i].Trim()); i += 1; } Console.WriteLine("Result is {0:f3}", num); \\\\\\\\\\////////// With the program i modified, i was able to take out the need for that single space and also allow the minus sign up front. However, i couldnt do the -4.5 at the back!! I tried to do some other stuff.. But lol when the -4.5 worked, the single space couldnt! XD It would be great if someone could help me out.. Thanks in advance! |
Re: C# Console Programming
|
i also learn c programming.but for now,i cant really help u.sumimasen T-T this reminds me.next sem we are taking software developement.huhu so on the very 1st day of next sem,we're gonna have a test on programming.T-T y cnt they let us enjoy our holidays???huhu i'll try to figure that out =.= |
Re: C# Console Programming
|
the code run without spaces right then try making a function that removes the extra space before you tokenize the string. For example b4 you tokenize removeSpace(string param) :-);
http://czetsuya-travel.blogspot.com
|
Re: C# Console Programming
|
hey.. lol.. i got the answer.. after 8hrs of figuring how to solve for 8 days long, i got it!! aniwae thanks to the ppl who replied ^_^ and if anyone was wondering, i used Substring to eliminate those annoying extra spaces XD |