[ --> 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;
}
}
}
//
#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;
}
}
}
相关文章推荐
- < Unity 3D专栏 >动态创建对象,并渲染贴图,动态添加删除脚本
- 在shuiguo.xml文件中第三个水果中的苹果节点中添加节点<小苹果>small apple</小苹果>,添加小苹果后将其文本内容修改为“小苹果”,修改该节点后将其父节点苹果(即第三个苹果元素)删除
- Windows核心编程<读书笔记四之程序>显示系统中进程、线程、模块详细信息
- S动态添加与删除select中的Option对象 2009年09月23日 15:26 <html> <head> <meta http-equiv="Content-Type" content="t
- DIV+CSS布局入门示例(四)页面顶部 列表<li>制作菜单
- Flex使用<mx:Tree>控件创建树(可添加和删除节点)
- C#.Net:List<T>集合列表的Linq语句查询示例
- MCAPI学习笔记<一>——简介与Linux下示例程序测试
- Android实战简易教程<十一>(树形组件:ExpandableListView显示和动态添加删除)
- Flex使用<mx:Tree>控件创建树(可添加和删除节点)
- hibernate 返回List<Map>, List<xxx>对象列表类型
- java <!-- 这是我的第二个程序, Scanner类 -->
- 【Html】<address>标签,为网页加入地址信息
- <错误: 程序中有游离的‘357’>的解决方法
- 程序中所不需要访问到的功能,别忘了从WMAppManifest.xml文件中的<Capabilities>的小节将它们去掉。
- # include <errno.h >查看错误代码errno是调试程序的一个重要方法。当Linux C API函数发生异常时,一般会将errno变量赋值一个整数,不同的值表示不同的含义,可以通过查看
- Flex使用<mx:Tree>控件创建树(可添加和删除节点)
- 清除“添加或删除程序”列表中残留的程序项目
- 如何编写上传(PC->AS)和下载(AS->PC)程序示例
- 分布式编程->Remoting的一个代码示例(借助Remoting实现发送信息功能)