Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

(I posted this question 2 times and this is the 3rd time, different experts answered by just copying a calculator from an internet I believe.

(I posted this question 2 times and this is the 3rd time, different experts answered by just copying a calculator from an internet I believe. I don't need a calculator. Please read it carefully, I am paying for this and getting false results from the experts.) I am making a basic calculator in c# "I have my code and I included!!!"(console version "not an app") and I have to add this use case to my 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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

1. Identify three approaches to culture.

Answered: 1 week ago

Question

2. Define communication.

Answered: 1 week ago