C语言实现房屋管理系统分享!

本文实例为大家分享了C语言房屋管理系统的具体代码,供大家参考,具体内容如下

  #include<stdio.h>  #include<windows.h>  #include<string.h>  #include<conio.h>  #define N 100  int res = 0;   struct Lodginghouse  {    char Lodginghouse_type[20];    //公寓类型  } Lodginghouse[100];  struct CommonHouse  {    char building_style[20];     //住宅建筑风格  } CommonHouse[100];  struct Villa  {    char villa_style[20];  //别墅建筑材料  } villa[100];     struct House  {    char type[100];   //房屋类型    int number;   //房屋编号    // int buildtime;   //已经建立多长时间    char sold[100];  //是否售出    int price;     //售卖价格    int area;   //占地面积(平方米)    // int postion; //房屋位置,例如北京三环    // char balcony[20];  //阳台朝向    // int total_floors;  //房屋总层数  } house[100];        void main();  void menu();  void input();  void save(int);  void display();  void del();  void add();  void array();  void search();  void search_num();  void search_name();  void yusuan();  void modify();  void start(); /*定义各函数*/  void start() //开始界面  {    system("cls");//清屏    system("color 1F"); //设置文字颜色为黄色 背景色蓝色    printf("nnnnnnnnnnnn");       printf("tt********************************************************************************n");    printf("nn");    printf("tt************************欢迎使用我们的房屋销售管理系统*************************");    printf("nn");    printf("tt********************************************************************************nnnnn");    printf("tttt   制作人: n");    printf("tttt      XXX   XXX   XXXn");    printf("tttt         XXX   XXX n");    printf("tttt          XXX XX n");    printf("tttt            XXn");    printf("tttt   制作时间: n");    printf("tttt      2018年6月n");       printf("ttt  ***按任意键进入***n");  }  void menu() //菜单界面  {    system("cls");    system("color 1F");    printf("nnnnnnnnnnnn");    printf("ttttt************************nn");    printf("ttttt**********菜单**********nn");    printf("ttttt************************nn");    printf(">>>>>>>>>>>>>>>>>>>>>>>>> 1 输入   nn");    printf(">>>>>>>>>>>>>>>>>>>>>>>>> 2 显示   nn");    printf(">>>>>>>>>>>>>>>>>>>>>>>>> 3 查找   nn");    printf(">>>>>>>>>>>>>>>>>>>>>>>>> 4 删除   nn");    printf(">>>>>>>>>>>>>>>>>>>>>>>>> 5 添加   nn");    printf(">>>>>>>>>>>>>>>>>>>>>>>>> 6 修改   nn");    printf(">>>>>>>>>>>>>>>>>>>>>>>>> 7 预算   nn");    printf(">>>>>>>>>>>>>>>>>>>>>>>>> 8 排序   nn");  printf(">>>>>>>>>>>>>>>>>>>>>>>>> 9 退出   nn");    printf("注意:n");    printf("首次使用该系统,请选择输入选项,且输入选项会覆盖原有信息。");  }  void input()  /*录入函数*/  {    int i,m;    system("cls");    printf("需要录入的房屋个数(1--100):n");    scanf("%d",&m);    for (i = res; i < m + res; i++)    {      printf("请输入房屋类型(公寓,普通住宅,别墅): ");      scanf("%s",&house[i].type);      printf("请输入房屋编号: ");      scanf("%d",&house[i].number);      // printf("请输入已经建立多长时间: ");      // scanf("%d",&house[i].buildtime);      printf("请输入是否售出(是/否): ");      scanf("%s",&house[i].sold);      printf("请输入售卖价格: ");      scanf("%d",&house[i].price);      printf("请输入占地面积: ");      scanf("%d",&house[i].area);      // printf("请输入房屋位置(例如三环输入3): ");      // scanf("%d",&house[i].postion);     // printf("请输入阳台朝向(东南西北): ");     // scanf("%s",&house[i].balcony);     // printf("请输入房屋总层数: ");      // scanf("%d",&house[i].total_floors);      if(strcmp(house[i].type, "公寓") == 0)      {        printf("请输入公寓类型: ");        scanf("%s",&Lodginghouse[i].Lodginghouse_type);      }      else if(strcmp(house[i].type, "普通住宅") == 0)      {        printf("普通住宅建筑风格: ");        scanf("%s",&CommonHouse[i].building_style);      }      else      {        printf("请输入别墅建筑材料: ");        scanf("%s",&villa[i].villa_style);      }      printf("n");    }    res = i;    printf("n创建完毕!n");    save(m);//保存房屋总数m  }  void save(int m) /*保存文件函数*/  {    int i, type;    FILE*fp1, *fp2, *fp3;  //声明fp是指针,用来指向FILE类型的对象    if ((fp1=fopen("Lodginghouse","wb"))==NULL) //打开房屋列表文件为空    {      printf ("打开失败n");      exit(0);    }    if ((fp2=fopen("Ordinary house","wb"))==NULL) //打开房屋列表文件为空    {      printf ("打开失败n");      exit(0);    }    if ((fp3=fopen("Villa","wb"))==NULL) //打开房屋列表文件为空    {      printf ("打开失败n");      exit(0);    }    for (i=0; i<m; i++) /*将内存中房屋的信息输出到磁盘文件中去*/    {      if(strcmp(house[i].type, "公寓") == 0)      {        if (fwrite(&house[i],sizeof(struct House),1,fp1)!=1)//写入数据块  &em[i]:是要输出数据的地址 sizeof(struct clerk):获取一个单体的长度 1:数据项的个数 fp:目标文件指针          printf("文件读写错误n");        if (fwrite(&Lodginghouse[i],sizeof(struct Lodginghouse),1,fp1)!=1)          printf("文件读写错误n");      }      else if(strcmp(house[i].type, "普通住宅") == 0)      {        if (fwrite(&house[i],sizeof(struct House),1,fp2)!=1)//写入数据块  &em[i]:是要输出数据的地址 sizeof(struct clerk):获取一个单体的长度 1:数据项的个数 fp:目标文件指针          printf("文件读写错误n");        if (fwrite(&CommonHouse[i],sizeof(struct CommonHouse),1,fp2)!=1)          printf("文件读写错误n");      }      else      {        if (fwrite(&house[i],sizeof(struct House),1,fp3)!=1)//写入数据块  &em[i]:是要输出数据的地址 sizeof(struct clerk):获取一个单体的长度 1:数据项的个数 fp:目标文件指针          printf("文件读写错误n");        if (fwrite(&villa[i],sizeof(struct Villa),1,fp3)!=1)          printf("文件读写错误n");      }    }       fclose(fp1);// 缓冲区内最后剩余的数据输出到磁盘文件中,并释放文件指针和有关的缓冲区    fclose(fp2);    fclose(fp3);  }  int load(int type) /*导入函数 int型*/  {    int k;    FILE*fp;    int i=0;    if(type == 1)  //公寓    {      if((fp=fopen("Lodginghouse","rb"))==NULL)      {        printf ("cannot open filen");        exit(0);      }      while(feof(fp)==0) //检测流上的文件结束符      {        fread(&house[i],sizeof(struct House),1,fp); //读取        fread(&Lodginghouse[i],sizeof(struct Lodginghouse),1,fp);        i++;      }    }    else if(type == 2)   //普通住宅    {      if((fp=fopen("Ordinary house","rb"))==NULL)      {        printf ("cannot open filen");        exit(0);      }      while(feof(fp)==0)      {        fread(&house[i],sizeof(struct House),1,fp); //读取        fread(&CommonHouse[i],sizeof(struct CommonHouse),1,fp);        i++;      }    }    else     //别墅    {      if((fp=fopen("Villa","rb"))==NULL)      {        printf ("cannot open filen");        exit(0);      }      while(feof(fp)==0)      {        fread(&house[i],sizeof(struct House),1,fp); //读取        fread(&villa,sizeof(struct Villa),1,fp);        i++;      }    }    fclose(fp);    return i - 1;//返回该类房屋个数  }  void search()/*查询函数*/  {    int t,button;    system("cls");//清屏    do    {      printf("n按1 查询n按2 回主菜单n");      scanf("%d",&t);      if(t>=1&&t<=2)      {        button=1;        break;      }      else      {        button=0;        printf("输入错误");      }    }    while(button==0);//回到查询选择项    while(button==1)    {      switch(t)//选择查询方式      {      case 1:        printf("正在查询n");        search_num();        break;      case 2:        main();        break;      default:        break;      }    }  }  void search_num()//按房屋编号查询  {    int a, k;    int i,t;    int m;    char s[20];    printf("请输入要查询的房屋类型:n");    scanf("%s", &s);    if(strcmp(s, "公寓") == 0)      k=1;    else if(strcmp(s, "普通住宅") == 0)      k=2;    else      k=3;    m=load(k);    printf("请输入要查找的房屋编号:n");    scanf("%d",&a);    for(i=0; i<m; i++)      if(a==house[i].number)      {        printf("房屋类型  房屋编号  是否售出  价格(元)  面积(平方米)  位置(环)  阳台朝向  总层数(层)");        if(k == 1) printf("   公寓类型");        else if(k == 2) printf("   住宅风格");        else printf("    别墅材料");        printf("n");        printf("n %-6s   %-6d   %-8s  %-7d", house[i].type, house[i].number, house[i].sold, house[i].price);        printf("    %-8d ", house[i].area);        if(k == 1) printf("  %s", Lodginghouse[i].Lodginghouse_type);        else if(k == 2) printf("  %s", CommonHouse[i].building_style);        else printf("  %s", villa[i].villa_style);        break;         }    if(i==m)      printf("n没有查询到该房n");    printf("n");    printf("返回查询函数请按1,继续查询房屋编号请按2n");    scanf("%d",&t);    switch(t)    {    case 1:      search();      break;    case 2:      break;    default:      break;    }  }  void display() /*浏览函数*/  {    int sold, unsold;    sold = 0;     //各类房屋已售数量    int i, type;    char s[20];    printf("请输入要显示的房屋的种类:");    scanf("%s", &s);    if(strcmp(s, "公寓") == 0)      type=1;    else if(strcmp(s, "普通住宅") == 0)      type=2;    else      type=3;    int m=load(type);    system("cls");    printf("房屋类型  房屋编号  是否售出  价格(元)  面积(平方米)  位置(环)  阳台朝向  总层数(层)");    if(type == 1) printf("   公寓类型");    else if(type == 2) printf("   住宅风格");    else printf("    别墅材料");    printf("n");    for(i=0; i<m+res - 1; i++)    {      if(strcmp(house[i].sold, "是") == 0) sold++;      printf("n %-6s   %-6d    %-8s  %-7d", house[i].type, house[i].number, house[i].sold, house[i].price);      printf("    %-8d  ", house[i].area);      if(type == 1) printf("  %s", Lodginghouse[i].Lodginghouse_type);      else if(type == 2) printf("  %s", CommonHouse[i].building_style);      else printf("  %s", villa[i].villa_style);    }    printf("n该类楼房已售数量为:%d", sold);    printf("n该类楼房未售数量为:%d", m + res - 1 -sold);  }  void add()/*添加函数*/  {    FILE*fp;    int n;    int type;    int count=0;    int i;    char s[20];    printf("请输入要增添的房屋的种类:");    scanf("%s", &s);    if(strcmp(s, "公寓") == 0)      type=1;    else if(strcmp(s, "普通住宅") == 0)      type=2;    else      type=3;    int m=load(type);    printf("n 原来的房屋信息:n");    display();    printf("n");    printf("请输入想增加的房屋数:n");//确定要加入的房屋数n    scanf("%d",&n);    for (i=m; i<(m+n); i++) //添加n个房屋的信息    {      printf("请输入房屋类型(公寓,普通住宅,别墅): ");      scanf("%s",&house[i].type);      printf("请输入房屋编号: ");      scanf("%d",&house[i].number);         printf("请输入是否售出(是/否): ");      scanf("%s",&house[i].sold);      printf("请输入售卖价格: ");      scanf("%d",&house[i].price);      printf("请输入占地面积: ");      scanf("%d",&house[i].area);         if(type == 1)      {        printf("请输入公寓类型: ");        scanf("%s",&Lodginghouse[i].Lodginghouse_type);      }      else if(type == 2)      {        printf("普通住宅建筑风格: ");        scanf("%s",&CommonHouse[i].building_style);      }      else      {        printf("请输入别墅建筑材料: ");        scanf("%s",&villa[i].villa_style);      }      printf("n");      count=count+1;      printf("已增加的房屋数:n");      printf("%dn",count);    }    save(m + n);    printf("n添加成功n");    printf("n增加后的所有房屋信息:n");    display();//显示添加后的信息  }  void modify() /*修改函数*/  {    int k;    char s[100];    char type[100];    int number;       char sold[100];    int price;    int area;       char Lodginghouse_type[100];    char building_style[20];    char villa_style[20];    int b,c,i,n,t,button;    printf("请输入要修改的房屋所属种类:");    scanf("%s", &s);    if(strcmp(s, "公寓") == 0)      k=1;    else if(strcmp(s, "普通住宅") == 0)      k=2;    else      k=3;    int m=load(k);    system("cls");       printf("n 原来的房屋信息:n");    display();    printf("n");       printf("请输入要修改的房屋的编号:n");    scanf("%d",&number);    for(button=1,i=0; button&&i<m; i++)    {      if(house[i].number == number)      {        printf("n该房屋原始记录为:n");        printf("房屋类型  房屋编号  是否售出  价格(元)  面积(平方米) ");        if(k == 1) printf("   公寓类型");        else if(k == 2) printf("   住宅风格");        else printf("    别墅材料");        printf("n %-6s   %-6d    %-8s  %-7d", house[i].type, house[i].number, house[i].sold, house[i].price);        printf("    %-8d  ", house[i].area);        if(k == 1) printf("  %s", Lodginghouse[i].Lodginghouse_type);        else if(k == 2) printf("  %s", CommonHouse[i].building_style);        else printf("  %s", villa[i].villa_style);        printf("n确定 按1 ; 不修改请按0n");        scanf("%d",&n);        if(n==1)        {          printf("n需要进行修改的选项n 1.房屋类型 2.房屋编号4.是否售出 5.价格 6.面积");          if(k==1) printf("10.公寓类型 ");          else if(k == 2) printf("10.住宅风格 ");          else printf("10.别墅材料 ");          printf("11.返回上层n");          printf("请选择序号1-11:n");          scanf("%d",&c);          if(c>11||c<1)            printf("n选择错误,请重新选择!n");        }        button=0;      }       }    if(button==1)      printf("n没有查到该房屋n");       do    {      switch(c)   /*因为当找到第i个房屋时,for语句后i自加了1,所以下面的应该把改后的信息赋值给第i-1个*/      {      case 1:        printf("房屋类型改为: ");        scanf("%s",&type);        strcpy(house[i - 1].type,type);        break;      case 2:        printf("房屋编号改为: ");        scanf("%d",&number);        house[i-1].number=number;        break;         case 4:        printf("该房屋是否售出改为: ");        scanf("%s",&sold);        strcpy(house[i-1].sold, sold);        break;      case 5:        printf("房屋价格改为: ");        scanf("%d",&price);        house[i-1].price=price;        break;      case 6:        printf("房屋面积改为:");        scanf("%d", &area);        house[i-1].area=area;        break;         case 10:        if(k == 1)        {          printf("公寓类型改为:");          scanf("%s", &Lodginghouse_type);          strcpy(Lodginghouse[i-1].Lodginghouse_type, Lodginghouse_type);          break;        }        else if(k == 2)        {          printf("住宅风格改为:");          scanf("%s", &building_style);          strcpy(CommonHouse[i-1].building_style, building_style);          break;        }        else        {          printf("别墅建筑材料改为:");          scanf("%s", &villa_style);          strcpy(villa[i-1].villa_style, villa_style);          break;        }        break;      case 11:        modify();        break;      }         printf("n");      printf("nn 确定修改 请按1 ; 重新修改 请按2: n");      scanf("%d",&b);    }while(b==2);    printf("n修改后的所有房屋信息:n");    printf("n");    save(m);       display();    printf("n按1 继续修改 ,不再修改请按0n");    scanf("%d",&t);    switch(t)    {    case 1:      modify();      break;    case 0:      break;    default :      break;    }  }  void del()  /*删除函数 */  {    int type;    char s[100];    int i,j,n,t,button;    int number;    printf("请输入需要删除的房屋所属种类:");    scanf("%s", &s);    if(strcmp(s, "公寓") == 0)      type=1;    else if(strcmp(s, "普通住宅") == 0)      type=2;    else      type=3;    int m=load(type);    printf("n 原来的房屋信息:n");    display(); //显示删除前的房屋信息    printf("n");       printf("请输入需要删除的房屋的编号:n");    scanf("%d",&number);    for(button=1,i=0; button&&i<m; i++) //主函数中确定button==1时各函数才可以被调用    {      if(house[i].number == number)//按员工姓名查找到某员工 并调出其资料      {        printf("n该房屋原始记录为:n");//显示选定员工的信息        printf("房屋类型  房屋编号 是否售出  价格(元)  面积(平方米) ");        if(type == 1) printf("   公寓类型");        else if(type == 2) printf("   住宅风格");        else printf("    别墅材料");        printf("n %-6s   %-6d   %-8d   %-8s  %-7d", house[i].type, house[i].number, house[i].sold, house[i].price);        printf("    %-8d ", house[i].area);        if(type == 1) printf("  %s", Lodginghouse[i].Lodginghouse_type);        else if(type == 2) printf("  %s", CommonHouse[i].building_style);        else printf("  %s", villa[i].villa_style);        printf("n确定删除 请按1,不删除请按0n");        scanf("%d",&n);        if(n==1)        {          for(j=i; j<m-1; j++) //从第i项开始 将后一项的各值赋给前一项各对应的 完成对第i项的删除          {            strcpy(house[j].type,house[j+1].type);            house[j].number=house[j+1].number;            // house[j].buildtime=house[j+1].buildtime;            strcpy(house[j].sold, house[j+1].sold);            house[j].price=house[j+1].price;            house[j].area=house[j+1].area;            //house[j].postion=house[j+1].postion;            // strcpy(house[j].balcony, house[j+1].balcony);            //house[j].total_floors=house[j+1].total_floors;            if(type == 1) strcpy(Lodginghouse[j].Lodginghouse_type, Lodginghouse[j+1].Lodginghouse_type);            else if(type == 2) strcpy(CommonHouse[j].building_style, CommonHouse[j+1].building_style);            else strcpy(villa[j].villa_style, villa[j+1].villa_style);          }          button=0;        }      }    }    if(!button)//button==0表明删除已完成      m=m-1;//总房屋数减少一人    else      printf("n查无此房屋!n");    printf("n 删除后的所有房屋信息:n");    save(m);   //调用保存函数    display(); //调用浏览函数       printf("n继续删除请按1,不再删除请按0n");    scanf("%d",&t);    switch(t)    {    case 1:      del();      break;    case 0:      break;    default :      break;    }  }  void yusuan()  {    int i;    int type;    char s[100];    int ayusuan = 0;     //代表预算    printf("请输入需要预算的房屋种类:");    scanf("%s", &s);    if(strcmp(s, "公寓") == 0)      type=1;    else if(strcmp(s, "普通住宅") == 0)      type=2;    else      type=3;    int m=load(type);    for (i=0; i<m; i++)    {      ayusuan += house[i].price;    }    if(type == 1)      printf("公寓全部卖出的预算为:");    else if(type == 2)      printf("普通住宅全部卖出的预算为:");    else      printf("别墅全部卖出的预算为:");    printf("%dn", ayusuan);  }  void sort_realw()  {    int i,j,k,m,l;    char s;    printf("请输入需要排序的房屋所属种类:");    scanf("%s",&s);    if(strcmp(s, "公寓") == 0)      l=1;    else if(strcmp(s, "普通住宅") == 0)      l=2;    else      l=3;    m=load(k);    struct House replace;    struct House *p;    p=house;    for(i=0;i<m-1;i++)    {      k=i;      for(j=i+1;j<m;j++)      {       if(p[k].price<p[j].price)        k=j;       replace=house[k];       house[k]=house[i];       house[i]=replace;      }    }       for(i=0;i<m;i++)    {         printf("房间号  面积  价格 类型  销售状态  n");      printf("n %-6d %-6d %-8d %-8s %-8s ",house[i].number,house[i].area,house[i].price,house[i].type,house[i].sold);    }     }  void main()//主函数  {    int n, m, button;    char a;    start();    getch();    menu();    do  {        printf("功能选择(1—9 ):n");      scanf("%d",&n);      if(n>=1&&n<=8)      {        button=1;        break;      }      else if(n == 9)      {        exit(0);      }      else      {        button=0;        printf("您输入有误,请重新选择!");      }    }    while(button==0);    while(button==1)    {      switch(n)      {      case 1:        input();        break;      case 2:        display();        break;      case 3:        search();        break;         case 4:        del();        break;      case 5:        add();        break;      case 6:        modify();        break;      case 7:        yusuan();        break;  case 8:        yusuan();        break;         case 9:        exit(0);        break;      default :        break;      }      getchar();      printf("n");      printf("按任意键继续n");      getch();      system("cls"); /*清屏*/      menu(); /*调用菜单函数*/      printf("功能选择(1--9):n");      scanf("%d",&n);      printf("n");    }  }

注意:计算房屋的总数的时候,一定注意全局变量数值的初始化与更新

更多学习资料请关注专题《管理系统开发》。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持<计算机技术网(www.ctvol.com)!!>。

—-想了解C语言实现房屋管理系统分享!全部内容且更多的C语言教程关注<计算机技术网(www.ctvol.com)!!>

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/c-cdevelopment/483512.html

(0)
上一篇 2020年11月10日
下一篇 2020年11月10日

精彩推荐