我遇到了问题,我不知道如何处理它。我做了一个功能很少的简单计算器。每个选项都工作正常,但在二次方程方面我有问题。当我输入任何数字时,我得到的响应是“方程式的根源是:x = 1.79599e-307。我不知道要处理它 - 你能帮助我吗?我会发布包含我的部分代码问题。感谢您的任何反馈和帮助!
主:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "kalkulator.cpp"
...
using namespace std;
case 10:
cout<<"\nQuadratic Equation \n";
cout<<"\nSelect number \n";
cin>>a;
cout<<"\nSelect number \n";
cin>>b;
cout<<"\nSelect bumber \n";
cin>>c;
int r;
r=equation(a, b, c, x1, x2);
if (r==0) cout << "\n No roots\n" << endl;
else if (r==1) cout << "\n 1 root x = " << x1 << endl;
else if (r==2) cout << "\n 2 roots x1 = " << x1 << " and x2 = " << x2 << endl;
break;
kalkulator.cpp
#include "kalkulator.h"
using namespace std;
...
double equation(double a, double b, double c, double x1, double
x2)
{
double delta=b*b-4*a*c;
if (delta<0.0)
{
return 0;}
if (delta==0.0)
{
x1=-b/(2*a);
return 1;}
else
{
delta=sqrt(delta);
x1==(-b-delta)/(2*a);
x2==(-b+delta)/(2*a);
return 2;}}
kalkulator.h
#ifndef kalkulator_H
#define kalkulator_H
class kalkulator
{
private:
double a;
double b;
double c;
double x1;
double x2;
public:
double equation(double, double, double, double, double);
};
#endif
答案 0 :(得分:0)
如果要通过提供的参数设置x1和x2,则需要通过引用传递它们:
double equation(double, double, double, double&, double&);
和
double equation(double a, double b, double c, double & x1, double & x2)
此外,在代码的最后几行中,“x1 ==”和“x2 ==”应为“x1 =”和“x2 =”。