You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C语言树高计算程序scanf仅读1值及编译报错问题求解

问题1:编译报错"called object is not a function or function pointer"

产生原因

C语言中乘法运算不能省略乘号,你写的(shorty_to_lefty/lofty_to_tree)(lofty-shorty)语法会被编译器识别为调用名为(shorty_to_lefty/lofty_to_tree)的函数,但这是一个float类型的计算结果,不是函数指针,因此触发报错。

修复方案

在两个括号之间补乘号*即可,若你的树高计算逻辑是基于相似三角形原理,还可以同时约掉分子分母重复的shorty_to_lefty简化计算:

// 语法修复后的计算式,可根据实际数学公式调整
arbre= lofty + (shorty_to_lefty / lofty_to_tree) * (lofty - shorty) / shorty_to_lefty;
问题2:scanf仅能读取第一个输入值

产生原因

scanf的%f格式符不支持%0.2f这种带精度限制的写法,只有printf可以指定输出精度。scanf用%0.2f属于非法格式,会导致输入匹配失败,后续所有scanf调用都会因为输入缓冲区残留的非法内容直接跳过执行,变量保持初始值0,后续计算出现除以0的情况,才会输出-1.#J这种非法浮点值。

修复方案

把所有scanf里的%0.2f改成%f即可,输入值的精度限制可以在后续输出时通过printf处理:

scanf("%f", &shorty_to_lefty);
scanf("%f", &lofty_to_tree);
scanf("%f", &shorty);
scanf("%f", &lofty);
完整修正后的代码
#include <stdio.h>

// 标准C中main函数推荐返回int,void main属于非标准写法,兼容性差
int main()
{
     float shorty_to_lefty=0.0f , lofty_to_tree=0.0f, lofty=0.0f , shorty=0.0f , arbre=0.0f;

     printf("\nRentrer la distance entre le petit et le grand:" );
     scanf("%f", &shorty_to_lefty);
     printf("\nRentrer la distance entre le grand et l'arbre:" );
     scanf("%f", &lofty_to_tree);
     printf("\nRentrer la taille du petit:" );
     scanf("%f", &shorty);
     printf("\nRentrer la taille du grand:" );
     scanf("%f", &lofty);
     
    arbre= lofty + (shorty_to_lefty / lofty_to_tree) * (lofty - shorty) / shorty_to_lefty;
    printf("La taille de l'arbre est de %.2f m", arbre);
    return 0;
}

内容的提问来源于stack exchange,提问作者Mister-3

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 01:45:02