Question
I am making a basic calculator in c# (console version not an app) and I have to add this use case, I also included my
I am making a basic calculator in c# (console version "not an app") and I have to add this use case, I also included my current code:
Users should be able to use the M character to save the last input or result. Users can save as many numbers as they want. Users can list the saved numbers by using the P character. User can retrieve and continue with any number they want from the list by using Rn where n is any number from the list larger than 0 (e.g. R1 will retrieve the first save element, R2 second, ) Entering invalid number should print: Result does not exist at location. List exists as long as the program runs. Everytime program is restarted the list is empty. Store list in memory.
My Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
public float AddNum(float a, float b)
{
return a + b;
}
public float Subtract(float a, float b)
{
return a - b;
}
public float Multiply(float a, float b)
{
return a * b;
}
public float Divide(float a, float b)
{
String error = "You cannot divide that number to 0, Please check again!";
if (b > 0)
{
return a / b;
}
else
{
return float.Parse(error);
}
}
public float Remainder(float a, float b)
{
a = (int)a;
b = (int)b;
return a % b;
}
public float Exponential(float a, float b)
{
float result = 1;
for (int i = 0; i < b; i++)
{
result = result * a;
}
return result;
}
public float Square(float a)
{
float result = 1;
int i = 0;
while (true)
{
i = i + 1;
result = (a / result + result) / 2;
if (i == a + 1)
{
break;
}
}
return result;
}
static void Main(string[] args)
{
Program calculator = new Program();
float x = 0;
float y = 0;
String opperator = "";
float result = 0;
Console.WriteLine("Keys Descriptions");
Console.WriteLine("+ Sum opperation");
Console.WriteLine("- Subtraction opperation");
Console.WriteLine("/ Divide opperation");
Console.WriteLine("* Multiplication opperation");
Console.WriteLine("% Remainder opperation");
Console.WriteLine("^ Exponential opperation");
Console.WriteLine("s Square root opperation");
Console.WriteLine("= Used for calculation");
Console.WriteLine("q or Q Used for to quit");
Console.WriteLine("You have to enter every input one by one!");
try
{
x = (float)Convert.ToDouble(Console.ReadLine());
}
catch (Exception)
{
Console.WriteLine("Should start with number operations not allowed");
Environment.Exit(0);
}
while (true)
{
opperator = Console.ReadLine();
if (opperator != "=" && opperator.All(char.IsNumber))
{
x = (float)Convert.ToDouble(opperator);
}
else
{
if (opperator == "+")
{
y = (float)Convert.ToDouble(Console.ReadLine());
result = calculator.AddNum(x, y);
x = result;
}
else if (opperator == "-")
{
y = Convert.ToInt32(Console.ReadLine());
result = calculator.Subtract(x, y);
x = result;
}
else if (opperator == "/")
{
y = Convert.ToInt32(Console.ReadLine());
result = calculator.Divide(x, y);
x = result;
}
else if (opperator == "*")
{
y = Convert.ToInt32(Console.ReadLine());
result = calculator.Multiply(x, y);
x = result;
}
else if (opperator == "%")
{
y = Convert.ToInt32(Console.ReadLine());
result = calculator.Remainder(x, y);
x = result;
}
else if (opperator == "^")
{
y = Convert.ToInt32(Console.ReadLine());
result = calculator.Exponential(x, y);
x = result;
}
else if (opperator == "s")
{
opperator = Console.ReadLine();
if (opperator == "=")
{
result = calculator.Square(x);
Console.WriteLine(result);
x = result;
}
}
else if (opperator == "=")
{
Console.WriteLine(result);
x = result;
}
else if (opperator == "q" || opperator == "Q")
{
Environment.Exit(0);
}
}
}
Console.ReadKey();
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started