鱼C论坛

 找回密码
 立即注册
查看: 997|回复: 3

怎么样把这两个功能改成读取文本文件中的数据

[复制链接]
发表于 2020-7-2 08:37:41 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
stu * lookdata(        stu *p1)     //查看数据的函数
{
    printf("\n\t\t\t☆☆☆显示数据☆☆☆\n");
    printf("----------------------------------------------------------------------\n");
    while(p1!=NULL)
    {
        printf("姓名:%s\n",p1->name);
        printf("学号:%s\t",p1->num);
        printf("性别:%s\t",p1->sex);
        printf("地址:%s\t",p1->address);
        printf("政治面貌:%s\t",p1->political);
        printf("手机号:%s\t",p1->phone);
        printf("QQ号:%s\t",p1->QQ);
        printf("宿舍:%s\n",p1->dorm);
        printf("邮编:%s\n",p1->PC);
        printf("Email:%s\n",p1->Email);
        printf("======================================================================\n");
        p1=p1->next;
    }
    return p1;
}












find(stu *p2)        //通过姓名查找查看数据的函数
{
    char name[20];
    int b=0;
    printf("\n\t\t\t☆☆☆查看数据☆☆☆\n");
    printf("----------------------------------------------------------------------\n");
    printf("请输入您想查找人的姓名:");
    scanf("%s",name);
    while(p2!=NULL)
    {
        if(strcmp(name,p2->name)==0)
        {
            printf("你要找到的数据\n");
            printf("姓名:%s\n",p2->name);
            printf("学号:%s\t",p2->num);
            printf("性别:%s\t",p2->sex);
            printf("地址:%s\t",p2->address);
            printf("政治面貌:%s\t",p2->political);
            printf("手机号:%s\t",p2->phone);
            printf("QQ号:%s\t",p2->QQ);
            printf("宿舍:%s\n",p2->dorm);
            printf("邮编:%s\n",p2->PC);
            printf("Email:%s\n",p2->Email);
            printf("======================================================================\n");
            b=1;
        }
        p2=p2->next;
    }
    if(b==0)
    {
        printf("\n您要查找的人不存在!\n");
    }
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-7-2 09:55:43 | 显示全部楼层
对照下面代码看看
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>

  4. /*-----信息存储区-----*/

  5. struct Caipin{
  6.     char cai[10];
  7.     int price;
  8. }caipin[7]={        //7种菜品
  9.     {"steak",50},{"lamb",30},{"bacon",25},{"chicken",30},
  10.     {"vegetable",15},{"meatball",20},{"lobster",100}
  11. };

  12. struct Zhushi{
  13.     char zhu[10];
  14.     int price;
  15. }zhushi[5]={        //5种主食
  16.     {"rice",5},{"cake",3},{"noodles",7},
  17.     {"jiaozi",20},{"bun",10}
  18. };

  19. struct Yinliao{
  20.     char yin[10];
  21.     int price;
  22. }yinliao[5]={       //5种饮料
  23.     {"juice",3},{"coca-cola",5},{"tea",20},
  24.     {"coffee",15},{"milk",7}
  25. };

  26. struct VIP{         //用链表形式保存会员信息
  27.     char name[20];
  28.     char tel[20];
  29.     struct VIP *next;
  30. };


  31. /*-----函数声明区-----*/
  32. float diancan();
  33. float choice_caipin();
  34. float choice_zhushi();
  35. float choice_yinliao();

  36. int jiesuan(struct VIP *infor,float sum);
  37. struct VIP *check_huiyuan(struct VIP *infor,char *input);

  38. int huiyuan(struct VIP **infor);
  39. void getInput(struct VIP *infor);
  40. void huiyuan_upload(struct VIP **xinxi);
  41. void huiyuan_print(struct VIP *infor);
  42. void huiyuan_delete(struct VIP **infor);
  43. void saveVIP(struct VIP *xinxi);

  44. struct VIP *readVIP();

  45. /*-----主函数区-----*/
  46. int main()
  47. {
  48.     struct VIP *infor;
  49.     infor = readVIP();
  50.    // saveVIP(infor);
  51.     printf("欢迎使用XX餐厅前台管理系统\n");
  52.     printf("请选择您需要的服务项目:\n");
  53.     printf("1\t点餐\n2\t结算\n3\t会员卡管理\n");
  54.     int temp=0;
  55.     float sum = 0;
  56.     while(1){
  57.         scanf("%d",&temp);
  58.         switch(temp)
  59.         {
  60.             case 1:sum = diancan();break;       //进入点餐界面
  61.             case 2:jiesuan(infor,sum);break;          //进入结算系统
  62.             case 3:huiyuan(&infor);break;             //进入会员管理系统
  63.             default:printf("输入错误!请重新输入");
  64.         }
  65.     }
  66.     return 0;
  67. }

  68. /*-----点餐函数区-----*/
  69. float diancan()
  70. {
  71.     printf("欢迎点餐!\n");
  72.     printf("请选择:\n1\t菜品\n2\t主食\n3\t饮料\n0\t返回主菜单\n");
  73.     int temp;
  74.     float sum = 0;
  75.     while(1){
  76.         scanf("%d",&temp);
  77.         switch(temp)
  78.         {
  79.             case 1:
  80.             sum += choice_caipin();
  81.             printf("请选择:\n1\t菜品\n2\t主食\n3\t饮料\n0\t返回主菜单\n");
  82.             break;    //调用菜品选择函数,并计算总价格
  83.             case 2:
  84.             sum += choice_zhushi();
  85.             printf("请选择:\n1\t菜品\n2\t主食\n3\t饮料\n0\t返回主菜单\n");
  86.             break;    //调用主食选择函数,并计算总价格
  87.             case 3:
  88.             sum += choice_yinliao();
  89.             printf("请选择:\n1\t菜品\n2\t主食\n3\t饮料\n0\t返回主菜单\n");
  90.             break;   //调用饮料选择函数,并计算总价格
  91.             case 0:printf("1\t点餐\n2\t结算\n3\t会员卡管理\n");return sum; //返回主菜单
  92.             default:printf("输入错误!请重新输入");break;
  93.         }
  94.     }
  95. }
  96. float choice_caipin()
  97. {
  98.     printf("请选择菜品:\n");
  99.     for(int i=0;i<7;i++){
  100.         printf("%d\t%s\t\t%d\n",i+1,caipin[i].cai,caipin[i].price);
  101.     }
  102.     printf("输入0返回上一级\n");
  103.     int choice;
  104.     float sum_caipin = 0;
  105.     while(1){
  106.         scanf("%d",&choice);
  107.         if(choice == 0){
  108.             break;
  109.         }
  110.         for (int j=0;j<7;j++){
  111.             if(choice == j+1){
  112.                 printf("%s已选择\n",caipin[j].cai);
  113.                 sum_caipin += caipin[j].price;
  114.             }
  115.         }
  116.     }
  117.     return sum_caipin;
  118. }
  119. float choice_zhushi()
  120. {
  121.     printf("请选择主食:\n");
  122.     for(int i=0;i<5;i++){
  123.         printf("%d\t%s\t\t%d\n",i+1,zhushi[i].zhu,zhushi[i].price);
  124.     }
  125.     printf("输入0返回上一级\n");
  126.     int choice;
  127.     float sum_zhushi = 0;
  128.     while(1){
  129.         scanf("%d",&choice);
  130.         if(choice == 0){
  131.             break;
  132.         }
  133.         for (int j=0;j<7;j++){
  134.             if(choice == j+1){
  135.                 printf("%s已选择\n",zhushi[j].zhu);
  136.                 sum_zhushi += zhushi[j].price;
  137.             }
  138.         }
  139.     }
  140.     return sum_zhushi;
  141. }
  142. float choice_yinliao()
  143. {
  144.     printf("请选择主食:\n");
  145.     for(int i=0;i<5;i++){
  146.         printf("%d\t%s\t\t%d\n",i+1,yinliao[i].yin,yinliao[i].price);
  147.     }
  148.     printf("输入0返回上一级\n");
  149.     int choice;
  150.     float sum_yinliao = 0;
  151.     while(1){
  152.         scanf("%d",&choice);
  153.         if(choice == 0){
  154.             break;
  155.         }
  156.         for (int j=0;j<7;j++){
  157.             if(choice == j+1){
  158.                 printf("%s已选择\n",yinliao[j].yin);
  159.                 sum_yinliao += yinliao[j].price;
  160.             }
  161.         }
  162.     }
  163.     return sum_yinliao;
  164. }

  165. /*-----结算函数区-----*/
  166. int jiesuan(struct VIP *infor,float sum)
  167. {
  168.     printf("您本次消费 %.2f 元",sum);
  169.     int temp = 1;
  170.     float rate = 0.7;
  171.     // struct VIP *infor = NULL;
  172.     char input[20];
  173.     while(temp == 1){
  174.         printf("您是否有会员卡?\n");
  175.         printf("1\t有\n0\t没有\n");
  176.         scanf("%d",&temp);
  177.         if(temp == 0){
  178.             break;
  179.         }else{
  180.             printf("请输入您的手机号码:\n");
  181.             scanf("%s",input);
  182.             if(check_huiyuan(infor,input) != NULL){
  183.                 printf("会员卡享受7折优惠\n");
  184.                 sum *= rate;
  185.                 break;
  186.             }else{
  187.                 printf("未查询到会员信息!请重新输入\n");
  188.                 continue;
  189.             }
  190.         }
  191.     }
  192.     printf("您应付金额为:%.2f\n",sum);
  193.     printf("1\t点餐\n2\t结算\n3\t会员卡管理\n");
  194.     return 0;
  195. }
  196. struct VIP *check_huiyuan(struct VIP *infor,char *input)             //检查会员信息是否存在
  197. {
  198.    
  199.     struct VIP *xinxi;
  200.     xinxi = infor;
  201.     while(xinxi != NULL){
  202.         if(strcmp(xinxi->tel,input) == 0){
  203.             return xinxi;
  204.         }
  205.         xinxi = xinxi->next;
  206.     }
  207.     return NULL;
  208. }

  209. /*-----会员管理函数区-----*/
  210. int huiyuan(struct VIP **infor)
  211. {
  212.     printf("欢迎进入会员管理系统\n");
  213.     printf("请选择您需要的服务项目:\n");
  214.     printf("1\t会员信息录入\n2\t会员信息显示\n3\t会员卡注销\n");
  215.     printf("输入0返回上一级\n");
  216.     int temp = 0,flag = 1;
  217.     // struct VIP *infor = NULL;
  218.     while(1){
  219.         scanf("%d",&temp);
  220.         switch(temp)
  221.         {
  222.             case 1:huiyuan_upload(infor);break;      //会员信息录入
  223.             case 2:huiyuan_print(*infor)  ;break;      //会员信息打印
  224.             case 3:huiyuan_delete(infor) ;break;      //会员信息注销
  225.             case 0:printf("1\t点餐\n2\t结算\n3\t会员卡管理\n");return 0;
  226.             default:printf("输入错误!请重新输入");continue;
  227.         }
  228.     }
  229. }
  230. void getInput(struct VIP *infor)
  231. {
  232.     printf("请输入姓名:");
  233.     scanf("%s",infor->name);
  234.     printf("请输入手机号码:");
  235.     scanf("%s",infor->tel);
  236.     infor->next=NULL;
  237. }
  238. void huiyuan_upload(struct VIP **xinxi)
  239. {
  240.     struct VIP *infor,*temp;
  241.     infor = (struct VIP *)malloc(sizeof(struct VIP));
  242.     if (infor == NULL){
  243.         printf("内存分配失败!");
  244.         exit(1);
  245.     }
  246.     getInput(infor);
  247.    
  248.     if(*xinxi != NULL){
  249.         temp = *xinxi;
  250.         *xinxi = infor;
  251.         infor->next = temp;
  252.     }
  253.     else{
  254.         *xinxi = infor;
  255.         infor->next = NULL;
  256.     }
  257.     saveVIP(*xinxi);
  258. }
  259. void saveVIP(struct VIP *xinxi)
  260. {
  261.     FILE *fp;
  262.     struct VIP *infor = xinxi;
  263.     fp = fopen("huiyuan.txt","wb+");
  264.     if(fp == NULL){
  265.                 printf("can't open file!\n");
  266.                 exit(1);
  267.     }
  268.     if(infor != NULL){
  269.         //fprintf(fp,"%s\t%s\n",infor->name,infor->tel);
  270.                 rewind(fp);
  271.                 fwrite(infor,sizeof(struct VIP),1,fp);
  272.                 while (infor->next!=NULL)
  273.                 {
  274.          fwrite(infor->next,sizeof(struct VIP),1,fp);
  275.          infor = infor->next;
  276.                 }
  277.     }
  278.     fclose(fp);

  279. }
  280. struct VIP *readVIP() //运行前把文件内容读取到电脑内存  
  281. {  
  282.     struct VIP *p,*q,*infor = (struct VIP *)malloc(sizeof(struct VIP));
  283.     p = q = infor;
  284.         FILE *fp;
  285.    if ( (fp=fopen("huiyuan.txt", "rb+"))==NULL)
  286.         {
  287.         if ( (fp=fopen("huiyuan.txt", "wb+"))==NULL)
  288.                 {
  289.                          printf("无法创建或打开"huiyuan.txt"文件\n");
  290.                      exit(0);
  291.                 }
  292.          return NULL;
  293.         }
  294.         else
  295.         {
  296.                 if (fread(infor,sizeof(struct VIP),1,fp)!=0)
  297.                 {
  298.                          q= (struct VIP*)malloc(sizeof(struct VIP));
  299.                         while(fread(q,sizeof(struct VIP),1,fp)!=0)
  300.                     {
  301.                             p->next = q;
  302.                                 p = q;
  303.                             q= (struct VIP*)malloc(sizeof(struct VIP));
  304.                     }
  305.             p->next =NULL ;
  306.            
  307.             }
  308.                 else
  309.                 {
  310.                             infor=NULL;
  311.                 }
  312.                          fclose(fp);
  313.                      return infor;
  314.         }
  315.    
  316.    
  317. }  
  318. void huiyuan_print(struct VIP *infor)
  319. {
  320.     struct VIP *xinxi;
  321.     int num = 1;
  322.     xinxi = infor;
  323.     while(xinxi != NULL){
  324.         printf("Number %d:",num);
  325.         printf("  姓名:%s",xinxi->name);
  326.         printf("  手机号码:%s\n",xinxi->tel);
  327.         xinxi = xinxi->next;
  328.         num++;
  329.     }
  330. }
  331. void huiyuan_delete(struct VIP **infor)
  332. {
  333.     struct VIP *xinxi,*temp;
  334.     char input[20];
  335.     printf("请输入需要删除的会员的手机号码:\n");
  336.     scanf("%s",input);
  337.     temp = *infor;
  338.     xinxi = temp;
  339.     while(xinxi != NULL){
  340.         if(strcmp(xinxi->tel,input)==0){
  341.                 if (xinxi==*infor)
  342.                 {
  343.                     *infor=(*infor)->next;
  344.             free(xinxi);
  345.                         break;
  346.                 }
  347.                 else
  348.                 {
  349.             temp->next = xinxi->next;
  350.                         free(xinxi);
  351.             break;
  352.                 }
  353.            
  354.         }
  355.         temp = xinxi;
  356.         xinxi = xinxi->next;
  357.     }
  358.     saveVIP(*infor);
  359. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-7-2 09:59:40 | 显示全部楼层
特别是下面这两个函数看看
  1. void saveVIP(struct VIP *xinxi)
  2. {
  3.     FILE *fp;
  4.     struct VIP *infor = xinxi;
  5.     fp = fopen("huiyuan.txt","wb+");
  6.     if(fp == NULL){
  7.                 printf("can't open file!\n");
  8.                 exit(1);
  9.     }
  10.     if(infor != NULL){
  11.         //fprintf(fp,"%s\t%s\n",infor->name,infor->tel);
  12.                 rewind(fp);
  13.                 fwrite(infor,sizeof(struct VIP),1,fp);
  14.                 while (infor->next!=NULL)
  15.                 {
  16.          fwrite(infor->next,sizeof(struct VIP),1,fp);
  17.          infor = infor->next;
  18.                 }
  19.     }
  20.     fclose(fp);

  21. }
  22. struct VIP *readVIP() //运行前把文件内容读取到电脑内存  
  23. {  
  24.     struct VIP *p,*q,*infor = (struct VIP *)malloc(sizeof(struct VIP));
  25.     p = q = infor;
  26.         FILE *fp;
  27.    if ( (fp=fopen("huiyuan.txt", "rb+"))==NULL)
  28.         {
  29.         if ( (fp=fopen("huiyuan.txt", "wb+"))==NULL)
  30.                 {
  31.                          printf("无法创建或打开"huiyuan.txt"文件\n");
  32.                      exit(0);
  33.                 }
  34.          return NULL;
  35.         }
  36.         else
  37.         {
  38.                 if (fread(infor,sizeof(struct VIP),1,fp)!=0)
  39.                 {
  40.                          q= (struct VIP*)malloc(sizeof(struct VIP));
  41.                         while(fread(q,sizeof(struct VIP),1,fp)!=0)
  42.                     {
  43.                             p->next = q;
  44.                                 p = q;
  45.                             q= (struct VIP*)malloc(sizeof(struct VIP));
  46.                     }
  47.             p->next =NULL ;
  48.            
  49.             }
  50.                 else
  51.                 {
  52.                             infor=NULL;
  53.                 }
  54.                          fclose(fp);
  55.                      return infor;
  56.         }
  57.    
  58.    
  59. }  
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-7-2 11:14:32 | 显示全部楼层
在函数开始部分加上读取文件的语句。当然前面输入完成时要有存储到文件的语句
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-22 07:15

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表