The following is a program for a 4-function calculator using the idea of assigen
ID: 3544378 • Letter: T
Question
The following is a program for a 4-function calculator using the idea of assigend variables and functions:
--- This week we need to adjust the program shown above so it becomes more efficient. Most calculators today prompt users for a full statement to calculate rather that one input at a time, for example if the user input 1 + 1 the program will perform the summation and display "2" as a result. Adjust your program so it accepts a complete mathematical input, FOLLOWING these instructions:
You MUST use the concept of Array AND the split function in this project, example code:
{
string s = " David Jones 1990 "
string [ ] split = s. split (' ');
console.writeline ( split [0]);
console.readkey( );
}
From the idea of the above example, let your program accept a mathematical string (e.g. x + y) and perform the operation. For the split function, you may use an index of your choice. In this lab ASSUME that the user will always use spaces between every character (e.g. x + y and not x+y).
Also, if the user does not enter any numbers and hits enter, your program should terminate.
-- Hints: You may only need to change few things in the shown program above, and those changes are most likely to be in the main function (// main function). Your four mathematical functions should remain unchanged. Your resulting program should look like this:
Enter your : 1 + 2 (// <-- user input)
1 + 2 = 3 (// <-- program output)
Enter equation: (// <-- repeat until user enter a blank equation [no equation])
----
I'm stuck with this!! If anyone knows how to do it, your help would be greatly appericited!!!
Explanation / Answer
// Try verifing the program using 45 + 56 or 67 / 34 and so on as you wish. Make sure there is spaces between
//operator and numbers on two sides. This is mentioned in your assignment. If you just press enter
//without entering expression the program terminates as requested in program.
using System;
namespace Program
{
class program
{
static void Main(string[] args)
{
string val;
double num1, num2;
while (true)
{
Console.Write("Enter mathematical input: ");
val = Console.ReadLine();
if (val.Equals(""))
{
Console.WriteLine("the user did not enter any number, terminating..");
Console.ReadKey();
Environment.Exit(0);
}
String[] expression = val.Split();
num1 = double.Parse(expression[0]);
num2 = double.Parse(expression[2]);
String opr = expression[1];
if(opr=="+")
{
Console.WriteLine(num1 + " + " + num2 + " = " + add(num1, num2));
}
else if(opr=="-")
{
Console.WriteLine(num1 + " - " + num2 + " = " + sub(num1, num2));
}
else if(opr=="*")
{
Console.WriteLine(num1 + " * " + num2 + " = " + multiply(num1, num2));
}
else if(opr=="/")
{
Console.WriteLine(num1 + " / " + num2 + " = " + division(num1, num2));
}
else
{
Console.WriteLine("You have entered an invalid operation!");
}
}
}
public static double add(double x, double y)
{
double z = x + y;
return z;
}
public static double sub(double x, double y)
{
double z = x - y;
return z;
}
public static double multiply(double x, double y)
{
double z = x * y;
return z;
}
public static double division(double x, double y)
{
double z = x / y;
return z;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.