c - 程序挂起,不输出任何东西

标签 c arrays loops recursion

当我运行这段代码时,我收到一条警告:控件到达非无效函数 [Wreturn-type] 的末尾。我认为这可能是一个无限递归的情况,但我不知道如何解决它。

在您输入非数字的内容之前,该程序应该会接收数字。 poramnet() 函数将任何 9 与 7 交换并返回它们。我还有一个条件,我必须打印出最大的 5 个数字,如果数组中没有 5 个数字,我必须将它们全部打印出来。

#include <stdio.h>
#include<string.h>
#include<malloc.h>
#include<ctype.h>

int poramnet(int n, int m, int i){ //i should start with 1 
    if(n==0)
        return m;
    if(n%10==9){
        m+=i*7;
    }
    else{
        m+=i*(n%10);
        return poramnet(n/10, m, i*10);
    }
}

int main()
{
    int array1[100], i=0, output[100], br=1, j, temp, m=0, n;
    while(1){
    scanf("%d", &array1[i]);
    if(!isdigit(array1[i]))
        break;
        i++;
    }
    n=i;
    for(i=0;i<n;i++){
        output[i]=poramnet(array1[i], m, br);
    }
    for(i=0;i<n-1;i++){
        for(j=1;i<n;j++){
            if(output[i]>output[j]){
                temp=output[i];
                output[i]=output[j];
                output[j]=temp;
            }
        }
    }
    if(n<5){
        for(i=0;i<n;i++)
            printf("%d ", output[i]);
    }
    else{
        for(i=0;i<5;i++){
        printf("%d ", output[i]);
        }
    }
    return 0;
}

最佳答案

注意事项:

  1. 始终使用括号 if阻止,即使您认为没有必要。
  2. 替换了 scanfgetc .
    1. 这里必须忽略“ENTER”键,
    2. 然后转换为int ,
    3. 然后用isdigit检查.
  3. 您的一个嵌套 for 循环有一个错误:j<n是需要的,不是 i<n .
  4. 将函数的退出/返回次数保持在最低限度。如果您仍然想从一个函数中使用多个返回值,那么至少要设置一个默认返回值,您知道它总是会被命中,即使您的程序出现错误也是如此。

查看代码中的注释以获取更多详细信息。

main.cpp

#include <stdio.h>
#include<string.h>
#include<malloc.h>
#include<ctype.h>

int poramnet(int n, int m, int i){ //i should start with 1
    // Always put brackets with if !!! It avoid confusion
    if(n==0) {
        return m;
    }
    if(n%10==9){
        m+=i*7;
        return m;
    }
    else{
        m+=i*(n%10);
        return poramnet(n/10, m, i*10);
    }
    // put a default return always, so you know if the function returns
    // in an expected way, that you can track it back down.
    return -1;
}

int main()
{
    int array1[100], i=0, output[100], br=1, j, temp, m=0, n;

    // Instead of scanf, you could use getc, ignore the "enter" key-press,
    // and convert to an int from char and then check with isdigit().
    while(1){
        char tempChar;
        tempChar = getc(stdin);
        if (tempChar != '\n') {
            // printf("what you entered is, %c\n", tempChar);
            // printf("is it a digit:  %d\n", !isdigit(tempChar));
            tempChar = tempChar - '0';
            // printf("is it a digit:  %d\n", !isdigit(array1[i]));
            if(isdigit(tempChar)) {
                break;
            }
            array1[i] = tempChar;
            i++;
        }
    }

    n=i;
    for(i=0;i<n;i++){
        output[i]=poramnet(array1[i], m, br);
    }



    for(i=0;i<n-1;i++){
        for(j=1;j<n;j++){ // you had a bug here also! you need j<n and not i<n !! copy-paste error!
            if(output[i]>output[j]){
                temp=output[i];
                output[i]=output[j];
                output[j]=temp;
            }
        }
    }

    if(n<5){
        for(i=0;i<n;i++)
            printf("%d ", output[i]);
    }
    else{
        for(i=0;i<5;i++){
            printf("%d ", output[i]);
        }
    }
    return 0;
}

输出

junglefox@ubuntu:~/test_programs$./test

1

2

3

4

5

a

1 4 3 2 5 junglefox@ubuntu:~/test_programs$

关于c - 程序挂起,不输出任何东西,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54178306/

相关文章:

arrays - 将json文件转换为linux jq命令格式的csv

JavaScript 在日期范围和返回正确格式之间循环

c - 以char数据类型存储字符C语言

c++ - C/C++ 中的循环依赖结构

c - C 中相同整数的多个条件

c - 在 C 中打破或合并数组上的循环?

javascript - 简单 JSON 调用的 console.log() 错误行为

javascript - 如果包含特定单词,则从数组中返回匹配项

java - 如何在鼠标事件发生后从数组列表中删除对象?

c - scanf 不会让我离开循环 — 知道为什么以及如何解决它吗?