1. 程式人生 > 程式設計 >C語言實現房屋管理系統

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("\n\n\n\n\n\n\n\n\n\n\n\n");
 
  printf("\t\t********************************************************************************\n");
  printf("\n\n");
  printf("\t\t************************歡迎使用我們的房屋銷售管理系統*************************");
  printf("\n\n");
  printf("\t\t********************************************************************************\n\n\n\n\n");
  printf("\t\t\t\t   製作人: \n");
  printf("\t\t\t\t      XXX   XXX   XXX\n");
  printf("\t\t\t\t         XXX   XXX \n");
  printf("\t\t\t\t          XXX XX \n");
  printf("\t\t\t\t            XX\n");
  printf("\t\t\t\t   製作時間: \n");
  printf("\t\t\t\t      2018年6月\n");
 
  printf("\t\t\t  ***按任意鍵進入***\n");
}
void menu() //選單介面
{
  system("cls");
  system("color 1F");
  printf("\n\n\n\n\n\n\n\n\n\n\n\n");
  printf("\t\t\t\t\t************************\n\n");
  printf("\t\t\t\t\t**********選單**********\n\n");
  printf("\t\t\t\t\t************************\n\n");
  printf(">>>>>>>>>>>>>>>>>>>>>>>>> 1 輸入   \n\n");
  printf(">>>>>>>>>>>>>>>>>>>>>>>>> 2 顯示   \n\n");
  printf(">>>>>>>>>>>>>>>>>>>>>>>>> 3 查詢   \n\n");
  printf(">>>>>>>>>>>>>>>>>>>>>>>>> 4 刪除   \n\n");
  printf(">>>>>>>>>>>>>>>>>>>>>>>>> 5 新增   \n\n");
  printf(">>>>>>>>>>>>>>>>>>>>>>>>> 6 修改   \n\n");
  printf(">>>>>>>>>>>>>>>>>>>>>>>>> 7 預算   \n\n");
  printf(">>>>>>>>>>>>>>>>>>>>>>>>> 8 排序   \n\n");
printf(">>>>>>>>>>>>>>>>>>>>>>>>> 9 退出   \n\n");
  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),fp1)!=1)
        printf("檔案讀寫錯誤\n");
    }
    else if(strcmp(house[i].type,"普通住宅") == 0)
    {
      if (fwrite(&house[i],fp2)!=1)//寫入資料塊  &em[i]:是要輸出資料的地址 sizeof(struct clerk):獲取一個單體的長度 1:資料項的個數 fp:目標檔案指標
        printf("檔案讀寫錯誤\n");
      if (fwrite(&CommonHouse[i],sizeof(struct CommonHouse),fp2)!=1)
        printf("檔案讀寫錯誤\n");
    }
    else
    {
      if (fwrite(&house[i],fp3)!=1)//寫入資料塊  &em[i]:是要輸出資料的地址 sizeof(struct clerk):獲取一個單體的長度 1:資料項的個數 fp:目標檔案指標
        printf("檔案讀寫錯誤\n");
      if (fwrite(&villa[i],sizeof(struct Villa),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 file\n");
      exit(0);
    }
    while(feof(fp)==0) //檢測流上的檔案結束符
    {
      fread(&house[i],fp); //讀取
      fread(&Lodginghouse[i],fp);
      i++;
    }
  }
  else if(type == 2)   //普通住宅
  {
    if((fp=fopen("Ordinary house","rb"))==NULL)
    {
      printf ("cannot open file\n");
      exit(0);
    }
    while(feof(fp)==0)
    {
      fread(&house[i],fp); //讀取
      fread(&CommonHouse[i],fp);
      i++;
    }
  }
  else     //別墅
  {
    if((fp=fopen("Villa",fp); //讀取
      fread(&villa,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,繼續查詢房屋編號請按2\n");
  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","公寓") == 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].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","普通住宅") == 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("請輸入房屋型別(公寓,&house[i].number);
 
    printf("請輸入是否售出(是/否): ");
    scanf("%s",&house[i].area);
 
    if(type == 1)
    {
      printf("請輸入公寓型別: ");
      scanf("%s",&Lodginghouse[i].Lodginghouse_type);
    }
    else if(type == 2)
    {
      printf("普通住宅建築風格: ");
      scanf("%s",&villa[i].villa_style);
    }
    printf("\n");
    count=count+1;
    printf("已增加的房屋數:\n");
    printf("%d\n",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","普通住宅") == 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].price);
      printf("    %-8d  ",villa[i].villa_style);
      printf("\n確定 按1 ; 不修改請按0\n");
      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("\n\n 確定修改 請按1 ; 重新修改 請按2: \n");
    scanf("%d",&b);
  }while(b==2);
  printf("\n修改後的所有房屋資訊:\n");
  printf("\n");
  save(m);
 
  display();
  printf("\n按1 繼續修改,不再修改請按0\n");
  scanf("%d",&t);
  switch(t)
  {
  case 1:
    modify();
    break;
  case 0:
    break;
  default :
    break;
  }
}
void del()  /*刪除函式 */
{
  int type;
  char s[100];
  int i,j,button;
  int number;
  printf("請輸入需要刪除的房屋所屬種類:");
  scanf("%s","普通住宅") == 0)
    type=2;
  else
    type=3;
  int m=load(type);
  printf("\n 原來的房屋資訊:\n");
  display(); //顯示刪除前的房屋資訊
  printf("\n");
 
  printf("請輸入需要刪除的房屋的編號:\n");
  scanf("%d",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].area);
      if(type == 1) printf("  %s",Lodginghouse[i].Lodginghouse_type);
      else if(type == 2) printf("  %s",villa[i].villa_style);
      printf("\n確定刪除 請按1,不刪除請按0\n");
      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,不再刪除請按0\n");
  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","普通住宅") == 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("%d\n",ayusuan);
}
void sort_realw()
{
  int i,k,m,l;
  char s;
  printf("請輸入需要排序的房屋所屬種類:");
  scanf("%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].area,house[i].price,house[i].sold);
  }
 
}
void main()//主函式
{
  int n,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");
  }
}

注意:計算房屋的總數的時候,一定注意全域性變數數值的初始化與更新

更多學習資料請關注專題《管理系統開發》。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。