在此 C 代码中的何处添加循环?

Where to add a loop in this C code?

我有一个作业涉及在 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);
}

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

然后在 main 中放入这个循环(伪代码):

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

这可以通过 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);
}