-1

我的英语不是很好,但是,我希望你能理解我。我知道在 C++ 中将字符串转换为代码是不可能的,但我只是看不到另一种解决问题的方法。

我有一系列结构。你可以说它是一个数据库。任务是用户应该提出组合请求。在控制台中,用户选择结构的两个参数并发出组合请求。像这样的东西

        cout<<"\nВыберите первый параметр для поиска:" // Choose first parameter
            <<"\n1. processor"
            <<"\n2. videocard"                    
            <<"\n3. display"
            <<"\n4. RAM"
            <<"\n5. size"<<endl;
        int first;
        cin>>first;
        cout<<"\nВыберите второй параметр для поиска:" // Choose second parameter
            <<"\n1. processor"
            <<"\n2. videocard"
            <<"\n3. display"
            <<"\n4. RAM"
            <<"\n5. size"<<endl;    
        int second;
        cin>>second;

        cout<<"enter searchkey for first value: "
        string search1;
        cin>>search1;
        cout<<"enter searchkey for second value: "
        string search2;
        cin>>search2;

        string parameters[ 5 ] = { "processor", "videocard", "display", "RAM", "size" };
        for ( i = 0; i < size; i++ ) // And that's where it all goes wrong.
        if ( arrayOfStructs.parameters[ first ] == search1 && arrayOfStructs.parameters[ second ] == search2 )
               cout<<"found a match"<<endl;

我知道为什么代码不起作用。我真的很确定存在一个看起来与我相似的解决方案。我的“解决方案”看起来像枚举,但在这种情况下枚举是不合适的。如果您知道解决方案,请在下面写下。

我的程序的完整代码

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;

struct computer
{
    string processor;
    string videocard;
    string display;
    string RAM;
    string size;

    string getFieldValue( int );
};

string computer::getFieldValue( int fieldNumber )
{
    stringstream str;

    switch (fieldNumber) 
    {
        case 1: str<<this->processor;
        case 2: str<<this->videocard;
        case 3: str<<this->display;
        case 4: str<<this->RAM;
        case 5: str<<this->size;
    }

    return str.str();
}

void coutAndWrite( computer aStruct, ofstream &aFile );

int main()
{
    setlocale( 0, "" );
    computer sklad[ 30 ];
    computer temp;
    int i = 0, j;

    ifstream fromFile("structury.txt");
    while ( !fromFile.eof() )
    {
        fromFile>>sklad[ i ].processor
                >>sklad[ i ].videocard
                >>sklad[ i ].display
                >>sklad[ i ].RAM
                >>sklad[ i ].size;
        i++;
    }
    fromFile.close();
    ofstream rezultaty("rezultaty.txt");

    for ( i = 0; i < 30; i++ )
            for ( j = 0; j < 29; j++ )
                if ( sklad[ j + 1 ].processor[ 0 ] < sklad[ j ].processor[ 0 ] )
                {
                    temp = sklad[ j + 1 ];
                    sklad[ j + 1 ] = sklad[ j ];
                    sklad[ j ] = temp;
                }

    while ( 1 )
    {
        cout<<"\nВыберите тип запроса:"
            <<"\n1. Простой"
            <<"\n2. Составной"
            <<"\n0. Выход из программы\n";
        int prostoiIliSostavnoi;
        cin>>prostoiIliSostavnoi;

        if ( prostoiIliSostavnoi == 0 )
            break;
        if ( prostoiIliSostavnoi == 1 )
        {
            cout<<"\nВыберите параметр для поиска:"
                <<"\n1. processor"
                <<"\n2. videocard"
                <<"\n3. display"
                <<"\n4. RAM"
                <<"\n5. size"<<endl;
            int parametr;
            cin>>parametr;

            cout<<"Введите ключ поиска: ";
            string poisk;
            cin>>poisk;
            cout<<"Результаты поиска: ";
            for ( i = 0; i < 30; i++ )
                if ( sklad[ i ].getFieldValue( parametr ) == poisk )
                    coutAndWrite( sklad[ i ], rezultaty );
        }

    }

    system("pause");
}

void coutAndWrite( computer aStruct, ofstream &aFile )
{
    cout<<"\nprocessor: "<<aStruct.processor
        <<"\nvideocard: "<<aStruct.videocard
        <<"\ndisplay: "<<aStruct.display
        <<"\nRAM: "<<aStruct.RAM
        <<"\nsize: "<<aStruct.size<<endl<<endl;

    aFile<<setw( 15 )<<aStruct.processor
         <<setw( 15 )<<aStruct.videocard
         <<setw( 20 )<<aStruct.display
         <<setw( 10 )<<aStruct.RAM
         <<setw( 10 )<<aStruct.size<<endl;
}
4

1 回答 1

3

Break it into pieces. Let's ignore the loop and the double search, and focus on the core problem: getting a field's value when you have its field number (or name):

string Computer::getFieldValue(int fieldNumber)
{
    stringstream str;

    switch (fieldNumber) {
        case 1: str << this->processor; break;
        case 2: str << this->videocard; break;
        case 3: str << this->display;   break;
        case 4: str << this->RAM;       break;
        case 5: str << this->size;      break;
    }

    return str.str();
}

Given this helper function, it's now possible to write the checking code.

for (int i = 0; i < size; ++i) {
    if (computers[i].getFieldValue(first)  == search1 &&
        computers[i].getFieldValue(second) == search2)
    {
        cout << "found a match" << endl;
    }
}
于 2013-07-08T22:24:38.040 回答