dir显示目录
从C盘进入E盘: E:
动态建立一个文件,进入将要运行的文件夹下的Debug文件夹中,输入.exe文件的名和后缀,+ 2个空格建。
dat文件的名字。
如:\Debug>proj1.exe ..\test.dat
然后,就可以运行程序了。
也就是说,这个程序的运行是从控制台进去的
//main.cpp
#include<stdio.h>
#include<stdlib.h>
#include"my_file.h"
#include"my_array.h"
#include"main.h"
void menu()
{
printf("***********职工信息管理系统*************\n");
printf(" 0.exit\n");
printf("1.Read file 2.Save file\n");
printf("3.Insert 4.Delete\n");
printf("5.Display 6.Delete All\n");
printf("7.SortNo 8.DispNo\n");
printf("9.SortDepno 10.DispDepno\n");
printf("11.SortSalary 12.DispSalary\n");
printf("****************************************\n");
}
int main(int argc, char *argv[])
{
int array_size = 2;
EmpType *emp = NULL;
emp = (EmpType *)malloc(sizeof(EmpType) * array_size);
emp[0].pno = emp[0].pdepno = emp[0].psalary = -1;
int n=0, choose; //n:数组中元素的个数;
while(1)
{
menu();
scanf("%d", &choose);
getchar();
switch(choose)
{
case 0:
printf("您已退出职工管理系统^_^\n");
return 0;
break;
case 1:
ReadFile(&emp, &n, &array_size, argv[1]);
break;
case 2:
SaveFile(emp, n, argv[1]);
break;
case 3:
Insert(&emp, &n, &array_size);
break;
case 4:
Delete(&emp, &n, &array_size);
break;
case 5:
Display(emp, n);
break;
case 6:
DelAll(emp, &n, &array_size, argv[1]);
case 7:
SortNo(emp, n);
break;
case 8:
DispNo(emp, n);
break;
case 9:
SortDepno(emp, n);
break;
case 10:
DispDepno(emp, n);
break;
case 11:
SortSalary(emp, n);
break;
case 12:
DispSalary(emp, n);
break;
default:
printf("输入错误,请重新输入!\n");
}
}//while
return 0;
}
//my_array.cpp
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include"main.h"
Status Insert(EmpType * *emp, int *n, int *array_size)
{
(*n)++; //元素个数加1;下标为0的位置不放元素。
if((*n) >= (*array_size)) //是否重新分配存储空间;
{
(*array_size) = (*n) + 10;
(*emp) = (EmpType*)realloc((*emp), sizeof(EmpType) * (*array_size));
}
printf("请输入职工信息:\n");
printf("no:");
scanf("%d", &(*emp)[*n].no);
getchar();
printf("name:");
scanf("%s", &(*emp)[*n].name);
getchar();
printf("depno:");
scanf("%d", &(*emp)[*n].depno);
getchar();
printf("salary:");
scanf("%f", &(*emp)[*n].salary);
getchar();
(*emp)[0].pno = (*emp)[0].pdepno = (*emp)[0].psalary = -1; //排序失效;
return OK;
}
Status Delete(EmpType * *emp, int *n, int *array_size)
{
if((*n) == 0)
{
printf("emp empty!\n");
return ERR;
}
int no;
printf("input the no:");
scanf("%d", &no);
for(int i = 0; i <= (*n); i++)
{
if((*emp)[i].no == no) //将最后一个元素前移,避免移动大量元素的操作;
{
(*emp)[i].no = (*emp)[*n].no;
(*emp)[i].depno = (*emp)[*n].depno ;
(*emp)[i].salary = (*emp)[*n].salary ;
strcpy((*emp)[i].name, (*emp)[*n].name);
(*n)--;
break;
}
}
if(i > (*n) + 1)
printf("Cannot find the emp\n");
if((*n) < (*array_size) - 10) //回收多余的空间;
{
(*array_size) = (*n) + 10;
(*emp) = (EmpType*)realloc((*emp), sizeof(EmpType) * (*array_size));
}
(*emp)[0].pno = (*emp)[0].pdepno = (*emp)[0].psalary = -1; //排序失效;
return OK;
}
Status Display(EmpType emp[], int n)
{
if(n == 0)
{
printf("emp is empty!\n");
return OK;
}
printf("no\tname\tdepno\tsalary\n");
for(int i = 1; i <= n; i++)
{
printf("%d\t%s\t%d\t%.2f\n", emp[i].no ,emp[i].name , emp[i].depno ,emp[i].salary );
}
return OK;
}
Status SortNo(EmpType emp[], int n) //直接插入排序(默认升序)
{
int i, j;
if(n == 0)
{
printf("emp is empty!\n");
return OK;
}
if(emp[0].pno != -1)
{
printf("sorted!\n");
return OK;
}
emp[0].pno = 1; //构造有序序列;
emp[1].pno = -1;
//向有序序列中插入存储顺序中的第i个元素;
for(i = 2; i <= n; i++) //遍历每一个将要插入的元素
{
j = 0;
while(emp[j].pno != -1 && emp[emp[j].pno].no < emp[i].no )//在有序序列中寻找合适的插入位置;
{
j = emp[j].pno;
}
emp[i].pno = emp[j].pno;
emp[j].pno = i;
}
return OK;
}
Status DispNo(EmpType emp[], int n)
{
int i;
printf("no\tname\tdepno\tsalary\n");
for(i = emp[0].pno; i != -1; i = emp[i].pno)
{
printf("%d\t%s\t%d\t%.2f\n", emp[i].no ,emp[i].name , emp[i].depno ,emp[i].salary );
}
return OK;
}
Status SortDepno(EmpType emp[], int n)
{
int i, j;
if(n == 0)
{
printf("emp is empty!\n");
return OK;
}
if(emp[0].pdepno != -1)
{
printf("sorted!\n");
return OK;
}
emp[0].pdepno = 1; //构造有序序列;
emp[1].pdepno = -1;
//向有序序列中插入存储顺序中的第i个元素;
for(i = 2; i <= n; i++) //遍历每一个将要插入的元素
{
j = 0;
while(emp[j].pdepno != -1 && emp[i].depno > emp[emp[j].pdepno].depno ) //在有序序列中寻找合适的插入位置;
{
j = emp[j].pdepno;
}
emp[i].pdepno = emp[j].pdepno;
emp[j].pdepno = i;
}
return OK;
}
Status DispDepno(EmpType emp[], int n)
{
int i, j;
printf("no\tname\tdepno\tsalary\n");
for(i = emp[0].pdepno; i != -1; i = emp[i].pdepno)
{
printf("%d\t%s\t%d\t%.2f\n", emp[i].no ,emp[i].name , emp[i].depno ,emp[i].salary );
}
return OK;
}
Status SortSalary(EmpType emp[], int n)
{
int i, j;
if(n == 0)
{
printf("emp is empty!\n");
return OK;
}
if(emp[0].psalary != -1)
{
printf("sorted!\n");
return OK;
}
emp[0].psalary = 1; //构造有序序列;
emp[1].psalary = -1;
//向有序序列中插入存储顺序中的第i个元素;
for(i = 2; i <= n; i++) //遍历每一个将要插入的元素
{
j = 0;
while(emp[j].psalary != -1 && emp[i].salary > emp[emp[j].psalary].salary ) //在有序序列中寻找合适的插入位置;
{
j = emp[j].psalary;
}
emp[i].psalary= emp[j].psalary;
emp[j].psalary = i;
}
return OK;
}
Status DispSalary(EmpType emp[], int n)
{
int i, j;
printf("no\tname\tdepno\tsalary\n");
for(i = emp[0].psalary; i != -1; i = emp[i].psalary)
{
printf("%d\t%s\t%d\t%.2f\n", emp[i].no ,emp[i].name , emp[i].depno ,emp[i].salary );
}
return OK;
}
/*
Status SortNo(EmpType emp[], int n)//(默认升序)使用一个临时数组进行排序
{
int temp[MaxSize];
int i, j;
for(i = 1; i <= n; i++)
{
temp[i] = emp[i].no;
}
sort(temp + 1, temp + n + 1);//排序
emp[0].pno = temp[1];
for(i = 1; i <= n; i++)//用下标将排序好的元素连起来;
{
for(j = 1; j <= n; j++)
{
if(temp[i] == emp[j].no)
{
if(i == n)
emp[j].pno = -1;
else if(i == 1)
emp[0].pno = j;
else
emp[j].pno = j + 1;
}
}
}
return OK;
}*/
//my_file.cpp
#include<stdio.h>
#include<stdlib.h>
#include"main.h"
Status ReadFile(EmpType * *emp, int *n, int *array_size, char *filename)
{
FILE *fp = NULL;
long len;
int i;
if((fp = fopen(filename, "rb")) == NULL) //以只读方式打开二进制文件;
{
printf("数据文件不能打开\n");
return ERR;
}
fseek(fp, 0, 2); //文件位置指针移到文件尾;0:偏移量;2:偏移起始位置。(0:SEE_SET;1:SEEK_CUR;2:SEEK_END)
len = ftell(fp); //计算文件长度;
rewind(fp); //文件位置指针移到文件首,fseek(fp, 0, 0)
(*n) = len / sizeof(EmpType); //n求出文件中的记录个数;
(*n)--;
if((*n) == 0)
{
(*emp)[0].pno = (*emp)[0].pdepno = (*emp)[0].psalary = -1;//初始化,-1代表未排序
return OK;
}
if((*n) >= (*array_size)) //重新分配存储空间;
{
(*n) = (*array_size) + 10;
(*emp) = (EmpType *)realloc((*emp), sizeof(EmpType) * (*array_size));
}
if((*n) != 0)
{
for(i = 0; i <= (*n); i++) //将文件的数据读到emp中,n+1次循环,emp[0]不记录数据,只记录排序结果;
{
fread(&(*emp)[i], sizeof(EmpType), 1, fp);
}
}
fclose(fp);
return OK;
}
Status SaveFile(EmpType emp[], int n, char *filename)//将emp数组存入数据文件
{
int i;
FILE *fp = NULL;
if((fp = fopen(filename, "wb")) == NULL)
{
printf("数据文件不能打开\n");
return OK;
}
if(n > 0)
{
for(i = 0; i <= n; i++)
{
fwrite(&emp[i], sizeof(EmpType), 1, fp);
}
}
fclose(fp);
return OK;
}
Status DelAll(EmpType emp[], int *n, int *array_size, char *filename)
{
char ch;
printf("Are you sure?(y:yes, n:no)\n");
scanf("%c", &ch);
if(ch == 'n' || ch == 'N')
{
return OK;
}
FILE *fp = NULL;
if((fp = fopen(filename, "wb")) == NULL)
{
printf(">>数据文件不能打开\n");
return OK;
}
emp[0].pno = emp[0].depno = emp[0].psalary = -1; //初始化,-1代表未排序
(*n) = 0;
fclose(fp);
return OK;
}
//main.h
#ifndef main_h
#define main_h
#define OK 0
#define ERR 1
typedef int Status;
typedef struct node
{
int no;
char name[20];
int depno;
float salary;
int pno, pdepno, psalary;
}EmpType;
#endif
//my_array.h
Status Insert(EmpType* *emp, int *n, int *array_size);
Status Delete(EmpType* *emp, int *n, int *array_size);
Status Display(EmpType emp[], int n);
Status SortNo(EmpType emp[], int n);
Status DispNo(EmpType emp[], int n);
Status SortDepno(EmpType emp[], int n);
Status DispDepno(EmpType emp[], int n);
Status SortSalary(EmpType emp[], int n);
Status DispSalary(EmpType emp[], int n);
//my_file.h
#ifndef my_file_h
#define my_file_h
#include"main.h"
Status ReadFile(EmpType* *emp, int *n, int *array_size, char *filename);
Status SaveFile(EmpType emp[], int n, char *filename);
Status DelAll(EmpType emp[], int *n, int *array_size, char *filename);
#endif