第8週目プロジェクト1-配列データメンバー(3)

2474 ワード

問題およびコード:
main:
/*
 *Copyright (c) 2016,         
 *All rights reserved.
 *    :zwj.cpp
 *      :   
 *    :2016 4 17 
 *     :v1.0
 *
 *    :        ,          ,         ,               ,           
 *    :
 *    :          
 */
#include<iostream>
#include<fstream>
#include<cstdlib>
#include"class.h"
using namespace std;
//const int N=500;

int main()
{
    Salary gongzi;
    gongzi.read_data();
    gongzi.sort_salarys();
    gongzi.show_salarys();
    gongzi.save_salarys();
    return 0;
}

classクラス:
#ifndef CLASS_H_INCLUDED
#define CLASS_H_INCLUDED

#include<iostream>
#include<fstream>
#include<cstdlib>
using namespace std;
const int N=500;
class Salary
{
public:
    void read_data();
    void write_data();
    void sort_salarys();
    void show_salarys();
    void save_salarys();
private:
    double salarys[N];
    int number;
};

#endif // CLASS_H_INCLUDED

file :
#include<iostream>
#include<fstream>
#include<cstdlib>
#include"class.h"
using namespace std;
//const int N=500;

void Salary::read_data()
{
    int i;
    ifstream infile("salary.txt",ios::in);
    if(!infile)
    {
        cerr<<"open error!"<<endl;
        exit(1);
    }
    i=0;
    while(infile>>salarys[i])
        i++;
    number=i;
    infile.close();
}
void Salary::save_salarys()
{
    int i;
    ofstream outfile("salary2.txt",ios::out);
    if(!outfile)
    {
        cerr<<"save error!"<<endl;
        exit(1);
    }
    for(i=0;i<number;i++)
        outfile<<salarys[i]<<" ";
    outfile.close();
    cout<<"         "<<endl;
}

sort_show :
#include<iostream>
#include<fstream>
#include<cstdlib>
#include"class.h"
using namespace std;
//const int N=500;
void Salary::sort_salarys()
{
    int i,j;
    double t;
    for(i=0;i<number-1;i++)
    {
        for(j=0;j<number-i-1;j++)
        {
            if(salarys[j]<salarys[j+1])
            {
                t=salarys[j];
                salarys[j]=salarys[j+1];
                salarys[j+1]=t;
            }
        }
    }
}
void Salary::show_salarys()
{
    int i;
    for(i=0;i<number;i++)
        cout<<salarys[i]<<"\t";
}