gpt4 book ai didi

C算法和代码反馈和建议

转载 作者:行者123 更新时间:2023-11-30 20:13:41 24 4
gpt4 key购买 nike

我已经整理了一个作业算法。我已尽力使其保持专业且可读的标准。我将其发布在这里,以便我可以获得一些反馈和建议,了解它的情况以及我是否可以以某种方式改进算法。

  /***** Algorithm 

Comments - Name: Shivan Kamal
Purpose: To create a C program which takes 3 integers and produces a result that shows which triangle(If valid) they have chosen.*****/



Variable Declaration: sideA = integer
Variable Declaration: sideB = integer
Variable Declaration: sideC = integer
Character Declaration = ch

PRINT -- " Lets explore triangles! Please insert a value for side A of your triangle \n"
PRINT -- " Ranging from 1-15cm"

PRINT -- " Now insert a value for side B of your triangle ranging from 1-15cm.\n"
INPUT -- sideB

PRINT -- "And finally, insert a value for side C of your triangle ranging from 1-15cm.\n"
INPUT -- sideC

IF (sideA || sideB || sideC <=0)
PRINT " You cannot have a triangle with any side having a value of 0.\n"
ELSE
IF(sideA || sideB || sideC >15) THEN
PRINT " Please insert a value between 1cm - 15cm only."
ELSE
IF (sideA AND sideB == sideC OR sideB AND sideC == sideA OR sideC AND sideA == side C ) THEN
PRINT "Your input creates a valid EQUILATERAL triangle.\n"
ELSE
IF (sideA==sideB OR sideB==sideC OR sideC==sideA )
PRINT " Your input creates a valid SCALENE triangle.\n"
ELSE
IF
PRINT " Your input creates a valid ISOSCELES triangle.\n"
ELSE
PRINT " You have inserted invalid range of values, as a result your triangle is Invalid.\n"
PRINT " Please restart the program by running it again and insert valid values in order to check what triangle you would get. Goodbye."
ENDIF
ENDIF
ENDIF
ENDIF
ENDIF
END PROGRAM

您可能会注意到我做了一个 char 声明,但没有在算法的其余部分中使用它。那是因为我试图扩展算法和测试条件。我想做的一件事是插入一个循环条件,除非用户插入有效的整数,否则系统将提示用户插入有效的整数,一旦插入,程序就会继续。这种情况一开始就出现过 3 次。

我还制作了一系列特定数字之间的整数。我如何能够改进算法,以便用户可以插入任何数字,而条件之一是三角形的一条边不能比其他两条边长,否则三角形无效。

例如,如果用户插入

A 侧为 = 10侧B = 20 那么sideC不能超过30。同样的条件是SideA不能大于sideB和sideC的总和,并且sideC和sideA不能高于sideB。

在我的代码中,我目前已将其设置为确保用户只能插入有效的整数,因此不允许用户插入除整数之外的任何字符。

在算法的末尾以及代码中,我有一个 ELSE-IF 语句,以防用户插入任何无效内容。但由于我试图从一开始就使其防错,那么我必须添加什么才能删除最终的 ELSE-IF 语句。

最后,我想在算法中添加一个部分,其结果是“用户得到答案后,然后要求用户重试或直接退出。

如果您想编译,代码粘贴在下面,请检查它并根据我试图让程序执行的操作提出一些建议。

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

int sideA;
int sideB;
int sideC;
char ch;

printf("Lets explore triangles! Please insert a value for side 'A' of your triangle.\n");
printf(" Ranging from 1-15cm.\n");
while(scanf("%d", &sideA) != 1)
{
printf("You inserted an incorrect value. Please insert a number ranging between 1-15cm, and try again.\n");
while ( (ch=getchar()) != '\n' );
}

printf(" Now insert a value for side 'B' of your triangle ranging from 1-15cm.\n");
while(scanf("%d", &sideB) != 1 )
{
printf("You inserted an incorrect value. Please insert a number ranging from 1-15cm, and try again.\n");
while ( (ch=getchar()) != '\n' );
}

printf(" And finally, insert a value for side C of your triangle ranging from 1-15cm.\n");
while(scanf("%d", &sideC) != 1 )
{
printf("You inserted an incorrect value. Please insert a number ranging from 1-15cm, and try again.\n");
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(" You cannot have a triangle with any side having a value of 0.\n");
}
else
if(sideA>15 || sideB>15 || sideC >15)
{
printf("Please insert a value between 1cm-15cm only\n.");
}
else
if( (sideA==sideC && sideB==sideC) || (sideB==sideA && sideC==sideA) || (sideC==sideB && sideA==sideB) ) /*** Code to determine EQUILATERAL TRIANGLE***/
{
printf(" Your input creates a valid EQUILATERAL triangle.\n");
}
else
if( (sideA == sideB) || (sideB == sideC) || (sideC == sideA) )/*** Code to determine ISOSCELES TRIANGLE***/
{
printf("Your input creates a valid ISOSCELES triangle.\n");
}
else
if( (sideA!= sideB) && (sideB != sideC) )/*** Code to determine SCALENE triangle ***/
{
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 restart the program by closing it and opening it again to retry\n.");
printf("Goodbye.\n");
}
return(0);
}

注意:您可能会发现代码中有些东西不在算法中,这纯粹是因为我有点不确定如何编写它们,以便程序员在编写它们时有意义尝试编写这个程序。

最佳答案

我注意到您的代码中有很多 if-else 语句,因此我认为表驱动方法可能是实现此逻辑的更简单的方法。

#after range checking

CHAR *result[4] = {"a SCALENE triangle", "a ISOSCELES triangle", "Invalid", "a EQUILATERAL triangle"};
int a = sideA==sideB;
int b = sideB==sideC;
int c = sideC==sideA;
printf("Your input is %s",result[a+b+c]);

关于C算法和代码反馈和建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29205748/

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