1

这是我被赋予的任务

你的任务是创建一个名为 equation 的类,它具有数据成员 a、b 和 c,它们是二次方程的系数。该类将有另外两个数据成员,即 proot 和 nroot,它们代表方程的正根和负根。假设变量 a、b 和 c 是整数。其中 proroot 和 nroot 是浮点数。- 使用空构造函数构造类对象。- 然后设计一个友元函数来确定方程的根和nroot。- 创建另一个友元函数,它将显示 proot 和 nroot 的值。

我有一些问题

  • 我试图将“a”声明为整数并取其平方根,它给出了一个错误,提示“多个 sqrt 实例与参数列表匹配”。当我将“a”声明为双精度并类型转换为整数时,同样的事情。为什么会这样?

  • 输出应该是 -1 -1.5 但我的输出完全不同。我究竟做错了什么?

  • 我的教授告诉我,要成为一个类的函数朋友,我们必须在类中编写它的原型。原型不包括“&”,但如果我不写它,程序就不能工作

在此处输入代码

#include <iostream>
#include <math.h>
using namespace std;
class Equation 
{    
    friend void Roots (Equation & );
    friend void Display (Equation &);
    int a;
    int b;
    int c;
    float proot;
    float nroot;
public:
    Equation ()
    {
        a=0;
        b=0;
        c=0;
        proot=0;
        nroot=0;
    }
    Equation (int _a, int _b, int _c)
    {
        a=_a;
        b=_b;
        c=_c;
    }

};
void Roots (Equation &obj1)
{
    double a;
    int determinant;
    a=(obj1.b^2)-(4*obj1.a * obj1.c);
    if (a>-1)
        determinant=int(sqrt(a));
    else
    {
        cout<<"Determinant returns an imaginary number; solution not possible\n";
        exit (0);
    }
    obj1.proot= (-obj1.b + determinant)/2*obj1.a;
    obj1.nroot= (-obj1.b - determinant)/2*obj1.a;
}
void Display (Equation &obj1)
{    
    cout<<"Value of positive root : "<<obj1.proot<<endl;
    cout<<"Value of negative root : "<<obj1.nroot<<endl;
}
void main ()
{
    int a,b,c;
    cout<<"Calculate Quadratic Equation"<<endl<<"Enter A : ";
    cin>>a;
    cout<<"Enter B : ";
    cin>>b;
    cout<<"Enter C  ";
    cin>>c;
    Equation obj(a,b,c);
    Display (obj);
    Display (obj);

}
4

4 回答 4

4
a=(obj1.b^2)-(4*obj1.a * obj1.c);

C++ 中的^运算符是按位异或,因此obj1.b^2部分计算obj1.b和 位模式的异或000...10。这绝对不是你想要的。

C++ 中的幂函数pow,因此您可以pow(obj1.b, 2)通过.cmathmath.h

编辑:你也从不打电话Roots()来计算任何东西:

Equation obj(a,b,c);
Display (obj);

在这里,您构建您的方程式并立即尝试显示其结果,而无需先调用Roots(obj). 这至少会计算出一个答案,但仍然是错误的,因为您的计算似乎有误。

2 * obj1.a您还需要在计算中使用括号。尝试使用和不使用它们,看看有什么不同!

Calculate Quadratic Equation
Enter A : 10
Enter B : 10
Enter C  2
Value of positive root : -0.276393
Value of negative root : -0.723607

是正确的。尽管您显然期望这两个词根具有不同的符号,但情况并非如此。

于 2013-10-29T13:32:15.010 回答
2

代替

obj1.b^2

尝试

pow(obj1.b, 2)

^ 正在执行 XOR 操作 - 可能不是您的想法。

您似乎也从未调用过您的 Roots 函数。你需要类似的东西:

Equation obj(a,b,c);
Roots(obj);
Display (obj);

您将返回大数字,因为当您调用 Equation (int _a, int _b, int _c) 构造函数时, proot 和 nroot 未初始化。然后你在最后返回它们,因为你从不调用 Roots 函数。

于 2013-10-29T13:30:41.953 回答
1

作为如何创建有用的朋友函数的示例:

#include <iostream>
#include <cmath>

class Equation 
{   
public:
    Equation () : m_a(0), m_b(0), m_c(0), m_proot(0), m_nroot(0) 
    {
    }

    Equation (int a, int b, int c) : m_a(a), m_b(b), m_c(c), m_proot(0, false), m_nroot(0, false)
    {
    }

private: 
    // these would be better as floats or doubles, but your requirement appears to want them to be ints.  You'll need to cast them when doing division operations.
    int m_a;
    int m_b;
    int m_c;
    // std::optional would be more useful, but it was removed from the last C++14 draft
    std::pair<float, bool> m_proot;
    std::pair<float, bool> m_nroot;

    void Calculate()
    {
        // do your actual calculations here
        // note that you will need to set the m_proot.second and m_nroot.second values to true if they are valid
        // also note that ^ is not a power operation; you need to use std::pow for that
    }

    // this friend function is useful
    friend std::ostream& operator<<(std::ostream&, const Equation&);
    // this one is created just to meet the requirements of the assignment
    friend void CalculateRoots(Equation&);
};

std::ostream& operator<<(std::ostream& os, const Equation& e)
{
    std::cout << "Roots of (" << e.m_a << ")x^2 + (" << e.m_b << ")x + " << e.m_c << ": "
    if (m_nroot.second || m_proot.second)
    {
        std::cout << "(";
        if (m_nroot.second)
        {
            std::cout << m_nroot.first << ", ";
        }

        if (m_proot.second)
        {
            std::cout << m_proot.first;
        }
        std::cout << ")" << std::endl;
    }
    else
    {
        std::cout << "No real roots" << std::endl;
    }
    return os;
}

// must be friend function to call private member function Calculate
void CalculateRoots(Equation &obj1)
{
    obj1.Calculate();
}

// does not need to be a friend function - using operator<< overload (which is a friend function itself)
void DisplayRoots(Equation &obj1)
{    
    std::cout << obj1;
}

int main ()
{
    int a,b,c;
    cout<<"Calculate Quadratic Equation"<<endl<<"Enter A : ";
    cin>>a;
    cout<<"Enter B : ";
    cin>>b;
    cout<<"Enter C  ";
    cin>>c;
    Equation obj(a,b,c);
    CalculateRoots(obj);
    DisplayRoots(obj);
}

这为您提供了作业描述中所需的朋友功能,同时至少使您更接近更好的设计。

于 2013-10-29T15:39:35.477 回答
0

好的,伙计们,我将变量的数据类型从

int a;
int b;
int c;

float a;
float b;
float c;

因为它给出了有关转换和数据丢失的警告,并且还更改了计算 proot 和 nroot 的语句

 obj1.proot= (-obj1.b + determinant)/2*obj1.a;
obj1.nroot= (-obj1.b - determinant)/2*obj1.a;

 obj1.proot= (-obj1.b + determinant)/(2*obj1.a);
obj1.nroot= (-obj1.b - determinant)/(2*obj1.a);

现在它按预期工作......产生正确的结果!:)

于 2013-10-29T14:05:31.167 回答