永发信息网

C语言 编写一个统计会员总人数,总积分的模块,录入模块已做好

答案:1  悬赏:80  手机版
解决时间 2021-05-03 12:05
  • 提问者网友:十年饮冰
  • 2021-05-02 14:23

怎么统计录入模块中登记的会员总数,与积分总数?录入模块如下:

#include"vip.h"
#include<stdio.h>
void addinf()
{
    FILE *fp = NULL;   //定义指向FILE类型结构体的指针变量fp
    struct vip TmpS;//结构体变量Tmps
    char DataFile[40] = "dataentry.txt";      //文件名
    int count = 1; //循环变量count控制输入数据的最大值

 //输入存储数据的文件名
    printf("\nplease input the file name to store data,end with enter");
    printf("\n(:Name of file can't exceed 8 characters,\nsuffix can't exceed 3 characters,\npart of exceed will be discarded.)\n");
    scanf("%s",DataFile);
   
 //判断输入的文件名是否为空,如果为空重新输入
    while(DataFile[0] == ('\0'))
    {
        printf("\nplease input new file name to store data,end with enter.");
        printf("\n(Notice:Name of file can't exceed 8 characters,\nsuffix can't exceed 3 characters,\npart of exceed will be discarded.)\n");
        scanf("%s",DataFile);
    }

 fp = fopen(DataFile,"wb+"); //打开二进制文件用来更新(读/写)数据,使fp指向该文件
    if (fp==NULL)             //判断打开文件是否成功,如果失败则关闭文件
    {
        printf("\n Open file %s fail!End with any key.\n",DataFile);
        exit(1);
    }

 //输入0代表输入完毕
    printf("Please input vip's infomation.\n0 means end.\n");
    printf("(Notice:Number is not exceed 9 figures;Name is not exceed 20 characters)\n");
    while(count<= SIZE)  //控制输入的数据必须小于SIZE
    {
        printf("会员号:");
        scanf("%d",&TmpS.number);  
        if (TmpS.number==0)
   break;
        printf("会员的姓名:");    
        scanf("%s",TmpS.name);
        printf("\n会员的生日(例如19900811):");
        scanf("%d",&TmpS.age);    
  printf("\n会员的积分:");
        scanf("%d",&TmpS.integral);
  printf("\n会员的性别:");
        scanf("%s",TmpS.sex);
  printf("\n会员的电话:");
        scanf("%d",&TmpS.telephone);
        printf("\n");
        if(fwrite(&TmpS,sizeof(vip),1,fp)!=1)//判断写入是否成功,如果不成功则退出
        {
            printf("\nwrite file %s fail!End with any key\n",DataFile);
            exit(1);
        }
        count++;     //写下一个数据项
    }
    if (count>SIZE)  //如果输入数据大于SIZE,则提示超出范围
        printf("\nsorry,number of data can not exceed %d\n",SIZE);
    fclose(fp);      //关闭文件,不能再通过指针对文件进行读写操作

 //将保存的数据显示在屏幕上
    printf("The data has stored successfully in file %s.\n",DataFile);
    printf("Content as follow:\n");

    //以只读的方式打开文件
 fp=fopen(DataFile,"rb");
    if (fp==NULL)
    {
       printf("\nOpen file%sfail!End with any key \n",DataFile);
       exit(1);
    }

    //输出信息
 printf("\n     Number\t    Name\tage\tintegral\tsex\t  telephone\n");
    while(fread(&TmpS,sizeof(vip),1,fp) != (int)NULL)
    {
        printf("\n         %d\t%s\t%d\t  %d\t         %s\t   %d\n",TmpS.number,TmpS.name,TmpS.age,TmpS.integral,TmpS.sex,TmpS.telephone);
    }
    fclose(fp);
}
link creatlist()
{
    FILE *fp = NULL;
    struct vip TmpS;
    char DataFile[40] = "";
    int count = 1;               //循环变量count控制输入数据的最大值
    link head,pointer,newpoint;  //定义三个指针变量

    //输入存储数据的文件名
 printf("\nplease input the file name to store data,end with enter.");
    printf("\n(Notice:Name of file can't exceed 8 characters,\nsuffix can't exceed 3 characters,\npart of exceed will be discarded.)\n");
    scanf("%s",DataFile);

    while(DataFile[0] == ('\0'))
    {
        printf("\nplease input newpoint file name to store data,end with enter.");
        printf("\n(Notice:Name of file can't exceed 8 characters,\nsuffix can't exceed 3 characters,\npart of exceed will be discarded.)\n");
        scanf("%s",DataFile);
    }

    fp = fopen(DataFile,"rb");
    if (fp == NULL)
    {
        printf("\n Open file %s fail!End with any key.\n",DataFile);
        exit(1);
    }

    head=(link)malloc(sizeof(vip));    //动态开辟内存,长度为一个结构体变量的数据项,返回值为结构体类型的指针
    if(head==NULL)                      //判断是否成功开辟内存
    {
        printf("Array memory failed!\n");
        exit(0);
    }
    else                                //成功开辟内存,给头结点的数据成员赋值
    {
        head->number=0;
        strcpy(head->name,"abc");
  head->age=0;
  head->integral;
  strcpy(head->sex,"def");
        head->telephone=0;
        head->next=NULL;
    }

    pointer=head;                       //使pointer指向头结点
    while(fread(&TmpS,sizeof(vip),1,fp) != (int)NULL)  //直到所读文件遇到NULL为止,执行以下循环体
    {
        newpoint=(link)malloc(sizeof(vip));    //为新结点开辟内存

        newpoint->number=TmpS.number;                   //为新结点的数据成员赋值
        strcpy(newpoint->name,TmpS.name);
  newpoint->age=TmpS.age;
  newpoint->integral=TmpS.integral;
  strcpy(newpoint->sex,TmpS.sex);
  newpoint->telephone=TmpS.telephone;

        newpoint->next=NULL;                            //使新结点的下一个结点为空(链尾)

        pointer->next=newpoint;                         //将新结点连接在pointer所指的结点后(循环的第一次是连在头结点后面)
        pointer=pointer->next;                          //使pointer指向下一个结点(循环执行前pointer指向头结点,循环一次向后移动一个结点)
    }

    fclose(fp);                                         //关闭文件

 pointer=head->next;                                 //使pointer指向头结点的下一个结点,即不输出头结点的信息
    printf("\nNumber\t  Name\t        age\t   integral\tsex\t  telephone\n");
    while(pointer!=NULL)                                //输出链表中的数据
    {
        printf("\n%d\t%s\t%d\t   %d\t       %s\t  %d\n",pointer->number,pointer->name,pointer->age,pointer->integral,pointer->sex,pointer->telephone);
  pointer=pointer->next;
    }

 return head;                                        //返回链表的头地址
}

最佳答案
  • 五星知识达人网友:千夜
  • 2021-05-02 15:43

这串代码在DOS下 录入不就行了吗?

我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯