Калькулятор C#: как сохранить ответ предыдущего расчета для повторного использования? [закрыто]C#

Место общения программистов C#
Ответить
Anonymous
 Калькулятор C#: как сохранить ответ предыдущего расчета для повторного использования? [закрыто]

Сообщение Anonymous »

Я хочу иметь возможность сохранить ответ последнего расчета, например. 2+4=6, а затем применить к ответу еще один расчет? например ans*2=12
namespace calculator2
{
class KPcalculator
{
static void Main(string[] args)
{
// Since i want the calculator to be able to restart I enclose the program in a while loop
ConsoleKeyInfo keyInfo;
bool loop = true;
while (loop)
{

// Declaring my variables
double num1, num2;
string operation;

// Asking the user for the operation, to call a function to perform that operation
Console.Write("\nPlease enter an operation you wish to perform (+, -, /, *, ^, ^1/2, !, f-1, log, rad): ");
operation = Console.ReadLine();

// Prompting the input of the first number
Console.Write("Please enter your first number: ");
num1 = Convert.ToDouble(Console.ReadLine());

// Prompting the input of the second number
Console.Write("Please enter your second number (if no other number is needed please press 0): ");
num2 = Convert.ToDouble(Console.ReadLine());

// Using if statements to decide which function to call based on the operation, as well as an error message for an invalid value
if (operation == "+")
{
Console.WriteLine(Sum(num1, num2));
}
else if (operation == "-")
{
Console.WriteLine(Minus(num1, num2));
}
else if (operation == "/")
{
Console.WriteLine(Divide(num1, num2));
}
else if (operation == "*")
{
Console.WriteLine(Multi(num1, num2));
}
else if (operation == "^")
{
Console.WriteLine(ToPower(num1, num2));
}

else if (operation == "^1/2")
{
Console.WriteLine(Sqroot(num1));
}
else if (operation == "!")
{
Console.WriteLine(Factorial(num1));
}
else if (operation == "f-1")
{
Console.WriteLine(ToInverse(num1));
}
else if (operation == "log")
{
Console.WriteLine(ToLog(num1));
}
else if (operation == "rad")
{
Console.WriteLine(ToRadian(num1));
}
else
{
Console.WriteLine("Invalid operation");
}

// Function for addition (Sum)
static double Sum(double num1, double num2)
{
double resultofSum = num1 + num2;
return resultofSum;
}

// Function for subtraction (Minus)
static double Minus(double num1, double num2)
{
double resultofMinus = num1 - num2;
return resultofMinus;
}

// Function for division (Divide)
static double Divide(double num1, double num2)
{
double resultofDivide = num1 / num2;
return resultofDivide;
}

// Function for multiplication (Multi)
static double Multi(double num1, double num2)
{
double resultofMulti = num1 * num2;
return resultofMulti;
}

// Function for raising x (num1) to the power of y (num2)
static double ToPower(double num1, double num2)
{
double resultofToPower = Math.Pow(num1, num2);
return resultofToPower;

}

// Function for square root of x (num1)
static double Sqroot(double num1)
{
double resultofSqroot = Math.Sqrt(num1);
return resultofSqroot;

}

// Function for finding factorial of x (num1),
static double Factorial(double num1)
{
double factorial = 1;

if (num1 < 0)
{
Console.WriteLine("Error: Can't find factorial of a negative number");
return 0;
}

else if (num1

Подробнее здесь: https://stackoverflow.com/questions/649 ... tion-to-be
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C#»