C语言第75课结构数组

C语言第75课结构数组

·function
·使用动态录入
·显示英雄详细信息
·计算平均生命值

·程序部分

//Hero.harderr***************************************
						#ifndef HERO_H_INCLUDED
						#define HERO_H_INCLUDED
						
						//定义结构体
						typedef struct _myTime
						{
							int year;   int month;  int day;
						}MyTime;
						
						typedef struct _hero
						{
							char *name;      //英雄名称
							char sex;           //英雄性别
							char *job;       //英雄职业
							int life;           //英雄生命值
							double speed;       //攻击速度
							char *ability;   //英雄的特殊能力
							MyTime pubTime;     //英雄的上线时间
						
						}Hero;
						//使用动态录入(malloc)
						//打印结果(Show)
						//计算平均生命值
						
						//函数声明
						void ShowAvg(); //计算平均生命值11
						void Input();   //使用动态录入
						void Show();    //显示英雄详细信息
						
						#endif // HERO_H_INCLUDED
//Hero.harderr***************************************
						
						
//Hero.c*********************************************************************************
					#include "Hero.h"
					#include<stdio.h>
					#include<stdlib.h>
					
					Hero heroes[100] = {
						{"影流之主劫",'m',"刺客",579,0.644,"位移",{2012,8,15}},
						{"琴瑟仙女唢呐",'f',"法师",666,0.644,"减速、治疗",{2010,9,20}},
						{"疾风剑豪",'f',"战士",517,0.67,"护盾、位移",{2013,12,23}}
					};
					int count = 3; //当前的英雄总数
					
					
					//函数------------------------------------动态录入START
					void Input()
					{
						//首先录入内容
						//第一个录入完毕后,询问是否继续录入
						char answer = 'y';
						do{
							if(count == 100)
							{
								printf("已达到最大录入英雄总数!不可再录入新的英雄!\n");
								break;
							}
							printf("\n当前录入第%d位英雄的信息:",count+1);
					
							printf("英雄名称:");
							heroes[count].name = (char*)malloc(50); //指针需要分配空间,用数组就不用分配空间了
							scanf("%s",heroes[count].name);
							printf("性别:");
							fflush(stdin);
							heroes[count].sex = getchar();
							fflush(stdin);
							printf("职业:");
							heroes[count].job = (char*)malloc(50); //指针需要分配空间,用数组就不用分配空间了
							scanf("%s",heroes[count].job);
							heroes[count].life = 1000;
							heroes[count].speed = 0.655;
							heroes[count].ability = "上天、入地";
							heroes[count].pubTime.year = 2019;
							heroes[count].pubTime.month = 3;
							heroes[count].pubTime.day = 21;
					
							count++;                                //录入完毕后英雄总数+1
							printf("是否继续录入英雄的信息?(Y/N)\n");
							answer = getch();                       //用户按下键后就立即触发,不需要再敲回车
							if(answer == 1) printf("Y\n");
							else printf("N\n");
						}while(answer =='y' || answer == 'Y');
					}
					//函数------------------------------------动态录入END
					
					
					//函数------------------------------------计算平均生命值STERT
					void ShowAvg()
					{
						int lifeOfSum = 0;
						double avg = 0;
						int i;
						for(i = 0; i < count; i++)
						{
							lifeOfSum += (heroes + i) -> life;
						}
						//计算平均值
						avg = lifeOfSum *1.0 / count;
						printf("目前所有英雄生命平均值为:%.2lf\n",avg);
						printf("\n**************************The END***************************\n");
					}
					//函数------------------------------------计算平均生命值END
					
					
					//函数--------------------------显示英雄详细信息START
					void Show()
					{
						int i;
						printf("\n******************以下是目前所有英雄列表********************\n");
						for (i = 0; i < count; i++)
						{
							printf("%s\t%s\t%d-%d-%d\n",heroes[i].name,heroes[i].job,heroes[i].pubTime.year,heroes[i].pubTime.month,heroes[i].pubTime.day);
						}
					}
					//函数--------------------------显示英雄详细信息END
//Hero.c*********************************************************************************

//Main.c*********************************************************************************
					#include <stdio.h>
					#include <stdlib.h>
					#include "Hero.h"
					
					extern Hero heroes[100];
					int main()
					{
						Input();
						Show();
						ShowAvg();
						return 0;
					}
//Main.c*********************************************************************************

·运行结果

		当前录入第4位英雄的信息:英雄名称:♀love♀
		性别:f
		职业:工程师
		是否继续录入英雄的信息?(Y/N)
		N
		
		******************以下是目前所有英雄列表********************
		影流之主劫      刺客    2012-8-15
		琴瑟仙女唢呐    法师    2010-9-20
		疾风剑豪        战士    2013-12-23
		♀love♀        工程师  2019-3-21
		目前所有英雄生命平均值为:690.50
		
		**************************The END***************************
		
		Process returned 0 (0x0)   execution time : 15.384 s
		Press any key to continue.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值