0

我有一个简单的结构:

struct Appartament
{
  char address[50];
  char telephoneNumber[20];
  char view[10];

  double price;
  double distanceFromCenter;

  int roomCount;
};

我有一些记录写在一个文件中。现在,我想从文件中读取所有记录,并且只获取那些 roomCount 小于数字(用户输入)的记录。这很容易,但记录应按价格排序显示。这就是我必须将它们放在一个数组中然后对它们进行排序的方式。

我有一些问题,我相信这是因为我没有很好地处理这些结构。

我尝试了不同的方法:

strcpy(CurrentRecords[index].address,currentRecord.address);
strcpy(CurrentRecords[index].telephoneNumber,currentRecord.telephoneNumber);
strcpy(CurrentRecords[index].view,currentRecord.view);
CurrentRecords[index].price=currentRecord.price;
CurrentRecords[index].distanceFromCenter=currentRecord.distanceFromCenter;
CurrentRecords[index].roomCount=currentRecord.roomCount;

或者

memcpy(CurrentRecords[index],currentRecord,sizeof(Appartament));

CurrentRecords[index]=currentRecord

但没有任何效果...

编辑:这是我的代码 - “没有任何效果”指的是无限循环。

    void AdvanceSearch()
    {
        clrscr();

        Appartament currentRecord;

        fstream currentFile("Records.dat",ios::binary|ios::in);

        if(!currentFile)
        {
            cout<<"Error - the file could not be opened."<<endl;
            return;
        }
        else
        {
            //Array with apartments records
            Appartament CurrentRecords[MaxRecords];

            currentFile.seekg(0L,ios::end);
            long int length=currentFile.tellg();
            currentFile.seekg(0L,ios::beg);

            int isAppartamentFound=0;

            if(length==0)
            {
                cout<<"The file is empty."<<endl;
                return;
            }
            else
            {
                int userRoomCount;

                do
                {
                    clrscr();
                    cout<<"Enter apartment room count - ";
                    cin>>userRoomCount;
                }while(userRoomCount<0);

                clrscr();

                cout<<endl<<"Apartments with "<<userRoomCount<<" rooms order by price:";

                currentFile.read((char*)(&currentRecord),sizeof(Appartament));

                int index=0;

                while(!currentFile.eof())
                {
                    if(currentRecord.roomCount==userRoomCount)
                    {

                         /*
                        strcpy(CurrentRecords[index].address,currentRecord.address);
                        strcpy(CurrentRecords[index].telephoneNumber,currentRecord.telephoneNumber);
                        strcpy(CurrentRecords[index].view,currentRecord.view);
                        CurrentRecords[index].price=currentRecord.price;
                        CurrentRecords[index].distanceFromCenter=currentRecord.distanceFromCenter;
                        CurrentRecords[index].roomCount=currentRecord.roomCount;
                         */

                         memcpy(CurrentRecords[index],currentRecord,sizeof(Appartament));

                        //CurrentRecords[index]=currentRecord;
                        index++;
                        isAppartamentFound=1;
                    }

                    currentFile.read((char*)(&currentRecord),sizeof(Appartament));
                }

                currentFile.close();
            }

            if(isAppartamentFound==0)
            {
                cout<<endl<<"There are no matches!"<<endl;
            }
            else
            {
                //If only one apartment is found
                if(sizeof(CurrentRecords)/sizeof(Appartament)==1)
                {
                    cout<<endl;
                    ShowRecord(currentRecord);

                }
                else
                {
                    //Sort the records

                    Appartament tempApartament;

                    int isChangeMade=1;

                    while(isChangeMade==1)
                    {
                        isChangeMade=0;

                        for(int index=0;index<(sizeof(CurrentRecords)/sizeof(Appartament))-1.0;index++)
                        {
                            if(CurrentRecords[index].price>CurrentRecords[index+1].price)
                            {
                                isChangeMade=1;

                                CopyApartament(tempApartament,CurrentRecords[index]);
                                CopyApartament(CurrentRecords[index],CurrentRecords[index+1]);
                                CopyApartament(CurrentRecords[index+1],tempApartament);
                            }
                        }
                    }

                    for(int index=0;index<sizeof(CurrentRecords)/sizeof(Appartament)-1.0;index++)
                    {
                        ShowRecord(CurrentRecords[index]);
                    }
                }
            }
         }
    }

    void CopyApartament(Appartament RecordOne,Appartament RecordTwo)
    { 
                    /*
        strcpy(RecordOne.address,RecordTwo.address);
        RecordOne.distanceFromCenter=RecordTwo.distanceFromCenter;
        RecordOne.price=RecordTwo.price;
        RecordOne.roomCount=RecordTwo.roomCount;
        strcpy(RecordOne.telephoneNumber,RecordTwo.telephoneNumber);
        strcpy(RecordOne.view,RecordTwo.view);
                    */
                    RecordOne=RecordTwo;

    }

注意:我认为我的问题是复制,因为我不知道该怎么做。

4

2 回答 2

4
CurrentRecords[index]=currentRecord;

这复制了整个结构,并且应该按预期工作。

你确定你排序正确吗?

于 2012-05-16T18:09:34.790 回答
0

我看不出有什么特别好的理由在任何地方显式复制数据。我想我会做这样的事情:

std::vector<Apartment> get_apts(int max_rooms) { 

    std::vector<Apartment> apts;

    std::copy_if(file.begin(), file.end(), std::back_inserter(apts),
                 [max_rooms](Apartment const &a) { 
                     return a.roomCount < max_rooms; 
                 });

    std::sort(apts.begin(), apts.end(), 
              [](Apartment const &a, Apartment const &b) { 
                  return a.price < b.price); 
              });

    return apts;
}
于 2012-05-16T19:13:25.317 回答