| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 894 人关注过本帖
标题:你们都来看看这个程序哪里错了? 急
取消只看楼主 加入收藏
飘飘来过
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2015-1-4
结帖率:0
收藏
已结贴  问题点数:20 回复次数:10 
你们都来看看这个程序哪里错了? 急

#include<stdio.h>

#include<stdlib.h>

#define MENU_NUM_MAX 100  // 假设有100种菜式

#define LEN sizeof(struct MenuInfo)

struct MenuInfo

{

    int ID;

    char MenuName[20];

    float price;

}Menu[MENU_NUM_MAX];

 

/*   基础数据维护 */

void AddMenu()

{

    FILE *fp;

    int menu_num;

     

    printf("\t\t 你要添加多少种菜?:");

    scanf("%d",&menu_num);

    for(int i=0;i<menu_num;i++)

    {

        printf("\n"); // added this line

        printf("\t\t请搜索输入ID:");

        scanf("%d",&Menu[i].ID);

        printf("\t\t请输入菜名:");

        scanf("%s",Menu[i].MenuName);

        printf("\t\t请输入[%s]菜的价格:",Menu[i].MenuName);

        Menu[i].price=0.0f; //initial float price

        scanf("%f",&Menu[i].price);

        fflush(stdin);

    }

     

    if((fp=fopen("MenuInfo.dat","ab"))==NULL) // open binary file

    {

        printf("Can't open file\n");

        exit(1);

    }

    for(int j=0;j<menu_num;j++)

    {   

        if(fwrite(&Menu[j],LEN,1,fp)!=1) //writing data to binary file

            printf("Error writing file.\n");

    }

   fclose(fp); // close file point

}

 

void DisplayMenuInfo()

{

    FILE *fp;

    printf("\n\t\tID  菜名\t\t价格\n"); // column headings

    if((fp=fopen("MenuInfo.dat","rb"))==NULL) // open binary file

    {

        printf("Can't open file\n");

        exit(1);

    }

     

    int i=0;

    do

    {

        fseek(fp,i*LEN,SEEK_SET); // move file head location

        if(fread(&Menu[i],LEN,1,fp)) // read data save to structure variable

        {

            printf("\t\t%d  %5s\t\t%5.1f元\n",Menu[i].ID,Menu[i].MenuName,Menu[i].price);

            i++;

        }

    }while(!feof(fp));

     

    fclose(fp);

}

void DeleteToMenu()

{

    FILE *fp;

    int MenuID;

    int todelete=-1;

    int i=0;

    printf("请输入要删除的菜名的ID:");

    scanf("%d",&MenuID);

     

    /* load or reload the file and check that record with that ID exists */

    if((fp=fopen("MenuInfo.dat","rb"))==NULL) // open binary file

    {

        printf("Can't open file\n");

        exit(1);

    }

     

    do

    {

        fseek(fp,i*LEN,SEEK_SET); // move file head location

        if(fread(&Menu[i],LEN,1,fp))

        {

            if (Menu[i].ID==MenuID) todelete=i;   

            i++;

        }

    }while(!feof(fp));

    fclose(fp);

     

    if (todelete==-1)

    {

        printf("A menu with that ID doesn't exist\n");

    }

    else

    {

        /* write records back to file excluding one to be deleted */

        if((fp=fopen("MenuInfo.dat","wb"))==NULL) // open binary file

        {

            printf("Can't open file\n");

            exit(1);

        }

         

        for(int j=0;j<i;j++)

        {

            if (j==todelete) continue;  /* skip record to be deleted */

            if(fwrite(&Menu[j],LEN,1,fp)!=1) //writing data to binary file

                printf("Error writing file.\n");

        }

        fclose(fp); // close file point

    }

}

void FindMenu()

{

    FILE *fp;

    int MenuID;

    bool find_mark=false;

    printf("\n\t\t请输入你要查找的菜名ID:");

    scanf("%d",&MenuID);

     

    printf("\n\t\tID  菜名\t\t价格\n"); // column headings

    if((fp=fopen("MenuInfo.dat","rb"))==NULL) // open binary file

    {

        printf("Can't open file\n");

        exit(1);

    }

     

    int i=0;

    do

    {

        fseek(fp,i*LEN,SEEK_SET); // move file head location

        fread(&Menu[i],LEN,1,fp);  // read data save to structure variable

        if(Menu[i].ID==MenuID)

        {

            printf("\t\t%d  %5s\t\t%5.1f元\n",Menu[i].ID,Menu[i].MenuName,Menu[i].price);

            find_mark=true;

            break;

        }

         

        i++;

    }while(!feof(fp));

     

    if(!find_mark) printf("\n\t 尊敬的客户:我们餐厅没有你要点的菜喔,你可以试试我们的招牌菜啊^-^.\n");

     

    fclose(fp);

}
/*   基础数据维护完毕 */  

// sc.cpp主文件

#include <stdio.h>

#include <stdlib.h>

#include "mm.h"

void main(void)



{

    //AddMenu();

    //DisplayMenuInfo();

    //FindMenu();

}
搜索更多相关主题的帖子: include price include price include price 
2015-01-04 00:42
飘飘来过
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2015-1-4
收藏
得分:0 
#include "mm.h"
   编译的时候总是说这里的错
2015-01-04 00:50
飘飘来过
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2015-1-4
收藏
得分:0 
回复 3楼 wp231957
那应该怎么改?
2015-01-04 10:33
飘飘来过
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2015-1-4
收藏
得分:0 
回复 5楼 wp231957
那我是不是要在开头加个头文件?
2015-01-04 10:49
飘飘来过
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2015-1-4
收藏
得分:0 
回复 7楼 wp231957
我系统没这个文件  我用这个是我希望把以上的操作记录保存在mm.h这个文件里    可是就是不知道怎么错了
2015-01-04 11:01
飘飘来过
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2015-1-4
收藏
得分:0 
回复 9楼 wp231957
好的  我现在就是想知道头文件应该怎么写?   mm.h应该在哪?
2015-01-04 11:11
飘飘来过
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2015-1-4
收藏
得分:0 
回复 11楼 wp231957
那他为什么总是mm.h  那里出错?  我是新手 请多见谅
2015-01-04 11:22
飘飘来过
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2015-1-4
收藏
得分:0 
回复 13楼 wp231957
执行的时候闪退了
2015-01-04 11:26
飘飘来过
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2015-1-4
收藏
得分:0 
回复 15楼 wp231957
那就麻烦你帮帮忙   我这里很急  明天要上交作业   你就在我这个系统上帮我修改下  只要可以执行就可以了   我用的是dev c++这个软件            麻烦了
2015-01-04 11:32
飘飘来过
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2015-1-4
收藏
得分:0 
回复 15楼 wp231957
我加下你qq  qq上说快点 或者你加我的1164719478
2015-01-04 11:36
快速回复:你们都来看看这个程序哪里错了? 急
数据加载中...
 
   



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

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