請問為什麼我的c語言程式無法執行呢?
這是程式
Write a program that reads three nonzero integers and determines if they can represent the sides of a right triangle. If they can, please print “Yes, they form a right triangle.” and print the rectangle with two legs of the right triangle; otherwise, please print “No, they do NOT form a right triangle.”.
Sample of the program execution:
Please input 3 integers: 5 12 13
Yes, they form a right triangle.
************
************
************
************
************
Or
Please input 3 integers: 7 8 9
No, they do NOT form a right triangle.
這是程式碼
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int a,b,c,j,i;
printf("Please enter three integers:");
scanf("%d%d%d",a,b,c);
if(a*a==b*b+c*c||b*b==a*a+c*c||c*c==a*a+b*b)
{
printf("Yes, they form a right triangle");
}
else
{
printf("No,they do not form a right triangle");
}
if(a>b>=c||a>c>=b)
{
for(i=1;i<=c;i++)
{
for(j=1;j<=b;j++)
{
printf("*");
}
printf("\n");
}
}
else if(b>a>=c||b>c>=a)
{
for(i=1;i<=c;i++)
{
for(j=1;j<=a;j++)
{
printf("*");
}
printf("\n");
}
}
else if(c>b>=a||c>a>=b)
{
for(i=1;i<=b;i++)
{
for(j=1;j<=a;j++)
{
printf("*");
}
printf("\n");
}
}
return 0;
}
1 個解答
- 歌篾Lv 41 0 年前最佳解答
錯誤一[第9行]:
讀入的變數之前需要半形的&
例如:scanf("%d%d%d",&a,&b,&c);
錯誤二[第19,30,41行]:
C語言不接受像a>b>=c的連續比較,所以要改成:
第9行:if((a>b&&b>=c)||(a>c&&c>=b))
第30行:else if((b>a&&a>=c)||(b>c&&c>=a))
第41行:else if((c>b&&b>=a)||(c>a&&a>=b))
但事實上,這個問題不需要用這麼複雜的邏輯,您只要用下面幾行就夠了:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int a,b,c,i,j,hyp,leg1,leg2;
printf("Please enter three integers: ");
scanf("%d%d%d",&a,&b,&c);
hyp=max(a,max(b,c));
leg1=min(a,min(b,c));
leg2=a+b+c-hyp-leg1;
if(hyp*hyp==leg1*leg1+leg2*leg2)
{
printf("Yes, they form a right triangle.\n");
for(i=1;i<=leg1;i++)
{
for(j=1;j<=leg2;j++)
printf("*");
printf("\n");
}
}
else
printf("No, they do not form a right triangle.\n");
return 0;
}
2008-12-17 01:05:32 補充:
抱歉,上面的出現了一些亂碼,我重新再post一次 (請把\><&"改成半形):
#include <stdio.h>
#include <stdlib.h>
2008-12-17 01:05:41 補充:
int main(void)
{
int a,b,c,i,j,hyp,leg1,leg2;
printf("Please enter three integers: ");
scanf("%d%d%d",&a,&b,&c);
hyp=max(a,max(b,c));
leg1=min(a,min(b,c));
leg2=a+b+c-hyp-leg1;
2008-12-17 01:05:47 補充:
if(hyp*hyp==leg1*leg1+leg2*leg2)
{
printf("Yes, they form a right triangle.\n");
for(i=1;i<=leg1;i++)
{
for(j=1;j<=leg2;j++)
printf("*");
printf("\n");
}
}
else
printf("No, they do not form a right triangle.\n");
return 0;
}