课程设计

所谓的课程设计。。很囧。

 

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#define W struct Worker

//职工结构体
struct Worker {
    int id; //职工号
    char name[20];  //职工姓名
    char sex;   //性别,M或者F
    int age;    //年龄
    char workTime[12];  //工作开始日期。格式:YYYY-MM-HH ,如2010-06-06
    char degree[10];    //学历
    char title[10]; //职位
    char partyTime[12]; //入党时间
    char isMarried; //婚否,Y或N
    char nativePlace[20];   //籍贯
    char address[50];   //通讯地址
    char tel[15];   //电话号码
    char department[20];    //所在部门
    char email[30]; //邮箱地址
    char selfIntro[150];    //个人简介

	struct Worker *next;	//下个节点地址
};

//全局变量
/*char *adminId;
char *adminPwd;*/

W* workers = NULL; //整个职工列表头结点


//函数说明
void showMainMenu();
char* loadProperty(FILE*);
void login();
W* create();
W* input(W*);
void output(W*);
W* add(W*,W*);
W* search();
W* searchById(W*,int);
W* searchByName(W*,char*);
W* searchByDep(W*,char*);
W* searchByNP(W*,char*);
W* del(W*);
W* remove(W*,int);
void modify();
W* edit(W*,int);
W* sortByAgeHelper(W*,W*);
W* sortByAge(W*);
W* sortById(W*);
char* getProperty(char*);
void init();
void sort();
void insert();
void doOutput();
void doDel();
void printing();
void exitAndSave();


//主函数
void main() {
	login();
	return;
}


//显示主菜单函数
void showMainMenu() {
	int select;
	printf("==============主菜单===============");
	printf("\n1.浏览全部");
	printf("\n2.录入");
	printf("\n3.编辑");
	printf("\n4.删除");
	printf("\n5.排序");
	printf("\n6.查找");
	printf("\n7.打印");
	printf("\n8.系统设置");
	printf("\n9.保存并退出");
	printf("\n0.退出\n\n");
	printf("请选择:");
	
	scanf("%d",&select);

	switch(select) {
	case 1:
		doOutput();
		break;
	case 2:
		insert();
		break;
	case 3:
		modify();
		break;
	case 4:
		doDel();
		break;
	case 5:
		sort();
		break;
	case 6:
		search();
		break;
	case 7:
		printing();
		break;
	case 8:

		break;
	case 9:
		exitAndSave();
		break;
	case 0:
		exit(0);
		break;
	default :
		showMainMenu();
	}

}

/**
*	读取属性函数
*/
char* loadProperty(FILE* file,char* key) {
	char* value;
	char properties[255];
	char* result;
	//	FILE* file;
	//file = fopen(filePath,"r");
	if(file == NULL) {
		printf("文件读取错误!请检查文件\"%s\"是否存在!程序即将退出。\n","a");
		system("pause");
		exit(0);
	} 
	else {
		fgets(properties,255,file);
		result = strtok(properties,"=");
		while(strcmp(result,key) != 0) {
			fgets(properties,255,file);
			result = strtok(properties,"=");

		}
		if(strcmp(result,key) == 0) {
			result = strtok(NULL,"=");
			result = getProperty(result);
			return result;
		}
	}
	return 0;
}


//登录函数

void login() {
	char userName[255];
	char userPwd[255];
    char systemName[255];

	FILE* file;
	file = fopen("Mysys.properties","r");
    printf("=================================================\n");
	printf("========欢迎使用%s职工管理系统========\n",loadProperty(file,"systemName"));
	printf("=================================================\n");
	printf("==============请登录===============");
	printf("%s","\n请输入用户名:");
	scanf("%s",userName);
	printf("%s","\n请输入密码:");
	scanf("%s",userPwd);

	
	if(strcmp(userName,loadProperty(file,"adminId")) == 0 && strcmp(userPwd,loadProperty(file,"adminPwd")) == 0) {
		printf("\n登录成功!\n");
		init();
		showMainMenu();
	}
	else {
		printf("\n用户名或密码错误,请重新登录!\n");
		login();
	}
	fclose(file);
}

//创建链表函数
W* create() {
	W* list = (W*)malloc(sizeof(W));
	input(list);
	workers = list;
	return list;
}


//输入函数
W* input(W* aWorker) {
	printf("-------------请输入职工信息------------------\n");
	printf("职工号:");
	scanf("%d",&aWorker->id);
	printf("姓名:");
	scanf("%s",aWorker->name);
	printf("年龄:");
	scanf("%d",&aWorker->age);
	fflush(stdin);	//清空标准输入缓存
	printf("性别(M/F):");
	scanf("%c",&aWorker->sex);
	fflush(stdin);	//清空标准输入缓存
	printf("婚否(Y/N):");
	scanf("%c",&aWorker->isMarried);
	printf("籍贯:");
	scanf("%s",aWorker->nativePlace);
	printf("学历:"); 
	scanf("%s",aWorker->degree);
	printf("所在部门:");
	scanf("%s",aWorker->department);
	printf("通讯地址:");
	scanf("%s",aWorker->address);
	printf("电话号码:");
	scanf("%s",aWorker->tel);
	printf("电子邮箱:");
	scanf("%s",aWorker->email);
	printf("参加工作时间(YYYY-MM-DD):");
	scanf("%s",aWorker->workTime);
	printf("入党时间(YYYY-MM-DD):");
	scanf("%s",aWorker->partyTime);
	printf("个人简介(100字以内):");
	scanf("%s",aWorker->selfIntro);

	aWorker->next = 0;

	return aWorker;
}

//输出函数
void output(W* list) {
	if(list != NULL) {
		W *p,*q;
		p = list;
		q = list;
		printf("==================职工信息=============================\n");
		
		do {
				
			if(q != NULL) {

				printf("------------------------------------------------------\n");
				printf("职工号:%d\t姓名:%s\n",q->id,q->name);	
				printf("年龄:%d\t性别:%c\n",q->age,q->sex);
				printf("婚否:%c\t籍贯:%s\n",q->isMarried,q->nativePlace);
				printf("学历:%s\t所在部门:%s\n",q->degree,q->department);
				printf("通讯地址:%s\t电话号码:%s\n",q->address,q->tel);
				printf("电子邮箱:%s\t参加工作时间:%s\n",q->email,q->workTime);
				printf("入党时间:%s\n",q->partyTime);
				printf("个人简介:%s\n",q->selfIntro);	
			}
			p = q;
			q = p->next;	
		} while (p->next != NULL);
	}
	else {
		printf("没有相关职工信息!\n");
	}
}


//录入函数(按职工号排序)
W* add(W* list,W* aWorker) {
	W* head;
	W *p,*q;
	p = q = head = list;
	if(aWorker->id < p->id) {
		aWorker->next = list;
		head = aWorker;
		return head;
	}
	else if(aWorker->id == p->id) {
		printf("\n错误:录入失败,职工号重复。\n");
		return list;
	}
	else {
		while(p->next != 0) {
			q = p;
			p = q->next;
			if(aWorker->id > q->id && aWorker->id < p->id) {
				q->next = aWorker;
				aWorker->next = p;
				return head;
			}
		}
			
		if(aWorker->id > p->id) {
			p->next = aWorker;
			return head;
		}
		else {
			printf("\n错误:录入失败,职工号重复。\n");
			return head;
		}
		
	}
}

//查询函数
W* search() {
	W* result;
	if(workers == NULL) { 
		printf("\n\n没有任何可供查询的信息。\n");
	}
	else {
		int select;
		W* result;
		printf("-------------查询----------\n");
		printf("1.根据职工号查询\n");
		printf("2.根据姓名查询\n");
		printf("3.根据所在部门查询\n");
		printf("4.根据籍贯查询\n");
		printf("5.返回主菜单\n");
		printf("\n请选择查询方式:\n");
		scanf("%d",&select);

		switch(select) {
		case 1:
			workers = sortById(workers);//
			int keyId;
			printf("请输入职工号:");
			scanf("%d",&keyId);
			if(workers != 0)
				result = searchById(workers,keyId);
			else
				result = NULL;
			break;
		case 2:
			char keyName[10];
			printf("请输入职工姓名:");
			scanf("%s",&keyName);
			if(workers != 0)
				result = searchByName(workers,keyName);
			else
				result = NULL;
			break;
		case 3:
			char keyDep[10];
			printf("请输入职工所在部门:");
			scanf("%s",&keyDep);
			if(workers != 0)
				result = searchByDep(workers,keyDep);
			else
				result = NULL;
			break;
		case 4:
			char keyNP[10];
			printf("请输入职工籍贯:");
			scanf("%s",&keyNP);
			if(workers != 0)
				result = searchByDep(workers,keyNP);
			else
				result = NULL;
			break;
		case 5:
			showMainMenu();
			break;
		default:
			search();
			break;
		}
		if(result != NULL) {
			output(result);
		}
		else {
			printf("没有相关职工信息。\n");
		}
	}
	
	int goWhere;
	printf("\n\n请选择:1.继续查询\t2.返回主菜单\n");
	scanf("%d",&goWhere);
	if(goWhere == 1)
		search();
	else
		showMainMenu();
	return result;
}

//根据职工号查询
W* searchById(W* list,int id) {
	while(list != 0)   {
		if(list->id == id) {
			W* result = (W*)malloc(sizeof(W));
			*result = *list;
			result->next = NULL;
			return result;
		}
		list = list->next;
	} 
	return NULL;
}

//根据姓名查询
W* searchByName(W* list,char* name) {
	W *result,*p,*q;
	int i = 0;
	while(list != 0)   {
		if(strcmp(list->name,name) == 0) {
			i++;
			if(i == 1) {
				result = (W*)malloc(sizeof(W));
				p = q = result;
				*result = *list;
				result->next = 0;
			}
			else {
				q = (W*)malloc(sizeof(W));
				*q = *list;
				p->next = q;
				p = q;
				q->next = 0;
			}
		}
		list = list->next;
	} 
	return result;
}

//根据部门查询
W* searchByDep(W* list,char* dep) {
	W *result,*p,*q;
	int i = 0;
	while(list != 0)   {
		if(strcmp(list->department,dep) == 0) {
			i++;
			if(i == 1) {
				result = (W*)malloc(sizeof(W));
				p = q = result;
				*result = *list;
				result->next = 0;
			}
			else {
				q = (W*)malloc(sizeof(W));
				*q = *list;
				p->next = q;
				p = q;
				q->next = 0;
			}
		}
		list = list->next;
	} 
	return result;
}

//根据籍贯查询
W* searchByNP(W* list,char* np) {
	W *result,*p,*q;
	int i = 0;
	while(list != 0)   {
		if(strcmp(list->nativePlace,np) == 0) {
			i++;
			if(i == 1) {
				result = (W*)malloc(sizeof(W));
				p = q = result;
				*result = *list;
				result->next = 0;
			}
			else {
				q = (W*)malloc(sizeof(W));
				*q = *list;
				p->next = q;
				p = q;
				q->next = 0;
			}
		}
		list = list->next;
	} 
	return result;
}


//删除节点函数
W* remove(W* list,int id) {
	W *head,*p,*q;
	head = p = q = list;
	if(list->id == id) {
		head = list->next;
		free(list);
		return head;
	}
	while(p->next != 0) {
		q = p;
		p = q->next;
		if(p->id == id) {
			q->next = p->next;
			free(p);
			return head;
		}
	}
	printf("没有相应的职工信息,删除失败。\n");
	return head;
}

//删除职工函数
W* del(W* list) {
	if(list != NULL) {
		int key;
		printf("\n请输入待删除职工信息的职工号:");
		scanf("%d",&key);
		return remove(list,key);
		
	}
	else {
		printf("没有相应的职工信息,删除失败。\n");
		return list;
	}
}

//编辑节点
W* edit(W* list,int id) {
	W *head,*p,*q,*next;
	p = q = head = list;
	while(p != 0) {
		if(p->id == id) {
			W* temp = (W*)malloc(sizeof(W));
			//*temp = *p;
			next = p->next;
			input(temp);
			//p->next = next;
		//	printf("测试测试:%d",p->id);
			if(searchById(list,temp->id) != NULL) {
				printf("\n新的职工号%d已经存在,编辑失败。",searchById(list,temp->id)->id);
				//*p = *temp;
			}
			else {
				*p = *temp;
				p->next = next;
			}
			free(temp);
			return head;
		}
		q = p;
		p = q->next;
	}
	printf("找不到相应信息。\n");
	return head;
}

//编辑职工信息
void modify() {
	if(workers == NULL) {
		printf("没有任何待编辑信息。\n");
	}
	else {
		int key;
		printf("========编辑职工信息==========\n");
		printf("请输入待编辑的职工信息职工号:");
		scanf("%d",&key);
		edit(workers,key);
	}
	int select;
	printf("\n\n请选择:1.继续编辑\t2.返回主菜单\n");
	scanf("%d",&select);
	if(select ==1) 
		modify();
	else 
		showMainMenu();
	return;
}



//按职工号排序
W* sortById(W* list) {
	W *head,*p,*q,*r;
	head = list;
	

	
	if(head->next == 0)
		return head;

	p = head->next;
	head->next = NULL;
	while(p != 0) {

		q = p;
		p = q->next;
		q->next = 0;
		head = add(head,q); 
	}
	return head;
}


//按年龄排序
W* sortByAge(W* list) {
	W *head,*p,*q,*r;
	head = list;
	

	
	if(head->next == 0)
		return head;

	p = head->next;
	head->next = NULL;
	while(p != 0) {

		q = p;
		p = q->next;
		q->next = 0;
		head = sortByAgeHelper(head,q); 
	}
	/*while(r->next != 0) {
		i++;
		q = p ;
		p = r;
		p = q->next;
		r = p->next;
		if(i == 1)
			q->next = 0;
		p->next = 0;
			head = sortByAgeHelper(head,p);
	}
		head = sortByAgeHelper(head,r);
		//p->next = 0;*/
	return head;
}


//按年龄排序协助函数
W* sortByAgeHelper(W* list,W* aWorker) {
	W* head;
	W *p,*q;
	p = q = head = list;
	if(aWorker->age < p->age) {
		aWorker->next = list;
		head = aWorker;
		return head;
	}
	else {
		while(p->next != 0) {
			q = p;
			p = q->next;

			if(aWorker->age >= q->age && aWorker->age <= p->age) {
				q->next = aWorker;
				aWorker->next = p;
				return head;
			}
		}
			
		if(aWorker->age > p->age) {
			p->next = aWorker;
			return head;
		}
		
	}
}

//排序
void sort() {
	if(workers == NULL) {
		printf("没有可供排序的数据!\n");
		showMainMenu();
	}
	else {
		int select;
		printf("\n----------排序-------------\n");
		printf("1.按职工号排序\n");
		printf("2.按年龄排序\n");
		scanf("%d",&select);
		switch(select) {
		case 1:
			workers = sortById(workers);
			printf("排序成功!\n");
			output(workers);
			printf("\n按任意键回到主菜单...\n");
			system("pause");
			showMainMenu();
			break;
		case 2:
			workers = sortByAge(workers);
			printf("排序成功!\n");
			output(workers);
			
			printf("\n按任意键回到主菜单...\n");
			system("pause");
			workers = sortById(workers);
			showMainMenu();
			break;
		default:
			sort();
		}
	}
}

//录入
void insert() {
	if(workers == NULL) {
		create();
	}
	else {
		workers = sortById(workers);
		workers = add(workers,input((W*)malloc(sizeof(W))));
	}

	int select;
	printf("\n请选择:1.继续录入\t2.回主菜单\n");
	scanf("%d",&select);
	switch(select) {
	case 1:
		insert();
	default:
		showMainMenu();
	}
}

//浏览全部
void doOutput() {
	output(workers);
	printf("\n\n按任意键返回主菜单...\n");
	system("pause");
	showMainMenu();
}

//删除模块
void doDel() {
	workers = del(workers);
	printf("\n\n请选择:1.继续删除\t2.返回主菜单");
	int select;
	scanf("%d",&select);
	if(select == 1) 
		doDel();
	else
		showMainMenu();
}

//打印模块
void printing() {
	if(workers != NULL) {
		W* q;
		q = workers;
		printf("--------------------\n");
		printf("正在打印...\n");
		printf("--------------------\n");
		/*   VC++不支持
		
		  fprintf(stdprn,"=================职工信息表===================\n");
		while(q != 0) {
			fprintf(stdprn,"------------------------------------------------------\n");
			fprintf(stdprn,"职工号:%d\t姓名:%s\n",q->id,q->name);	
			fprintf(stdprn,"年龄:%d\t性别:%c\n",q->age,q->sex);
			fprintf(stdprn,"婚否:%c\t籍贯:%s\n",q->isMarried,q->nativePlace);
			fprintf(stdprn,"学历:%s\t所在部门:%s\n",q->degree,q->department);
			fprintf(stdprn,"通讯地址:%s\t电话号码:%s\n",q->address,q->tel);
			fprintf(stdprn,"电子邮箱:%s\t参加工作时间:%s\n",q->email,q->workTime);
			fprintf(stdprn,"入党时间:%s\n",q->partyTime);
			fprintf(stdprn,"个人简介:%s\n",q->selfIntro);

			q = q->next;
		}*/
		printf("\n\n打印完成。\n");
	}
	else {
		printf("\n没有任何信息可以打印。\n");
	}
	printf("\n\n任意键回到主菜单...\n");
	system("pause");
	showMainMenu();

}
//初始化函数
void init() {
	printf("正在初始化...\n");
	FILE* file = fopen("data.data","r");
	if(file == NULL) {
		printf("职工信息无存档。\n");
	}
	else {
		printf("正在读入职工信息...\n");
		//读入信息
		W *p,*q;
		int i=0;
		p = q;
		while(!feof(file)) {
			
			p = (W*)malloc(sizeof(W));
			p->id = atoi(loadProperty(file,"id"));
			strcpy(p->name,loadProperty(file,"name"));
			p->age = atoi(loadProperty(file,"age"));
			p->sex = *loadProperty(file,"sex");
			p->isMarried = *loadProperty(file,"isMarried");
			strcpy(p->nativePlace,loadProperty(file,"nativePlace"));
			strcpy(p->degree,loadProperty(file,"degree"));
			strcpy(p->department,loadProperty(file,"department"));
			strcpy(p->address,loadProperty(file,"address"));
			strcpy(p->tel,loadProperty(file,"tel"));
			strcpy(p->email,loadProperty(file,"email"));
			strcpy(p->workTime,loadProperty(file,"workTime"));
			strcpy(p->partyTime,loadProperty(file,"partyTime"));
			strcpy(p->selfIntro,loadProperty(file,"selfIntro"));

			i++;
			if(i == 1) {
				workers = p;
				q = p;
				workers->next = 0;
				
			}
			else {
				q->next = p;
				q = p;
				p->next = 0;
			}
			if(strcmp(loadProperty(file,"end"),"true") == 0)
				return;
		}
	}
	fclose(file);
}
//退出模块
void exitAndSave() {
	if(workers != NULL){
		printf("正在储存数据....\n");
		workers = sortById(workers);
		W* p;
		FILE *file = fopen("data.data","w");
		p = workers;
		while(p != 0) {
			fprintf(file,"id=%d\n",p->id);
			fprintf(file,"name=%s\n",p->name);
			fprintf(file,"age=%d\n",p->age);
			fprintf(file,"sex=%c\n",p->sex);
			fprintf(file,"isMarried=%c\n",p->isMarried);
			fprintf(file,"nativePlace=%s\n",p->nativePlace);
			fprintf(file,"degree=%s\n",p->degree);

			fprintf(file,"department=%s\n",p->department);
			fprintf(file,"address=%s\n",p->address);
			fprintf(file,"tel=%s\n",p->tel);
			fprintf(file,"email=%s\n",p->email);
			fprintf(file,"workTime=%s\n",p->workTime);
			fprintf(file,"partyTime=%s\n",p->partyTime);
			fprintf(file,"selfIntro=%s\n",p->selfIntro);
			if(p->next != 0) {
				fprintf(file,"end=false\n\n");
			}
			else {
				fprintf(file,"end=true\n\n");
			}

			p = p->next;
		}
		printf("正在退出....\n");
		fclose(file);
	}
	printf("退出成功!\n");
	system("pause");
	exit(0);
}

//getproperty
char* getProperty(char* str) {
	char* head;
	head = str;
	while(*str != '\0') {
		str++;
	}
	str--;
	*str = '\0';
	str++;
	*str = NULL;
	return head;
}

//系统设置
/*void settings() {
	int select;
	char user[255],pwd[255],systemName[255];
	printf("\n==========系统设置============\n\n");
	printf("1.修改单位名称\n");
	printf("2.修改密码\n");
	printf("3.返回主菜单\n");

	scanf("%d",&select);
	switch(select) {
	case 1:
		File file = fopen("Mysys.properties","r");
		strcpy(p->nativePlace,loadProperty(file,"nativePlace"));
		break;
	case 2:
		break;
	default:
		break;
	}
}*/

posted on 2010-06-12 19:01  PSJay  阅读(289)  评论(0编辑  收藏  举报

导航