zoukankan      html  css  js  c++  java
  • C++复习-练习-1

    上周做多媒体技术的作业,JPEG编码问题:FDCT、量化、逆量化和IDCT,只是简单套公式,但还是感觉自己C++好渣。。。太久没做,手生了,可怕可怕。

    所以复习了下文件操作和。。基础操作。这里贴一些当时被坑到的地方==


    1. 文件操作

    #include <fstream>
    
    ifstream infile("E://Desktop/SourceData.txt");
    ofstream outfile1("E://Desktop/First.txt");
    
    int i = 0;
    string temp = "";
    // read from SourceData.txt
    while (!infile.eof()) {
    getline(infile, temp);
    for (int j = 0 ; j < 16 ; j++)
    dataSource[i][j] = toDec(temp[3*j], temp[3*j+1]) - 128;
    i++;
    }
    infile.close();
    
    // output data (before FDCT)
    for (int i = 0 ; i < 16 ; i++) {
    for (int j = 0 ; j < 16; j++) {
    outfile1 << setw(5) << dataSource[i][j] << " ";
    if (j == 7)
    outfile1 << " ";
    }
    if (i == 7)
    outfile1 << endl;
    outfile1 << endl;
    }
    outfile1.close();

    //***********************写文件***************

    #include <cstring> // for c_str()
    
    void write(char* str) {
    ofstream out(str); //这里要用字符数组的。。字符串不行
    out << "Hello" << endl;
    out.close(); 
    }
    
    char file[256];
    string str = "E://Desktop/Test-for-file.txt";
    strcpy(file, str.c_str()); //把字符串转为字符数组,copy到字符数组中
    
    write(file);

    #include <cmath>
    #define PI 3.14159265

    3. 输出格式控制

    #include <iomanip>
    
    setw()
    
    fixed << setprecision(2)这种是直接截断,四舍五入另外写

  • 相关阅读:
    nginx1配置文件
    div中添加滚动条
    django错误笔记——1242 Subquery returns more than 1 row
    Django中合并同一个model的多个QuerySet
    js正则匹配数字字母汉字
    django错误笔记——URL
    python发送邮件
    SMTP——MIME
    Python读取Excel中的数据并导入到MySQL
    css3选择器
  • 原文地址:https://www.cnblogs.com/pxy7896/p/5984993.html
Copyright © 2011-2022 走看看