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)这种是直接截断,四舍五入另外写

  • 相关阅读:
    python第七十九天--第十四周作业
    python第七十七天---HTML
    python第七十六天--堡垒机完成
    python第七十一天---堡垒机
    python第六十八天--第十二周作业
    XmlHepler(拿去就能用)
    .NET中代理服务器WebProxy的各种用法
    XML VS DataSet
    C#操作XML方式
    C#读取XML方式
  • 原文地址:https://www.cnblogs.com/pxy7896/p/5984993.html
Copyright © 2011-2022 走看看