所以我試圖讓一個數組類可以讓你添加一個數字,並且添加函數會將該數字放置到一個數組中,使用下列條件:數組行和列必須按升序編號排序。所以在C++自定義類中自我排序數組的排序函數
是細的,而
失敗,如圖4> 2和3> 2,但
is accept能夠的
賦值是模糊的,只要滿足約束條件,它是否使數組成爲第一個或第三個並不重要。我通過簡單地寫出一個案例列表並試圖關注每個案例來完成我的添加功能。當數組初始化時,它會將INTEGER_MAX放置在每個位置以進行佔位和評估。我不確定這是評估的順序還是什麼,但有時它會在INTEGER_MAX或其他順序下添加一個數字。我一直在研究這個問題,並且對尋求幫助猶豫不決,但我認爲用一雙新鮮的眼睛可能會更容易。我將爲函數和類添加代碼,我不知道是否/應該如何包含依賴類以允許其他人編譯代碼?我是新手,有點不舒服,所以只需要忍耐一下,我會提供任何需要幫助的信息。謝謝!
這裏的add()函數本身:
void add(int i) {
//THIS NEEDS TO BE FIXED ITS GOING TO THE WRONG PLACES
if (matrix[row - 1][col - 1] != INT_MAX){ //if the last element in the VNT is full
cout<<"VNT is full!"<<endl;
}
else {
matrix[row - 1][col - 1] = i;
//cout << "matrix["<<row-1<<"]["<<col-1<<"] = " <<matrix[row - 1][col - 1]<<endl;
int r = row - 1;
int c = col - 1;
while (true) {
if (r == 0 && c == 0) //no neighbor left, no neighbor above, correct position
break;
else if (c == 0) { //no neighbor left
if (matrix[c][r-1] > i) { //if above is larger, swap
swap (r, c, r-1, c);
r--; //decrement row to go through stability check again
}
else //above is smaller, break
break;
}
else if (r == 0) { //no neighbor above
if (matrix[c-1][r] > i) { //if left is larger, swap
swap (r, c, r, c-1);
c--; //decrement column to go through stability check again
}
else //left is smaller, break
break;
}
else if (matrix[r][c-1] < i && matrix[r-1][c] < i) //left and above are both smaller, right position
break;
else if (matrix[r][c-1] > i && matrix[r-1][c] > i) { //both left and above are potential candidates for switch
if (matrix[r][c-1] >= matrix[c][r-1]) { //if left candidate is larger than top candidate, swap with that to preserve column
swap (r, c, r, c-1);
cout << "Swapping a["<<r<<"]["<<c<<"] with a["<<r<<"]["<<c-1<<"]"<<endl;
c--; //decrement column to go through stability check again
}
else { //otherwise swap with neighbor above
swap (r, c, r-1, c);
cout << "Swapping a["<<r<<"]["<<c<<"] with a["<<r-1<<"]["<<c<<"]"<<endl;
r--; //decrement row to go through stability check again
}
}
else if (matrix[r][c-1] > i) { //only left neighbor is larger, swap left
swap (r, c, r, c-1);
cout << "Swapping a["<<r<<"]["<<c<<"] with a["<<r<<"]["<<c-1<<"]"<<endl;
r--;
}
else if (matrix[r-1][c] > i) { //only the above neighbor is larger, switch with that
swap (r, c, r-1, c);
cout << "Swapping a["<<r<<"]["<<c<<"] with a["<<r-1<<"]["<<c<<"]"<<endl;
c--;
}
}
}
}
,然後這裏是上下文較大的類文件(請讓我知道如果有更多需要):
#include <cmath>
#include <climits>
#define main poop
#include "SA.cpp"
#include "safeMatrix.cpp"
using namespace std;
#undef main
//friend ostream& operator<< (ostream& os, VNT v);
class VNT {
private:
SafeMatrix <int> matrix;
int row;
int col;
public:
VNT (int r, int c) : matrix (r, c) { //2 param constructor
//cout << "r = " << r << " c = " << c << endl;
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
//cout << "i = " << i << " j = " << j;
matrix[i][j] = INT_MAX;
}
cout << endl;
}
row = r; //initialize the rows and cols vars to hold the SIZE of the array !POSSIBLE OFF BY 1 ERRORS!
col = c;
}
~VNT() { //destructor
cout << "VNT Destructor called\n";
}
void add(int i) {
//THIS NEEDS TO BE FIXED ITS GOING TO THE WRONG PLACES
if (matrix[row - 1][col - 1] != INT_MAX){ //if the last element in the VNT is full
cout<<"VNT is full!"<<endl;
}
else {
matrix[row - 1][col - 1] = i;
//cout << "matrix["<<row-1<<"]["<<col-1<<"] = " <<matrix[row - 1][col - 1]<<endl;
int r = row - 1;
int c = col - 1;
while (true) {
if (r == 0 && c == 0) //no neighbor left, no neighbor above, correct position
break;
else if (c == 0) { //no neighbor left
if (matrix[c][r-1] > i) { //if above is larger, swap
swap (r, c, r-1, c);
r--; //decrement row to go through stability check again
}
else //above is smaller, break
break;
}
else if (r == 0) { //no neighbor above
if (matrix[c-1][r] > i) { //if left is larger, swap
swap (r, c, r, c-1);
c--; //decrement column to go through stability check again
}
else //left is smaller, break
break;
}
else if (matrix[r][c-1] < i && matrix[r-1][c] < i) //left and above are both smaller, right position
break;
else if (matrix[r][c-1] > i && matrix[r-1][c] > i) { //both left and above are potential candidates for switch
if (matrix[r][c-1] >= matrix[c][r-1]) { //if left candidate is larger than top candidate, swap with that to preserve column
swap (r, c, r, c-1);
cout << "Swapping a["<<r<<"]["<<c<<"] with a["<<r<<"]["<<c-1<<"]"<<endl;
c--; //decrement column to go through stability check again
}
else { //otherwise swap with neighbor above
swap (r, c, r-1, c);
cout << "Swapping a["<<r<<"]["<<c<<"] with a["<<r-1<<"]["<<c<<"]"<<endl;
r--; //decrement row to go through stability check again
}
}
else if (matrix[r][c-1] > i) { //only left neighbor is larger, swap left
swap (r, c, r, c-1);
cout << "Swapping a["<<r<<"]["<<c<<"] with a["<<r<<"]["<<c-1<<"]"<<endl;
r--;
}
else if (matrix[r-1][c] > i) { //only the above neighbor is larger, switch with that
swap (r, c, r-1, c);
cout << "Swapping a["<<r<<"]["<<c<<"] with a["<<r-1<<"]["<<c<<"]"<<endl;
c--;
}
}
}
}
int getMin() { //removes the first element and then resort the matrix
int value;
if (matrix[0][0] == INT_MAX){ // if the VNT is empty it will output this message but
cout<<"VNT is empty"<<endl;
return -1;
}
else {
value = matrix[0][0]; // value to be returned
matrix[0][0] = INT_MAX; // set the first element to INT_MAX
int r = 0;
int c = 0;
while (r < row || c < col) {
if (matrix[r][c] > matrix[r+1][c] && matrix[r][c] > matrix[r][c+1]) { //if both the element to the right and below are candidates
if (matrix[r][c+1] < matrix[r+1][c]) { //if swapping with left wont invalidate the column, do that
swap(r, c, r, c+1);
c++;
}
else { //otherwise swap with bottom
swap(r, c, r+1, c);
r++;
}
}
else if (matrix[r][c] > matrix[r][c+1]) {
swap(r, c, r, c+1);
c++;
}
else if (matrix[r][c] > matrix[r+1][c]) {
swap(r, c, r+1, c);
r++;
}
else
break;
}
}
return value; //scope?!?
}
void sort(int k[], int size) {
if (size > row*col)
cout << "Too many elements for the VNT"<< endl;
else {
for (int i = 0; i < row; i++){
for(int j = 0; j < col; j++){
matrix[i][j] = INT_MAX; //set every element in the VNT to INT_MAX
}
}
for(int i = 0; i < size; i++){
add(k[i]); // will use the member function to add each element to the VNT
}
}
}
void swap (int r1, int c1, int r2, int c2) {
int temp = matrix[r1][c1];
matrix[r1][c1] = matrix[r2][c2];
matrix [r2][c2] = temp;
}
bool find (int i) {
for (int r = 0; r < row; r++) {
for (int c = 0; c < col; c++) {
if (matrix[r][c] == i)
return true;
// else if (matrix[r][c] > i)
// return false;
}
}
return false;
}
friend ostream& operator<< (ostream& os, VNT v);
};
ostream& operator<< (ostream& os, VNT v) {
for (int i = 0; i < v.row; i++) {
for (int j = 0; j < v.col; j++) {
if (v.matrix[i][j] == INT_MAX)
os << "*" << " ";
else
os << v.matrix[i][j] << " ";
}
os << endl;
}
return os;
}
int main(){
VNT a(5,5);
cout << a;
VNT b(3,3);
cout << b;
b.add(1);
b.add(5);
//cout << a.getMin();
//a.add(1);
//cout << b;
//b.add(10);
cout << b;
b.add(2);
b.add(3);
cout << b;
b.add(10);
b.add(7);
b.add(11);
cout << b;
cout << b.find(1)<<endl;
VNT m(3,5);
cout << m;
//cout << c;
};
現在,對我來說,對於矩陣b的代碼的輸出爲:
1 3 *
5 10 *
(*代表INTEGER_MAX)
希望我格式化它的權利,在此先感謝!
感謝你的洞察(甚至是斜向!)!我甚至沒有這樣想過,但是這比我試圖交換它的無數案件要容易得多,更加平易近人。 – user1048834