gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 08:28:58 24 4
gpt4 key购买 nike

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

最佳答案

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

关于c - 在此 C 代码中的何处添加循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29228272/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com