这是我写的最长的一段代码,也是我使用的最基本的知识。很多要求我只达到的最低的标准。
描述:
说明:一个员工工资管理程序,有新建、存储、显示、修改和删除的功能。
要求:
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;
}