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

scanf无法识别换行符,求实现回车结束的点间总距离计算方案

嘿,我明白你遇到的问题了——scanf()确实有个“小坑”:它会自动跳过包括换行在内的所有空白字符,所以根本识别不了用户只按回车的操作。结合你要计算多点总距离的需求,给你两个实用的解决方案:

解决输入处理问题:识别空回车结束输入

方案一:用fgets()读取整行,再解析坐标(推荐!)

这种方法更稳妥,能直接判断用户是否输入了空行(只按回车),还能更好地处理各种输入异常。步骤很清晰:

  1. 用fgets()读取整行输入到缓冲区
  2. 检查读取到的内容是不是只有换行符——如果是,就结束输入循环
  3. 如果不是空行,用sscanf()从缓冲区里解析出点的x、y坐标

示例代码片段:

#include <stdio.h>
#include <string.h>
#include <math.h>

// 定义点的结构体,方便存储坐标
typedef struct {
    double x;
    double y;
} Point;

int main() {
    Point points[100]; // 假设最多存储100个点,可按需调整
    int point_count = 0;
    char input_buffer[100];

    printf("请输入点的坐标(格式:x y),直接按回车结束输入:\n");
    while (1) {
        // 读取整行输入
        if (fgets(input_buffer, sizeof(input_buffer), stdin) == NULL) {
            break; // 读取出错或遇到EOF,直接结束
        }
        // 判断是不是空行(用户只按了回车)
        if (strcmp(input_buffer, "\n") == 0) {
            break;
        }
        // 解析坐标到结构体里
        if (sscanf(input_buffer, "%lf %lf", &points[point_count].x, &points[point_count].y) == 2) {
            point_count++;
        } else {
            printf("输入格式不对,请重新输入「x y」格式的坐标哦\n");
        }
    }

    // 计算总距离的逻辑
    double total_distance = 0.0;
    for (int i = 1; i < point_count; i++) {
        double dx = points[i].x - points[i-1].x;
        double dy = points[i].y - points[i-1].y;
        total_distance += sqrt(dx*dx + dy*dy);
    }
    printf("这些点之间的总距离是:%.2lf\n", total_distance);

    return 0;
}

方案二:改进scanf()的使用,配合getchar()检测换行

如果你坚持想用scanf(),可以在每次读取后手动清空输入缓冲区,同时检查是否遇到了换行符(也就是用户只按了回车):

#include <stdio.h>
#include <math.h>

typedef struct {
    double x;
    double y;
} Point;

int main() {
    Point points[100];
    int point_count = 0;
    int input_result;
    char temp_char;

    printf("请输入点的坐标(格式:x y),直接按回车结束输入:\n");
    while (1) {
        // 尝试读取坐标
        input_result = scanf("%lf %lf", &points[point_count].x, &points[point_count].y);
        if (input_result == EOF) {
            break; // 读取出错或EOF
        } else if (input_result == 0) {
            // 没读到有效数字,检查是不是用户只按了回车
            while ((temp_char = getchar()) != '\n' && temp_char != EOF); // 清空缓冲区
            if (temp_char == '\n') {
                break; // 确认是空回车,结束输入
            }
        } else {
            point_count++;
            // 清空缓冲区里的剩余字符(比如输入坐标后按的回车)
            while ((temp_char = getchar()) != '\n' && temp_char != EOF);
        }
    }

    // 计算总距离
    double total_distance = 0.0;
    for (int i = 1; i < point_count; i++) {
        double dx = points[i].x - points[i-1].x;
        double dy = points[i].y - points[i-1].y;
        total_distance += sqrt(dx*dx + dy*dy);
    }
    printf("这些点之间的总距离是:%.2lf\n", total_distance);

    return 0;
}

小提示:

  • 编译的时候记得加上-lm参数(比如gcc your_code.c -o distance_calc -lm),因为用到了sqrt()函数,需要链接数学库
  • 可以根据实际需求调整点数组的最大容量

内容的提问来源于stack exchange,提问作者user9511542

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 11:25:20