3rd week Item 5-Array as data member (4)

Source: Internet
Author: User

*   Copyright (c) 2015, Yantai University School of Computer   * All rights reserved.   * File name: Test.cpp   *:    Liu Chang    * completion date: March 22, 2015   * version number: v1.0   *  * Problem Description: Read the textbook P255 Example 8.4, notice that a data member in a class can be an array 。 Design a Payroll class (Salary), where the data members of the class are as follows:     class Salary     {     private:         double salarys[50];//multi-person wage         int number ;  Actual number     };     The member functions to be designed are:     void Set_salarys (): Enter the employee's salary (input-1 marks the end of the payroll input), the wages are saved to the salary array, the actual number is saved to the numbers;     void Add_salarys ( int x): Give everyone a raise x yuan pay     void Sort_salarys (): Sort the payroll     void Show_salarys (): Show payroll information (4) Add a member function to save the sorted result to a file. * Input Description: NULL; * Program output: Output as required.


The code is as follows:

#include <iostream> #include <fstream> #include <cstdlib>using namespace Std;class salary{public:             void Set_salarys ();    Enter the employee's salary (input-1 is the end of the payroll input), and the payroll is saved to the salary array, and the actual number is saved in the numbers.         void Add_salarys (int x);            Give everyone a raise x yuan pay void Sort_salarys ();            Sort of the Payroll void Show_salarys ();            Show payroll information void Save_salarys ();              Store payroll information private:double salarys[500];                       Multi-person salary int number;    Actual number};int Main () {Salary s;    int x;    S.set_salarys ();    S.show_salarys ();    cout<< "Please enter the amount of salary to be increased:";    cin>>x;    S.add_salarys (x);    S.sort_salarys ();    S.save_salarys (); return 0;}    void Salary::set_salarys () {int i=0;    Ifstream infile ("Salarys.txt", ios::in); if (!infile) {cerr<< "Open error!"        <<endl;    Exit (1);    } while (Infile>>salarys[i]) {i++;    } number=i; Infile.close ();}    void Salary::add_salarys (int x) {int i; For(i=0;i<number;i++) salarys[i]+=x;}    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])//from high to low row                column {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]<<endl; Cout<<endl;}    void Salary::save_salarys () {int i;   Ofstream outfile ("Salary_data.txt", ios::out); Open file as input if (!outfile)//test successfully opened {cerr<< "open error!"        <<endl;    Exit (1);    } for (i=0; i<number; ++i) {outfile<<salarys[i]<<endl; } outfile.close ();}


Operation Result:

3rd week Item 5-Array as data member (4)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.