zoukankan      html  css  js  c++  java
  • C++ 面向对象: I/O对象的应用

    1. 补充完整从text.txt文件读取数据后,再写入in.txt文件。
    2. 完成从text.txt文件读取数据后,进行排序,再写入in.txt文件。数据量自行设定。
    3. 请解决在主程序文件中加载多个头文件时,命名冲突问题。

    解决方案

    用随机生成函数生成数据写入文件流,

    然后再读取文件流,讲数据存入 num[],排序,

    将 num[] 中数据写入新文件,

    不在全局打开 std 命名空间,在调用时使用 std::xxx 等方法。

    代码

    main.cpp

    #include <iostream>
    #include <string>
    #include "file.hpp"
    int main(int argc, const char * argv[])
    {    int n;    std::cin >> n;    int *num = new int[n];  
        std::string text = "text.txt";  
        std::string in = "in.txt";
    
        File f = File(text, n, num);
    
        f.randDate();
        f.sortDate();
    
        File f_ = File(in, n, num);
    
        f_.coutDate();    return 0;
    }

    file.hpp

    #ifndef file_hpp
    #define file_hpp
    #include <stdio.h>
    #include <string>
    #include <fstream>
    class File{
        std::string fileName;   //  文件名
        int n;                  //  数据量
        int *num;public:
        File(std::string fileName_, int n_, int *num_) :
        fileName(fileName_), n(n_), num(num_) {}    void randDate();                    //  生成随机数据文件 fileName
        void sortDate();                    //  读取文件 fileName 中的随机序列并排序,存入 num
        void coutDate();                    //  将 num 中数据存储到 fileName 文件中
    
        int random(double st, double ed);   //  生成 st~ed 随机数};#endif /* file_hpp */

    file.cpp

    #include "file.hpp"
    #include <algorithm>
    #include <ctime>
    #include <cstdlib>
    #define S 0
    #define T 1000000//  生成随机数据文件void File::randDate()
    {    std::fstream fp(fileName, std::ios::out);    while (!fp)
        {
            fp.open(fileName, std::ios::out);
        }
    
        srand(unsigned(time(0)));    int k = n;    while (k--)
        {
            fp << random(S, T) << '
    ';
        }
    
        fp.close();
    }//  读取数据文件,存入num,并排序void File::sortDate()
    {    std::fstream fp(fileName, std::ios::in);    while (!fp)
        {
            fp.open(fileName, std::ios::in);
        }    int k = 0;    while (fp >> num[k++]) {}    std::sort(num, num + n);
    
        fp.close();
    }//  将 num 中数据存入文件中void File::coutDate()
    {    std::fstream fp(fileName, std::ios::out);    while (!fp)
        {
            fp.open(fileName, std::ios::out);
        }    for (int i = 0; i < n; i++)
        {
            fp << num[i] << '
    ';
        }
    
        fp.close();
    }//  生成随机数,随机数在 st~ed 之内int File::random(double st, double ed)
    {    
    return (int)st + (ed - st) * rand() / (RAND_MAX + 1.0);
    }
  • 相关阅读:
    Siege 3.0 正式版发布,压力测试工具
    Pomm 1.1.2 发布,专为 PG 设计的 ORM 框架
    Whonix 0.5.6 发布,匿名通用操作系统
    国内开源 java cms,Jspxcms 2.0 发布
    EZNamespaceExtensions.Net v2013增加对上下文菜单、缩略图、图标、属性表的支持
    GNU Guile 2.0.9 发布,Scheme 实现
    jdao 1.0.4 发布 轻量级的orm工具包
    OpenSearchServer 1.4 RC4 发布
    Percona Server for MySQL 5.5.3030.2
    Samba 4.0.5 发布
  • 原文地址:https://www.cnblogs.com/YangGC/p/6807425.html
Copyright © 2011-2022 走看看