#include<stdio.h> int main() { double r; int a,b,c,t; scanf("%d%d%d",&a,&b,&c); t = (a+b+c) / 2 ; r = t (t-a) (t-b) (t-c) / r; printf("%f",r); return 0; } 分析上面错误
时间: 2024-04-14 15:28:20 浏览: 34
这段代码存在几个错误:
1. 在计算面积的公式中,你使用了变量 `r`,但是在赋值之前就尝试使用它。这会导致错误。你需要先计算面积,然后再将结果赋给 `r`。
2. 在计算面积的公式中,你没有使用乘法运算符 *。你需要在乘法操作符前后使用 *。
下面是修正后的代码:
```c
#include <stdio.h>
int main() {
double r;
int a, b, c, t;
scanf("%d%d%d", &a, &b, &c);
t = (a + b + c) / 2;
r = t * (t - a) * (t - b) * (t - c);
printf("%f", r);
return 0;
}
```
修正后的代码将正确计算三角形的面积并输出结果。
相关问题
C语言中#include <stdlib.h> #include <conio.h> #include <stdio.h> double fun(int m) {double t=1.0; int i; for(i=2;i<=m;i++) t+=1.0/i; return i; } void main() ( int m; system("CLS"); printf("\nPlease enter linteger number:"); scanf("%d",&m); printf("\nThe resultis %1f\n",fun(m)));
这段代码中有一个语法错误,函数fun的返回值类型应该是double,但是代码中返回的是整型变量i,应该改为返回变量t。另外,代码中的“;”符号也需要修改,应该改为英文的分号。
正确的代码如下所示:
```c
#include <stdlib.h>
#include <conio.h>
#include <stdio.h>
double fun(int m) {
double t = 1.0;
int i;
for(i = 2; i <= m; i++) {
t += 1.0 / i;
}
return t;
}
void main() {
int m;
system("CLS");
printf("\nPlease enter integer number:");
scanf("%d", &m);
printf("\nThe result is %1f\n", fun(m));
}
```
这段代码的功能是计算从2到m的分数和,其中m是用户输入的整数。函数fun的实现中使用了循环语句for来计算分数和,最后返回分数和的值。主函数main中调用函数fun,并输出计算结果。
#include<stdio.h> int main() { double r; int a,b,c,t; scanf("%d%d%d",&a,&b,&c); t = (a+b+c) / 2 ; area = t (t-a) (t-b) (t-c) / r; printf("%f",r); return 0; }
这段代码存在一些错误。首先,你声明了一个变量 `r`,但是没有给它赋初值。其次,你使用了一个未声明的变量 `area`,我猜测你可能想计算三角形的面积并将结果赋给它。最后,你在计算面积的公式中引用了变量 `r`,但是你没有给它赋值。
下面是修正后的代码:
```c
#include <stdio.h>
#include <math.h>
int main() {
double a, b, c, t, area;
scanf("%lf%lf%lf", &a, &b, &c);
t = (a + b + c) / 2;
area = sqrt(t * (t - a) * (t - b) * (t - c));
printf("%lf", area);
return 0;
}
```
这段代码通过 `scanf` 函数从输入中获取三角形的三边长,并使用海伦公式计算三角形的面积。修正后的代码会正确地计算并输出三角形的面积。
相关推荐
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)