我正在尝试制作一个简单的基于控制台的程序,它可以解决二次方程。如果判别式为负数,我仍然没有想出如何计算答案,所以我只是将一条消息显示为占位符。那么任何人都可以解释如何做到这一点?我会发布整个程序的任何建议。 感谢。
正确使用代码(谢谢,Gunther Fox):
if(discriminant < 0)
{
double x1 = -b/(2*a);
discriminant = -discriminant ;
double x2 = Math.Sqrt(discriminant)/(2*a);
double x3 = -Math.Sqrt(discriminant)/(2*a);
Console.WriteLine(x1.ToString() + " + " + x2.ToString() + " * i ");
Console.WriteLine(x1.ToString() + " + " + x3.ToString() + " * i ");
Console.ReadLine();
}
旧代码:
class Program
{
static void Main(string[] args)
{
int choice;
double ans1;
double ans2;
Console.WriteLine("~~~~~~~~ TERMINAL CALCULATOR ~~~~~~~~");
Console.WriteLine("1. Standard Calculator\n2. Quadratic Equation Solver\n3. Simple Interest Calculator");
choice = int.Parse(Console.ReadLine());
try
{
if (choice == 2)
{
Console.Write("Enter a value for 'a': ");
double a = double.Parse(Console.ReadLine());
Console.Write("Enter a value for 'b': ");
double b = double.Parse(Console.ReadLine());
Console.Write("Enter a value for 'c': ");
double c = double.Parse(Console.ReadLine());
//Quadratic Formula: x = (-b +- sqrt(b^2 - 4ac)) / 2a
//Solve disriminant: (b*b) - (4*a*c)
double discriminant = (b * b) - (4 * a * c);
if (discriminant < 0)
{
Console.WriteLine("No real solutions");
Console.ReadLine();
}
else
{
double sqrt = Math.Sqrt(discriminant);
ans1 = (-b + sqrt) / (2 * a);
ans2 = (-b - sqrt) / (2 * a);
Console.WriteLine("{" + ans1 + " , " + ans2 + "}");
Console.ReadLine();
}
}
if (choice == 1)
{
Console.Write("Enter first number: ");
double num1 = double.Parse(Console.ReadLine());
Console.Write("Enter second number (Enter 0 if you want to use sqrt): ");
double num2 = double.Parse(Console.ReadLine());
//Prompt user to choose an operation
Console.WriteLine("Choose a math operator:\n1. +\n2. -\n3. x\n4. /\n5. ^\n6. Square root");
int mathOpr = int.Parse(Console.ReadLine());
if (mathOpr == 1)
{
double answer = num1 + num2;
Console.WriteLine("\n\n" + answer);
Console.ReadLine();
}
if (mathOpr == 2)
{
double answer = num1 - num2;
Console.WriteLine("\n\n" + answer);
Console.ReadLine();
}
if (mathOpr == 3)
{
double answer = num1 * num2;
Console.WriteLine("\n\n" + answer);
Console.ReadLine();
}
if (mathOpr == 4)
{
double answer = num1 / num2;
Console.WriteLine("\n\n" + answer);
Console.ReadLine();
}
if (mathOpr == 5)
{
double answer = Math.Pow(num1, num2);
Console.WriteLine("\n\n" + answer);
Console.ReadLine();
}
if (mathOpr == 6)
{
if (num1 < 0)
{
Console.WriteLine("\n\nNo real solutions");
}
else
{
double answer = Math.Sqrt(num1);
Console.WriteLine("\n\n" + answer);
}
}
else
{
Console.WriteLine("That is not a valid option, idiot.");
Console.ReadLine();
}
}
if (choice == 3)
{
Console.WriteLine("Enter initial amount: ");
double initAmount = double.Parse(Console.ReadLine());
Console.WriteLine("Enter interest rate (ex. 6% = .06): ");
double rate = double.Parse(Console.ReadLine());
Console.WriteLine("Enter time range (Year, Month, Week, etc.): ");
string timeRange = Console.ReadLine();
Console.WriteLine("Enter amount of " + timeRange.ToLower() + "s: ");
int time = int.Parse(Console.ReadLine());
for (int time2 = 1; time2 <= time; time2++)
{
double totalAmount = initAmount * Math.Pow(1 + rate, time2);
Console.WriteLine("\n" + timeRange + " " + time2 + " ---------- " + totalAmount);
}
Console.ReadLine();
}
}
catch
{
Console.WriteLine("That is not a valid option.");
}
}
}
答案 0 :(得分:3)
如果判别式是否定的,你可以只是翻转判别式的符号,进行根计算,然后将i加到结果的末尾吗?
答案 1 :(得分:3)
翻转辨别并乘以i:
double discriminate = (b*b)-(4*a*c);
if(discriminate < 0){
double x1 = -b/(2*a);
discriminate = -discriminate ;
double x2 = Math.Sqrt(discriminate)/(2*a);
double x3 = -Math.Sqrt(discriminate)/(2*a);
}
Console.WriteLine(x1.ToString() + " + " + x2.ToString() + " * i ");
Console.WriteLine(x1.ToString() + " + " + x3.ToString() + " * i ");