C 使用用户输入查找插值

C 使用用户输入查找插值,c,function,char,interpolation,do-while,C,Function,Char,Interpolation,Do While,我对算法做了一些修改,在函数中得到了一个值(虽然不正确),但返回值拒绝将其发送回主函数。此外,我无法获得要求程序重新运行以正常运行的代码的yes-no部分 #include <stdio.h> #include <stdlib.h> #include <math.h> #include <float.h> /*Prototype for functions used*/ float getdistance(float[], float[], flo

我对算法做了一些修改,在函数中得到了一个值(虽然不正确),但返回值拒绝将其发送回主函数。此外,我无法获得要求程序重新运行以正常运行的代码的yes-no部分

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <float.h>
/*Prototype for functions used*/
float getdistance(float[], float[], float, int);
float printvalue(float);

int main()
{

      int maxtime = 18;
      float usertime = 0;
      char reiteration;
      char y, n;
      int reit_choice = 0;
      float interpolation = 0.0;
      float time_arr[]={0,3,5,8,10,12,15,18};
      float distance_arr[]={2,5.2,6,10,10.4,13.4,14.8,18};
      do
      { 
       printf("Please enter a number between 0 and 18 for the starting time:");
       scanf("%f", &usertime, "\b");
        while(usertime <0 || usertime >18)
       {
          printf("The value you have entered is not valid, please enter a value between 1 and 18");
          scanf("%f", &usertime, "\b");
       }/*End of data verification loop*/
       getdistance(time_arr, distance_arr, usertime, maxtime);
       printf("%f", interpolation);
       printvalue(interpolation);

       system("pause");

       printf("would you like to check another time?(y/n)");
       scanf("%c[y n]", &reiteration, "\b");
       while(reiteration!='y' || reiteration!='n')
       {
          printf("Please enter either y for yes or n for no");
          scanf("%c", &reiteration, "\b");
       }/*End of choice verification loop*/
       if(reiteration = 'y')
       {
         reit_choice = 1;
       }
       else
       {
         reit_choice = 0;
       }
}/*End of do loop*/
while(reit_choice);

}/*End of main loop*/

float getdistance(float time_arr[], float distance_arr[], float usertime, int maxtime)
{
   int index=0;
   float interpolation = 0;

   for(index; usertime > time_arr[index]; index++)
   {
      if(usertime<3)
      {
         break;
      }
   }/*End of value assignment for loop*/
   interpolation = (time_arr[index]) + (((time_arr[index +1] -     time_arr[index])/(distance_arr[index +1] - distance_arr[index])) * (usertime - distance_arr[index]));
   printf("%f", interpolation);
   return interpolation;
}/*End of distance calculation loop*/

float printvalue(float interpolation)
{
   printf("The interpolation was %f", interpolation);
}/*End of screen print loop*/
#包括
#包括
#包括
#包括
/*所用函数的原型*/
float getdistance(float[],float[],float,int);
浮点打印值(float);
int main()
{
int maxtime=18;
float usertime=0;
字符重复;
chary,n;
int reit_choice=0;
浮点插值=0.0;
浮动时间_arr[]={0,3,5,8,10,12,15,18};
浮动距离_arr[]={2,5.2,6,10,10.4,13.4,14.8,18};
做
{ 
printf(“请输入一个介于0和18之间的数字作为开始时间:”);
scanf(“%f”,&usertime,“\b”);
while(usertime 18)
{
printf(“您输入的值无效,请输入一个介于1和18之间的值”);
scanf(“%f”,&usertime,“\b”);
}/*数据验证循环结束*/
getdistance(时间、距离、用户时间、最大时间);
printf(“%f”,插值);
打印值(插值);
系统(“暂停”);
printf(“您想下次再查吗?(是/否)”);
scanf(“%c[y-n]”,&重复,“\b”);
while(重复!='y'|重复!='n')
{
printf(“请输入y表示是或n表示否”);
scanf(“%c”和重复(“\b”);
}/*选择结束验证循环*/
if(重复='y')
{
房地产投资信托基金选择=1;
}
其他的
{
房地产投资信托基金选择=0;
}
}/*do循环结束*/
while(房地产投资信托基金选择);
}/*主回路末端*/
浮点getdistance(浮点时间_arr[],浮点距离_arr[],浮点用户时间,整数最大时间)
{
int指数=0;
浮点插值=0;
对于(索引;usertime>time_arr[index];index++)
{

如果(usertime0输出的原因是

getdistance(time_arr, distance_arr, usertime, maxtime);
在int main()函数中,您调用的是计算插值值并返回值的函数getdistance,但在main函数中,返回的值未指定给变量插值

interpolation = getdistance(time_arr, distance_arr, usertime, maxtime);
printvalue(interpolation);

这将打印输出

尝试通过调试器运行代码并检查中间值以更好地定位错误。或者,打印中间值。还要注意的是,问题是不明确的:有很多方法可以在数字之间进行插值。我的主要问题是,我的值不断为零,而循环that要求用户继续忽略键盘输入。您能提供查找插值的算法吗?插值=(time_arr[curpos])+((time_arr[curpos+1]-time_arr[curpos])/(distance_arr[index+1]-distance_arr[index])*(usertime-distance_arr[curpos]));即使知道了插值算法,我仍然不会花时间调试您的代码并编写答案。代码示例包含太多死角,主要是字符串解析。请提供错误代码的最小示例,以及您希望它执行的操作。这样,您在编写问题时至少显示了最小的工作量在上,您可能会自己发现错误,并使问题对其他读者有用。