在这个C代码中添加循环的位置?

时间:2015-03-24 08:50:30

标签: c while-loop

我有一个处理在C中创建程序的任务,其中用户必须插入3个整数并根据输入,它为用户提供一个三角形类型(只有它有效)才能用这些值创建。

代码本身有效,我已经设法插入一些循环来测试条件并使其更加健壮。

但是,我想为用户提供再次尝试不同结果的选项,或者只是关闭程序。我有一种感觉,我必须添加另一个while循环但是我不确定在哪里以及如何使程序以这种方式工作。

理想情况下,循环将替换结尾处的输出,或者在用户根据输入获得无效三角形的情况下。当这些情况发生时,我希望它能让用户选择重试或退出程序

请参阅我的代码:

#include <stdio.h>
int main()
{
    /*** Declaring triangle variable sides ****/

  float sideA;
  float sideB;
  float sideC;
  char ch;

  printf("Lets explore triangles! Please insert a value for side 'A' of your triangle:");
  while(scanf("%f", &sideA) != 1)
  {
    printf("You inserted an incorrect value. Please insert a number for side 'A' of your triangle:");
    while ( (ch=getchar()) != '\n' );
  }

  printf(" Now insert a value for side 'B' of your triangle:");
  while(scanf("%f", &sideB) != 1 )
  {
    printf("You inserted an incorrect value. Please insert a number for side 'B' of your triangle:");
    while ( (ch=getchar()) != '\n' );
  }

  printf(" And finally, insert a value for side 'C' of your triangle:");
  while(scanf("%f", &sideC) != 1 )
  {
    printf("You inserted an incorrect value. Please insert a number for side 'C' of your triangle:");
    while ( (ch=getchar()) != '\n' );
  }

  /*** List of conditions based on user input to identify if the triangle is valid and if so, what type of triangle they get***/

  if(sideA <=0 || sideB<=0 || sideC <=0)
  {
      printf("YOUR TRIANGLE IS 'INVALID'.\n\n");
      printf("REASON: You cannot have a triangle with any side having a value of 0.\n");
      printf("Please exit the program and restart it to try again.\n");
  }
  else
      if( (sideA+sideB<sideC) || (sideB+sideC<sideA) || (sideC+sideA<sideB) )
      {
          printf("YOUR TRIANGLE IS 'INVALID'.\n\n");
          printf("REASON: The sum of every pair of sides must be greater than the third side of a triangle.!\n");
          printf("Please exit the program and restart it to try again.\n");
      }
      else
           if( (sideA==sideC && sideB==sideC) || (sideB==sideA && sideC==sideA) || (sideC==sideB && sideA==sideB) ) /*** Code to determine EQUILATERAL TRIANGLE***/
           {
              printf("YOUR TRIANGLE IS 'VALID'.\n");
              printf(" Your input creates a valid EQUILATERAL triangle.\n");
           }
           else 
               if( (sideA == sideB ) || (sideB == sideC ) || (sideC == sideA ) )/*** Code to determine ISOSCELES TRIANGLE***/
               {
                   printf("YOUR TRIANGLE IS 'VALID'.\n");
                   printf("Your input creates a valid ISOSCELES triangle.\n");
               }
               else
                   if( (sideA!= sideB) && (sideB != sideC) )/*** Code to determine SCALENE triangle ***/
                   {
                       printf("YOUR TRIANGLE IS 'VALID'.\n");
                       printf("Your input creates a valid SCALENE triangle.\n");
                   }
                   else
                   {
                       printf("You have inserted invalid range of values, as a result your triangle is invalid.\n");
                       printf("Please exit the program and restart it to try again.\n");
                       printf("Goodbye.\n");
                   }
return(0);
}

2 个答案:

答案 0 :(得分:2)

使用do while循环可以轻松实现。

尝试此算法

char a;
do
  {
    /*  Your code */
    printf(" Do you want to do more ( Y/N ) " );
    scanf( " %c",a );
  } while( a == 'Y' || a == 'y' );

现在,这是代码中实现的代码

#include <string.h>
#include <stdio.h>
int main()
{
    /*** Declaring triangle variable sides ****/

  float sideA;
  float sideB;
  float sideC;
  char a;

  do 
   {

      printf("Lets explore triangles! Please insert a value for side 'A' of your triangle:");
      while(scanf("%f", &sideA) != 1)
       {
         printf("You inserted an incorrect value. Please insert a number for side 'A' of your triangle:");
         while ( (getchar()) != '\n' );
       }

      printf(" Now insert a value for side 'B' of your triangle:");
      while(scanf("%f", &sideB) != 1 )
       {
         printf("You inserted an incorrect value. Please insert a number for side 'B' of your triangle:");
         while ( (getchar()) != '\n' );
       }

      printf(" And finally, insert a value for side 'C' of your triangle:");
      while(scanf("%f", &sideC) != 1 )
       {
         printf("You inserted an incorrect value. Please insert a number for side 'C' of your triangle:");
         while ( (getchar()) != '\n' );
       }

      /*** List of conditions based on user input to identify if the triangle is valid and if so, what type of triangle they get***/

      if(sideA <=0 || sideB<=0 || sideC <=0)
       {
           printf("YOUR TRIANGLE IS 'INVALID'.\n\n");
           printf("REASON: You cannot have a triangle with any side having a value of 0.\n");
       }
      else if( (sideA+sideB<sideC) || (sideB+sideC<sideA) || (sideC+sideA<sideB) )
       {
         printf("YOUR TRIANGLE IS 'INVALID'.\n\n");
         printf("REASON: The sum of every pair of sides must be greater than the third side of a triangle.!\n");
       }
      else if( (sideA==sideC && sideB==sideC) || (sideB==sideA && sideC==sideA) || (sideC==sideB && sideA==sideB) ) /*** Code to determine EQUILATERAL TRIANGLE***/
       {
         printf("YOUR TRIANGLE IS 'VALID'.\n");
         printf(" Your input creates a valid EQUILATERAL triangle.\n");
       }
      else if( (sideA == sideB ) || (sideB == sideC ) || (sideC == sideA ) )/*** Code to determine ISOSCELES TRIANGLE***/
       {
         printf("YOUR TRIANGLE IS 'VALID'.\n");
         printf("Your input creates a valid ISOSCELES triangle.\n");
       }
      else if( (sideA!= sideB) && (sideB != sideC) )/*** Code to determine SCALENE triangle ***/
       {
         printf("YOUR TRIANGLE IS 'VALID'.\n");
         printf("Your input creates a valid SCALENE triangle.\n");
       }
      else
       {
         printf("You have inserted invalid range of values, as a result your triangle is invalid.\n");
       }
      printf("Do you want to try again ( Y/N  )");
      scanf(" %c",&a);

   }while( a=='Y' || a=='y'  );

  return(0);
}

答案 1 :(得分:0)

main()内的所有代码放在一个单独的函数中,例如HandleOneTriangle()

然后在main中你把这个循环(伪代码):

main()
{
     while(true)
     {
         HandleOneTriangle();
         boll answer = AskUserIfHeWantsOneMore();
         if(!answer) break;
     }
     SayGoodbye();
}