| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 508 人关注过本帖
标题:求看下我这个代码
只看楼主 加入收藏
刘欣2
Rank: 1
等 级:新手上路
帖 子:32
专家分:6
注 册:2014-5-12
结帖率:72.73%
收藏
已结贴  问题点数:20 回复次数:2 
求看下我这个代码
#include<time.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
struct car_type                     //定义结构体变量
{
    int num;
    char name[10];
    int age;
    float price;
    struct student *naxt;
}car[100];


int sum = 0;
FILE *fp;                   //定义文件指针

void input();                      //定义输入函数
void output();                                   //定义输出函数
void save();                          //定义文件接受函数
void Addrecode();                       //定义添加汽车信息函数
int menu1();                                   //定义菜单函数
int Selectsearch();                  //定义选择查找类型函数
void cancel();                         //定义删除函数
void search();                            //定义查找函数
void searchName();                         //定义查找名字类型函数
void searchNum();                            //定义查找编码类型函数
void searchAge();                                //定义查找车龄类型函数

void save()                                      //定义文件接受函数
{
    int i;
    if((fp=fopen("car.dat","wb"))==NULL)         //判断能不能打开文件
    {
         printf("can not open file\n");
         exit(0);
    }
    for(i=0;i<sum;i++)                                         //判断能不能输入文件信息
    {
        if(fwrite(&car[i],sizeof(struct car_type),1,fp)!=1)
        {
            printf("file write error\n");
        }
   }
   fclose(fp);
}



void input()
{
    int i,j;
    srand((int)time(0));
    for(i=0;i<100;i++)                        //进行输入汽车信息
    {
        system("cls");
        sum=sum+1;
        printf("请输入第%d汽车的编码    名字     价格:\n",i+1);
        scanf("%d%s%f",&car[i].num,car[i].name,&car[i].price);
        car[i].age=1+(int)(10.0*rand()/(RAND_MAX+1.0));
        printf("第%d辆汽车的车龄为:%d",i+1,car[i].age);
        printf("\n是否继续添加:1是0否:   ");               //判断是否继续输入
        scanf("%d",&j);
        if(j==1)
        {
            printf("\n");
        }
        else
        {
            break;
        }
    }
    save();
}


int menu1()
{
    int num;
    while(1)
    {
        system("cls");
        printf("***************二手车管理系统*****************\n");
        printf("*********************功能*********************\n");
        printf("*                0.退出程序                  *\n");
        printf("*              1.创建车辆信息                *\n");
        printf("*              2.添加车辆信息                *\n");
        printf("*              3.删除车辆信息                *\n");
        printf("*              4.显示车辆信息                *\n");
        printf("*              5.查找车辆信息                *\n");
        printf("**************6.按价格进行排序****************\n");
        printf("**********************************************\n");
        printf("请输入您需要的功能编号0--5:");
        scanf("%d",&num);
        if(num<0||num>6)
        {
            printf("您输入的编号有误,按任意键继续输入:");
            scanf("%d",&num);
        }
        else
        {
            break;
        }
    }
    return num;
}

void Addrecode()
{
    int i,j;
    srand((int)time(0));
    printf("请输入你要插入的车辆数:  ");
    scanf("%d",&j);
    if((fp = fopen("car.dat","rb"))==NULL)
    {
        printf("不能打开文件\n");
        system("pause");
        return;
    }
    for(i = 0;i<j;i++)
    {
         sum = sum + 1;
         printf("请输入你要插入的第%d辆汽车信息(编码  名字   价格)\n",i+1);
         fread(&car[i],sizeof(struct car_type),1,fp);
         scanf("%d%s%f",&car[sum].num,car[sum+i].name,&car[sum+i].price);
         car[sum].age=1+(int)(10.0*rand()/(RAND_MAX+1.0));
         save();
    }

}

int main()
{

    int menu;
    while(1)
    {
        system("cls");
        menu = menu1();
        switch(menu)
        {
             case 0: exit(0);
                     break;
             case 1: input();
                     break;
             case 2: Addrecode();
                     break;
             case 3: cancel();
                     break;
             case 4: output();
                     break;
             case 5: search();
                     break;
        }
    }
}




void cancel()
{
    int i,t,j,k,count;
    if((fp = fopen("car.dat","rb"))==NULL)
    {
        printf("不能打开文件\n");
        system("pause");
        return;
    }
    for(i = 0;i<sum;i++)
    {
        fread(&car[i],sizeof(struct car_type),1,fp);
        if(car[i].age>=8)
        {
             for(j=i;j<t+1;j++)
             {
                 car[j].age=car[j+1].age;
             }
             count++;
             t--;
        }

        if(count==0)
        {
            printf("没有该汽车的信息!\n");
        }
        else
        {
            output();
        }
     }
     save();
}



void output()
{
    int i;
    if((fp = fopen("car.dat","rb"))==NULL)
    {
        printf("不能打开文件\n");
        system("pause");
        return;
    }
    printf("编码     名字      车龄     价格\n");
    for(i = 0;i<sum;i++)
    {
        fread(&car[i],sizeof(struct car_type),1,fp);
        printf("%d%10s    %d    %4.2f\n",car[i].num,car[i].name,car[i].age,car[i].price);
    }

    system("pause");
    fclose(fp);
}


int Selectsearch()
{
    int i;
    while(1)
    {
         system("cls");
         printf("***************1.按编码查找***************\n");
         printf("***************2.按名字查找***************\n");
         printf("***************3.按车龄查找***************\n");
         printf("*******************0.退出*****************\n");
         printf("请输入你要进行那种查找:  ");
         scanf("%d",&i);
         if(i<0||i>3)
         {
               printf("输入错误,请重新输入:\n");
               scanf("%d",&i);
         }
         else
         {
            exit(0);
         }
    }
    return i;
}



void search()
{
    int num;
    while(1)
    {
        system("cls");
        num = Selectsearch();
        switch(num)
        {
            case 0:
                   exit(0);
                   break;
            case 1:
                   searchNum();
                   break;
            case 2:
                   searchName();
                   break;
            case 3:
                   searchAge();
                   break;
        }
    }
}


void searchNum()
{
    int NUm;
    int i,j;
    if((fp = fopen("car.dat","rb"))==NULL)
    {
        printf("不能打开\n");
        system("pause");
        return;
    }
    printf("请输入你要查找的编码:  ");
    scanf("%d",NUm);
    for(i =0;i<sum;i++)
    {
         fread(&car[i],sizeof(struct car_type),1,fp);
         if(car[i].num==NUm)
         {
              printf("%d %10s   %d   %4.2f\n",car[i].num,car[i].name,car[i].age,car[i].price);
         }
    }
}



void searchName()
{
    char NAME[10];
    int i,j;
    if((fp = fopen("car.dat","rb"))==NULL)
    {
        printf("不能打开\n");
        system("pause");
        return;
    }
    while(1)
    {
        printf("请输入你要查找的汽车名字:\n");
        gets(NAME);
        fflush(stdin);
        for(i = 0;i<sum;i++)
        {
            fread(&car[i],sizeof(struct car_type),1,fp);
            if(NAME==car[i].name)
            {
                   printf("%d   %10s   %d   %4.2f\n",car[i].num,car[i].name,car[i].age,car[i].price);
            }
        }
        printf("请问是否还有继续  1是   0否\n");
        scanf("%d",&j);
        if(j<0||j>1)
        {
              printf("输入错误,请从新输入\n");
              scanf("%d",&j);
        }
        if(j == 0)
        {
            exit(0);
        }
    }

}


void searchAge()
{
    int Age,i,j;
    if((fp = fopen("car.dat","rb"))==NULL)
    {
        printf("不能打开\n");
        system("pause");
        return;
    }
    while(1)
    {
        printf("请输入你要查找的汽车车龄:  \n");
        scanf("%d",&Age);
        for(i = 0;i<sum;i++)
        {
            fread(&car[i],sizeof(struct car_type),1,fp);
            if(Age==car[i].age)
            {
                   printf("%d   %10s   %d   %4.2f\n",car[i].num,car[i].name,car[i].age,car[i].price);
            }
        }
        printf("请问是否还有继续  1是   0否\n");
        scanf("%d",&j);
        if(j<0||j>1)
        {
              printf("输入错误,请从新输入\n");
              scanf("%d",&j);
        }
        if(j == 0)
        {
            exit(0);
        }
    }
}




我这个代码有俩个问题:一我输入了汽车的信息,但是关了程序,再次打开就不能显示;也就是说,我输入的信息没有存入文件中! 求帮我改下!
                      还有就我为什么我运行case5   然后运行searchNum和searchName和searchAge没有运行就直接退出!求各位大神解答!
搜索更多相关主题的帖子: include 结构体 price 
2014-05-27 22:52
刘欣2
Rank: 1
等 级:新手上路
帖 子:32
专家分:6
注 册:2014-5-12
收藏
得分:0 
int Selectsearch()
{
    int i;
    while(1)
    {
         system("cls");
         printf("***************1.按编码查找***************\n");
         printf("***************2.按名字查找***************\n");
         printf("***************3.按车龄查找***************\n");
         printf("*******************0.退出*****************\n");
         printf("请输入你要进行那种查找:  ");
         scanf("%d",&i);
         if(i<0||i>3)
         {
               printf("输入错误,请重新输入:\n");
               scanf("%d",&i);
         }
         else
         {
            exit(0);
         }
    }
    return i;
}



void search()
{
    int num;
    while(1)
    {
        system("cls");
        num = Selectsearch();
        switch(num)
        {
            case 0:
                   exit(0);
                   break;
            case 1:
                   searchNum();
                   break;
            case 2:
                   searchName();
                   break;
            case 3:
                   searchAge();
                   break;
        }
    }
}


void searchNum()
{
    int NUm;
    int i,j;
    if((fp = fopen("car.dat","rb"))==NULL)
    {
        printf("不能打开\n");
        system("pause");
        return;
    }
    printf("请输入你要查找的编码:  ");
    scanf("%d",NUm);
    for(i =0;i<sum;i++)
    {
         fread(&car[i],sizeof(struct car_type),1,fp);
         if(car[i].num==NUm)
         {
              printf("%d %10s   %d   %4.2f\n",car[i].num,car[i].name,car[i].age,car[i].price);
         }
    }
}


各位大神帮我看看这些代码呗
2014-05-27 22:53
Dua瀚狼
Rank: 2
来 自:湖南长沙
等 级:论坛游民
帖 子:59
专家分:78
注 册:2012-3-11
收藏
得分:20 
首先,你得说明你的代码要干什么,其次,你要说一下的代码有什么问题。。。你让人看这么长一段代码,谁有心思

我怀旧,因为我看不到未来。
2014-05-28 00:24
快速回复:求看下我这个代码
数据加载中...
 
   



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

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