-2
編輯:這絕不是我以前的問題的重複。我的代碼已經相應地改變了我從前一個線程收到的答案,而且我仍然收到這些錯誤,這些錯誤與我上次提到的不一樣。我的合併排序中的邏輯錯誤是什麼C
我在寫代碼來排序用戶使用合併排序輸入的區間。它從一個用戶輸入,它存儲在一個結構interval
中,用來按升序排序。它使用正值和負值來檢查間隔側是打開還是關閉,但與正在執行的代碼無關。下面是我的代碼和我得到的輸出類型的示例。提前感謝大家的幫助。
#include <stdio.h>
typedef struct interval{
int num_left, denum_left;
int num_right, denum_right;
int left_state, right_state;
}interval;
interval combine(interval x, interval y);
int combineCheck(interval x, interval y);
int valueCheck(interval x, interval y);
void mergeSort(interval x[], int l, int r);
void merge(interval x[], int l, int m, int r);
interval takeInput();
int main(){
int response, i;
char d;
printf("Enter the number of intervals to input: ");
scanf("%d", &response);
interval data[response];
for(i = 0; i < response; i++){
data[i] = takeInput();
}
mergeSort(data, 0, response-1);
printf("%d %d %d\n", data[0].num_left, data[1].num_left, data[2].num_left);
for(i = 0; i < response; i++){
printf("%d/%d %d/%d\n", data[i].num_left, data[i].denum_left, data[i].num_right, data[i].denum_right);
}
}
interval takeInput(){
interval temp;
printf("Enter left numerator: ");
scanf("%d", &temp.num_left);
printf("Enter left denominator: ");
scanf("%d", &temp.denum_left);
printf("Enter right numerator: ");
scanf("%d", &temp.num_right);
printf("Enter right denominator: ");
scanf("%d", &temp.denum_right);
printf("\n");
if(temp.num_left < 0){
temp.num_left = temp.num_left*-1;
temp.left_state = -1;}
else{
temp.left_state = 0;}
if(temp.num_right < 0){
temp.num_right = temp.num_right*-1;
temp.right_state = -1;}
else{
temp.right_state = 0;}
return temp;
}
int combineCheck(interval x, interval y){
int left, right;
left = x.num_right * y.denum_left; //used to find relationship between 2 fractions
right = y.num_left * x.denum_right;
if(left == right && (x.right_state + x.left_state) == 0){
return 1;
}
else if(left > right){
return 1;
}
return 0;
}
interval combine(interval x, interval y){
int left, right; //used to check if one interval is all encompassing
left = x.num_right * y.denum_right;
right = x.denum_right * y.num_right;
interval temp;
temp.num_left = x.num_left;
temp.denum_left = x.denum_left;
temp.left_state;
if(left > right){
temp.num_right = x.num_right;
temp.denum_right = x.denum_right;
temp.right_state = x.right_state;
return temp;
}
temp.num_right = y.num_right;
temp.denum_right = y.denum_right;
temp.right_state = y.right_state;
return temp;
}
int valueCheck(interval x, interval y){
int first, second; //used to check values
first = x.num_left * y.denum_left;
second = y.num_left * x.denum_left;
if(first > second){
return 1;
}
return -1;
}
void mergeSort(interval x[], int l, int r){
if(l < r){
int m = l + (r-l)/2;
mergeSort(x, l, m);
mergeSort(x, m+1, r);
merge(x, l, m, r);
}
}
void merge(interval arr[], int l, int m, int r){
int i, j, k;
int n1 = m-l +1;
int n2 = r-m;
interval L[n1], R[n2];
for(i = 0; i < n1; i++)
L[i] = arr[l + i];
for(j = 0; j < n2; j++)
R[j] = arr[m + 1 + j];
j = 0;
i = 0;
k = l;
while(i < n1 && j < n2){
if(valueCheck(L[i], R[j]) == -1){
arr[k] = L[i];
i++;
}
else{
arr[k] = R[j];
j++;
}
}
while(i < n1){
arr[k] = L[i];
i++;
k++;
}
while(j < n2){
arr[k] = R[j];
j++;
k++;
}
}
作爲文本輸出:
Enter the number of intervals to input: 5
Enter Left numerator: 1
Enter left denominator: 1
Enter right numerator: 1
Enter right denominator: 1
Enter Left numerator: 2
Enter left denominator: 1
Enter right numerator: 2
Enter right denominator: 1
Enter Left numerator: 3
Enter left denominator: 1
Enter right numerator: 3
Enter right denominator: 1
Enter Left numerator: 4
Enter left denominator: 1
Enter right numerator: 4
Enter right denominator: 1
Enter Left numerator: 5
Enter left denominator: 1
Enter right numerator: 5
Enter right denominator: 1
5 5 3
5/1 5/1
5/1 5/1
3/1 3/1
5/1 5/1
5/1 5/1
帖子是什麼,你給文本旁邊的圖像中顯示後,只是缺少K的迭代題。 –
我編譯這個時會得到兩個警告 - 你忽略了警告還是因爲某些原因讓它們被禁用?無論如何,第二個警告看起來很有趣:'83:10:警告:表達式結果未使用[-Wunused-value] temp.left_state;' - 該行是否意味着做一些有用的事情? –
@PaulR它檢查左側的間隔是打開還是關閉,但是它並沒有用在我的排序算法中,我只是針對另一個檢查一個分數。 –