?? pay_average.h
字號:
//文件pay_average.h(顯示工資平均值函數pay_average()的實現。
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<iomanip>
#include<string>
using namespace std;
void pay_average()
{
int max; //定義一個變量,職工最大數變量max.
int wage_average; //定義了職工平均值變量。
int wage_sum=0; //定義了職工工資和變量。
ifstream fcin("max.dat"); //從文件讀取職工最大數。
if(fcin.fail())
{
cout<<"文件max.dat打開失敗,程序將結束"<<endl;
exit(1);
}
fcin>>max;
fcin.close();
struct Employee //Employee結構的定義。
{
int num;
string name;
string sex;
int age;
int wage; //定義結構體數據成員,wage(工資項)
};
Employee *employee=new Employee[max]; //定義動態結構體數組。
ifstream fin("name.dat"); //定義一個輸入流對象,用于讀取文件的記錄。
if(fin.fail()) //新建數據文件時不要在文件中多加空格或換行符號。
{
cout<<"文件name.dat打開失敗,程序將結束"<<endl;
exit(1);
}
int i=0; //記錄從文件中讀取的職工數。
while(!fin.eof()) //文件末尾檢查,如果文件讀取沒結束,返回0。
{ //即使事空格或換行符,eof函數也會把它當成字符。(注意)
fin>>employee[i].num>>employee[i].name
>>employee[i].sex>>employee[i].age>>employee[i].wage;
i++;
}
max=i; //從文件name.dat讀取當前的職工個數。
//cout<<"max="<<max<<endl; //測試此變量的值。
for(i=0;i<max;i++)
wage_sum+=employee[i].wage; //求取所有職工的工資和。
//cout<<"wage_sum="<<wage_sum<<endl; //測試此變量的值.
wage_average=wage_sum/max; //求工資平均值。
cout<<"**********************************************\n";
cout<<"********職工的平均月工資約為:"<<wage_average<<"元**********"<<endl;
cout<<"**********************************************\n";
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -