69,371
社区成员
发帖
与我相关
我的任务
分享
//功能:通过调用函数的形式接受数据输入,并调用另外一个打印函数输出信息
//时间:2012.02.28
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define CSIZE 3
#define LEN 15
#define SCORE 3
struct Name
{
char m_sLasName[LEN];
char m_sFamName[LEN];
};
struct People
{
struct Name m_sName;
double m_fScore[SCORE];
double m_fAverage;
};
void InputInfor(struct People people[], int iCount); //接收输入
void PrintInfor(struct People people[], int iCount); //打印信息
void CleanLine(void);
int main(int argc, char * argv[])
{
struct People people[CSIZE] = { //部分初始化
{"Liang", "Bai"},
{"Chunxi", "Wang"},
{"Lei", "Bai"}
};
printf("**************Enter information for 3 people**************\n");
InputInfor(people, CSIZE);
printf("**************After receive information*******************\n");
PrintInfor(people, CSIZE);
return 0;
}
void InputInfor(struct People people[], int iCount)
{
int iOut, iInner;
for (iOut = 0; iOut < iCount; ++iOut)
{
printf("NO.%d\n", iOut + 1);
puts("Enter last name:");
gets(people[iOut].m_sName.m_sLasName);
puts("Enter family name:");
gets(people[iOut].m_sName.m_sFamName);
for (iInner = 0; iInner < SCORE; ++iInner)
{
printf("Enter %d score:\n", iInner + 1);
scanf("%f", &people[iOut].m_fScore[iInner]);
}
CleanLine(); //清除接收数据时的回车
}
}
void PrintInfor(struct People people[], int iCount)
{
int iVal;
for (iVal = 0; iVal < iCount; ++iVal)
{
printf("NAME |%10s|%10s\n", people[iVal].m_sName.m_sFamName,
people[iVal].m_sName.m_sLasName);
printf("SCORE |%10.2f|%10.2f|%10.2f\n", people[iVal].m_fScore[0],
people[iVal].m_fScore[1], people[iVal].m_fScore[2]);
printf("AVERAGE|%10.2f\n\n", people[iVal].m_fAverage);
}
}
void CleanLine(void)
{
while (getchar() != '\n')
{
continue; //如果不是回车继续,将回车之前的字符都忽略
}
}