我的程序无限循环,老实说,我不确定为什么

时间:2019-05-21 01:46:16

标签: c

因此,这里的问题是编写一个使用指针指向函数的程序,并编写该程序以使其收集10个double,将反馈提供给程序的用户,对它们进行排序并打印排序后的结果作为证明。问题是,程序要么无限打印开头的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循环会永远无休止地收集双精度数。

我到底做错了什么?

1 个答案:

答案 0 :(得分:0)

在您的更新代码中查看我的评论。 还需要进行其他修改,但是使您的代码正常工作所需的最低更新为

#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

}

Demo Here