Weekly Project 5-array as data member (4), weekly 5-
** Copyright (c) 2015, School of Computer Science, Yantai University * All rights reserved. * file name: test. cpp * Author: Liu Chang * Completion Date: July 15, March 22, 2015 * version No.: v1.0 ** Problem description: Read the P255 example 8.4, and notice that the data member in the class can be an array. Design a Salary class (Salary). The data members of the class are as follows: class Salary {private: double salarys [50]; // The int number of the employees; // the actual number of people }; the member functions to be designed include void set_salarys (): Enter the employee's salary (input-1 indicates the end of the wage input), save the salary to the salary array, and save the actual number of people to the number; void add_salarys (int x): raise the salary of x yuan for each user void sort_salarys (): sort the salary void show_salarys (): display the salary information (4) Add a member function, save the sorted results 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 ends with the standard salary input), save the salary to the salary array, and save the actual number of employees to the number. Void add_salarys (int x); // raise the salary of x yuan for each user void sort_salarys (); // sort the salary by void show_salarys (); // display the salary information void save_salarys (); // Save the Salary information private: double salarys [500]; // The int number of the employees; // the actual number of people}; int main () {Salary s; int x; s. set_salarys (); s. show_salarys (); cout <"Enter the expected salary:"; 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]) // arrange from high to low {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 the file as input if (! Outfile) // test whether {cerr <"open error! "<Endl; exit (1) ;}for (I = 0; I <number; ++ I) {outfile <salarys [I] <endl;} outfile. close ();}
Running result: