* Copyright (c) 2015, Yantai University School of Computer * All rights reserved. * File name: Test.cpp *: Liu Chang * completion Date: March 21, 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 (3) Manually enter the salary?! It's so unbearable. Now the paper contains less than 500 workers ' wages salary.txt (download), read data from the file, complete the above work. * 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 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.show_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;}
Operation Result:
There is a case where the input box is not enough before the entry disappears. At first, there was no direct +500 of the increase in wages, the result of the output was thought to be wrong, and then changed. There should be no problem with the program,
3rd week Item 5-Array as data member (3)