您的位置:首页 > 其它

[ --> C Language<-- ] employee信息添加,删除,列表的示例程序

2006-12-15 17:53 651 查看
// Test.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct {
int empno;
char * empname;
char * gender;
}Data_t;

struct Node_t{
Data_t data;
Node_t *next;
};

typedef struct Node_t NODE;

void addEmp(NODE *head){

printf("==========> Input new employee's information <==========/n");

int empno;
char name[20];
char gender[2];

printf("Input emp no:");
scanf("%d",&empno);
printf("emp name:");
scanf("%s",name);
printf("emp gender:");
scanf("%s",gender);
printf("/n");

NODE *n=(NODE *)malloc(sizeof(NODE));
n->data.empno=empno;
//n->data.empname=(char *)malloc(strlen(name)); 原来是这里有问题。
n->data.empname=(char *)malloc(strlen(name)+1);
strcpy(n->data.empname,name);
//n->data.gender=(char *)malloc(strlen(gender));
n->data.gender=(char *)malloc(strlen(gender)+1);
strcpy(n->data.gender,gender);
n->next=NULL;

NODE *p=head->next;
NODE *lastN=head;
while(p!=NULL){
if(p->data.empno == empno){
printf("OUCH!!!/nDuplicate Empno!/nPlease check your input./n/n");
break;
}

if(p->data.empno > empno){
lastN->next=n;
n->next=p;
//insert the new emp and then return
return;
}
lastN=p;
p=p->next;
}
//add an emp at the end
lastN->next=n;
}

void reverseOrder(NODE * head){
NODE *p=head->next;
NODE *lastN=NULL;
NODE *nextN=NULL;
while(p!=NULL){
nextN=p->next;
p->next=lastN;
lastN=p;
p=nextN;
}
head->next=lastN;
}

void listEmps(NODE *head){

if(head->next==NULL){
printf("--------> No data yet. <-----------/n/n");
return;
}
Choice:
printf("Please Choose Output Order: /n");
printf("1: Ascending /n");
printf("2: Descending /n");
printf("Choice:");

//this array must be big enough to contain possible wrong input

char input[100];
scanf("%s",input);
int order=atoi(input);
printf("/n");

if(order==1){
printf("--------> Asecending Employees List : <---------/n");
NODE *p=head->next;
while(p!=NULL){
printf("Employee No. :%d/n",p->data.empno);
printf("Employee Name :%s/n",p->data.empname);
printf("Employee Gender :%s/n/n",p->data.gender);
p=p->next;
}
}else if(order==2){
printf("--------> Descending Employees List : <---------/n");
reverseOrder(head);
NODE *p=head->next;
while(p!=NULL){
printf("Employee No. :%d/n",p->data.empno);
printf("Employee Name :%s/n",p->data.empname);
printf("Employee Gender :%s/n/n",p->data.gender);
p=p->next;
}
reverseOrder(head);
}else{
printf("Invalid choice!/n");
goto Choice;
}
}

int delEmp(NODE *head){
int delempno=0;
printf("Please enter the employ's empno:");
scanf("%d",&delempno);
printf("/n");

NODE *lastN=head;
NODE *p=head->next;
while(p!=NULL){
if(p->data.empno==delempno){
lastN->next=p->next;
free((p->data).empname);
free((p->data).gender);
p->next=NULL;
free(p);
p=NULL;
return 1;
}
lastN=p;
p=p->next;
}
//no emp with the specified empno
return 0;

}

void main(){
char input[3]="-1";
NODE *head=(NODE *)malloc(sizeof(NODE));
head->next=NULL;

printf("##### Welcome! #####/n");
printf("Menu:/n");
printf("1. Add new employee;/n");
printf("2. Delete an employee;/n");
printf("3. List all employees;/n");
printf("4. Exit./n");

while(1==1){
printf("Please enter a choice:");
scanf("%s",input);
int choice = atoi(input);

switch(choice){
case 1:
addEmp(head);
break;
case 2:
if(delEmp(head)==0){
printf("OUCH!!!/nNo employee with the specified empno./nPlease check your input!/n/n");
}
break;
case 3:
listEmps(head);
break;
case 4:
return;
default:
printf("Invalid choice!/n/n");
break;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐