目的:
设计一套代码,将结构体声明放在自定义头文件中,在主程序中实现对800到801个人的信息录入,并将最后一个录入的人员信息打印出来。人员信息包括卡主姓名、性别、卡号、身份证号、余额、出生日期、发卡日期等。
需求分析:
1、建立一个 .h 头文件,用于存储结构体类型定义以及其他宏定义。
2、建立一个 .c 源文件,并且在主程序中定义一个 5000维结构体数组,用于存储银行卡持有者的信息。
3、设计二个子函数,输入以及输出录入人员的信息。
技术难点以及解决方案:
需要重复的代码量大,但是我们使用宏定义以后,这个问题得到了解决。
改进方向:
使用GUI技术,使得界面可视化,且可用鼠标操作。
头文件 PeopleInfoBank.h 代码:
#include<stdio.h>
#include<string.h>
#define PEOPLE_NUM 5000 //定义数组长度
#define PROMPT_INFO(a) printf("Information: ");printf(a); //信息输出宏定义
#define PROMPT_OUTPUT(a, b) printf("Information: ");printf(a, b);printf("\n");
struct Birthday
{
unsigned int year;
unsigned int month;
unsigned int day;
};
struct Startday
{
unsigned int year;
unsigned int month;
unsigned int day;
};
struct People_Info_BandCard
{
char name[30]; //姓名
char sex[10]; //性别
char card_id[20]; //卡号
char identity[20]; //身份证号
struct Birthday birth; //生日
struct Startday start; //办卡日期
char money[20]; //余额
};
c源文件(银行卡录入系统.c)代码:
#include "PeopleInfoBank.h"
struct People_Info_BandCard PeopleInfo[PEOPLE_NUM] = {0}; //定义结构体数组
int main()
{
void InputInfo(int Star_Flag, int End_Flag); //输入银行卡信息函数声明
void OutputInfo(int Index); //输出持卡人信息函数声明
//函数调用
InputInfo(800, 801);
OutputInfo(801);
return 0;
}
//函数定义
void InputInfo(int Star_Flag, int End_Flag)
{
int i = 0;
PROMPT_INFO("开始处理函数InputInfo()\n");
PROMPT_INFO("请输入银行卡持有人信息:\n");
for(i = Star_Flag; i <= End_Flag; i ++)
{
PROMPT_INFO("持卡人姓名:\t");
getchar();
gets(PeopleInfo[i].name);
//scanf("%s",PeopleInfo[i].name);
PROMPT_INFO("持卡人性别:\t");
scanf("%s",PeopleInfo[i].sex);
PROMPT_INFO("持卡人卡号:\t");
scanf("%s",PeopleInfo[i].card_id);
PROMPT_INFO("持卡人身份证号码:\t");
scanf("%s",PeopleInfo[i].identity);
PROMPT_INFO("持卡人生日:\t");
scanf("%u %u %u", &PeopleInfo[i].birth.year, &PeopleInfo[i].birth.month,
&PeopleInfo[i].birth.day);
PROMPT_INFO("办卡日期:\t");
scanf("%u %u %u", &PeopleInfo[i].start.year, &PeopleInfo[i].start.month,
&PeopleInfo[i].start.day);
PROMPT_INFO("卡余额:\t");
scanf("%s", PeopleInfo[i].money);
}
}
void OutputInfo(int Index)
{
PROMPT_INFO("开始处理函数OutputInfo()\n");
PROMPT_OUTPUT("持卡人姓名:\t%s", PeopleInfo[Index].name);
PROMPT_OUTPUT("持卡人性别:\t%s", PeopleInfo[Index].sex);
PROMPT_OUTPUT("持卡人卡号:\t%s", PeopleInfo[Index].card_id);
PROMPT_OUTPUT("持卡人身份证号:\t%s", PeopleInfo[Index].identity);
printf("Information: ");
printf("持卡人生日:\t%u\t", PeopleInfo[Index].birth.year);
printf("%u\t", PeopleInfo[Index].birth.month);
printf("%u\n", PeopleInfo[Index].birth.day);
printf("Information: ");
printf("办卡日期:\t%u\t", PeopleInfo[Index].start.year);
printf("%u\t", PeopleInfo[Index].start.month);
printf("%u\n", PeopleInfo[Index].start.day);
PROMPT_OUTPUT("卡余额:\t%s", PeopleInfo[Index].money);
}
在Code::Blocks中的输出结果为:
Information: 持卡人性别: male
Information: 持卡人卡号: 6227001689690747284
Information: 持卡人身份证号码: 341227199404021512
Information: 持卡人生日: 1994 06 15
Information: 办卡日期: 2012 06 15
Information: 卡余额: 8800
Information: 持卡人姓名: Zhang Qinqin
Information: 持卡人性别: female
Information: 持卡人卡号: 622700168969
Information: 持卡人身份证号码: 3412221993
Information: 持卡人生日: 1993 03 28
Information: 办卡日期: 2012 06 15
Information: 卡余额: 9800
Information: 开始处理函数OutputInfo()
Information: 持卡人姓名: Zhang Qinqin
Information: 持卡人性别: female
Information: 持卡人卡号: 622700168969
Information: 持卡人身份证号: 3412221993
Information: 持卡人生日: 1993 3 28
Information: 办卡日期: 2012 6 15
Information: 卡余额: 9800
出错分析:
错误语句:
PROMPT_OUTPUT("持卡人生日:\t%u %u %u", PeopleInfo[Index].birth.year, PeopleInfo[Index].birth.month,PeopleInfo[Index].birth.day);
PROMPT_OUTPUT("办卡日期:\t%u %u %u", PeopleInfo[Index].start.year, PeopleInfo[Index].start.month ,PeopleInfo[Index].start.day);
以上两条语句是没有调试时候出错的语句,问题在于代码与宏定义语句不符合,改正后语句:
printf("Information: ");
printf("持卡人生日:\t%u\t", PeopleInfo[Index].birth.year);
printf("%u\t", PeopleInfo[Index].birth.month);
printf("%u\n", PeopleInfo[Index].birth.day);
printf("Information: ");
printf("办卡日期:\t%u\t", PeopleInfo[Index].start.year);
printf("%u\t", PeopleInfo[Index].start.month);
printf("%u\n", PeopleInfo[Index].start.day);
此处不再使用宏定义,因为宏定义在此处与输出格式需求不符。尽管代码多了一点,但是会让输出结果更加的人性化。
疑点:掌握scanf(“%s”,指针)以及gets(指针)的具体细节,以及如何消除缓存中的回车符的程序设计方法,这是需要进一步探讨的问题。