這是我的代碼。我試圖為三角形找到不同的東西,我有一些條件。
我是 c 編程的新手,我想知道如何回圈這些條件。例如,如果不滿足第一個條件,則會顯示相應的訊息并要求您再次輸入,以此類推,直到輸入的數字正確為止。
do {
printf("Enter the first side of the triangle: ");
scanf("%f", &a);
printf("Enter the second side of the triangle: ");
scanf("%f", &b);
printf("Enter the third side of the triangle: ");
scanf("%f", &c);
if (a > (b c)) {
printf("Invalid value a");
}
else if (b > (a c)) {
printf("Invalid value b");
}
else if (c > (a b)) {
printf("Invalid value c");
}
else if (a < 0) {
printf("The number a must be positive");
}
else if (b < 0) {
printf("The number b must be positive");
}
else if (c < 0) {
printf("The number c must be positive");
}
else {
perimeter = a b c;
half_p = perimeter / 2;
printf("\nPerimeter of triangle: %f\n", perimeter);
height = ((2 * sqrt(half_p * (half_p - a) * (half_p - b) * (half_p - c))) / a);
printf("\nHeight of triangle: %f\n", height);
square = sqrt(half_p * (half_p - a) * (half_p - b) * (half_p - c));
printf("\nSquare of triangle: %f\n", square);
median = 0.5 * sqrt(2 * pow(b, 2) 2 * pow(c, 2) - pow(a, 2));
printf("\nMedian of triangle: %f\n", median);
bisector = ((2 / (b c))) * sqrt(b * c * half_p * (half_p - a));
printf("\nBisector of triangle: %f\n", bisector);
}
} while ()
我想我應該在這里添加一些東西以使程式完美運行,但我不明白是什么。謝謝!
uj5u.com熱心網友回復:
_Bool invalid = 1; //Assume invalid
do{
printf("Enter the first side of the triangle: ");
scanf("%f", &a);
printf("Enter the second side of the triangle: ");
scanf("%f", &b);
printf("Enter the third side of the triangle: ");
scanf("%f", &c);
if (a > (b c)) {
printf("Invalid value a");
}
else if (b > (a c)) {
printf("Invalid value b");
}
else if (c > (a b)) {
printf("Invalid value c");
}
else if (a < 0) {
printf("The number a must be positive");
}
else if (b < 0) {
printf("The number b must be positive");
}
else if (c < 0) {
printf("The number c must be positive");
}
else
invalid = 0; //Change to valid if passes all tests
}while(invalid); //If invalid loop back to the scanf calls
//This stuff shouldn't happen until the input is valid so it should be outside the loop
perimeter = a b c;
half_p = perimeter / 2;
printf("\nPerimeter of triangle: %f\n", perimeter);
height = ((2 * sqrt(half_p * (half_p - a) * (half_p - b) * (half_p - c))) / a);
printf("\nHeight of triangle: %f\n", height);
square = sqrt(half_p * (half_p - a) * (half_p - b) * (half_p - c));
printf("\nSquare of triangle: %f\n", square);
median = 0.5 * sqrt(2 * pow(b, 2) 2 * pow(c, 2) - pow(a, 2));
printf("\nMedian of triangle: %f\n", median);
bisector = ((2 / (b c))) * sqrt(b * c * half_p * (half_p - a));
printf("\nBisector of triangle: %f\n", bisector);
uj5u.com熱心網友回復:
#include <math.h>
#include <stdio.h>
int main()
{
float a,b,c;
float median, perimeter, half_p, height, square, bisector;
do {
printf("Enter the first side of the triangle: ");
scanf("%f", &a);
printf("Enter the second side of the triangle: ");
scanf("%f", &b);
printf("Enter the third side of the triangle: ");
scanf("%f", &c);
if(a > (b c)) {
printf("Invalid value a");
while(a > (b c)){
printf("Enter the first side of the triangle: ");
scanf("%f", &a);
}
}
else if (b > (a c)) {
printf("Invalid value b");
while(b > (a c)){
printf("Enter the second side of the triangle: ");
scanf("%f", &b);
}
}
else if (c > (a b)) {
while(c > (a b)){
printf("Enter the third side of the triangle: ");
scanf("%f", &c);
} }
else if (a < 0) {
printf("The number a must be positive");
while(a<0){
printf("Enter the first side of the triangle: ");
scanf("%f", &a);
}
}
else if (b < 0) {
printf("The number b must be positive");
while(b<0){
printf("Enter the second side of the triangle: ");
scanf("%f", &b);
}
}
else if (c < 0) {
printf("The number c must be positive");
while(c<0){
printf("Enter the third side of the triangle: ");
scanf("%f", &c);
}
} else {
perimeter = a b c;
half_p = perimeter / 2;
printf("\nPerimeter of triangle: %f\n", perimeter);
height = ((2 * sqrt(half_p * (half_p - a) * (half_p - b) * (half_p - c))) / a);
printf("\nHeight of triangle: %f\n", height);
square = sqrt(half_p * (half_p - a) * (half_p - b) * (half_p - c));
printf("\nSquare of triangle: %f\n", square);
median = 0.5 * sqrt(2 * pow(b, 2) 2 * pow(c, 2) - pow(a, 2));
printf("\nMedian of triangle: %f\n", median);
bisector = ((2 / (b c))) * sqrt(b * c * half_p * (half_p - a));
printf("\nBisector of triangle: %f\n", bisector);
}
} while (1);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/511152.html
上一篇:如何兩次運行行內函式?
