C++工资管理系统(源代码)

2007-12-27 10:49:44| 分类: 学习 |字号 订阅

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#include <conio.h>
#define TITLE "\n 月份 工号 姓名 应发工资 各种折扣 实发工资\n"
#define DATA " %2d %4ld %-10s %6.1f %6.1f %6.1f\n", p1->month, p1->num, p1->name, p1->should_pay, p1->rebate, p1->actual_pay
typedef struct pay
{
    long num;
    short month;
    char name[20];
    float gw, jn, zw, zl, jt, yj;
    float should_pay;
    float fz, gj, sd, others;
    float rebate;
    float actual_pay;
    struct pay *next;
} PAY;
FILE *fp;
PAY *p1 = NULL, *p2 = NULL, *head = NULL;
void menu();
PAY *input();
void output();
void save(PAY *head);
PAY *open();
PAY *insert();
void search();
PAY *del();
PAY *revise();
/*主程序模块*/
void main(void)
{
    short sel;
    do
    {
        menu();
        scanf("%d", &sel);
        switch (sel)
        {
        case 1:
            head = input(); // 输入模块
            break;
        case 2:
            head = insert(); // 插入模块
            break;
        case 3:
            head = revise(); // 修改模块
            break;
        case 4:
            head = del(); // 删除模块
            break;
        case 5:
            search(); // 查找模块
            break;
        case 6:
            output(); // 输出模块
            break;
        case 7:
            save(head); // 保存模块
            break;
        case 0:
            break;
        }
    } while (sel != 0);
}
/*菜单*/
void menu()
{
    printf("\n\n\n\n\n\n\n");
    printf("\t\t\t\t *主菜单选项*\n\n");
    printf("\t\t\t\t1.创建工资档案\n");
    printf("\t\t\t\t2.新增工资信息\n");
    printf("\t\t\t\t3.修改工资信息\n");
    printf("\t\t\t\t4.删除工资信息\n");
    printf("\t\t\t\t5.查找工资信息\n");
    printf("\t\t\t\t6.分类信息列表\n");
    printf("\t\t\t\t7.保 存 信 息\n");
    printf("\t\t\t\t0.退 出\n");
    printf("\n\n\n\n\n\n\n\n");
}
/*输入模块*/
PAY *input()
{
    short n = 1;
    p1 = (PAY *)malloc(sizeof(PAY));
    if (p1 == NULL)
    {
        printf("内存不足,无法创建链表!");
        getch();
    }
    printf("\n请输入第%d位职工的信息(以工号为0结束):\n", n);
    printf(" 工号:");
    scanf("%d", &p1->num);
    if (p1->num == 0)
    {
        printf("按任意键返回!");
        getch();
        free(p1);
        return 0;
    }
    getchar();
    printf(" 姓名:");
    gets(p1->name);
    printf(" 工资月份:");
    scanf("%d", &p1->month);
    printf(" 岗位工资,技能工资,职务津贴,职龄津贴,交通补贴,业绩津贴:\n");
    scanf("%f,%f,%f,%f,%f,%f", &p1->gw, &p1->jn, &p1->zw, &p1->zl, &p1->jt, &p1->yj);
    printf(" 房租扣款,公积金,水电费,其它扣款:\n");
    scanf("%f,%f,%f,%f", &p1->fz, &p1->gj, &p1->sd, &p1->others);
    p1->should_pay = p1->gw + p1->jn + p1->zw + p1->zl + p1->jt + p1->yj;
    p1->rebate = p1->fz + p1->gj + p1->sd + p1->others;
    p1->actual_pay = p1->should_pay - p1->rebate;
    while (p1 != NULL)
    {
        if (n == 1)
            head = p1;
        else
            p2->next = p1;
        n++;
        p2 = p1;
        p1 = (PAY *)malloc(sizeof(PAY));
        if (p1 == NULL)
        {
            printf("内存不足,无法创建链表!");
            getch();
        }
        printf("请输入第%d位职工的信息(以工号为0结束):\n", n);
        printf(" 工号:");
        scanf("%d", &p1->num);
        getchar();
        if (p1->num == 0)
        {
            p1 = NULL;
            break;
        }
        printf(" 姓名:");
        gets(p1->name);
        printf(" 工资月份:");
        scanf("%d", &p1->month);
        printf(" 岗位工资,技能工资,职务津贴,职龄津贴,交通补贴,业绩津贴:\n");
        scanf("%f,%f,%f,%f,%f,%f", &p1->gw, &p1->jn, &p1->zw, &p1->zl, &p1->jt, &p1->yj);
        printf(" 房租扣款,公积金,水电费,其它扣款:\n");
        scanf("%f,%f,%f,%f", &p1->fz, &p1->gj, &p1->sd, &p1->others);
        p1->should_pay = p1->gw + p1->jn + p1->zw + p1->zl + p1->jt + p1->yj;
        p1->rebate = p1->fz + p1->gj + p1->sd + p1->others;
        p1->actual_pay = p1->should_pay - p1->rebate;
    }
    p2->next = NULL;
    // edit by Yan-Xingyu Class Network-021
    printf("职工信息已输入,请按任意键返回,并存盘……");
    getch();
    return head;
}
/*输出模块*/
void output()
{
    short sel, flag, month;
    float min, max;
    do
    {
        do
        {
            printf("\n\n请选择您的列表方式:\n");
            printf("\t\t1.全部清单\n");
            printf("\t\t2.按月输出\n");
            printf("\t\t3.按工资段输出\n");
            printf("\t\t0.返回\n");
            scanf("%d", &sel);
        } while (sel != 1 && sel != 2 && sel != 3 && sel != 0);
        head = open();
        if (head == NULL)
            return;
        else
            switch (sel)
            {
            case 1:
                printf("\n\t\t\t ******全部清单******\n");
                printf(TITLE);
                p1 = head;
                do
                {
                    printf(DATA);
                    p1 = p1->next;
                } while (p1 != NULL);
                break;
            case 2:
                flag = 0;
                printf("输入要查的月份:");
                scanf("%d", &month);
                printf("\n\t\t\t******第%d月工资清单******\n", month);
                printf(TITLE);
                p1 = head;
                do
                {
                    if (p1->month == month)
                    {
                        printf(DATA);
                        p1 = p1->next;
                        flag = 1;
                    }
                    else
                        p1 = p1->next;
                } while (p1 != NULL);
                if (flag == 0)
                    printf("没有该月清单\n");
                break;
            case 3:
                flag = 0;
                printf("输入最低工资:");
                scanf("%f", &min);
                printf("输入最高工资:");
                scanf("%f", &max);
                printf("\n\t\t ******%5.1f到%5.1f工资的名单******\n", min, max);
                printf(TITLE);
                p1 = head;
                do
                {
                    if (p1->actual_pay > min && p1->actual_pay < max)
                    {
                        printf(DATA);
                        p1 = p1->next;
                        flag = 1;
                    }
                    else
                        p1 = p1->next;
                } while (p1 != NULL);
                if (flag == 0)
                    printf("没有该工资段的职工\n");
                break;
            case 0:
                break;
            }
    } while (sel != 0);
}
/*保存文件*/
void save(PAY *head)
{
    short write;
    fp = fopen("pay.txt", "w");
    if (fp == NULL)
    {
        printf("创建文件失败!");
        getch();
        return;
    }
    p1 = p2 = head;
    while (p1 != NULL)
    {
        write = fwrite(p1, sizeof(PAY), 1, fp);
        if (!write)
        {
            printf("无法写入文件!");
            getch();
            return;
        }
        p1 = p1->next;
    }
    fclose(fp);
    printf("成功存盘!");
    getch();
}
/*打开文件*/
PAY *open()
{
    short n = 1, read;
    fp = fopen("pay.txt", "r");
    if (fp == NULL)
    {
        printf("无法打开档案文件!");
        getch();
        return NULL;
    }
    if ((p1 = p2 = (PAY *)malloc(sizeof(PAY))) == NULL)
    {
        printf("内存不足!");
        getch();
        return NULL;
    }
    read = fread(p1, sizeof(PAY), 1, fp);
    if (!read)
    {
        printf("文件读取出错!1");
        getch();
        return NULL;
    }
    if (p1->next == NULL)
        head = p1;
    else
    {
        do
        {
            if (n == 1)
                head = p1;
            else
                p2->next = p1;
            n++;
            p2 = p1;
            if ((p1 = (PAY *)malloc(sizeof(PAY))) == NULL)
            {
                printf("内存不足!");
                getch();
                return NULL;
            }
            read = fread(p1, sizeof(PAY), 1, fp);
            if (!read)
            {
                printf("文件读取出错!2");
                getch();
                return NULL;
            }
        } while (p1->next != NULL);
        p2->next = p1;
    }
    return head;
    fclose(fp);
}
/*插入模块*/
PAY *insert()
{
    PAY *p3 = NULL;
    char sel;
    head = open();
    if (head == NULL)
        return NULL;
    else
        do
        {
            printf("请输入要插入的职工信息:\n");
            p3 = (PAY *)malloc(sizeof(PAY));
            if (p3 == NULL)
            {
                printf("内存不足,无法创建链表!");
                getch();
            }
            printf(" 工号:");
            scanf("%d", &p3->num);
            getchar();
            printf(" 姓名:");
            gets(p3->name);
            printf(" 工资月份:");
            scanf("%d", &p3->month);
            printf(" 岗位工资,技能工资,职务津贴,职龄津贴,交通补贴,业绩津贴:\n");
            scanf("%f,%f,%f,%f,%f,%f", &p3->gw, &p3->jn, &p3->zw, &p3->zl, &p3->jt, &p3->yj);
            printf(" 房租扣款,公积金,水电费,其它扣款:\n");
            scanf("%f,%f,%f,%f", &p3->fz, &p3->gj, &p3->sd, &p3->others);
            p3->should_pay = p3->gw + p3->jn + p3->zw + p3->zl + p3->jt + p3->yj;
            p3->rebate = p3->fz + p3->gj + p3->sd + p3->others;
            p3->actual_pay = p3->should_pay - p3->rebate;
            p1 = head;
            while (p1 != NULL)
            {
                p2 = p1;
                p1 = p1->next;
            }
            p2->next = p3;
            p3->next = NULL;
            printf("成功插入,是否继续?是(Y) 否(N)\n");
            sel = getch();
        } while (sel == 'Y' || sel == 'y');
    printf("\n任意键返回,并存盘……\n");
    getch();
    return head;
}
/*查找模块*/
void search()
{
    short sel;
    long num;
    char name[20], choose;
    head = open();
    if (head == NULL)
        return;
    else
        do
        {
            do
            {
                printf("\n选择查找方式:\n");
                printf("\t\t1.按工号查找\n");
                printf("\t\t2.按姓名查找\n");
                scanf("%d", &sel);
                getchar();
            } while (sel != 1 && sel != 2);
            switch (sel)
            {
            case 1:
                printf("输入要查的工号:");
                scanf("%ld", &num);
                break;
            case 2:
                printf("输入要查的姓名:");
                gets(name);
                break;
            }
            p1 = head;
            while ((p1->next != NULL) && (p1->num != num) && (strcmp(p1->name, name) != 0)) // 用成p1!=NULL有问题?
                p1 = p1->next;
            if ((p1->num == num) || strcmp(p1->name, name) == 0)
            {
                printf("您要查找的信息如下:\n");
                printf(TITLE);
                printf(DATA);
            }
            else
                printf("没有该信息!\n");
            printf("是否继续?是(Y) 否(N)\n");
            choose = getch();
        } while (choose == 'Y' || choose == 'y');
    printf("任意键返回");
    getch();
}
/*删除模块*/
PAY *del()
{
    short sel;
    long num;
    char name[20], choose;
    head = open();
    if (head == NULL)
        return NULL;
    else
        do
        {
            do
            {
                printf("\n选择删除方式:\n");
                printf("\t\t1.按工号删除\n");
                printf("\t\t2.按姓名删除\n");
                scanf("%d", &sel);
                getchar();
            } while (sel != 1 && sel != 2);
            switch (sel)
            {
            case 1:
                printf("输入要删除的工号:");
                scanf("%ld", &num);
                break;
            case 2:
                printf("输入要删除的姓名:");
                gets(name);
                break;
            }
            p1 = head;
            if ((p1->num == num) || strcmp(p1->name, name) == 0)
            {
                head = p1->next;
                printf("删除信息成功!");
            }
            else
            {
                while ((p1->next != NULL) && (p1->num != num) && (strcmp(p1->name, name) != 0))
                {
                    p2 = p1;
                    p1 = p1->next;
                }
                if ((p1->num == num) || strcmp(p1->name, name) == 0)
                {
                    p2->next = p1->next;
                    free(p1);
                    printf("\n删除信息成功!\n");
                }
                else
                    printf("\n没有该信息!\n");
            }
            printf("是否继续?是(Y) 否(N)\n");
            choose = getch();
        } while (choose == 'Y' || choose == 'y');
    printf("任意键返回,并存盘……");
    getch();
    return head;
}
/*修改模块*/
PAY *revise()
{
    short sel;
    long num;
    char name[20], choose;
    head = open();
    if (head == NULL)
        return NULL;
    else
        do
        {
            do
            {
                printf("\n查找要修改的对象:\n");
                printf("\t\t1.按工号查找\n");
                printf("\t\t2.按姓名查找\n");
                scanf("%d", &sel);
                getchar();
            } while (sel != 1 && sel != 2);
            switch (sel)
            {
            case 1:
                printf("输入要查的工号:");
                scanf("%ld", &num);
                break;
            case 2:
                printf("输入要查的姓名:");
                gets(name);
                break;
            }
            p1 = head;
            while ((p1->next != NULL) && (p1->num != num) && (strcmp(p1->name, name) != 0)) // 用成p1!=NULL有问题?
                p1 = p1->next;
            if ((p1->num == num) || strcmp(p1->name, name) == 0)
            {
                printf("您要修改的原始信息如下:\n");
                printf(TITLE);
                printf(DATA);
                printf("\n将上述信息改为如下信息:\n");
                printf(" 工号:");
                scanf("%d", &p1->num);
                getchar();
                printf(" 姓名:");
                gets(p1->name);
                printf(" 工资月份:");
                scanf("%d", &p1->month);
                printf(" 岗位工资,技能工资,职务津贴,职龄津贴,交通补贴,业绩津贴:\n");
                scanf("%f,%f,%f,%f,%f,%f", &p1->gw, &p1->jn, &p1->zw, &p1->zl, &p1->jt, &p1->yj);
                printf(" 房租扣款,公积金,水电费,其它扣款:\n");
                scanf("%f,%f,%f,%f", &p1->fz, &p1->gj, &p1->sd, &p1->others);
                p1->should_pay = p1->gw + p1->jn + p1->zw + p1->zl + p1->jt + p1->yj;
                p1->rebate = p1->fz + p1->gj + p1->sd + p1->others;
                p1->actual_pay = p1->should_pay - p1->rebate;
            }
            else
                printf("你要修改的信息不存在!\n");
            printf("是否继续?是(Y) 否(N)\n");
            choose = getch();
        } while (choose == 'Y' || choose == 'y');
    printf("任意键返回,并存盘……");
    getch();
    return head;
}
This entry was posted in 应用. Bookmark the permalink.

发表评论