判斷任意三角形的型別。編程要求:
①提示"輸入三角形的三個邊:",如果不能組成三角形繼續輸入邊值,用回圈控制,回圈條件應用到邏輯運算的特殊規則。
②應用簡單的if()~陳述句判斷三角形的型別,并輸出結果。如,該三角形是直角(或等腰或等邊或任意三角形),if條件應用到邏輯運算的特殊規則。
③第一次計算結束之后,給出提示"是否繼續判斷其它三角形的型別[Y/N]?,輸入Y或y時繼續輸入,否則結束程式,回圈結構的嵌套。
uj5u.com熱心網友回復:
供參考:#include <stdio.h>
int main()
{
int a, b, c;
char s='Y';
while(s=='Y'|| s=='y'){
while (1){ //輸入
printf("輸入三角形的三條邊(正整數):");
if (scanf("%d%d%d", &a,&b,&c) != 3 || a < 0 || b < 0 || c < 0){
while ((getchar())!='\n');
printf("\n輸入錯誤!\n");
continue;
}else
if (a+b<=c || a+c<=b || b+c<=a){ //判斷是否可構成三角形,任意兩邊之和大于第三邊
printf("\n輸入的邊長無法構成三角形!\n");
continue;
}else
break;
}
if (a == b || a == c || b == c){//判斷三角形型別
if (a==b && b==c && a==c){
printf("\n輸入的邊長為等邊三角形\n");
}else{
printf("\n輸入的邊長為等腰三角形\n");
}
}
else
if (a*a+b*b==c*c || a*a+c*c==b*b || b*b+c*c==a*a){
printf("\n輸入的邊長為直角三角形\n");
}else{
printf("\n輸入的邊長為任意三角形\n");
}
printf("\n是否繼續判斷其它三角形的型別[Y/N]?");
getchar();
scanf("%c",&s);
}
return 0;
}
uj5u.com熱心網友回復:
輸入輸出更可靠點,修改樓上的:#include <stdio.h>
int main()
{
int a, b, c;
char ch[2]={'Y'};
while(ch[0]=='Y'|| ch[0]=='y'){
while (1){ //輸入
printf("輸入三角形的三條邊(正整數):");
fflush(stdout);
rewind(stdin);
if (scanf("%d%d%d", &a,&b,&c) != 3 || a < 0 || b < 0 || c < 0){
printf("\n輸入錯誤!\n");
continue;
}else
if (a+b<=c || a+c<=b || b+c<=a){ //判斷是否可構成三角形,任意兩邊之和大于第三邊
printf("\n輸入的邊長無法構成三角形!\n");
continue;
}else
break;
}
if (a == b || a == c || b == c){//判斷三角形型別
if (a==b && b==c && a==c){
printf("\n輸入的邊長為等邊三角形\n");
}else{
printf("\n輸入的邊長為等腰三角形\n");
}
}
else
if (a*a+b*b==c*c || a*a+c*c==b*b || b*b+c*c==a*a){
printf("\n輸入的邊長為直角三角形\n");
}else{
printf("\n輸入的邊長為任意三角形\n");
}
printf("\n是否繼續判斷其它三角形的型別[Y/N]?");
fflush(stdout);
rewind(stdin);
scanf("%s",ch);
}
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/267193.html
標籤:C語言
下一篇:C語言指標部分
