[英]Inputting data into and sorting a struct
我創建了一個結構並創建了在其上執行一些操作的函數。 我在使用sortLength函數時遇到了麻煩,因為當我運行程序時,只有長度上的數據才被排序。 其他所有事物都可以正常運行,盡管我想知道我的命令是否可以正常運行,因為我使用的數組從0開始計數。我請我的教授澄清一下,他告訴我是否有這些(長度然后寬度)並使用逗號將其清楚地分開
10,8 4,3 6,5 5,1 2,1 3, 2
然后按長度排序,然后數組將是:
2 ,1 3,2 4, 3 5,1 6,5 10,8
但是,我沒有得到我的輸出。 這是我得到的輸出。
這是sortLength函數的說明。
請注意,函數將使用每個矩形的長度值(按升序)進行排序。
您選擇任何要使用的算法; 氣泡,選擇或插入。
請記住,您正在對數組進行排序!
#include "stdafx.h"
#include <iostream>
using namespace std;
struct RECTANGLE
{
int length;
int width;
int area;
};
const int SIZE = 4;
// Function prototypes
void getValues(RECTANGLE[]);
void print(RECTANGLE[]);
int findMaxArea(RECTANGLE[]);
void sortLength(RECTANGLE[]);
int main()
{
RECTANGLE arrRect[SIZE];
//an array of type RECTANGLE
int whereMax; //where find max area
//put values to each element of array
getValues(arrRect);
cout << endl << endl;
//print out each element of array
print(arrRect);
whereMax = findMaxArea(arrRect);
//find where max area is
cout << endl;
cout << "Max area is " << arrRect[whereMax].area << " at position " << whereMax;
cout << endl;
sortLength(arrRect); //sort base on Length
cout << endl;
//print out each element of array
print(arrRect);
return 0;
}
/**
* Pre-Condition: This function accepts an array of type RECTANGLE.
* Post-Condition: It prompts the user for a length and width value
* and calculates the area.
*/
void getValues(RECTANGLE arrRect[])
{
//put values to each element of array
for (int i = 0; i<SIZE; i++)
{
cout << "\nEnter length and width : ";
cin >> arrRect[i].length >> arrRect[i].width;
arrRect[i].area = arrRect[i].length * arrRect[i].width;
//calculate area
}
}
/**
* Pre-Condition: This function accepts an array of type RECTANGLE.
* Post-Condition: It prints the data for length, width, and area.
*/
void print(RECTANGLE arrRect[])
{
//print out each element of array
cout << "Length Width Area" << endl;
for (int i = 0; i<SIZE; i++)
{
cout << arrRect[i].length << "\t\t" << arrRect[i].width << "\t"
<< arrRect[i].area << endl;
}
}
/**
* Pre-Condition: This function accepts an array of type RECTANGLE.
* Post-Condition: It returns an int which represents the position
* of the highest area in the data.
*/
int findMaxArea(RECTANGLE arrRect[])
{
int maxIndex = 0;
int max = arrRect[0].area;
for (int i = 0; i<SIZE; i++)
{
if (max < arrRect[i].area)
{
max = arrRect[i].area;
maxIndex = i;
}
}
return maxIndex;
}
/**
* Pre-Condition: This function accepts an array of type RECTANGLE.
* Post-Condition: It sorts the data in the array according to the
* length value.
*/
void sortLength(RECTANGLE arrRect[])
{
int temp;
for (int i = 0; i < (SIZE - 1); i++)
{
for (int j = i + 1; j < SIZE; j++)
{
if (arrRect[i].length > arrRect[j].length)
{
temp = arrRect[i].length;
arrRect[i].length = arrRect[j].length;
arrRect[j].length = temp;
}
}
}
}
由於在排序時僅更改了length
字段,因此您得到了意外的輸出。
void sortLength(RECTANGLE arrRect[])
{
int temp;
for (int i = 0; i < (SIZE - 1); i++)
{
for (int j = i + 1; j < SIZE; j++)
{
if (arrRect[i].length > arrRect[j].length)
{
// ****** PROBLEM ******
// The lines below swap only the length field.
temp = arrRect[i].length;
arrRect[i].length = arrRect[j].length;
arrRect[j].length = temp;
}
}
}
}
您應該做的是交換整個對象。 采用
void sortLength(RECTANGLE arrRect[])
{
// Make temp a RECTANGLE.
RECTANGLE temp;
for (int i = 0; i < (SIZE - 1); i++)
{
for (int j = i + 1; j < SIZE; j++)
{
if (arrRect[i].length > arrRect[j].length)
{
// Swap the entire object.
temp = arrRect[i];
arrRect[i] = arrRect[j];
arrRect[j] = temp;
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.