欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C语言课程设计(房屋管理系统)

程序员文章站 2024-03-19 21:48:52
...
#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),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 file\n");
            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 file\n");
            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 file\n");
            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,继续查询房屋编号请按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", &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("%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", &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 ; 不修改请按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,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,不删除请按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", &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("%d\n", 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");
    }
}

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

相关标签: e