我已经为一项作业整理了一个算法。我已尽力尝试将其保持在专业且可读的标准。我在这里发帖,以便我可以得到一些反馈和建议,以及我是否可以通过某种方式改进算法。
/***** 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次发生。
此外,我在一系列特定数字之间做了整数。我如何能够改进算法,以便用户可以插入任何数字,其中一个条件是三角形的一边不能长于另外两边,否则三角形无效。
如果用户插入例如
sideA as = 10 sideB as = 20 然后sideC不能超过30.同样条件是SideA不能超过sideB和sideC组合,以及sideC和sideA不高于sideB。
在我的代码中,我目前已将其设置为确保用户只能插入有效整数,因此不允许用户插入除整数之外的任何字符。
在算法结束时以及在代码中,我已经获得了ELSE-IF语句,以防用户插入任何无效的内容。但是因为我试图从一开始就使它成为错误证明,所以我必须添加什么才能删除最终的ELSE-IF语句。
最后我想在算法中添加一个结果为&#34的部分;用户得到答案后,要求用户再试一次或者直接退出。
如果你想编译,检查它并根据我试图让程序做的事情提出一些建议,下面会粘贴代码。
#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);
}
注意:您可能会发现代码中有些东西不在算法中,这纯粹是因为我不太清楚如何编写它们以便对程序员有意义。他们试着写这个程序。
答案 0 :(得分:2)
我注意到你的代码中有很多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]);