简体   繁体   English

接受输入后,C程序“卡住”

[英]C Program 'stuck' After Accepting Input

Sorry for the wording of the title, I could not think of a better way to word my question. 很抱歉标题的措辞,我想不出一种更好的方式来表达我的问题。 In my code I am taking n sets of input for a struct I call interval. 在我的代码中,我将n个输入集用于一个我称之为时间间隔的结构。 If the user only wants to enter 1 structure, then the program accepts input and prints just fine. 如果用户只想输入1个结构,则程序接受输入并打印就好。 However, anything greater than 1 will accept all the input, but after the last input that stores an integer inside the structure, it will become 'stuck' where the cursor will move down one line as if it is waiting for input. 但是,任何大于1的值都将接受所有输入,但是在最后一个在结构内部存储整数的输入之后,它将变为“粘滞”状态,光标将向下移动一行,就好像它在等待输入一样。 Except no matter what you enter the program doesn't progress at all. 不管您输入什么,程序都不会进行。 I need to understand what is stopping my program from progressing. 我需要了解导致我的程序停止运行的原因。 Thanks for any and all help. 感谢您提供的所有帮助。

The error (I assume) is in the main when the function takeInput is called. 调用函数takeInput时,错误(我假设是)主要存在。

I compiled using gcc on a Linux machine. 我在Linux机器上使用gcc进行了编译。 Below is the Code. 以下是代码。

#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++;
    }
}

Here is what the output looks like 这是输出的样子 在此处输入图片说明

You have some problem in your merge sort routine: 您的合并排序例程中存在一些问题:

k = l;
while(i < n1 && j < n2){
    if(valueCheck(L[i], R[j]) == -1){
        arr[k] = L[i];
    }
    else{
        arr[k] = R[j];
        j++;
    }
}

You're not incrementing i after assigning to arr. 分配给arr后,您不会增加i。 Since i is never changing, your while loop never terminates. 由于我永不改变,因此while循环永远不会终止。 Add a i++ in that first if condition and it would work. 如果条件成立,请在该条件中首先添加一个i++ Also see the point made by MondKin, that's also a very serious problem in your code. 另请参见MondKin提出的观点,这也是您的代码中非常严重的问题。

Checking your merge sort algorithm I see this: 检查您的合并排序算法,我看到以下内容:

int m = l + (r-1)/2;

I think you meant: 我想你的意思是:

int m = l + (r-l)/2;

Because what you want is the middle index. 因为您想要的是中间索引。

I think that the middle index m is incorrect. 我认为中间索引m是不正确的。

Change this: 更改此:

int m = l + (r-1)/2;

to this: 对此:

int m = (l + r) / 2;

There is an infinite loop problem as others indicate. 正如其他人指出的那样,存在无限循环问题。 You cannot indicate this in your debug prints, because it may not flush whenever they are called. 您不能在调试打印中指出这一点,因为调用它们时可能不会刷新。 You can add newline to your debug prints to see where your program stucks. 您可以在调试打印中添加换行符,以查看程序卡在哪里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM