【发布时间】:2018-03-02 08:15:13
【问题描述】:
抱歉标题的措辞,我想不出更好的方式来表达我的问题。在我的代码中,我为我调用间隔的结构获取 n 组输入。如果用户只想输入 1 个结构,那么程序接受输入并打印就可以了。但是,任何大于 1 的值都将接受所有输入,但在结构中存储整数的最后一个输入之后,它将变为“卡住”,光标将向下移动一行,就好像它正在等待输入一样。除非无论您输入什么程序都没有进展。我需要了解是什么阻止了我的程序进行。感谢您的所有帮助。
错误(我假设)在调用函数 takeInput 时主要出现。
我在 Linux 机器上使用 gcc 编译。下面是代码。
#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();
}
printf("%d/%d %d/%d", data[0].num_left, data[0].denum_left, data[0].num_right, data[0].denum_right);
mergeSort(data, 0, response-1);
printf("%d/%d %d/%d", data[0].num_left, data[0].denum_left, data[0].num_right, data[0].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;}
printf("Testing Shit");
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-1)/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];
}
else{
arr[k] = R[j];
j++;
}
}
while(i < n1){
arr[k] = L[i];
i++;
k++;
}
while(j < n2){
arr[k] = R[j];
j++;
k++;
}
}
【问题讨论】:
-
我没有看过你的代码,但你所描述的看起来像一个无限循环。检查 for 和 while 循环中的条件值(在循环中打印它们)。尼古拉斯 :)
-
编译所有警告和调试信息 (
gcc -Wall -Wextra -g) 并使用调试器gdb