I would appreciate if someone here can help me with the following:
I need to write a small program in C# that does the following:
1. Collect from a user 2 numbers (different bases - Bin, Oct, Dec or Hex) and store them in integer
2. Then collect the operation from the user (+, -, *, /)
3. Then collect in what base the result will be presented (Bin, Oct, Dec or Hex)
4. And then print the result
No graphics is required here. Something to run from the command line.
I know it is simple but could not figure how to do it,
I would appreciate your help here,
Thanks,
Dana.
Loading
GuyavPosted Jul 30, 2007, 9:18 AM
Thanks so much,
Dana.
SamPosted Jul 30, 2007, 8:43 AM
Try This!
static
void Main(string[] args){
/*for the sake of simplicity i haven't icluded error checking
therefore exceptions are not handled!
Also -ve numbers in bases 2, 8, 16 are two's compliment repressentations
*/
Console.WriteLine("Number 1' s Base"); int InputBaseA = int.Parse(Console.ReadLine()); Console.WriteLine("Number 1"); int a = Convert.ToInt32(Console.ReadLine(), InputBaseA); // exception if InputbaseA != 2, 8, 10, 16 Console.WriteLine("Number 2' s Base"); int InputBaseB = int.Parse(Console.ReadLine()); Console.WriteLine("Number 2"); int b = Convert.ToInt32(Console.ReadLine(), InputBaseB);// exception if InputbaseB != 2, 8, 10, 16 Console.WriteLine("Operator - +,-,* or /");
char Operator = char.Parse(Console.ReadLine()); int Result = 0;
switch (Operator){
case '+':Result = a + b;
break; case '-':Result = a - b;
break; case '*':Result = a * b;
break; case '/':Result = a / b;
break;}
Console.WriteLine("OutputBase"); int OutputBase = int.Parse(Console.ReadLine()); Console.WriteLine("Answer is {0}", Convert.ToString(Result, OutputBase)); // exception if OutputBase != 2, 8, 10, 16 Console.ReadKey();}
Hope this helps,
Sam