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

C++抛体运动程序日志序号添加及日志保存问题求助

问题描述

我是编程新手,写了一段计算抛体运动参数的C++代码,用double类型保证输入灵活性。现在遇到两个问题:

  • 程序生成的日志每次运行都会覆盖原有内容,没法保存历史记录
  • 想给logbook.txt里的每条日志前加递增序号(格式示例见下文),尝试过用getline()读取已有序号,但不知道怎么生成下一个序号,也不确定是否必须用getline()

原代码如下:

#include <iostream>
#define _USE_MATH_DEFINES
#include <math.h>
#include <fstream>
#include <string>

using namespace std;

void Math(double v, double angle);

int main()
{
    double velocity;
    double angle;

    cout << "input velocity(m/s): "<< "\n";
    cin >> velocity;   
    cout << "input angle(deg): "<< "\n";
    cin >> angle;

    Math(velocity, angle);
}

void Math(double v, double angle)
{
    double g = 9.8;
    double Vx = v*cos(angle * M_PI/180);
    double Vy = v*sin(angle * M_PI/180);

    double s;
    double t;
    double h;

    h = (Vy*Vy)/(2*g);
    t = 2*Vy/g;
    s = Vx*t;
    
    cout << "distance traveled: " << s << " meters" << "\n";
    cout << "maximum height reached: "<< h << " meters" << "\n";
    cout << "time of travel: " << t << " seconds" << "\n";

    ofstream logbook("logbook.txt");
    logbook<< "\n";//instead of this line I want the No.
    logbook << "distance traveled: " << s << " meters" << "\n";
    logbook << "maximum height reached: "<< h << " meters" << "\n";
    logbook << "time of travel: " << t << " seconds" << "\n";
}

期望日志格式:

log 1.
distance traveled: 883.699 meters
maximum height reached: 127.551 meters
time of travel: 10.2041 seconds
log 2.
...

解决方案

1. 解决日志覆盖问题

你当前用ofstream logbook("logbook.txt");打开文件时,默认是覆盖模式,每次运行都会清空原有内容重新写入。要改成追加模式,只需在打开时加上ios::app参数:

ofstream logbook("logbook.txt", ios::app);

ios::app的作用是让新内容直接追加到文件末尾,不会覆盖之前的日志。

2. 生成递增序号

要实现序号递增,需要先读取现有日志里的最大序号,再加1作为新日志的序号。这里确实需要用getline()逐行读取文件内容,检查每一行是否是日志标题行,提取其中的数字。

修改后的完整代码

#include <iostream>
#define _USE_MATH_DEFINES
#include <math.h>
#include <fstream>
#include <string>
#include <sstream>

using namespace std;

// 获取下一个日志序号
int getNextLogNumber() {
    int maxNum = 0;
    ifstream logFile("logbook.txt");
    string line;

    // 逐行读取文件
    while (getline(logFile, line)) {
        // 检查行是否以"log "开头
        if (line.substr(0, 4) == "log ") {
            // 提取数字部分:从第4个字符开始,到"."结束
            string numStr = line.substr(4, line.find('.') - 4);
            // 把字符串转成整数
            int num = stoi(numStr);
            if (num > maxNum) {
                maxNum = num;
            }
        }
    }
    // 下一个序号是最大序号+1
    return maxNum + 1;
}

void Math(double v, double angle);

int main() {
    double velocity;
    double angle;

    cout << "input velocity(m/s): " << "\n";
    cin >> velocity;   
    cout << "input angle(deg): " << "\n";
    cin >> angle;

    Math(velocity, angle);
    return 0;
}

void Math(double v, double angle) {
    double g = 9.8;
    double Vx = v * cos(angle * M_PI / 180);
    double Vy = v * sin(angle * M_PI / 180);

    double s = Vx * 2 * Vy / g;
    double t = 2 * Vy / g;
    double h = (Vy * Vy) / (2 * g);
    
    cout << "distance traveled: " << s << " meters" << "\n";
    cout << "maximum height reached: " << h << " meters" << "\n";
    cout << "time of travel: " << t << " seconds" << "\n";

    // 获取下一个日志序号
    int logNum = getNextLogNumber();
    // 以追加模式打开日志文件
    ofstream logbook("logbook.txt", ios::app);
    // 写入序号标题
    logbook << "log " << logNum << ".\n";
    logbook << "distance traveled: " << s << " meters\n";
    logbook << "maximum height reached: " << h << " meters\n";
    logbook << "time of travel: " << t << " seconds\n\n"; // 加空行分隔不同日志
}

关键说明

  • getNextLogNumber()函数专门负责读取并计算下一个序号,逻辑清晰且复用性强
  • 用substr()和find()提取序号数字,无需复杂正则,新手易理解
  • 追加模式ios::app确保历史日志不丢失
  • 最后添加空行\n\n,让不同日志之间分隔更清晰,符合示例格式

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 12:18:12