C#计算器:如何存储上次计算的答案以便再次使用



我想能够存储上一次计算的答案,例如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 <= 1)
{
return 1;
}
else
{
for (double i = 1; i <= num1; i++)
{
factorial = factorial * i;
}
Console.WriteLine("{0}! = {1}", num1, factorial);
return factorial;
}
}
// Function for obtaining the inverse of x (1/num1)
static double ToInverse(double num1)
{
double ToInverse = 1 / num1;
return ToInverse;
}
// Function for obtaining the base 10 log of x (num1)
static double ToLog(double num1)
{
double Tolog = Math.Log10(num1);
return Tolog;
}
// Function for converting an angle x (num1) from degrees to radians
static double ToRadian(double num1)
{
double Toradian = (num1 * (Math.PI)) / 180;
return Toradian;
}
// Prompting the user for whether they want to perform another calculation or want to end the program
Console.WriteLine("Do you want to perform another calculation? (Y/N)");
keyInfo = Console.ReadKey();
// The loop ends if N is selected
if (keyInfo.Key == ConsoleKey.N)
{
loop = false;
}
else if (keyInfo.Key == ConsoleKey.Y)
{
loop = true;
}
}
}
}
}

1-在代码中已有的变量声明部分声明另一个变量。

2-Console.WriteLine("{0}! = {1}", num1, factorial);之后将答案保存到新的专用变量中。

3-使用您的专用变量

自杀

根据代码中的要求。

最新更新