first commit

master
556 6 years ago
parent 56dfb991ae
commit 54ff949ca5

Binary file not shown.

@ -0,0 +1,278 @@
#include "stdio.h"
#include "stdlib.h"
#define FILENAME "worker.txt"
#define N 20
/*******************************************************
*******************************************************/
struct worker
{
int number; /*工号*/
int counts; /*数量*/
int grade; /*排名*/
};
/*******************************************************
*******************************************************/
void xinxi()
{
printf("\n+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
printf("+ 职工工作量统计系统 +\n");
printf("+ +\n");
printf("+ 1. 职工基本信息输入 +\n");
printf("+ +\n");
printf("+ 2. 工作量输入 +\n");
printf("+ +\n");
printf("+ 3. 按工作量排序 +\n");
printf("+ +\n");
printf("+ 4. 按职工工号进行信息删除 +\n");
printf("+ +\n");
printf("+ 5. 结束程序 +\n");
printf("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
}
/*******************************************************
*******************************************************/
int readFromFile(struct worker w[])
{
FILE *fp = NULL;
int i = 0;
fp = fopen(FILENAME, "rb"); /*打开文件*/
if (fp != NULL)
{
while (!feof(fp)) /*从文件中读入学生*/
{
if (i >= N)
break;
if (fread(w + i, sizeof(struct worker), 1, fp) == 1)
i++;
}
fclose(fp);
}
return i;
}
/*******************************************************
:
*******************************************************/
int f(struct worker w[], int length)
{
FILE *fp;
if ((fp = fopen(FILENAME, "ab+")) == NULL)
{
exit(0);
}
int i = length;
while (i < N) {
printf("第%d个职工\n", i);
printf("\n");
printf("请你输入工号(以职工号为0结束):\n");/*输入工号*/
scanf("%d", &w[i].number);
if (w[i].number == 0)
break;
printf("\n");
printf("\n");
printf("请你输入数量:\n");/*输入数量*/
scanf("%d", &w[i].counts);
fwrite(&w[i], sizeof(struct worker), 1, fp);
printf("\n");
i++;
}
fclose(fp);
return i;
}
/*******************************************************
:
*******************************************************/
void input(struct worker w[], int length)
{
int i = 1;
int number, counts;
printf("请输入工号:");
scanf("%d", &number);
while (number != 0)
{
printf("请输入完成数量:\n");
scanf("%d", &counts);
for (i = 0; i < length; i++)
if (w[i].number == number)
{
w[i].counts += counts;
break;
}
if (i >= length)
printf("工号不存在:\n");
printf("请输入工号:");
scanf("%d", &number);
}
FILE *fp;
if((fp=fopen("worker.txt","wb"))==NULL)
{
printf("cannot open this file!\n");
exit(0);
}
for(i=0;i<length;i++)
fwrite(&w[i],sizeof(struct worker),1,fp);
fclose(fp);
}
////////////////////////////////////////////////////////////////////////////////
/* 按工作量从大到小排序并标上名次 */////////////////////////////////////////////////////////////////////////////////
void sortCounts(struct worker w[], int length)
{
int i = 0, j = 0, k = 0;
struct worker s;
for (i = 0; i < length; i++)
{
k = i;
for (j = i + 1; j < length; j++)for (j = i + 1; j < length; j++)
if (w[j].counts > w[k].counts)
k = j;
/* 交换 w[i] and w[k] */
s = w[k];
w[k] = w[i];
w[i] = s;
}
if (length > 0)
w[0].grade = 1;
for (i = 1; i < length; i++)
if (w[i].counts == w[i - 1].counts)
w[i].grade = w[i - 1].grade;
else
w[i].grade = w[i - 1].grade + 1;
}
////////////////////////////////////////////////////////////////////////////////
/* 输出名次、同一名次的职工人数及他们的工号,姓名*/
///////////////////////////////////////////////////////////////////////////////
void sortNumber(struct worker w[], int length)
{
int i = 0;
int grade, count;
if (length > 0)
{
while (i < length)
{
count = 0;
grade = w[i].grade;
printf("名次:%d ", grade);
printf(" 工作量:%d ", w[i].counts);
printf("%s", "工号:");
while (i < length)
{
if (grade == w[i].grade)
{
printf("%d ", w[i].number);
count++;
}
else
break;
i++;
}
printf("人数:%d\n", count);
}
}
else
printf("职工数据不存在!\n");
}
/********************************************************
********************************************************/
int delet(struct worker w[], int length, int stuNum)
{
int i, j;
char choice;
for (i = 0; i < length; i++)
{
if (stuNum == w[i].number)
{
printf("欲删除职工号为:");
printf("%d\n", w[i].number);
break;
}
}
if (i < length)
{
printf("你确实要删除这个职工吗?(Y/N)");
scanf("%c", &choice);
if (choice == 'Y' || choice == 'y')
{
for (j = i; j < length; j++)
w[j] = w[j + 1];
length--;
printf("删除成功!\n");
}
}
else
printf("\n指定的职工不存在!\n");
FILE *fp;
if((fp=fopen("worker.txt","wb"))==NULL)
{
printf("cannot open this file!\n");
exit(0);
}
for(i=0;i<length;i++)
fwrite(&w[i],sizeof(struct worker),1,fp);
fclose(fp);
return length;
}
/********************************************************
********************************************************/
void writeToFile(struct worker w[], int length)
{
FILE *fp = NULL;
int i = 0;
fp = fopen(FILENAME, "ab");
if (fp == NULL)
{
printf("打开文件出错!\n");
exit(0);
}
for (i = 0; i < length; i++)
fwrite(w + i, sizeof(struct worker), 1, fp);
fclose(fp);
}
int main()
{
int choice;
struct worker w[N];
int NUM;
char stuName[100];
int stuNum;
int length = 0;
length = readFromFile(w);//读入
do
{
xinxi();
scanf("%d", &choice);
switch (choice)
{
case 1:
length = f(w, length);
break;
case 2:
input(w, length);
break;
case 3:
sortCounts(w, length);
sortNumber(w, length);
break;
case 4:
printf("请输入欲删除的职工号");
scanf("%d", &stuNum);
length = delet(w, length, stuNum);
break;
case 5:
{
writeToFile(w, length);//保存
printf("程序使用以结束\n");
exit(0);
}
default:
{printf("输入有错误请重新输入:\n");
}
}
} while (1);
}

Binary file not shown.
Loading…
Cancel
Save