main.c
#include <stdio.h>
#include <stdlib.h>
#include "hero.h"
extern Hero heros[100];
int main()
{
Input();
Show();
ShowAvg();
return 0;
}
hero.h
#ifndef HERO_H_INCLUDED
#define HERO_H_INCLUDED
#include <stdio.h>
#include <stdlib.h>
typedef struct _myTime
{
int year; int month; int day;
}MyTime;
typedef struct _hero
{
char *name; //英雄名称
char sex; //英雄性别
char *job; //英雄职业
int life; //英雄生命值
double speeed; //攻击速度
char *abillity; //英雄的特殊能力
MyTime pubTime; //英雄的上线时间
}Hero;
//使用动态录入
//打印计算结果
//计算平均生命值
void ShowAvg();
void Input();//动态录入内容
void Show();//显示英雄的详细信息
#endif // HERO_H_INCLUDED
hero.c
#include "Hero.h"
char name[50]; //英雄名称
char sex; //英雄性别
char job[20]; //英雄职业
int life; //英雄生命值
double speeed; //攻击速度
char abillity[20]; //英雄的特殊能力
MyTime pubTime; //英雄的上线时间
Hero heros[100] = {
{"影流之主劫", 'm', "刺客", 579, 0.644, "位移", {2012, 8, 15}},
{"琴瑟仙女霎那", 'F', "法师", 482, 0.644, "减速", {2010, 9, 20}},
{"疾风剑豪", 'm', "战士", 579, 0.67, "护盾、位移", {2013, 12, 23}}
};
int count = 3;//当前的英雄总数
void Input()
{
// 首先录入内容
// 第一个录入完毕后, 询问是否继续录入?
char answer = 'y';
do{
if(count == 100)
{
printf("英雄的栏位已满,请到游戏商城购买!");
break;
}
printf("当前录入第%d位英雄的信息: \n", count + 1);
printf("英雄名称: ");
heros[count].name = (char*)malloc(50);
scanf("%s", heros[count].name);
printf("性别: ");
fflush(stdin);
heros[count].sex = getchar();
fflush(stdin);
printf("职业: ");
heros[count].job = (char*)malloc(50);
scanf("%s", heros[count].job);
heros[count].life = 1000;
heros[count].speeed = 0.655;
heros[count].abillity = "上天、入地";
heros[count].pubTime.year = 2016;
heros[count].pubTime.month = 4;
heros[count].pubTime.day = 9;
count++; //录入完毕后,英雄总数+1
printf("是否继续录入英雄的信息?(y/n)");
answer = getch(); //用户按下键后就立即触发下面语句,不会在让用户敲回车了
}while(answer == 'y' || answer == 'Y');
}
void ShowAvg()
{
int lifeOfSum = 0;
double avg = 0;
int i;
for(i = 0;i<count;i++)
{
lifeOfSum += (heros + i)->life;
}
// 计算平均值
avg = lifeOfSum *1.0 / count;
printf("生命值的平均值为: %.2lf\n", avg);
}
void Show()
{
//如何知道结构数组的大小呢?
//int len = sizeof(heros) / sizeof(Hero);
//数组元素使用指针时,动态赋值需要首先分配内存
//printf("请输入名称:");
//heros[0].name = (char *)malloc(50);// char name[50]
//scanf("%s", heros[0].name);
//printf("结构数组的元素个数: %d\n", len);
int i;
printf("\n");
for(i = 0;i < count; i++)
{
printf("%s\t%s\t%d-%d-%d\n", heros[i].name, heros[i].job,
heros[i].pubTime.year, heros[i].pubTime.month, heros[i].pubTime.day);
}
}
运行结果