0

我用 C++ 编写了一段代码,然后决定将其更改为面向对象,并且我的所有函数都可以正常工作,而不是 qsort。我查过它与重载运算符有关,但我无法弄清楚如何做到这一点。有人可以帮忙吗?

#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;

class OpAmps {
private:
    char Name[20]; 
    unsigned int PinCount; 
    double SlewRate; 
public:
    void Enter(OpAmps&, unsigned long&);
    void Save(const OpAmps*, unsigned long);
    void Load(OpAmps*, unsigned long&);
    void Sort(OpAmps*, unsigned long);
    int SortName(const void*, const void*);
    int SortSlewRate(const void*, const void*);
    void Display(const OpAmps*, unsigned long);
};

#define DATABASE_MAX 10
#define DATABASE_FILENAME "database.txt"

int main()
{
    OpAmps OpAmp[DATABASE_MAX]; 
    OpAmps Menu;
    unsigned long database_length = 0; 
    char UserInput;
    while (1) {
        cout << endl;
        cout << "Op-amp database menu" << endl;
        cout << "--------------------" << endl;
        cout << "1. Enter a new op-amp into the database" << endl;
        cout << "2. Save the database to disk" << endl;
        cout << "3. Load the database from disk" << endl;
        cout << "4. Sort the database" << endl;
        cout << "5. Display the database" << endl;
        cout << "6. Exit from the program" << endl << endl;
        cout << "Enter your option: ";
        cin >> UserInput;
        cout << endl;
        switch(UserInput) {
            case '1':
            Menu.Enter(OpAmp[database_length], database_length);
            break;
            case '2':
            Menu.Save(OpAmp, database_length);
            break;
            case '3':
            Menu.Load(OpAmp, database_length);
            break;
            case '4':
            Menu.Sort(OpAmp, database_length);
            break;
            case '5':
            Menu.Display(OpAmp, database_length);
            break;
            case '6':
            return 0;
            default:
            cout << "Invalid entry" << endl << endl;
            break;
        }
    }
}

void OpAmps::Enter(OpAmps& Op, unsigned long& length)
{
    if (length == DATABASE_MAX) {
        cout << "The database is full" << endl;
    }
    else {
        cout << "Add new data" << endl;
        cout << "------------" << endl;
        cout << "Enter op-amp name: ";
        cin >> Op.Name;
        cout << "Enter number of pins: ";
        cin >> Op.PinCount;
        cout << "Enter slew rate: ";
        cin >> Op.SlewRate;
        cout << endl;
        length++;
    }
}

void OpAmps::Save(const OpAmps* Op, unsigned long length)
{
    fstream output_file;
    output_file.open(DATABASE_FILENAME, ios::out);
    if(output_file.good()) {
        output_file << length << endl << endl;
        for (unsigned long i=0;i<length;i++) {
            output_file << Op[i].Name << endl;
            output_file << Op[i].PinCount << endl;
            output_file << Op[i].SlewRate << endl << endl;
        }
    }
    output_file.close();
}

void OpAmps::Load(OpAmps* Op, unsigned long& length)
{
    fstream input_file; 
    input_file.open(DATABASE_FILENAME, ios::in);
    if(input_file.good()) {
        input_file >> length;
        for (unsigned long i=0;i<length;i++) {
            input_file >> Op[i].Name;
            input_file >> Op[i].PinCount;
            input_file >> Op[i].SlewRate;
        }
    }
    input_file.close();
}

void OpAmps::Sort(OpAmps* Op, unsigned long length)
{
    char UserInput;
    cout << endl;
    cout << "Sorting options" << endl;
    cout << "---------------" << endl;
    cout << "1. To sort by name" << endl;
    cout << "2. To sort by slew rate" << endl;
    cout << "3. No sorting" << endl << endl;
    cout << "Enter your option: ";
    cin >> UserInput;
    cout << endl;
    switch(UserInput) {
        case '1':
        cout<<"sortName"<<endl;
        qsort(Op,length,sizeof(OpAmps),SortName);
        break;
        case '2':
        cout<<"sortslew"<<endl;
        qsort(Op,length,sizeof(OpAmps),SortSlewRate);
        break;
        case '3':
        return;
        default:
        cout << "Invalid entry" << endl << endl;
        break;
    }
}

int SortName(const void *First, const void* Second)
{
    return strcmp(((OpAmps *) First)->Name, ((OpAmps *) Second)->Name);
}

int SortSlewRate (const void *First, const void* Second)
{
    return (int) ((((OpAmps *) First)->SlewRate > ((OpAmps *) Second)->SlewRate)? 1 : -1);
}

void OpAmps::Display(const OpAmps* Op, unsigned long length)
{
    if (length == 0) {
        cout << "No elements in the database" << endl;
    }
    else {
        cout << endl;
        for (unsigned long i=0;i<length;i++) {
            cout << "Name: " << Op[i].Name <<endl;
            cout << "Number of Pins: " << Op[i].PinCount << endl;
            cout << "Slew Rate: " << Op[i].SlewRate << endl;
            cout << endl;
        }
    }
}

干杯家伙:D xx

4

1 回答 1

1

我认为您在这种情况下的问题是您定义了具有相同名称的不同类型的函数。您已对 SortName 和 SortSlewRate 进行了此操作。

一个定义说“SortName”和“SortSlewRate”是 OpAmps 类的成员函数,但是,在您的代码中,SortName 和 SortSlewRate 是全局函数。

qsort 是一个 C 函数,需要函数指针而不是成员函数指针。

由于您的函数旨在成为全局函数,但要访问 OpAmps 类的私有成员,因此您应该将“friend”关键字放在它们前面。

尝试将您的声明更改为此...

class OpAmps {
private:
    char Name[20]; 
    unsigned int PinCount; 
    double SlewRate; 
public:
    void Enter(OpAmps&, unsigned long&);
    void Save(const OpAmps*, unsigned long);
    void Load(OpAmps*, unsigned long&);
    void Sort(OpAmps*, unsigned long);
    void Display(const OpAmps*, unsigned long);

    friend int SortName(const void*, const void*);
    friend int SortSlewRate(const void*, const void*);
};

综上所述,您最好使用 std::sort 而不是 qsort,您将获得类型安全的优势,并且在某些情况下,由于编译器可以为您进行优化,性能甚至更高。

你的定义看起来像这样

class OpAmps {
private:
    char Name[20]; 
    unsigned int PinCount; 
    double SlewRate; 
public:
    void Enter(OpAmps&, unsigned long&);
    void Save(const OpAmps*, unsigned long);
    void Load(OpAmps*, unsigned long&);
    void Sort(OpAmps*, unsigned long);
    void Display(const OpAmps*, unsigned long);

    friend bool SortName(const OpAmps &, const OpAmps &);
    friend bool SortSlewRate(const OpAmps &, const OpAmps &);
};

你会像这样使用“排序”......

    sort(Op, Op + length,SortName);

    sort(Op,Op + length,SortSlewRate);

请注意,排序有点不同,函数返回 true(小于)或 false(等于或大于)而不是 -1(小于)、0(等于)、-1(大于),它们不是t 传递了指针,它们传递了引用。

你会这样定义它们。

bool SortName(const OpAmps &First, const OpAmps &Second)
{
    return strcmp(First.Name, Second.Name) < 0;
}

bool SortSlewRate (const OpAmps &First, const OpAmps &Second)
{
    return First.SlewRate < Second.SlewRate;
}

最后,为了更像 C++,你可以切换你的

char Name[20];

string Name;

这将简化您的 SortName 函数并提供针对讨厌的缓冲区溢出错误的安全性。

bool SortName(const OpAmps &First, const OpAmps &Second)
{
    return First.Name < Second.Name;
}
于 2013-02-01T00:14:52.823 回答