C++ 一些在工作中遇到的小问题的c++解决脚本程序,记录用的

文章目录

  • 前言
  • 1 从"txt"文件中读取数组
  • 2 线性插值生成数组后存入“.txt”文件


前言

本博客用于记录在工作中遇到的一些C++小脚本。


1 从"txt"文件中读取数组

txt内文本如下图所示,按行记录数据,每组数据包含第一列为Index即索引号,第二列为value数值,Index数值没有重复的。
C++ 一些在工作中遇到的小问题的c++解决脚本程序,记录用的_第1张图片

代码如下(示例):

#include 
#include 
#include 
#include 

using namespace std;

int main() {
    const int ARRAY_SIZE = 4096;  // 数组大小
    float Array[ARRAY_SIZE] = {};  // 数组初始化为全0

    string filename = "./T1.125F5.6.txt";  // 文件名  使用'./'应放置在build的文件夹内
    ifstream input_file(filename);  // 打开文件

    if (!input_file.is_open()) {  // 检查文件是否打开成功
        cerr << "Unable to open file " << filename << endl;
        return 1;
    }

    string line;
    while (getline(input_file, line)) {  // 逐行读取文件内容
        istringstream iss(line);
        int index;
        float value;
        if (iss >> index >> value) {  // 将读取到的字符串转换为整数和浮点数
            if (index < 0 || index >= ARRAY_SIZE) {  // 检查索引是否越界
                cerr << "Invalid index " << index << endl;
            } else {
                Array[index] = value;  // 存储数据到数组中
            }
        } else {
            cerr << "Invalid line: " << line << endl;
        }
    }

    input_file.close();  // 关闭文件

    // 打印数组内容
    for (int i = 0; i < ARRAY_SIZE; i++) {
        cout << "Array[" << i << "] = " << Array[i] << endl;
    }

    return 0;
}

2 线性插值生成数组后存入“.txt”文件

实现的效果如下图,从左图到右图的过程,先读取左图内的数据,通过线性插值运算后得到每个索引下的value,并记录入右图的txt中去。
C++ 一些在工作中遇到的小问题的c++解决脚本程序,记录用的_第2张图片
C++ 一些在工作中遇到的小问题的c++解决脚本程序,记录用的_第3张图片

#include 
#include 
#include 
#include 
#include 

using namespace std;

const int ARRAY_SIZE = 4096;
const int KNOWN_POINTS = 12;
// 初始化一个包含4096个元素的数组,所有元素初始值为0.0
float data_array[ARRAY_SIZE] = {0.0};

string filename = "./T2.25F5.6.txt";  // 文件名
string filename_out_2 = filename;
string filename_out = filename_out_2.insert(filename_out_2.find("T"), "Out_");


// 线性插值函数
float linearInterpolation(float x0, float y0, float x1, float y1, float x) {
    return y0 + (x - x0) * (y1 - y0) / (x1 - x0);
}

void get_data(int *known_x, float* known_y)
{
    // 对剩余的未知数值进行插值
    for (int i = 0; i < ARRAY_SIZE; i++)
    {
        if (i < known_x[0])
        {
            // 如果当前位置在已知数值的左侧,则将当前位置的值设为第一个已知数值
            data_array[i] = known_y[0];
            ofstream outFile(filename_out,ios::app);
            outFile << i <<" "<<data_array[i]<<endl;
            outFile.close();
        }
        else if (i > known_x[KNOWN_POINTS - 1])
        {
            // 如果当前位置在已知数值的右侧,则将当前位置的值设为最后一个已知数值
            data_array[i] = known_y[KNOWN_POINTS - 1];
            ofstream outFile(filename_out,ios::app);
            outFile<<i<<" "<<data_array[i]<<endl;
            outFile.close();
        }
        else
        {
            // 在已知数值的区间内进行线性插值
            int j = 0;
            while (i > known_x[j + 1])
            {
                j++;
            }
            data_array[i] = linearInterpolation(known_x[j], known_y[j], known_x[j + 1], known_y[j + 1], i);
            cout <<"data_array[" << i << "]" << data_array[i] <<endl;
            ofstream outFile(filename_out,ios::app);
            outFile<<i<<" "<<data_array[i]<<endl;
            outFile.close();
        }
    }
}

int main()
{
    const int ARRAY_SIZE = 12;  // 数组大小
    int Index[ARRAY_SIZE] = {};  // 索引数组初始化为全0
    float Array[ARRAY_SIZE] = {};  // 数组初始化为全0


    ifstream input_file(filename);  // 打开文件

    if (!input_file.is_open()) {  // 检查文件是否打开成功
        cerr << "Unable to open file " << filename << endl;
        return 1;
    }

    string line;
    int i = 0;  // 索引数组的当前索引
    while (getline(input_file, line))
    {
        cout<<line<<endl;
        // 逐行读取文件内容
        istringstream iss(line);
        int index;
        float value;
        if (iss >> index >> value)
        {
            // 将读取到的字符串转换为整数和浮点数
            if (i >= ARRAY_SIZE)
            {  // 检查数组是否已满
                cerr << "Array is full" << endl;
                break;
            }
            Index[i] = index;  // 存储索引到数组中
            Array[i] = value;  // 存储数据到数组中
            i++;
        }
        else
        {
            cerr << "Invalid line: " << line << endl;
        }
    }

    input_file.close();  // 关闭文件

    // 打印数组内容
    for (int i = 0; i < ARRAY_SIZE; i++) {
        cout << "Index[" << i << "] = " << Index[i] << endl;
        cout << "Array[" << i << "] = " << Array[i] << endl;
    }

    get_data(Index, Array);
    return 0;
}

你可能感兴趣的:(c++,算法,开发语言)