| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 458 人关注过本帖
标题:哪位大鸟帮忙看看哈 加排序
只看楼主 加入收藏
seyeboy
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2011-6-26
结帖率:100%
收藏
已结贴  问题点数:30 回复次数:5 
哪位大鸟帮忙看看哈 加排序
在该程序中加个:按工资排序输出所有职工工资报表的子函数




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

struct woker      //定义一个woker结构类型
{
     char name[30];     //定义woker的姓名为字符型
     char ID[10];       //定义woker的ID号为字符型
     int base_pay;    //定义woker的工资
     double pit;      //定义税金
};

int read(struct woker staff[]);       //申明读取文件函数
void start();                         //申明开始菜单函数
void save(struct woker staff[]);      //申明保存文件函数
void add();                           //申明增加记录函数
void see();                           //申明查看记录函数
void search();                        //申明查找记录函数
void search_ID();                     //按ID号查找
void search_name();                   //按姓名查找
void del();                           //删除记录
void name_del();                      //按姓名删除
void ID_del();                        //按ID号删除
void modify();                        //修改信息
void ID_modify();                     //按ID号修改
void name_modify();                   //按姓名修改
void reserve();                       //保存所有信息
void count();                         //计算税金
void back();                          //返回到主菜单
struct woker staff[100];              //保存所有的职工信息
int total_staff=0;                    //记录总的职工人数

void main()
{
     int choose=0;    //定义用户选择项目
     while(1)
     {
         start();      //显示主菜单      
         printf("\t请选择您需要的操作: ");
         scanf("%d",&choose);
         system("cls");   //清除屏幕
         switch(choose)   //根据用户选择执行相应的功能模块
         {
             case 0: exit(0); //退出
             break;
             case 1: add();    //输入信息
             back();   
             break;
             case 2: see();    //显示信息
             back();
             break;
             case 3: modify(); //修改信息
             back();
             break;
             case 4: search(); //查找信息
             back();
             break;
             case 5: del(); //删除信息
             back();
             break;
             case 6: reserve(); //保存数据
             back();
             break;
             case 7: count(); //计算税金
             back();
             break;
             default:printf("\t输入有误!\n"); //错误的输入
             back();
             break;
         }
     fflush(stdin);    //清空输入缓冲区
     getchar();        //消除输入残留垃圾
     system("cls");    //清除屏幕
     }
}


/**********************
      读取文件数据
**********************/
int read(struct woker staff[])   
{
     FILE *fp=NULL;
     int i=0;
     fp=fopen("C://woker.dat","rb");   //打开文件
     while(fread(&staff[i],sizeof(struct woker),1,fp))
     i++;
     fclose(fp);
     return i;
}


/********************
      开始主菜单
********************/
void start()
{
printf("\n");
printf("\t***********************************\n");
printf("\t    欢迎您来到职工工资纳税系统\n");
printf("\t-----------------------------------\n");
printf("\t\t [1] 输 入 信 息\n\n");
printf("\t\t [2] 显 示 信 息\n\n");
printf("\t\t [3] 修 改 信 息\n\n");
printf("\t\t [4] 查 找 信 息\n\n");
printf("\t\t [5] 删 除 信 息\n\n");
printf("\t\t [6] 保 存 数 据\n\n");
printf("\t\t [7] 计 算 税 金\n\n");
printf("\t\t [0] 退 出 系 统\n\n");
printf("\t***********************************\n\n");
}


/*********************
       保存信息
*********************/
void save(struct woker *staff)  
{
FILE *fp=NULL;
fp=fopen("C://woker.dat","ab+");
fwrite(staff,sizeof(struct woker),1,fp);
fclose(fp);
}


/*********************
      添加职员信息
*********************/
void add()
{
     int numberTemp=0;
     int i=0;
     struct woker temp;
     int q;
     printf("----------添加职工信息----------\n");
     printf("请输入您要添加职工个数:");
     scanf("%d",&numberTemp);
for(i=0;i<numberTemp;i++)
     {
         printf("输入第%d个职工信息\n",i+1);
         printf("姓名:");
         scanf("%s",temp.name);
         printf("工号:");
         scanf("%s",temp.ID);
         printf("工资:");
         scanf("%d",&temp.base_pay);
         staff[total_staff++]=temp; //写入到数组
         q=staff[i].base_pay-1600;         
         if(q<=0){
             staff[i].pit=0;}
         else
         {
             if(q<=500){
                 staff[i].pit=q*0.05;}
             else
             {
                 if(q>500&&q<=2000){
                     staff[i].pit=25+(q-500)*0.1;}
                 else
                 {
                     if(q>2000&&q<=5000){
                         staff[i].pit=25+150+(q-2000)*0.15;}
                     else
                     {
                         if(q>5000&&q<=20000){
                             staff[i].pit=25+150+450+(q-5000)*0.2;}
                         else
                         {
                             if(q>20000&&q<=40000){
                                 staff[i].pit=25+150+450+3000+(q-20000)*0.25;}
                             else
                             {
                                 if(q>40000&&q<=60000){
                                     staff[i].pit=25+150+450+3000+5000+(q-40000)*0.3;}
                                 else
                                 {
                                     if(q>60000&&q<=80000){
                                         staff[i].pit=25+150+450+3000+5000+6000+(q-60000)*0.35;}
                                     else
                                     {
                                         if(q>80000&&q<=100000){
                                             staff[i].pit=25+150+450+3000+5000+6000+7000+(q-80000)*0.40;}
                                         else
                                         {
                                             if(q>100000){
                                                 staff[i].pit=25+150+450+3000+5000+6000+7000+8000+(q-100000)*0.45;}
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }  
      
      
     }save(&temp); //写入到文件
     printf("--------------------------------\n");
     printf("添加成功!\n");
}

/**********************
      查看职员信息
**********************/
void see()
{
     int i=0;
     printf("-------全部职工信息-------\n");
     printf("姓名   工号      工资      纳税金额\n");
     for (i=0;i<total_staff; i++)
     printf("%s\t%s\t%d\t%lf\t\n",staff[i].name,staff[i].ID,staff[i].base_pay,staff[i].pit);
}


/***********************
查找职员信息
***********************/
void search()
{
     int n=0;
     printf("------查找职工信息-------\n");
     printf("[1] 按姓名查找\n");
     printf("[2] 按工号查找\n");
     scanf("%d",&n);
     switch(n)
     {
         case 1: search_name();
         break;
         case 2: search_ID();
         break;
         default: printf("输入错误!\n");
         break;
     }
}
void search_name()   //按姓名查找
{
     char name[30];
     int i=0;
     printf("请输入要查找职工姓名:");
     scanf("%s",name);
     system("cls");
     printf("-------全部职工信息-------\n");
     printf("姓名   工号      工资      纳税金额\n");
     for (i=0;i<total_staff;i++)
     {
         if (strcmp(name,staff[i].name)==0)
         {
             printf("%s\t%s\t%d\t%lf\t\n",staff[i].name,staff[i].ID,staff[i].base_pay,staff[i].pit);
         }
     }
     printf("-------------------------\n");
}
void search_ID()//按工号查找
{
     char id[10];
     int i=0;
     printf("请输入要查找职工工号:");
     scanf("%s",id);
     system("cls");
     printf("-------全部职工信息-------\n");
     printf("姓名   工号      工资      纳税金额\n");
     for (i=0;i<total_staff;i++)
     {
         if (strcmp(id,staff[i].ID)!=0)
         {
             printf("%s\t%s\t%d\t%lf\t\n",staff[i].name,staff[i].ID,staff[i].base_pay,staff[i].pit);
         }
     }
printf("-------------------------------------------------------------\n");
}


/********************
     删除职员信息
********************/
void del()
{
     int n=0;
     printf("------删除职工信息-------\n");
     printf("[1] 按姓名删除\n");
     printf("[2] 按工号删除\n");
     scanf("%d",&n);
     switch(n)
     {
         case 1: name_del();
         break;
         case 2: ID_del();
         break;
     }
}
void name_del() //按姓名删除
{
     FILE *fp=NULL;
     char name[30];
     int i=0;
     int j=0;
     printf("请输入要删除的职工姓名:");
     scanf("%s",name);
     for(i=0;i<total_staff;i++)
     {
         if (strcmp(name,staff[i].name)==0)
         {
             for (j=i;j<total_staff-1;j++)
             {
                 staff[j]=staff[j+1];
             }
             total_staff--;
         }
     }


     //更新数据
     fp=fopen("C://woker.dat","wb");
     for (i=0;i<total_staff;i++)
     {
         fwrite(&staff[i],sizeof(struct woker),1,fp);
     }
     fclose(fp);
     printf("删除成功!\n");

}
void ID_del() //按工号删除
{
     FILE *fp=NULL;
     char id[60];
     int i=0;
     int j=0;
     printf("请输入要删除的职工工号:");
     scanf("%s",id);
     for(i=0;i<total_staff;i++)
     {
         if (strcmp(id,staff[i].ID)==0)
         {
             for (j=i;j<total_staff-1;j++)
             {
staff[j]=staff[j+1];
             }
             total_staff--;
         }
     }

     //更新数据
     fp=fopen("C://woker.dat","wb");
     for (i=0;i<total_staff;i++)
     {
         fwrite(&staff[i],sizeof(struct woker),1,fp);
     }
     fclose(fp);
     printf("删除成功!\n");
}
/***********************
      修改职工信息
***********************/
void modify()
{
     int mode=0;
     printf("==========修改职工信息===========\n");
     printf("请选择修改查找方式\n");
     printf("[1] 按姓名查找修改\n");
     printf("[2] 按工号查找修改\n");
     scanf("%d",&mode);
     switch(mode)
     {
         case 1: name_modify();
         break;
         case 2: ID_modify();
         break;
         default: printf("输入错误!\n");
         break;
     }
}
void name_modify()//按姓名修改
{
     FILE *fp=NULL;
     char name[30];
     int i=0;
     printf("请输入要修改的职工姓名:");
     scanf("%s",name);
     for (i=0;i<total_staff;i++)
     {
         if (strcmp(name,staff[i].name));
         else
         break;
     }
     printf("---------------------------\n");
     printf("姓名   工号      工资      纳税金额\n");
     printf("%s\t%s\t%d\t%lf\t\n",staff[i].name,staff[i].ID,staff[i].base_pay,staff[i].pit);
     printf("-----------------------------------------------------\n");
     printf("请重新输入该职工信息:\n");
     printf("姓名:");
     scanf("%s",staff[i].name);
     printf("工号:");
     scanf("%s",staff[i].ID);
printf("工资:");
     scanf("%d",&staff[i].base_pay);
         //更新数据
     fp=fopen("C://woker.dat","wb");
     for (i=0;i<total_staff;i++)
     {
         fwrite(&staff[i],sizeof(struct woker),1,fp);
     }
     fclose(fp);
     printf("---------------------------------\n");
     printf("修改成功!\n");
}
void ID_modify()//按工号修改
{
     FILE *fp=NULL;
     char id[30];
     int i=0;
     printf("请输入要修改的职工工号:");
     scanf("%s",id);
     for (i=0;i<total_staff;i++)
     {
     if (strcmp(id,staff[i].ID));
     else
     break;
     }
     printf("--------------------------\n");
     printf("姓名   工号      工资      纳税金额\n");
     printf("%s\t%s\t%d\t%lf\t\n",staff[i].name,staff[i].ID,staff[i].base_pay,staff[i].pit);
     printf("-----------------------------------------------------\n");
     printf("请重新输入该职工信息:\n");
     printf("姓名:");
     scanf("%s",staff[i].name);
     printf("工号:");
     scanf("%s",staff[i].ID);
     printf("工资:");
     //更新数据
     fp=fopen("C://woker.dat","wb");
     for (i=0;i<total_staff;i++)
     {
     fwrite(&staff[i],sizeof(struct woker),1,fp);
     }
     fclose(fp);
     printf("---------------------------------\n");
     printf("修改成功!\n");
}


/*********************
       保存数据
*********************/
void reserve()
{   
     FILE *fp=NULL;
     char name[30];
     int i=0;
     printf("\t--------------保存数据--------------\n\n");
     printf("\t          正在保存数据......请稍等\n\n\n\n\n");
     fp=fopen("C://woker.dat","wb");
     for (i=0;i<total_staff;i++)
     {
         fwrite(&staff[i],sizeof(struct woker),1,fp);
     }
     fclose(fp);
     printf("\t           ^_^恭喜您 数据已保存成功!\n\n\n\n");
}
void count()
    {
    double q,salary, ns=0;
    printf("请输入您的工资:\n");
    scanf("%lf", &salary);
    q=salary-1600;
    if(q<=0){
        ns=0;}
    else
    {
        if(q<=500){
            ns=q*0.05;}
        else
        {
            if(q>500&&q<=2000){
                ns=25+(q-500)*0.1;}
            else
            {
                if(q>2000&&q<=5000){
                    ns=25+150+(q-2000)*0.15;}
                else
                {
                    if(q>5000&&q<=20000){
                        ns=25+150+450+(q-5000)*0.2;}
                    else
                    {
                        if(q>20000&&q<=40000){
                            ns=25+150+450+3000+(q-20000)*0.25;}
                        else
                        {
                            if(q>40000&&q<=60000){
                                ns=25+150+450+3000+5000+(q-40000)*0.3;}
                            else
                            {
                                if(q>60000&&q<=80000){
                                    ns=25+150+450+3000+5000+6000+(q-60000)*0.35;}
                                else
                                {
                                    if(q>80000&&q<=100000){
                                        ns=25+150+450+3000+5000+6000+7000+(q-80000)*0.4;}
                                    else
                                    {
                                        if(q>100000){
                                            ns=25+150+450+3000+5000+6000+7000+8000+(q-100000)*0.45;}
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    printf("您应缴纳的税金为:\n");
    printf("%lf",ns);
}

/*******************
      返回主菜单
*******************/
void back()
{
     printf("\n\t请按Enter键返回主菜单");
}


搜索更多相关主题的帖子: 姓名 double 职工工资 
2011-07-02 09:45
lz1091914999
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:四川
等 级:贵宾
威 望:37
帖 子:2011
专家分:5959
注 册:2010-11-1
收藏
得分:30 
程序代码:
void insertion_sort(void * data,     // VC6.0改为char * data
                    unsigned num,
                    unsigned size,
                    int (* compare)(const void *, const void *))
{
    unsigned i, j, k;
    void * temp = malloc(size);
    for(i = 1; i < num; i++) {
        for(j = 0; j < i; j++)
            if(compare(data + j * size, data + i * size) > 0)
                break;
        memcpy(temp, data + i * size, size);
        for(k = i; k > j; k--)
            memcpy(data + k * size, data + (k - 1) * size, size);
        memcpy(data + j * size, temp, size);
    }
    free(temp);
}

int compare(const void * p1, const void * p2) {
    return ((struct woker *)p1)->base_pay - ((struct woker *)p2)->base_pay;
}

void sort(void) {
    insertion_sort(staff, total_staff, sizeof(struct woker), compare);     // VC6.0改为insertion_sort((char *)staff, total_staff, sizeof(struct woker), compare);
}
// 把这三个函数放在main的前面,在main里调用sort


[ 本帖最后由 lz1091914999 于 2011-7-2 15:15 编辑 ]

My life is brilliant
2011-07-02 10:44
seyeboy
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2011-6-26
收藏
得分:0 
哥们怎么调用啊?给个完整的程序吧,急着交嘞
这几天头都看大了。。。。
2011-07-02 11:16
lz1091914999
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:四川
等 级:贵宾
威 望:37
帖 子:2011
专家分:5959
注 册:2010-11-1
收藏
得分:0 
上面的代码都是你自己写的?

My life is brilliant
2011-07-02 12:19
kelas
Rank: 6Rank: 6
等 级:侠之大者
帖 子:176
专家分:434
注 册:2010-5-28
收藏
得分:0 
期待大鸟。。。。
2011-07-02 12:27
seyeboy
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2011-6-26
收藏
得分:0 
嗯,对的,我们的实验课
2011-07-02 13:37
快速回复:哪位大鸟帮忙看看哈 加排序
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.048619 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved