Weekly Project 5-array as data member (4), weekly 5-

Source: Internet
Author: User

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:

 

 

 

 

 

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.