新人发个帖子问问前辈

劉俊 2010-11-08 10:40:35
自学C语言没有多久,自己做了一道求三角形面积的题目,好像有点问题,请前辈们帮忙看一下问题出在哪里,谢谢。



#include"stdio.h"
#include"math.h"
main()
{
int a,b,c;
double s,area;
printf("Please input a,b,c:\n");
loop:
scanf("%d%d%d",&a,&b,&c);
if((a+b)>c&&(b+c)>a&&a>0&&b>0&&c>0&&(a+c)>b)
{
s=((double)(a+b+c)/2);
area=sqrt(s*(s-a)*(s-b)*(s-c));
printf("area=%g\n",area);
return(getch());
}

else(printf("Please input the correct number:\n"));
goto loop;
getch();
}
...全文
123 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
劉俊 2010-11-08
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 jiadayang 的回复:]
C/C++ code
#include<stdio.h>
#include<math.h>
#include<conio.h>//使用getch必须包含此头文件

main()
{
int a,b,c;
int x=1;
double s,area;
printf("Please input a,b,c:\n");
while……
[/Quote]
原来可以用WHILE来代替。谢谢
劉俊 2010-11-08
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 hnuqinhuan 的回复:]
一个良好的变成人员是要避免使用goto语句
因为goto的功能完全可以利用循环替换
[/Quote]
哦哦。原来如此。
明月生寒 2010-11-08
  • 打赏
  • 举报
回复
#include<stdio.h>
#include<math.h>
#include<conio.h>//使用getch必须包含此头文件

main()
{
int a,b,c;
int x=1;
double s,area;
printf("Please input a,b,c:\n");
while(x)
{
scanf("%d%d%d",&a,&b,&c);
if( (a+b)>c && (b+c)>a && a>0 && b>0 && c>0 && (a+c)>b)
{
s=((double)(a+b+c)/2);
area=sqrt(s*(s-a)*(s-b)*(s-c));
printf("area=%g\n",area);
x=0;
}
else
{
printf("Please input the correct number:\n");
}
}

getch();
}
walkersfaint 2010-11-08
  • 打赏
  • 举报
回复
话说如果不是对C语言很熟悉,还是少用goto的好,
無_1024 2010-11-08
  • 打赏
  • 举报
回复
一个良好的变成人员是要避免使用goto语句
因为goto的功能完全可以利用循环替换
劉俊 2010-11-08
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 jiadayang 的回复:]
C/C++ code
#include<stdio.h>
#include<math.h>
#include<conio.h>//使用getch必须包含此头文件


main()
{
int a,b,c;
double s,area;
printf("Please input a,b,c:\n");
loop:
scanf("%d%d……
[/Quote]
谢谢前辈。呃。。。为什么要改掉GOTO的习惯?
劉俊 2010-11-08
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 ouyh12345 的回复:]
没有退出循环的代码
[/Quote]
嗯,问题好像就是这里。有时候会变成死循环。我那一个goto不知道能不能跳出循环。
明月生寒 2010-11-08
  • 打赏
  • 举报
回复
#include<stdio.h>
#include<math.h>
#include<conio.h>//使用getch必须包含此头文件


main()
{
int a,b,c;
double s,area;
printf("Please input a,b,c:\n");
loop:
scanf("%d%d%d",&a,&b,&c);
if((a+b)>c&&(b+c)>a&&a>0&&b>0&&c>0&&(a+c)>b)
{
s=((double)(a+b+c)/2);
area=sqrt(s*(s-a)*(s-b)*(s-c));
printf("area=%g\n",area);
return(getch());
}
else
{
printf("Please input the correct number:\n");
}
goto loop;
getch();
}
改了一下下,另外,希望改掉使用goto语句的习惯!
ouyh12345 2010-11-08
  • 打赏
  • 举报
回复
没有退出循环的代码
劉俊 2010-11-08
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 cao_julians 的回复:]
else(printf("Please input the correct number:\n"));
=====================================
else 后接语句而不是表达式
将()改为{}
[/Quote]
前辈,那后面还用不用加分号?
还有这一个问题是什么意思?:警告 31perf~1.c 20: 不能到达的代码在 main 函数中
cao_julians 2010-11-08
  • 打赏
  • 举报
回复
else(printf("Please input the correct number:\n"));
=====================================
else 后接语句而不是表达式
将()改为{}

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧