簡體   English   中英

我的程序無限循環,我老實說不清楚為什么

[英]My program is looping infinitely, and I'm honestly not sure why

所以這里的問題是編寫一個程序,使用指針指向函數並以這樣的方式編寫它,它收集10個雙打,向程序用戶提供反饋,對它們進行排序並打印排序結果作為證據。 問題是,程序要么無限地打印printf語句,要么無限地收集數字。

這是一些代碼

#include <stdio.h>


void func1(double x);
void below_five(void);
void above_five(void);
void other(void);
void sort(double *p[], int n);
void print_doubles(double *p[], int n);

int main(void){

  double *numbers[9];
  int nbr;
  printf("\nEnter 10 doubles that are less than 5 or greater than 5, type 0 to exit");
  for(int i = 0; i < 10 ; i++)
  {

      scanf("%d", &nbr);
      func1(nbr);
      numbers[i] = nbr;

      if(nbr == 0)
        break;


  }
  sort(numbers, 10);
  print_doubles(numbers, 10);
  return 0;
}

void func1(double val)
{
  double (*ptr)(void);

  if(val <= 5.00){
    ptr = below_five;
  }else if((val > 5.00) && (val <= 10.00)){
    ptr = above_five;
  }else
    ptr = other;
}

void below_five(void){
  puts("You entered a number below or equal to five");
}

void above_five(void){
  puts("You entered a number above five");
}

void other(void){
  puts("You entered a number well above five.");
}

void sort(double *p[], int n)
{
double *tmp;
for(int i = 0; i < n; i++)
{
  if(p[i] > p[i+1]){
  tmp = p[i];
  p[i] = p[i+1];
  p[i + 1] = tmp;
}
}
}

void print_doubles(double *p[], int n)
{
  int count;
  for(count = 0; count < n; count++)
    printf("%d\n", p[count]);

}

就像我說的那樣,我希望它能夠做的是將雙打收集到scanf方法中,然后在排序后打印數字,但似乎for循環在這種情況下永遠收集雙打而沒有結束。

我究竟做錯了什么?

更新的代碼中查看我的評論。 還需要進行其他修改,但下面是處理代碼所需的最低更新

#include <stdio.h>


void func1(double x);
void below_five(void);
void above_five(void);
void other(void);
    void sort(double p[], int n);  /* Simply use a array notation, 
arrays passed to functions decays to pointer */
    void print_doubles(double p[], int n); /* Simply use a array notation,
 arrays passed to functions decays to pointer */

int main(void){

  double numbers[10];
  double nbr; // Change type to double, as you're reading doubles
  printf("\nEnter 10 doubles that are less than 
           5 or greater than 5, type 0 to exit\n");
  for(int i = 0; i < 10 ; i++)
  {

      scanf("%lf", &nbr); // Use correct format specifier to read doubles
      func1(nbr);
      numbers[i] = nbr;

      if(nbr == 0)
        break;


  }
  sort(numbers, 10);
  print_doubles(numbers, 10);
  return 0;
}

void func1(double val)
{
  double (*ptr)(void);

  if(val <= 5.00){
    ptr = below_five;
  }else if((val > 5.00) && (val <= 10.00)){
    ptr = above_five;
  }else
    ptr = other;
  /* Why you set the pointer to function if you don't call it, 
     so call it here*/    
        (*ptr)();  
    }

void below_five(void){
  puts("You entered a number below or equal to five");
}

void above_five(void){
  puts("You entered a number above five");
}

void other(void){
  puts("You entered a number well above five.");
}

void sort(double p[], int n)  /* Your sorting routine is wrong ,
   see the modified code */
{
double tmp;
    for(int j = 0; j < n-1; j++)
    for(int i = 0; i < n-j-1; i++)
{
  if(p[i] > p[i+1]){
  tmp = p[i];
  p[i] = p[i+1];
  p[i + 1] = tmp;
}
}
}

void print_doubles(double p[], int n)
{
  int count;
  for(count = 0; count < n; count++)
    printf("%lf\n", p[count]); // Use correct format specifier

}

在這里演示

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM