gpt4 book ai didi

c++ - 我的结构数组打印出垃圾

转载 作者:太空宇宙 更新时间:2023-11-04 11:38:58 26 4
gpt4 key购买 nike

我正在编写一个程序,它使用文件 i/o 接收输入文件并将文件中的数据存储到结构数组中。请记住,我正在向您提供部分代码。不是全部。我知道一切正常,因为一旦我输入数据,我就会打印出所有结构数组。在我尝试对其进行排序之前,正在打印的垃圾发生在函数中。我尝试对其进行排序的那个函数在我的读取文件函数中被调用,我在其中打印了结构数组。在那里,它打印得很完美。当它调用排序函数时,结构数组打印出垃圾。


这是我一直在使用的输入文件的示例:

### building room_number capacity 
SAL 210 30
OHE 100 120
OHE 120 50

### ID prefix course# sect# #minutes #mtgsperweek #students
20001 CSCI 101 01 110 2 40
20002 CSCI 101 02 110 2 60
20003 CSCI 101 03 110 2 100
20004 CSCI 103 01 90 2 50
20005 CSCI 103 02 90 2 50
20006 CSCI 103 03 90 2 75
20007 CSCI 104 01 80 2 50
20008 CSCI 104 02 80 2 50
20009 CSCI 109 01 90 1 25
20010 CSCI 109 02 90 1 25
20011 CSCI 109 03 90 1 25
20012 CSCI 109 04 90 1 25

### ID days_constraint start_constraint end_constraint
20001 MW 1000 1400
20002 MW 1000 1400
20003 TR 1000 1400
20004 TR 0800 1200
20005 TR 0800 1200
20006 TR 0800 1200
20007 MW 0800 1200
20008 MW 0800 1200
20009 M 0800 1200
20010 M 0800 1200
20011 T 0800 1200
20012 T 0800 1200

我有一个函数,我在其中读取文件并使用字符串流将数据输入到结构数组中。我知道程序的那一部分是正确的,因为我随后打印出了结构数组,并且它们都完美地打印出来了。当我在我的 read_File 函数中调用一个名为 sort_ByClassroomSize 的新函数时,问题就出现了,在该函数中,我试图按类容量降序对结构数组进行排序。


结构如下:

 struct Room { 
char building_code[4];
int room_number;
int max_students;
};

我声明一个指针,然后根据用户输入的房间数动态分配。

         struct Room* roomsPtr;

roomsPtr = new struct Room[room_size];

其中 room_size 是一个计数器,用于计算声明房间的行数。

下面是我如何将数据输入到结构中:

   if( !(line[0] == '-' && line[1] == '-') ) {
stringstream ss;
ss << line;
ss >> roomsPtr[i].building_code;
ss >> roomsPtr[i].room_number;
ss >> roomsPtr[i].max_students;
if( ss.fail() ) { //checks format; terminates program if incorrect.
cout << "The file has been formatted incorrectly." << endl;
return;
}

//prints lines in section 1.
cout << roomsPtr[i].building_code << " " << roomsPtr[i].room_number <<
" " << roomsPtr[i].max_students << endl;

问题出在排序函数上:

    void order_ClassroomsBySize(int num_rooms) { //arranges classrooms in descending order 
by classroom size
struct Room temp_value;

//prints the array of structs before arrangement
cout << endl;
cout << "Arranged classrooms in descending order by classroom size: " << endl;
for(int i = 0; i < num_rooms; i++) {
cout << roomsPtr[i].building_code << " " << roomsPtr[i].room_number << " "
<< roomsPtr[i].max_students;
cout << endl;
}
}
//arranges the array of structs in descending order by classroom size
for(int i = 0; i < num_rooms; i++) {
for(int j = 0; j < num_rooms - 1; j++) {
if(roomsPtr[j].max_students < roomsPtr[j+1].max_students) {
temp_value = roomsPtr[j];
roomsPtr[j] = roomsPtr[j+1];
roomsPtr[j+1] = temp_value;
}
}
}

//prints the array of structs after arrangement
cout << endl;
cout << "Arranged classrooms in descending order by classroom size: " << endl;
for(int i = 0; i < num_rooms; i++) {
cout << roomsPtr[i].building_code << " " << roomsPtr[i].room_number << " "
<< roomsPtr[i].max_students;
cout << endl;
}
}

我的程序打印出以下内容:

### building room_number capacity 
SAL 210 30
OHE 100 120
OHE 120 50


### ID prefix course# sect# #minutes #mtgsperweek #students
20001 CSCI 101 01 110 2 40
20002 CSCI 101 02 110 2 60
20003 CSCI 101 03 110 2 100
20004 CSCI 103 01 90 2 50
20005 CSCI 103 02 90 2 50
20006 CSCI 103 03 90 2 75
20007 CSCI 104 01 80 2 50
20008 CSCI 104 02 80 2 50
20009 CSCI 109 01 90 1 25
20010 CSCI 109 02 90 1 25
20011 CSCI 109 03 90 1 25
20012 CSCI 109 04 90 1 25

### ID days_constraint start_contsraint end_constraint
20001 MW 1000 1400
20002 MW 1000 1400
20003 TR 1000 1400
20004 TR 0800 1200
20005 TR 0800 1200
20006 TR 0800 1200
20007 MW 0800 1200
20008 MW 0800 1200
20009 M 0800 1200
20010 M 0800 1200
20011 T 0800 1200
20012 T 0800 1200

Arranged classrooms in descending order by classroom size:
course# sect# #minutes OHE 593851250 1667592992
t# #minutes OHE 1970170221 544433524
OHE 120 50

Arranged classrooms in descending order by classroom size:
course# sect# #minutes OHE 593851250 1667592992
t# #minutes OHE 1970170221 544433524
OHE 120 50

最佳答案

正如许多人评论的那样,您在代码中使用了两种截然不同的范例:您将 C++ 当作 C 来使用。如果您真的在使用 C++,则有更简单的方法来实现这一点。

按照惯例,如果我使用 C++,我会简单地使用运算符重载。

像这样:

class Room {
// not generally a good idea to have public members, but if you must...
public:
std::string building_code;
int room_number;
int max_students;
bool operator == (const Room & other) {
if (max_students != other.max_students)
{
return false;
}
else return room_number == other.room_number && building_code == other.building_code;
}
bool operator < (const Room & other) {
// returning the oposite to ensure descending order.
return max_students > other.max_students;
}

};

然后,您可以将您的 Room 对象放入 std::set 中,它会为您需要的所有房间动态分配足够的内存,并且 使它们按降序排列,如下所示:

#include <set>

std::set<Room> rooms;
room.insert( /* populate a room and put it here */);
room.insert( /* populate a room and put it here */);

这样你就可以像这样输出它们:

cout << "Arranged classrooms in descending order by classroom size: " << endl;
for (std::set<Room>::iterator i = rooms.begin(); i != rooms.end(); i++)
{
cout << i->building_code << " " << i->room_number << " "
<< i->max_students;
cout << endl;

}

我学习的关于 C++ 的一切,我可能是从 here 学到的.

关于c++ - 我的结构数组打印出垃圾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22204309/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com