题目:北京市二手房交易信息录入程序

来源:互联网 发布:人工智能股龙头杜闵梁 编辑:程序博客网 时间:2024/04/27 19:16

 题目:北京市二手房交易信息录入程序


一、语言和环境:
实现语言:C  
使用Turbo   C   2.0、记事本


二、要求:
        你们正在为北京市住房管理中心设计和开发一个住房交易管理系统,该系统包括很多的模块,其中你负责二手房交易信息录入程序,现在你要编写一个C程序录入卖房信息,包括卖房人姓名和卖房人收入,并且将卖房信息按收入降序存储在文本文件中。
        程序功能要求如下:
        1,从键盘循环输入卖房信息,存储到结构数组中;
        2,将结构数组中的卖房信息按收入降序排序;
        3,将卖房信息写入文本文件中。


三、推荐实现步骤:
        1,定义结构体sell_record,包括卖房人姓名name和卖房人收入income
        2,定义全局变量count,用于记录输入的卖房信息的个数
        3,定义结构数组变量,用于存储输入的卖房信息
        4,从键盘循环输入卖房信息,存储到结构数组中,能够输入的信息,建议程序中设定为最多50个,每次输入完一条卖房信息后,提示“是否继续输入?输入Y为集训输入,输入其他任意字符,则程序退出。”
        5,建立函数sort(),将结构数组中的卖房信息按收入降序排序
        6,建立函数writefile()
              打开文本文件house.txt,文件打开模式为:创建文本文件进行读/写操作
              将卖房信息写入文本文件house.txt中
        7,完成主函数mian()


运行时,显示屏显示如下的目录:


北京市二手房交易信息录入


输入第1个二手房交易信息
输入卖房人姓名:MD
输入卖房收入:1000
是否继续输入(Y/N)?
Y


输入第2个二手房交易信息
输入卖房人姓名:LXX
输入卖房收入:1000
是否继续输入(Y/N)?
Y


输入第3个二手房交易信息
输入卖房人姓名:ZY
输入卖房收入:1000
是否继续输入(Y/N)?
N
Press   any   key   to   contiune


文本文件内容显示如下:
MD   1000
LXX   1000
ZY   1000
=========================================================================================================

#include   <stdio.h>
#include   <string.h>

#define   SZNAME   10
#define   SZREC   50

struct   sell_record{
    char   name[SZNAME];
    int   income;
}   rec[SZREC];

int   count   =   0;

void   sort(){
    int   i,   j;
    char   name[SZNAME];
    int   income;
    for   (i   =   0;   i   <   count   -   1;   i   ++){
        for   (j   =   i   +   1;   j   <   count;   j   ++){
            if   (rec[i].income   >   rec[j].income){
                strcpy(name,   rec[j].name);
                income   =   rec[j].income;
                strcpy(rec[j].name,   rec[i].name);
                rec[j].income   =   rec[i].income;
                strcpy(rec[i].name,   name);
                rec[i].income   =   income;
            }
        }
    }
}

void   writefile(){
    FILE*   fp;
    int   i;
    if((fp   =   fopen( "house.txt ",   "a+ "))   ==   NULL)   exit(0);
    for   (i   =   0;   i   <   count;   i   ++)
        fprintf(fp,   "%s   %d ",   rec[i].name,   rec[i].income)
    fclose(fp)
}

int   main(){
    char   c   =   'Y ';
    while   (c   ==   'Y '){
        printf( "请输入卖房信息\n房主名字: ");
        scanf( "%9s ",   rec[count].name);
        printf( "收入: ");
        scanf( "%d ",   &rec[count++].income);
        if   (SZREC   ==   count)   break;
        printf( "是否继续输入?输入Y为集训输入,输入其他任意字符,则程序退出。[   ]\b\b ");
        scanf( "%c ",   &c);
    }
    sort();
    writefile();
    return   0;
}
=====================================================================================================================

#include   <stdio.h>
#include   <string.h>

#define   SZNAME   10
#define   SZREC   50

struct   sell_record{
    char   name[SZNAME];
    int   income;
}   rec[SZREC];

int   count   =   0;

void   sort(){
    int   i,   j;
    char   name[SZNAME];
    int   income;
    for   (i   =   0;   i   <   count   -   1;   i   ++){
        for   (j   =   i   +   1;   j   <   count;   j   ++){
            if   (rec[i].income   >   rec[j].income){
                strcpy(name,   rec[j].name);
                income   =   rec[j].income;
                strcpy(rec[j].name,   rec[i].name);
                rec[j].income   =   rec[i].income;
                strcpy(rec[i].name,   name);
                rec[i].income   =   income;
            }
        }
    }
}

void   writefile(){
    FILE*   fp;
    int   i;
    if((fp   =   fopen( "house.txt ",   "a+ "))   ==   NULL)   exit(0);
    for   (i   =   0;   i   <   count;   i   ++)
        fprintf(fp,   "%s   %d ",   rec[i].name,   rec[i].income);
    fclose(fp);
}

int   main(){
    char   c   =   'Y ';
    while   (c   ==   'Y '){
        printf( "input   name:     ");
        scanf( "%9s ",   rec[count].name);
        printf( "income:     ");
        scanf( "%d ",   &rec[count++].income);
        if   (SZREC   ==   count)   break;
        printf( "continue?(y/n)[   ]\b\b ");
        fflush(stdin);/* <-----------------这里加一句,就可以了*/
        scanf( "%c ",   &c);
        if(c> = 'a '&&c <= 'z ')
              c=c-32;/*改为大写*/
    }
    sort();
    writefile();
    return   0;
}

turbo   c     不支持中文,所以改为英语了。
你的这个错误   是运行时的问题,用scanf()函数输入字符时,会把上一次输入的回车符作为有效的字符。
你在这里
while   (c   ==   'Y '){
        printf( "input   name:     ");
        scanf( "%9s ",   rec[count].name);
        printf( "income:     ");
        scanf( "%d ",   &rec[count++].income);/*把这里的回车符作为了下面的有效字符了*/
        if   (SZREC   ==   count)   break;
        printf( "continue?(y/n)[   ]\b\b ");
        fflush(stdin);/* <-----------------这里加一句,就可以了*/
        scanf( "%c ",   &c);
        if(c> = 'a '&&c <= 'z ')
              c=c-32;/*改为大写*/
    }
===============================================================================

加上fflush(stdin);这一句就是刷新输入缓冲区,把上一次的回车符清除掉。
你也可以用:
getchar();吃掉上面的回车符。
这一句,丢弃了返回值getchar()语句。

原创粉丝点击