瑞星卡卡安全论坛技术交流区系统软件 新手求教C程序设计问题(简易职工信息管理系统)

1   1  /  1  页   跳转

新手求教C程序设计问题(简易职工信息管理系统)

新手求教C程序设计问题(简易职工信息管理系统)

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#include <conio.h>
#define SIZE 50

/* 定义结构体 */
struct date
{int month;
int day;
int year;
};
typedef struct employee
{char name[50] ,num[10],sex,edu[10],duty[15];
int age,income;
struct employee *next;
}empl[SIZE];

/* 逐项输出链表中的内容 */
void view (struct employee* head)
{
    struct employee * p;
    p = head;
    while (p != NULL)
    {
        printf("%-10s%-15s%3c%-10s%-8s%6d%9d\n", p->name, p->num,p->sex, p->edu,p->duty, p->age,p->income);
        p = p->next;
    }
}

/* 定义保存函数 */
void save ()
{FILE *fp;
int i;
if ((fp=fopen("emp_list","wb"))==NULL)
{printf("cannot open file\n");
  return;
}
for (i=0;i<SIZE;i++)
     if (fwrite(&empl,sizeof(struct employee),1,fp)!=1)
       printf("file write error\n");
     fclose(fp);
}


/* 通过姓名查找职工 */
struct employee * Search1 (struct employee* head, char * name)
{
    struct employee * p = head;
    while (p && strcmp(p->name, name))
        p = p->next;   
    return p;
}

/* 通过工号查找职工 */
struct employee * Search2 (struct employee* head, char * num)
{
    struct employee * p = head;
    while (p && strcmp(p->num, num))
        p = p->next;   
    return p;
}

/* 删除职工信息 */
void Delete (struct employee* head, char * i)
{
    struct employee * p = head, *m, *q;
    while (p && strcmp(p->num, i) != 0)
    {
        q = p;
        p = p->next;
    }
    if ((q != NULL) && (q->next != NULL) && head != NULL)
    {
        m = q->next;
        q->next = m->next;
        free(m);
        printf("删除成功\n");
    }    else printf("职工不存在!\n"); 
}

/* 添加一个职工的信息 */
void AddHead (struct employee* head, char name [50], char num [10], char sex,char edu[10],char duty[15], int age, int income)    /*增加职工信息*/
{
    struct employee * p, *q = head;
    while (q->next != NULL)
        q = q->next; 
    p = (struct employee* )malloc(sizeof(employee));
    strcpy(p->name, name);
    strcpy(p->num, num);
    p->sex = sex;
    strcpy(p->edu, edu);
    strcpy(p->duty, duty);
    p->age = age;
    p->income = income;
    p->next = NULL;
    q->next = p;
}

void main (void)
{
    int i, f, g;
    struct employee * head, *s;
    char a [50], b [50], t, e [15], h [15],x[8],d[20], k [15];
    system("cls");
    /* 为首地址分配空间 */
    head=(struct employee* )malloc(sizeof(struct employee));
    head->next=NULL;
    /* 添加预置信息 */
    AddHead(head, "张天", "0610312100", 'M', "高中", "保安" ,20,2600);
    AddHead(head, "李行", "0610312101", 'M', "本科", "经理" ,36,5000);
    AddHead(head, "周若", "0610312102", 'W', "本科", "会计" ,20,1600);
    AddHead(head, "王昭", "0610312103", 'W', "硕士","工程师",27,6000);
    AddHead(head, "刘系", "0610312104", 'M', "硕士","工程师",27,6000);
    save();
    for ( ; ; )
    {
        printf("--------------------------------------------------------\n");
        printf("姓名    工号    性别    学历    职务 年龄 工资\n");
        view(head->next);
        printf("--------------------------------------------------------\n");
        printf("1.添加职工信息\n");
        printf("2.删除职工信息\n");
        printf("3.根据姓名查找职工\n");
        printf("4.根据工号查找职工\n\n");
        printf("请作出你的选择:");
        scanf("%d", &i);
        switch (i)
        {
            case 1:
                    printf("请输入职工的信息:");
                    printf("\n姓名:");
                    scanf("%s", a);
                    printf("工号:");
                    scanf("%s", e);
                    printf("性别:");
                    getchar();
                    scanf("%c", &t);
                 
                    printf("\n学历:");
                    scanf("%s", x);
                    printf("职务:");
                    scanf("%s", d);
                    printf("年龄:");
                    scanf("%d", &f);
                    printf("工资:");
                    scanf("%d", &g);
                    AddHead(head, a, e, t, x,d,f, g);
                    system("cls");
                    break;
                case 2:
                    printf("\n工号:");
                    scanf("%s", h);
                    Delete(head, h);
                    system("cls");
                    break;
                case 3:
                    printf("请输入职工的姓名:");
                    scanf("%s", b);
                    s = Search1(head, b);
                    system("cls");
                    printf("%-15s%-10s%3c%6d%-10s%-15s%9d\n", s->name, s->num,s->sex, s->edu,s->duty,s->age, s->income);
                    break;
                case 4:
                    printf("请输入职工的工号:");
                    scanf("%s", k);
                    s = Search2(head, k);
                    system("cls");
                    printf("%-15s%-10s%3c%6d%-10s%-15s%9d\n", s->name, s->num,s->sex, s->edu,s->duty,s->age, s->income);
                    break;
            }
    }       
}

要求:

职工信息包括职工号、姓名、性别、出生年月、学历、职务、工资、住址、电话等(职工号不重复)。试设计一职工信息管理系统,使之能提供以下功能:

职工信息录入(创建)功能(职工信息用文件保存)

职工信息浏览功能

查询或排序功能:(至少一种查询方式)
    按工资查询
    按学历查询等

职工信息添加、删除、修改功能



链表综合与磁盘存储结合存在问题,诚心恳请各位高手指教一二。(用的VC++6.0的编译器)
最后编辑2007-07-07 20:57:54
分享到:
gototop
 

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#include <conio.h>
#define SIZE 100

/* 定义结构体 */
struct date
{int month;
int day;
int year;
};
struct employee
{char name[50] ,num[15],sex,edu[15],duty[15];
int age,income;
struct employee *next;
}EMP[SIZE];

/* 逐项输出链表中的内容 */
void view (struct employee* head)
{
    struct employee * p;
    p = head;
    while (p != NULL)
    {
        printf("%-8s%8s%5c%10s%8s%6d%9d\n", p->name, p->num,p->sex, p->edu,p->duty, p->age,p->income);
        p = p->next;
    }
}

/* 定义保存函数 */
void save ()
{FILE *fp;
int i;
if ((fp=fopen("emp_list.txt","wb"))==NULL)
{printf("cannot open file\n");
  return;
}
for (i=0;i<SIZE;i++)
if (fwrite(&EMP,sizeof(struct employee),1,fp)!=1)
    printf("file write error\n");
fclose(fp);
}


/* 通过姓名查找职工 */
struct employee * Search1 (struct employee* head, char * name)
{
    struct employee * p = head;
    while (p && strcmp(p->name, name))
        p = p->next;   
    return p;
}

/* 通过工号查找职工 */
struct employee * Search2 (struct employee* head, char * num)
{
    struct employee * p = head;
    while (p && strcmp(p->num, num))
        p = p->next;   
    return p;
}

/* 删除职工信息 */
void Delete (struct employee* head, char * i)
{
    struct employee * p = head, *m, *q;
    while (p && strcmp(p->num, i) != 0)
    {
        q = p;
        p = p->next;
    }
    if ((q != NULL) && (q->next != NULL) && head != NULL)
    {
        m = q->next;
        q->next = m->next;
        free(m);
        printf("删除成功\n");
    }else printf("职工不存在!\n"); 
}

/* 添加一个职工的信息 */
void AddHead (struct employee* head, char name [50], char num [10], char sex,char edu[10],char duty[15], int age, int income)    /*增加职工信息*/
{
    struct employee * p, *q = head;
    while (q->next != NULL)
        q = q->next; 
    p = (struct employee* )malloc(sizeof(employee));
    strcpy(p->name, name);
    strcpy(p->num, num);
    p->sex = sex;
    strcpy(p->edu, edu);
    strcpy(p->duty, duty);
p->age = age;
    p->income = income;
p->next = NULL;
    q->next = p;
}

void main (void)

    int i, f, g;
    struct employee * head, *s;
    char a [50], b [50], t, e [15], h [15],x[8],d[20], k [15];
    system("cls");
    /* 为首地址分配空间 */
    head=(struct employee* )malloc(sizeof(struct employee));
    head->next=NULL;
    /* 添加预置信息 */
    AddHead(head, "张天", "0610312100", 'M', "高中","保安" ,20,2600);
    AddHead(head, "李行", "0610312101", 'M', "本科","经理" ,36,5000);
    AddHead(head, "周若", "0610312102", 'W', "本科","会计" ,20,1600);
    AddHead(head, "王昭", "0610312103", 'W', "硕士", "工程师",27,6000);
    AddHead(head, "刘系", "0610312104", 'M', "硕士", "工程师",27,6000);
    save ();
for ( ; ; )
{
        printf("--------------------------------------------------------\n");
        printf("姓名      工号      性别    学历  职务    年龄    工资\n");
        view(head->next);
        printf("--------------------------------------------------------\n");
        printf("1.添加职工信息\n");
        printf("2.删除职工信息\n");
        printf("3.根据姓名查找职工\n");
        printf("4.根据工号查找职工\n\n");
        printf("请作出你的选择:");
        scanf("%d", &i);
        switch (i)
        {
            case 1:
                    printf("请输入职工的信息:");
                    printf("\n姓名:");
                    scanf("%s", a);
                    printf("工号:");
                    scanf("%s", e);
                    printf("性别:");
                    getchar();
                    scanf("%c", &t);
                 
                    printf("\n学历:");
                    scanf("%s", x);
                    printf("职务:");
                    scanf("%s", d);
printf("年龄:");
                    scanf("%d", &f);
                    printf("工资:");
                    scanf("%d", &g);
                    AddHead(head, a, e, t, x,d,f, g);
                    system("cls");
                    break;
                case 2:
                    printf("\n工号:");
                    scanf("%s", h);
                    Delete(head, h);
                    system("cls");
                    break;
                case 3:
                    printf("请输入职工的姓名:");
                    scanf("%s", b);
                    s = Search1(head, b);
                    system("cls");
                    printf("%-15s%-10s%3c%6d%-10s%-15s%9d\n", s->name, s->num,s->sex, s->edu,s->duty,s->age, s->income);
                    break;
                case 4:
                    printf("请输入职工的工号:");
                    scanf("%s", k);
                    s = Search2(head, k);
                    system("cls");
                    printf("%-15s%-10s%3c%6d%-10s%-15s%9d\n", s->name, s->num,s->sex, s->edu,s->duty,s->age, s->income);
                    break;
            }
    }       
}
gototop
 

最新修改,但是不能写入生成的TXT文档里。
求教!
gototop
 

好长
而且你的数组 [ i]在论坛里会自动转成斜体符号
显示不出来
不如换个变量或是加个空格之类的
头昏了……
gototop
 

那怎么半啊?这样谁能不能就给我两个函数
一个将数据读入文档(非二进制)
和从文档读出数据(fprintf 和 fscanf  )
gototop
 
1   1  /  1  页   跳转
页面顶部
Powered by Discuz!NT