美文网首页
C++ practical

C++ practical

作者: 徐凯_xp | 来源:发表于2018-11-02 22:08 被阅读0次
    file organization
    #include <iostream>
    
    float distance(float velocity, float acceleration, float time_elapsed);
    
    int main() {
    
        std::cout << distance(3, 4, 5) << std::endl;  
        std::cout << distance(7.0, 2.1, 5.4) << std::endl;
    
        return 0;   
    }
    
    float distance(float velocity, float acceleration, float time_elapsed) {
        return velocity*time_elapsed + 0.5*acceleration*time_elapsed*time_elapsed;
    }
    
    

    分成多个文件

    1. main.cpp and distance.cpp
    //#main.cpp
    #include <iostream>
    
    float distance(float velocity, float acceleration, float time_elapsed);
    
    int main() {
    
        std::cout << distance(3, 4, 5) << std::endl;  
        std::cout << distance(7.0, 2.1, 5.4) << std::endl;
        
        return 0;   
    }
    
    
    //distance.cpp
    float distance(float velocity, float acceleration, float time_elapsed) {
        return velocity*time_elapsed + 0.5*acceleration*time_elapsed*time_elapsed;
    }
    
    1. 头文件: main.cpp and distance.cpp and distance.h
      头文件放函数申明
    // 头文件 distance.h
    
    float distance(float velocity, float acceleration, float time_elapsed);
    
    // main.cpp
    #include <iostream>
    #include "distance.h"
    
    int main() {
    
        std::cout << distance(3, 4, 5) << std::endl;  
        std::cout << distance(7.0, 2.1, 5.4) << std::endl;
        
        return 0;   
    }
    
    // distance.cpp
    float distance(float velocity, float acceleration, float time_elapsed) {
        return velocity*time_elapsed + 0.5*acceleration*time_elapsed*time_elapsed;
    }
    
    
    reading in the Text Files
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <sstream>
    #include <vector>
    
    using namespace std;
    
    int main() {
    
        // initialize string variables for reading in text file lines 
        string line;
        stringstream ss;
    
        // initialize variables to hold the matrix
        vector < vector <float> > matrix;
        vector<float> row;
    
        // counter for characters in a text file line
        float i;
    
        // read in the file
        ifstream matrixfile ("matrix.txt");
    
        // read in the matrix file line by line
        // parse the file
    
        if (matrixfile.is_open()) {
            while (getline (matrixfile, line)) {
    //getline() 读取每一行,每行都放在名为" line"的变量中
                // parse the text line with a stringstream
                // clear the string stream to hold the next line
                ss.clear();
                ss.str("");
                ss.str(line);
                row.clear();
    
                // parse each line and push to the end of the row vector
                while(ss >> i) {
                    row.push_back(i);
    
                    if (ss.peek() == ',' || ss.peek() == ' ') {
    // ss.peak() 查看下一个字符
                        ss.ignore();
                    }
                }
    
                // push the row to the end of the matrix
                matrix.push_back(row);
            }
    
            matrixfile.close();
    
            // print out the matrix
            for (int row = 0; row < matrix.size(); row++) {
                for (int column = 0; column < matrix[row].size(); column++) {
                    cout << matrix[row][column] << " " ;
                }
                cout << endl; 
            }
        }
    
        else cout << "Unable to open file";
    
        return 0;
    }
    
    
    // matrix.txt
    1, 6, 2, 10.5
    11, 15.2, 2, 21
    3, 9, 1, 7.5
    
    输出到文本文件
    #include <iostream>
    #include <fstream>
    #include <vector>
    
    using namespace std;
    
    int main() {
    
        // create the vector that will be outputted
        vector < vector <int> > matrix (5, vector <int> (3, 2));
        vector<int> row;
    
        // open a file for outputting the matrix
        ofstream outputfile;
        outputfile.open ("matrixoutput.txt");
    
        // output the matrix to the file
        if (outputfile.is_open()) {
            for (int row = 0; row < matrix.size(); row++) {
                for (int column = 0; column < matrix[row].size(); column++) {
                    if (column != matrix[row].size() - 1) {
                        outputfile << matrix[row][column] << ", ";
                    }
                    else {
                        outputfile << matrix[row][column];
                    }//if语句正在检查是否到达行的末尾。如果当前值是行的结尾,则不必在数字后面加上逗号分隔符:
                }
                outputfile << endl; 
            }
        }
    
        outputfile.close();
    
        return 0;
    }
    

    相关文章

      网友评论

          本文标题:C++ practical

          本文链接:https://www.haomeiwen.com/subject/gancxqtx.html