C language implements the house management system

  • 2020-06-23 01:26:35
  • OfStack

This article shares the specific code of C language house management system for your reference. The specific content is as follows


#include<stdio.h>
#include<windows.h>
#include<string.h>
#include<conio.h>
#define N 100
int res = 0;
 struct Lodginghouse
{
  char Lodginghouse_type[20];    // Apartment type 
} Lodginghouse[100];
struct CommonHouse
{
  char building_style[20];     // Residential architectural style 
} CommonHouse[100];
struct Villa
{
  char villa_style[20];  // Villa building materials 
} villa[100];
 
struct House
{
  char type[100];   // Housing types 
  int number;   // House number 
  // int buildtime;   // How long has it been established 
  char sold[100];  // Whether to sell 
  int price;     // Selling price 
  int area;   // Floor area (square meters) 
  // int postion; // Housing location, such as Beijing 3 ring 
  // char balcony[20];  // The balcony toward 
  // int total_floors;  // The total number of 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(); /* Define each function */
void start() // Start interface 
{
  system("cls");// Clear the screen 
  system("color 1F"); // Set the text color to yellow   Background blue 
  printf("\n\n\n\n\n\n\n\n\n\n\n\n");
 
  printf("\t\t********************************************************************************\n");
  printf("\n\n");
  printf("\t\t************************ Welcome to use our house sales management system *************************");
  printf("\n\n");
  printf("\t\t********************************************************************************\n\n\n\n\n");
  printf("\t\t\t\t    The producer : \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    Production time : \n");
  printf("\t\t\t\t      2018 years 6 month \n");
 
  printf("\t\t\t  *** Press any key to enter ***\n");
}
void menu() // Menu interface 
{
  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********** The menu **********\n\n");
  printf("\t\t\t\t\t************************\n\n");
  printf(">>>>>>>>>>>>>>>>>>>>>>>>> 1  The input    \n\n");
  printf(">>>>>>>>>>>>>>>>>>>>>>>>> 2  According to    \n\n");
  printf(">>>>>>>>>>>>>>>>>>>>>>>>> 3  To find the    \n\n");
  printf(">>>>>>>>>>>>>>>>>>>>>>>>> 4  delete    \n\n");
  printf(">>>>>>>>>>>>>>>>>>>>>>>>> 5  add    \n\n");
  printf(">>>>>>>>>>>>>>>>>>>>>>>>> 6  Modify the    \n\n");
  printf(">>>>>>>>>>>>>>>>>>>>>>>>> 7  The budget    \n\n");
  printf(">>>>>>>>>>>>>>>>>>>>>>>>> 8  The sorting    \n\n");
printf(">>>>>>>>>>>>>>>>>>>>>>>>> 9  exit    \n\n");
  printf(" Note: \n");
  printf(" When using the system for the first time, select the input option, and the input option overrides the original information. ");
}
void input()  /* Input function */
{
  int i,m;
  system("cls");
  printf(" The number of houses to enter (1--100):\n");
  scanf("%d",&m);
  for (i = res; i < m + res; i++)
  {
    printf(" Please enter the house type (apartment) , Ordinary residence, villa) : ");
    scanf("%s",&house[i].type);
    printf(" Please enter the house number : ");
    scanf("%d",&house[i].number);
    // printf(" Please enter how long it has been established : ");
    // scanf("%d",&house[i].buildtime);
    printf(" Please enter whether sold or not (yes / no ): ");
    scanf("%s",&house[i].sold);
    printf(" Please enter the selling price : ");
    scanf("%d",&house[i].price);
    printf(" Please enter the floor space : ");
    scanf("%d",&house[i].area);
    // printf(" Please enter the location of the house (for example 3 Ring type 3 ) : ");
    // scanf("%d",&house[i].postion);
   // printf(" Please enter the balcony orientation ( Southeast northwest) : ");
   // scanf("%s",&house[i].balcony);
   // printf(" Please enter the total number of floors : ");
    // scanf("%d",&house[i].total_floors);
    if(strcmp(house[i].type, " apartment ") == 0)
    {
      printf(" Please enter the apartment type : ");
      scanf("%s",&Lodginghouse[i].Lodginghouse_type);
    }
    else if(strcmp(house[i].type, " Average house ") == 0)
    {
      printf(" Ordinary residential building style : ");
      scanf("%s",&CommonHouse[i].building_style);
    }
    else
    {
      printf(" Please enter the villa building materials : ");
      scanf("%s",&villa[i].villa_style);
    }
    printf("\n");
  }
  res = i;
  printf("\n Is created !\n");
  save(m);// Total number of houses preserved m
}
void save(int m) /* Save file function */
{
  int i, type;
  FILE*fp1, *fp2, *fp3;  // The statement fp It's a pointer, it's a pointer to something FILE Object of type 
  if ((fp1=fopen("Lodginghouse","wb"))==NULL) // Open house list file empty 
  {
    printf (" Open the failure \n");
    exit(0);
  }
  if ((fp2=fopen("Ordinary house","wb"))==NULL) // Open house list file empty 
  {
    printf (" Open the failure \n");
    exit(0);
  }
  if ((fp3=fopen("Villa","wb"))==NULL) // Open house list file empty 
  {
    printf (" Open the failure \n");
    exit(0);
  }
  for (i=0; i<m; i++) /* Output the information of the house in memory to the disk file */
  {
    if(strcmp(house[i].type, " apartment ") == 0)
    {
      if (fwrite(&house[i],sizeof(struct House),1,fp1)!=1)// Write data block   &em[i] : is the address of the data to be output  sizeof(struct clerk) : get 1 The length of a monomer  1 : Number of data items  fp : A pointer to the target file 
        printf(" File read/write error \n");
      if (fwrite(&Lodginghouse[i],sizeof(struct Lodginghouse),1,fp1)!=1)
        printf(" File read/write error \n");
    }
    else if(strcmp(house[i].type, " Average house ") == 0)
    {
      if (fwrite(&house[i],sizeof(struct House),1,fp2)!=1)// Write data block   &em[i] : is the address of the data to be output  sizeof(struct clerk) : get 1 The length of a monomer  1 : Number of data items  fp : A pointer to the target file 
        printf(" File read/write error \n");
      if (fwrite(&CommonHouse[i],sizeof(struct CommonHouse),1,fp2)!=1)
        printf(" File read/write error \n");
    }
    else
    {
      if (fwrite(&house[i],sizeof(struct House),1,fp3)!=1)// Write data block   &em[i] : is the address of the data to be output  sizeof(struct clerk) : get 1 The length of a monomer  1 : Number of data items  fp : A pointer to the target file 
        printf(" File read/write error \n");
      if (fwrite(&villa[i],sizeof(struct Villa),1,fp3)!=1)
        printf(" File read/write error \n");
    }
  }
 
  fclose(fp1);//  The last remaining data in the buffer is output to the disk file, and the file pointer is freed along with the associated buffer 
  fclose(fp2);
  fclose(fp3);
}
int load(int type) /* The import function  int type */
{
  int k;
  FILE*fp;
  int i=0;
  if(type == 1)  // apartment 
  {
    if((fp=fopen("Lodginghouse","rb"))==NULL)
    {
      printf ("cannot open file\n");
      exit(0);
    }
    while(feof(fp)==0) // Detects end of file character on stream 
    {
      fread(&house[i],sizeof(struct House),1,fp); // read 
      fread(&Lodginghouse[i],sizeof(struct Lodginghouse),1,fp);
      i++;
    }
  }
  else if(type == 2)   // Average house 
  {
    if((fp=fopen("Ordinary house","rb"))==NULL)
    {
      printf ("cannot open file\n");
      exit(0);
    }
    while(feof(fp)==0)
    {
      fread(&house[i],sizeof(struct House),1,fp); // read 
      fread(&CommonHouse[i],sizeof(struct CommonHouse),1,fp);
      i++;
    }
  }
  else     // villa 
  {
    if((fp=fopen("Villa","rb"))==NULL)
    {
      printf ("cannot open file\n");
      exit(0);
    }
    while(feof(fp)==0)
    {
      fread(&house[i],sizeof(struct House),1,fp); // read 
      fread(&villa,sizeof(struct Villa),1,fp);
      i++;
    }
  }
  fclose(fp);
  return i - 1;// Returns the number of houses of this type 
}
void search()/* Query function */
{
  int t,button;
  system("cls");// Clear the screen 
  do
  {
    printf("\n According to the 1  The query \n According to the 2  Back to the main menu \n");
    scanf("%d",&t);
    if(t>=1&&t<=2)
    {
      button=1;
      break;
    }
    else
    {
      button=0;
      printf(" Input error ");
    }
  }
  while(button==0);// Go back to the query selection 
  while(button==1)
  {
    switch(t)// Select query mode 
    {
    case 1:
      printf(" Being queried \n");
      search_num();
      break;
    case 2:
      main();
      break;
    default:
      break;
    }
  }
}
void search_num()// Check by house number 
{
  int a, k;
  int i,t;
  int m;
  char s[20];
  printf(" Please enter the type of house you want to query :\n");
  scanf("%s", &s);
  if(strcmp(s, " apartment ") == 0)
    k=1;
  else if(strcmp(s, " Average house ") == 0)
    k=2;
  else
    k=3;
  m=load(k);
  printf(" Please enter the house number you are looking for :\n");
  scanf("%d",&a);
  for(i=0; i<m; i++)
    if(a==house[i].number)
    {
      printf(" Housing types    House number    Whether to sell    The price ( yuan )   area ( Square meters )   location ( ring )   The balcony toward    The total number of layer ( layer )");
      if(k == 1) printf("    Apartment type ");
      else if(k == 2) printf("    Residential style ");
      else printf("     Villa material ");
      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 No inquiry was made about the room \n");
  printf("\n");
  printf(" To return a query function, press 1, Please press to continue to check the housing number 2\n");
  scanf("%d",&t);
  switch(t)
  {
  case 1:
    search();
    break;
  case 2:
    break;
  default:
    break;
  }
}
void display() /* Browse function */
{
  int sold, unsold;
  sold = 0;     // The number of houses sold 
  int i, type;
  char s[20];
  printf(" Please enter the type of house to display :");
  scanf("%s", &s);
  if(strcmp(s, " apartment ") == 0)
    type=1;
  else if(strcmp(s, " Average house ") == 0)
    type=2;
  else
    type=3;
  int m=load(type);
  system("cls");
  printf(" Housing types    House number    Whether to sell    The price ( yuan )   area ( Square meters )   location ( ring )   The balcony toward    The total number of layer ( layer )");
  if(type == 1) printf("    Apartment type ");
  else if(type == 2) printf("    Residential style ");
  else printf("     Villa material ");
  printf("\n");
  for(i=0; i<m+res - 1; i++)
  {
    if(strcmp(house[i].sold, " is ") == 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 The number of such buildings sold is: %d", sold);
  printf("\n The unsold quantity of this kind of building is: %d", m + res - 1 -sold);
}
void add()/* Add function */
{
  FILE*fp;
  int n;
  int type;
  int count=0;
  int i;
  char s[20];
  printf(" Please enter the type of house to be added :");
  scanf("%s", &s);
  if(strcmp(s, " apartment ") == 0)
    type=1;
  else if(strcmp(s, " Average house ") == 0)
    type=2;
  else
    type=3;
  int m=load(type);
  printf("\n  Original house information :\n");
  display();
  printf("\n");
  printf(" Please enter the number of houses you want to increase :\n");// Determine the number of houses to be added n
  scanf("%d",&n);
  for (i=m; i<(m+n); i++) // add n Information about a house 
  {
    printf(" Please enter the house type (apartment) , Ordinary residence, villa) : ");
    scanf("%s",&house[i].type);
    printf(" Please enter the house number : ");
    scanf("%d",&house[i].number);
 
    printf(" Please enter whether sold or not (yes / no ): ");
    scanf("%s",&house[i].sold);
    printf(" Please enter the selling price : ");
    scanf("%d",&house[i].price);
    printf(" Please enter the floor space : ");
    scanf("%d",&house[i].area);
 
    if(type == 1)
    {
      printf(" Please enter the apartment type : ");
      scanf("%s",&Lodginghouse[i].Lodginghouse_type);
    }
    else if(type == 2)
    {
      printf(" Ordinary residential building style : ");
      scanf("%s",&CommonHouse[i].building_style);
    }
    else
    {
      printf(" Please enter the villa building materials : ");
      scanf("%s",&villa[i].villa_style);
    }
    printf("\n");
    count=count+1;
    printf(" The number of houses has increased :\n");
    printf("%d\n",count);
  }
  save(m + n);
  printf("\n Add a success \n");
  printf("\n Add all house information after :\n");
  display();// Displays the added information 
}
void modify() /* Modify the function */
{
  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(" Please enter the type of house you want to modify :");
  scanf("%s", &s);
  if(strcmp(s, " apartment ") == 0)
    k=1;
  else if(strcmp(s, " Average house ") == 0)
    k=2;
  else
    k=3;
  int m=load(k);
  system("cls");
 
  printf("\n  Original house information :\n");
  display();
  printf("\n");
 
  printf(" Please enter the number of the house to be modified :\n");
  scanf("%d",&number);
  for(button=1,i=0; button&&i<m; i++)
  {
    if(house[i].number == number)
    {
      printf("\n The house was originally recorded as :\n");
      printf(" Housing types    House number    Whether to sell    The price ( yuan )   area ( Square meters ) ");
      if(k == 1) printf("    Apartment type ");
      else if(k == 2) printf("    Residential style ");
      else printf("     Villa material ");
      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 determine   According to the 1 ;  Press if not modified 0\n");
      scanf("%d",&n);
      if(n==1)
      {
        printf("\n Options that need to be modified \n 1. Housing types  2. House number 4. Whether to sell  5. The price  6. area ");
        if(k==1) printf("10. Apartment type  ");
        else if(k == 2) printf("10. Residential style  ");
        else printf("10. Villa material  ");
        printf("11. Return to the top \n");
        printf(" Please select the serial number 1-11:\n");
        scanf("%d",&c);
        if(c>11||c<1)
          printf("\n The wrong choice , Please reselect !\n");
      }
      button=0;
    }
 
  }
  if(button==1)
    printf("\n No trace of the house was found \n");
 
  do
  {
    switch(c)   /* Because when you find the first i When building a ,for After the statement i Since the added 1, So the following should assign the modified information to the first i-1 a */
    {
    case 1:
      printf(" House type changed to : ");
      scanf("%s",&type);
      strcpy(house[i - 1].type,type);
      break;
    case 2:
      printf(" Change the building number to : ");
      scanf("%d",&number);
      house[i-1].number=number;
      break;
 
    case 4:
      printf(" Whether the house was sold to change : ");
      scanf("%s",&sold);
      strcpy(house[i-1].sold, sold);
      break;
    case 5:
      printf(" House price changed to : ");
      scanf("%d",&price);
      house[i-1].price=price;
      break;
    case 6:
      printf(" The size of the house changed to: ");
      scanf("%d", &area);
      house[i-1].area=area;
      break;
 
    case 10:
      if(k == 1)
      {
        printf(" Apartment type changed to: ");
        scanf("%s", &Lodginghouse_type);
        strcpy(Lodginghouse[i-1].Lodginghouse_type, Lodginghouse_type);
        break;
      }
      else if(k == 2)
      {
        printf(" The style of the house is changed to: ");
        scanf("%s", &building_style);
        strcpy(CommonHouse[i-1].building_style, building_style);
        break;
      }
      else
      {
        printf(" The building materials of the villa are changed to: ");
        scanf("%s", &villa_style);
        strcpy(villa[i-1].villa_style, villa_style);
        break;
      }
      break;
    case 11:
      modify();
      break;
    }
 
    printf("\n");
    printf("\n\n  Sure to modify   Please click 1 ;  To modify the   Please click 2: \n");
    scanf("%d",&b);
  }while(b==2);
  printf("\n All house information after modification :\n");
  printf("\n");
  save(m);
 
  display();
  printf("\n According to the 1  Continue to modify  , Please press for no further modifications 0\n");
  scanf("%d",&t);
  switch(t)
  {
  case 1:
    modify();
    break;
  case 0:
    break;
  default :
    break;
  }
}
void del()  /* Delete function  */
{
  int type;
  char s[100];
  int i,j,n,t,button;
  int number;
  printf(" Please enter the type of house to be deleted: ");
  scanf("%s", &s);
  if(strcmp(s, " apartment ") == 0)
    type=1;
  else if(strcmp(s, " Average house ") == 0)
    type=2;
  else
    type=3;
  int m=load(type);
  printf("\n  Original house information :\n");
  display(); // Displays house information before deletion 
  printf("\n");
 
  printf(" Please enter the number of the house you want to delete :\n");
  scanf("%d",&number);
  for(button=1,i=0; button&&i<m; i++) // In the main function button==1 The function can only be called 
  {
    if(house[i].number == number)// Locate an employee by name   And pull out its data 
    {
      printf("\n The house was originally recorded as :\n");// Displays information about the selected employee 
      printf(" Housing types    House number   Whether to sell    The price ( yuan )   area ( Square meters ) ");
      if(type == 1) printf("    Apartment type ");
      else if(type == 2) printf("    Residential style ");
      else printf("     Villa material ");
      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 Sure to delete   Please click 1, Click not to delete 0\n");
      scanf("%d",&n);
      if(n==1)
      {
        for(j=i; j<m-1; j++) // From the first i A start   After the 1 The values of the item are assigned before 1 The terms correspond to each other   Completion of the first i Delete the item 
        {
          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 Indicates that the deletion is complete 
    m=m-1;// The total number of homes is down 1 people 
  else
    printf("\n No such house is available !\n");
  printf("\n  All house information deleted :\n");
  save(m);   // Call save function 
  display(); // Call browse function 
 
  printf("\n Press to continue deletion 1, Please press no longer delete 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;     // On behalf of the budget 
  printf(" Please enter the type of housing to be budgeted for: ");
  scanf("%s", &s);
  if(strcmp(s, " apartment ") == 0)
    type=1;
  else if(strcmp(s, " Average house ") == 0)
    type=2;
  else
    type=3;
  int m=load(type);
  for (i=0; i<m; i++)
  {
    ayusuan += house[i].price;
  }
  if(type == 1)
    printf(" The budget of all apartments sold is as follows: ");
  else if(type == 2)
    printf(" The budget for selling all ordinary houses is: ");
  else
    printf(" The budget of all the villas sold is: ");
  printf("%d\n", ayusuan);
}
void sort_realw()
{
  int i,j,k,m,l;
  char s;
  printf(" Please enter the type of house you want to sort: ");
  scanf("%s",&s);
  if(strcmp(s, " apartment ") == 0)
    l=1;
  else if(strcmp(s, " Average house ") == 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(" Your room number,    area    The price   type    Sales status   \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()// The main function 
{
  int n, m, button;
  char a;
  start();
  getch();
  menu();
  do
{
      printf(" Feature selection (1 - 9 ):\n");
    scanf("%d",&n);
    if(n>=1&&n<=8)
    {
      button=1;
      break;
    }
    else if(n == 9)
    {
      exit(0);
    }
    else
    {
      button=0;
      printf(" You typed it incorrectly , Please reselect !");
    }
  }
  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(" Press any key to continue \n");
    getch();
    system("cls"); /* Clear the screen */
    menu(); /* Call menu function */
    printf(" Feature selection (1--9):\n");
    scanf("%d",&n);
    printf("\n");
  }
}

Note: When calculating the total number of houses, 1 pay attention to initializing and updating the global variable value

For more information, please pay attention to the topic management System Development.


Related articles: