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

#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
wp231957
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:神界
等 级:贵宾
威 望:423
帖 子:13688
专家分:53332
注 册:2012-10-18
收藏
得分:10 
你这个  默默点h  从何而来   明显不是系统头文件

DO IT YOURSELF !
2015-01-04 08:14
飘飘来过
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2015-1-4
收藏
得分:0 
回复 3楼 wp231957
那应该怎么改?
2015-01-04 10:33
wp231957
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:神界
等 级:贵宾
威 望:423
帖 子:13688
专家分:53332
注 册:2012-10-18
收藏
得分:0 
以下是引用飘飘来过在2015-1-4 10:33:50的发言:

那应该怎么改?



你问我  我问谁啊  你还是先回答我3楼的问题 再说吧

DO IT YOURSELF !
2015-01-04 10:39
飘飘来过
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2015-1-4
收藏
得分:0 
回复 5楼 wp231957
那我是不是要在开头加个头文件?
2015-01-04 10:49
wp231957
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:神界
等 级:贵宾
威 望:423
帖 子:13688
专家分:53332
注 册:2012-10-18
收藏
得分:0 
以下是引用wp231957在2015-1-4 08:14:40的发言:

你这个  默默点h  从何而来   明显不是系统头文件



你这个文件 从何而来  你电脑里是否有这个文件  你引用这个文件想干什么  

你一个都不回答  老是问 怎么改  还是那句话  我哪里知道怎么改

DO IT YOURSELF !
2015-01-04 10:56
飘飘来过
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2015-1-4
收藏
得分:0 
回复 7楼 wp231957
我系统没这个文件  我用这个是我希望把以上的操作记录保存在mm.h这个文件里    可是就是不知道怎么错了
2015-01-04 11:01
wp231957
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:神界
等 级:贵宾
威 望:423
帖 子:13688
专家分:53332
注 册:2012-10-18
收藏
得分:0 
以下是引用飘飘来过在2015-1-4 11:01:48的发言:

我系统没这个文件  我用这个是我希望把以上的操作记录保存在mm.h这个文件里    可是就是不知道怎么错了



请恕我没看懂 “我用这个是我希望把以上的操作记录保存在mm.h” 这句话是什么意思

点h 文件 在c语言中是头文件的意思  是别人编好的 我们拿来使用(当然自己也可以写点h文件)

不过你的目的如果是 文件存储的话 那是数据文件 不应该存成点h的扩展名 容易引起误会 更不能用include来引用

具体可以阅读参考教材中 文件操作相关内容

DO IT YOURSELF !
2015-01-04 11:09
飘飘来过
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2015-1-4
收藏
得分:0 
回复 9楼 wp231957
好的  我现在就是想知道头文件应该怎么写?   mm.h应该在哪?
2015-01-04 11:11
快速回复:你们都来看看这个程序哪里错了? 急
数据加载中...
 
   



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

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