| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 390 人关注过本帖
标题:求指导 代码为什么运行不了 i为什么是300?use=(user *)malloc(300*sizeof ...
只看楼主 加入收藏
lq32413931
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2011-12-20
结帖率:50%
收藏
已结贴  问题点数:10 回复次数:1 
求指导 代码为什么运行不了 i为什么是300?use=(user *)malloc(300*sizeof(user));什么意思?
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <string.h>

typedef struct   //定义职工信息包.括职工号、姓名、性别、年龄
{
    int number;
       char username[20];
       int sex;
    int age;
    int flag;   
}user;
int insert(user *use,char username[],int sex,int age)
{
    int i;
    for(i=0;i<300;i++)
    {
        if(use[i].flag==-1)
        {
            strcpy(use[i].username,username);
            use[i].sex=sex;
            use[i].age=age;
                        use[i].flag=1;
            return 1;
        }
        
    }
    return 0;
}
void look(user *use)
{
    int i;
    for(i=0;i<300;i++)
    {
        if(use[i].flag==1)
        {
            printf("/*************************************************************************/\n");
            printf("  职工号:%4d        姓名:%6s  ",use[i].number,use[i].username);
            if(use[i].sex==1)
            printf("     性别:男  ");
            if(use[i].sex==0)
            printf("    性别:女  ");
            printf("     年龄:%4d  \n   ",use[i].age);
            printf("\n");
            printf("/*************************************************************************/\n");
        }
    }
    system("pause");
}
int searchnumber(user *use)
{
    int i;
   
    int number;
   

    printf("\n\n\n   请输入你要查询的职工的工号:");
    scanf("%d",&number);
    for(i=0;i<300;i++)
    {
        if(use[i].number==number&&use[i].flag==1)
        {
            printf("/*************************************************************************/\n");
            printf("  职工号:%4d        姓名:%6s  ",use[i].number,use[i].username);
            if(use[i].sex==1)
            printf("     性别:男  ");
            if(use[i].sex==0)
            printf("    性别:女  ");
            printf("     年龄:%4d  \n  ",use[i].age);
            printf("\n");
            printf("/*************************************************************************/\n");
            
            return 1;
        }
    }
   
    return 0;
}
void searchname(user *use)
{
    int i;
   
    char name[10];
    printf("\n\n\n   请输入你要查询的职工的姓名:");
    scanf("%s",&name);
    for(i=0;i<300;i++)
    {
        if(strcmp(use[i].username,name)==0&&use[i].flag==1)
        {
            printf("/*************************************************************************/\n");
            printf("  职工号:%4d        姓名:%6s  ",use[i].number,use[i].username);
            if(use[i].sex==1)
            printf("     性别:男  ");
            if(use[i].sex==0)
            printf("    性别:女  ");
            printf("     年龄:%4d  \n  ",use[i].age);
            printf("\n");
            printf("/*************************************************************************/\n");
        }
    }
   
}
int searchsex(user *use)
{
    int i;
   
    int sex;
   
    while(1)
    {   
        printf("\n\n\n   请输入你要查询的职工的性别:(1,男 0,女)");
        scanf("%d",&sex);
        
        if(sex!=1&&sex!=0)
        {
            printf("\n\n\n    你的输入出现了错误!重新的选择!");
            system("pause");
            continue;
        }
        
        if(sex==1)
        {
            for(i=0;i<300;i++)
            {
                if(use[i].sex==1&&use[i].flag==1)
                {
                    printf("/*************************************************************************/\n");
                    printf("  职工号:%4d        姓名:%6s  ",use[i].number,use[i].username);
                    if(use[i].sex==1)
                    printf("     性别:男  ");
                    printf("    性别:女  ");
                    printf("     年龄:%4d  \n  ",use[i].age);
                    printf("\n");
                    printf("/*************************************************************************/\n");
                }
            }
            return 1;
        }
    if(sex==0)
        {
            for(i=0;i<300;i++)
            {
                if(use[i].sex==0&&use[i].flag==1)
                {
            printf("/*************************************************************************/\n");
                    printf("  职工号:%4d        姓名:%6s  ",use[i].number,use[i].username);
                    if(use[i].sex==1)
                        printf("     性别:男  ");
                    if(use[i].sex==0)
                        printf("    性别:女  ");
                        printf("     年龄:%4d  \n  ",use[i].age);
                        printf("\n");
            printf("/*************************************************************************/\n");
                }
            }
            return 1;
        }
        return 0;
    }
   
}
int save(user *use)
{
    int chose;
   
   
       char username[20];
       int sex;
    int age;
  while(1)
    {    printf(" \n请输入需要录入的职员的信息:\n\n");
        
        printf("       请输入职员的姓名:");
        scanf("%s",&username);
        printf("       请输入职员的性别(1,男  0,女):");
        scanf("%d",&sex);
        printf("       请输入职员的年龄:");
        scanf("%d",&age);
        printf("\n\n                请问上面输入的信息是否确认?(1,确认 2,重新填写)  ");            
        scanf("%d",&chose);
        if(chose==1)
        {
            insert(use,username,sex,age);
            printf("\n\n                职员信息录入完毕!");
            system("pause");
            return 1;
        }
        else continue;
    }
    return 0;
}
int searchmenu(user *use)
{
   
    int chose;
   
    while(1)
    {   
        printf("                       //////////////////////////////\n");
        printf("                       //                          //\n");
        printf("                       //       1、职工工号查询    //\n");
        printf("                       //       2、职工姓名查询    //\n");
        printf("                       //       3、退出查询系统    //\n");
        printf("                       //                          //\n");
        printf("                       //////////////////////////////\n\n\n");
        printf("     请选择操作:");
        scanf("%d",&chose);
        
        if(chose<1||chose>3)
        {
            printf("\n\n                   您的选择出现了错误,请重新的选择!\n\n\n");
            system("pause");
            continue;
        }
        switch(chose)
        {
        case 1:
            {
                searchnumber(use);
                printf("\n\n\n         ");
                system("pause");
                break;
            }
        case 2:
            {
                searchname(use);
                printf("\n\n\n         ");
                system("pause");
                break;
            }
        case 3:
            {
                return 0;
                break;
            }
        default: continue;
        }
    }
}
void fix(user *use)
{
    int i;
   
    int chose;
   
    int num;
    char name[10];
    int sex;
    printf("\n\n\n       请输入需要修改信息的职工工号:");
    scanf("%d",&num);
   
    for(i=0;i<300;i++)
    {
        if(use[i].number==num&&use[i].flag==1)
        {
            printf("/*************************************************************************/\n");
            printf("  职工号:%4d        姓名:%6s  ",use[i].number,use[i].username);
            if(use[i].sex==1)
            printf("     性别:男  ");
            if(use[i].sex==0)
            printf("    性别:女  ");
            printf("     年龄:%4d  \n  ",use[i].age);
            printf("\n");
            printf("/*************************************************************************/\n");
            
            
            printf("\n");
            printf("                       //       1、职工姓名修改   //\n");
            printf("                       //       3、职工性别修改    //\n");
            printf("                       //       5、退 出 系 统     //\n\n");
            
            printf("                请选择操作的类型:");
            scanf("%d",&chose);
            
            switch(chose)
            {
            case 1:
                {    printf("\n\n\n       请输入新的职工的姓名:");
                    scanf("%s",&name);
                    
                    for(i=0;i<300;i++)
                    {
                        if(use[i].number==num&&use[i].flag==1)
                        {
                            strcpy(use[i].username,name);
                            printf("\n\n\n       信息修改成功!!");
                            system("pause");
                        }
                    }
                    break;
                }
            case 2:
                {
                    printf("\n\n\n       请输入新的职工的性别:(1,男 0,女)");
                    scanf("%d",&sex);
                    
                    for(i=0;i<300;i++)
                    {
                        if(use[i].number==num&&use[i].flag==1)
                        {
                            use[i].sex=sex;
                            printf("\n\n\n       信息修改成功!!");
                            system("pause");
                }
            }
                    break;
        }
}

int del(user *use)
{
    int num;
   
    int i;
   
    while(1)
    {
        printf("\n\n\n               请输入需要删除的职工的工号:");
        
        scanf("%d",&num);
    for(i=0;i<300;i++)
        {
            if(num==use[i].number&&use[i].flag==1)
            {
                printf("\n\n               职工信息删除成功~!");
                system("pause");
               
                use[i].flag=-1;
                use[i].sex=-1;
                use[i].age=-1;
                strcpy(use[i].username,"*****");   
                use[i].number=i+1;
               
                return 1;
            }
        }
        
        printf("\n\n      找不到需要删除的职工的信息!");
        system("pause");
        return 0;
    }
}

int logoout()
{
    int chose;
   
    while(1)
    {   
        printf("\n\n\n\n       是否真的要退出程序?(1,退出 0,不退出) ");
        
        scanf("%d",&chose);
        if(chose!=1&&chose!=0)
        {
            printf("\n\n       你的选择出现了错误!请重新的选择!");
            system("pause");
        }
        
        if(chose==1)
        {
            return 1;
        }
        
        if(chose==0) return 0;
    }
   
   
}
int first(user *use)
{
    int chose;
   
    while(1)
    {
        printf("                       //////////////////////////////\n");
        printf("                       //                          //\n");
        printf("                       //       1、职工信息录入    //\n");
        printf("                       //       2、职工信息浏览    //\n");
        printf("                       //       3、职工信息查找    //\n");
        printf("                       //       4、职工信息修改    //\n");
        printf("                       //       5、职工信息删除    //\n");
        printf("                       //       6、退 出 系 统     //\n");
        printf("                       //                          //\n");
        printf("                       //////////////////////////////\n\n\n");
        printf("     请选择操作:");
        scanf("%d",&chose);
        
        if(chose<1||chose>6)
        {
            printf("\n\n                   您的选择出现了错误,请重新的选择!\n\n\n");
            system("pause");
            continue;
        }
        switch(chose)
        {
        case 1:
            {
                save(use);
                break;
            }
        case 2:
            {
                look(use);
                break;
            }
            
        case 3:
            {
                searchmenu(use);
                break;
            }
        case 4:
            {
                fix(use);
                break;
            }
        case 5:
            {
                del(use);
                break;
            }
        case 6:
            {
                if(logoout()==1)return 1;
                if(logoout()==0)continue;
                break;
            }
        default:continue;   
        }
    }
    return 0;
}
void main()
{
    int i;
    user *use;          //分配空间
    FILE *fp;
   
    if((fp=fopen("mdb.dat","r"))==NULL)
    {
        logo("数据文件调用错误");
        printf("\n\n\n                 打开文件出现错误!文件名为mdb.dat,请检测一下!\n\n\n  ");
        system("pause");
        exit(0);
    }
   
    use=(user *)malloc(300*sizeof(user));
    for(i=0;i<300;i++)
    {   
{
            fscanf(fp,"%5d",&use[i].number);
            fscanf(fp,"%20s",&use[i].username);
            fscanf(fp,"%5d",&use[i].sex);
            fscanf(fp,"%5d",&use[i].age);
            fscanf(fp,"%5d",&use[i].flag);
            }
}
   first(use);
    fclose(fp);   
   
    if((fp=fopen("mdb.dat","w"))==NULL)
    {
        logo("数据文件调用错误");
        printf("\n\n\n                 打开文件出现错误!文件名为mdb.dat,请检测一下!\n\n\n  ");
        system("pause");
        exit(0);
    }
    for(i=0;i<300;i++)
    {
        fprintf(fp,"%5d",use[i].number);
        fprintf(fp,"%20s",use[i].username);
        fprintf(fp,"%5d",use[i].sex);
        fprintf(fp,"%5d",use[i].age);
        fprintf(fp,"%5d",use[i].flag);
        fprintf(fp,"\n");
    }
   
    fclose(fp);   
        system("pause");
}


搜索更多相关主题的帖子: 300 信息 include number insert 
2011-12-22 19:44
hengde_li
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:131
专家分:178
注 册:2010-1-15
收藏
得分:10 
1)   300:不超过300人
2)   应当为 typedef struct user
     {} USER;
3)   malloc应当有struct或者如上面的,改为USER
2011-12-23 10:05
快速回复:求指导 代码为什么运行不了 i为什么是300?use=(user *)malloc(300*s ...
数据加载中...
 
   



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

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