這是我寫的最長的一段代碼,也是我使用的最基本的知識。很多要求我只達到的最低的標準。
描述:
說明:一個員工工資管理程序,有新建、存儲、顯示、修改和刪除的功能。
要求:
1) 利用工號區別員工身份,工號用字符數組的形式,從000001開始編號,該工號不能人工修改,是由計算機自動根據錄入的先後複製的,錄入數據時不必輸入工號。
2) 可以選擇現實的多種排序形式,如按姓名的順序、工資總額的順序、職務工資或獎金的順序。
3) 每次顯示時,將所有要顯示數據的工資總額、職務工資總額、獎金總額一起計算顯示。
4) 可以根據工號或姓名修改、刪除數據,在修改、刪除時現要求用戶確認,確認後再進行操作。
這是我的一個作業
缺陷://調試出來了
1、完全是由結構體做的
2、一個函數
3、不能實時保存,只是結束後單純的保存
4、不能讀取,保存後重新新建的文檔後,之前的就沒了
5、工號有問題
6、存儲因爲是結構體,只能存取一定量的數據,這些數據全都是自己用N定的。存取的數據需要去自己改。
7、調試了很長時間,自己因爲只是知道概念,而導致,作業不夠完善,經常出錯。
8、倆個作業,第一個基礎點,在選擇的第二個作業,自己使用類,讓自己做得更加的完善。
想法:
做下一個作業,使用類,函數不只放一個地方,主函數,只調用函數。
做的時候,讓自己可以讀取之前保存的數據。
可以使用讀取的指針的位置。
//
#include<iostream>
#include<string>
#include<fstream>
#define N 20
using namespace std;
struct sale
{
char num[6]="00000";
bool flag = 0;//證明是否有數據存在
char name[10]="abc";
int gongzi=999;//職務工資
int all=1000;//職務總額
int prize=1;//獎金
typename int *zhizheng;
}*head, *inputposition, *checkposition;
int input_message(sale people[])
{
checkposition = people;
int shuzi = 1;
for (int i = 0; i < N; i++)//輸入工號
{
int j;
for (j = 0; j < 5; j++)
{
people[i].num[j] = '0';
}
if (shuzi < 10)people[i].num[j - 1] = '0' + shuzi;
else {
people[i].num[j - 2] = '0' + shuzi / 10;
people[i].num[j - 1] = '0' + shuzi % 10;
}
shuzi++;
people[i].num[6] = '\0';
}
//for (int i = 0; i < N; i++)
//{
// cout << people[i].num << endl;
//}
int f = 1;
cout << "輸入信息請按1,修改信息請按2,顯示工資請按3,存儲信息請按4" << endl;
cin >> f;
while (f)
{
static int i2 = 0;
int i = 0;//case 1中的輸入定位
int f1 = 1;//case 1中的
int f2 = 1;//case2中的
int f3 = 1;//case選擇f3
int xuhao = 0;//case3
sale temppeople[N];//存放排序的數
sale temp;//存放完全的臨時數據
switch (f)
{
case 1:
for (i2; i2<20 && f1 != 0; i2++)
{
//cout << i2<<endl;
cout << "請輸入名字" << endl;
cin >> people[i2].name;
cout << "請輸入工資" << endl;
cin >> people[i2].gongzi;
cout << "請輸入獎金" << endl;
cin >> people[i2].prize;
people[i2].all = people[i2].gongzi + people[i2].prize;
people[i2].flag = 1;
cout << "輸入下一個數據請按1,退出請按0;" << endl;
cin >> f1;
}
break;
case 2://修改信息
while (f2)
{
cout << "現在的存儲的信息是(按0退出1繼續):" << endl;
checkposition = people;
for (int j = 0; j < i-1; j++)people[j].flag = 1;
i = 0;
cout << "\t\t" << people[i].name << "\t\t" << people[i].gongzi << "\t\t" << people[i].prize << "\t\t" << people[i].all << "\t\t" << people[i].flag << endl;///
cout << "==========================================================================================" << endl;
cout << "序號\t\t" << "工號\t\t" << "名字\t\t" << "工資\t\t" << "獎金\t\t" << "總工資" << endl;
xuhao = 1;
for (i = 0; i<N&&people[i].flag != 0; i++)//輸出
{//通過xuhao可以實現從上到下或者從下到上的排序。此次只固定排序。
cout << xuhao << "\t\t";
for (int j = 0; j<5; j++)
cout << people[i].num[j];
cout << "\t\t" << people[i].name << "\t\t" <<people[i].gongzi << "\t\t" << people[i].prize << "\t\t" << people[i].all << "\t\t"<<people[i].flag<<endl;
xuhao++;
}
cout << "==========================================================================================" << endl;
cin >> f2; if (f2 == 0)break;
cout << "選擇刪除按1,選擇修改按2" << endl;
int f2f = 0;
int f3f = 1;
cin >> f2f;
int xuhao2;
sale temp = people[0];//臨時數據
switch (f2f)
{
case 1://刪除
cout << "根據序號刪除按1,根據名字刪除按2" << endl;
cin >> f3f;
if (f3f == 1) {
cout << "輸入刪除序號" << endl;
cin >> xuhao2;
int i;//判斷位置
for (i = xuhao2 - 1; i < 20 && people[i].flag == 1; i++)
{
people[i] = people[i + 1];
//cout << i<<"\t\t" << people[i].name << "\t\t" << people[i].gongzi << "\t\t" << people[i].prize << "\t\t" << people[i].all<<"\t\t"<<people[i].flag << endl;
}
}
else {
cout << "輸入刪除的名字" << endl;
char tempname[10];
cin >> tempname;
int T;
for ( T = 0; T < 20 && people[T].flag != 0; T++)
{
if (strcmp(tempname, people[T].name) == 0)break;
}
if (T == 20) {
cout << "沒找到" << endl; break;
}
for (i = T - 1; i < 20 && people[i].flag == 1; i++)
{
people[i] = people[i + 1];
//cout << i<<"\t\t" << people[i].name << "\t\t" << people[i].gongzi << "\t\t" << people[i].prize << "\t\t" << people[i].all<<"\t\t"<<people[i].flag << endl;
}
}
shuzi = 1;
i2 = i2 - 1;
for (int i = 0; i < N; i++)//重置工號
{
int j;
for (j = 0; j < 5; j++)
{
people[i].num[j] = '0';
}
if (shuzi < 10)people[i].num[j - 1] = '0' + shuzi;
else {
people[i].num[j - 2] = '0' + shuzi / 10;
people[i].num[j - 1] = '0' + shuzi % 10;
}
people[i].num[6] = '\0';
shuzi++;
}
break;
case 2://修改
cout << "輸入修改的序號:";
cin >> xuhao2;
cout << "之前的名字是"<<people[xuhao2-1].name<<" "<<"輸入修改後的名字" << endl;
cin >> people[xuhao2-1].name;
cout << "之前的工資是" << people[xuhao2 - 1].gongzi << " " << "輸入修改後的工資" << endl;
cin >> people[xuhao2-1].gongzi;
cout << "之前的工資是" << people[xuhao2 - 1].prize << " " << "輸入修改後的獎金" << endl;
cin >> people[xuhao2-1].prize;
people[xuhao2-1].all = people[xuhao2-1].gongzi + people[xuhao2-1].prize;
break;
}
}break;
case 3://默認的排序,姓名的排序,工資總額的排序,職務工資的排序,獎金的排序。
cout << "顯示工資方式:\n工號順序請按1\n工資總額請按2\n職務工資請按3\n獎金順序請按4\n名字順序請按5" << endl;
for (int q = 0; q<N&&people[q].flag != 0; q++)
temppeople[q] = people[q];
cin >> f3;
while (f3)
{
if (f3 == 1)
{
cout << "===================================按工號順序==================================================" << endl;
cout << "序號\t\t" << "工號\t\t" << "名字\t\t" << "工資\t\t" << "獎金\t\t" << "總工資" << endl;
//按工號順序輸出
}
else if (f3 == 2)
{
cout << "===================================按總額順序=================================================" << endl;
cout << "序號\t\t" << "工號\t\t" << "名字\t\t" << "工資\t\t" << "獎金\t\t" << "總工資" << endl;
for (int i = 0; i < N&&temppeople[i].flag != 0; i++)
{
int k = i;
for (int j = i; temppeople[j].flag != 0; j++)
{
if (temppeople[k].all < temppeople[j].all)k = j;
}
sale linshi;
linshi = temppeople[i];
temppeople[i] = temppeople[k];
temppeople[k] = linshi;
}
}
else if (f3 == 3)
{
cout << "===================================按工資順序=================================================" << endl;
cout << "序號\t\t" << "工號\t\t" << "名字\t\t" << "工資\t\t" << "獎金\t\t" << "總工資" << endl;
for (int i = 0; i < N&&temppeople[i].flag != 0; i++)
{
int k=i;
for (int j = i; temppeople[j].flag!=0; j++)
{
if (temppeople[k].gongzi < temppeople[j].gongzi)k=j;
}
sale linshi;
linshi = temppeople[i];
temppeople[i]=temppeople[k];
temppeople[k] =linshi;
}
}
else if (f3 == 4)
{
cout << "===================================按獎金順序=================================================" << endl;
cout << "序號\t\t" << "工號\t\t" << "名字\t\t" << "工資\t\t" << "獎金\t\t" << "總工資" << endl;
for (int i = 0; i < N&&temppeople[i].flag != 0; i++)
{
int k = i;
for (int j = i; temppeople[j].flag != 0; j++)
{
if (temppeople[k].prize < temppeople[j].prize)k = j;
}
sale linshi;
linshi = temppeople[i];
temppeople[i] = temppeople[k];
temppeople[k] = linshi;
}
}
else if (f3 == 5)
{
cout << "===================================按名字順序=================================================" << endl;
cout << "序號\t\t" << "工號\t\t" << "名字\t\t" << "工資\t\t" << "獎金\t\t" << "總工資" << endl;
for (int i = 0; i < N&&people[i].flag != 0; i++)
{
int k = i;
for (int j = i; temppeople[j].flag!=0; j++)
{
if (strcmp(people[k].name, people[j].name))k = j;
}
sale linshi;
linshi = temppeople[i];
temppeople[i] = temppeople[k];
temppeople[k] = linshi;
}
}
xuhao = 1;
for(int i=0;i<N&&temppeople[i].flag!=0;i++)//輸出
{//通過xuhao可以實現從上到下或者從下到上的排序。此次只固定排序。
cout << xuhao << "\t\t" ;
for(int j=0;j<5;j++)
cout << temppeople[i].num[j];
cout<< "\t\t" << temppeople[i].name << "\t\t" << temppeople[i].gongzi << "\t\t" << temppeople[i].prize << "\t\t" << temppeople[i].all << endl;
xuhao++;
}
cout << "==========================================================================================" << endl;
cout << endl;
checkposition = people;
cout << "顯示工資方式:\n工號順序請按1\n工資總額請按2\n職務工資請按3\n獎金順序請按4\n名字順序請按5\n退出排序請按0\n" << endl;
cin >> f3;
}break;
case 4: //存儲信息並退出
return 0;
default:
break;
}
cout << "輸入信息請按1,修改信息請按2,顯示工資請按3,存儲信息請按4,退出循環請按0" << endl;
cin >> f;
}
cout << "已自動保存" << endl;
return 0;
}
int main()
{
sale people[N];
input_message(people);
ofstream outfile_gongzi("gognzi.dat", ios::binary);
if (!outfile_gongzi)
{
cerr << "open error!" << endl;
abort();
}
for (int i = 0; i < 20 && people[i].flag != 0; i++)
{
outfile_gongzi.write((char *)&people[i], sizeof(sale));
}
outfile_gongzi.close();
return 0;
}