Back | Reverse | Quick Reply | Post Reply |

C# Console Programming
Link | by Koshiro on 2006-06-17 22:07:55 (edited 2006-06-17 22:10:14)
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!

Photobucket - Video and Image Hosting

Re: C# Console Programming
Link | by Ai-chan on 2006-07-03 09:00:30
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
Link | by czetsuya on 2006-07-07 00:13:52
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
Link | by Koshiro on 2006-07-07 07:44:49
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

Photobucket - Video and Image Hosting

Back | Reverse | Quick Reply | Post Reply |

Copyright 2000-2024 Gendou | Terms of Use | Page loaded in 0.0021 seconds at 2024-05-03 05:51:02