我的英语不是很好,但是,我希望,你会理解我的。 我知道在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;
}
答案 0 :(得分:3)
把它分成几块。让我们忽略循环和双重搜索,并关注核心问题:当你有字段编号(或名称)时获取字段的值:
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();
}
鉴于这个辅助函数,现在可以编写检查代码。
for (int i = 0; i < size; ++i) {
if (computers[i].getFieldValue(first) == search1 &&
computers[i].getFieldValue(second) == search2)
{
cout << "found a match" << endl;
}
}