C结构体基础

大笑结构体数组,以及结构体的基本操作

/*************************************************************************
	> File Name: excise.c
	> Author: XXDK
	> Email: v.manstein@qq.com 
	> Created Time: Mon 06 Mar 2017 03:01:38 AM PST
 ************************************************************************/

#include<stdio.h>

#define ARRAY_SIZE(a)	(sizeof(a)/sizeof(a[0]))

struct commander {
	char name[24]; // 姓名
	int age;       // 年龄
	int wisdom;    // 智力
};

void input_cmd_info(struct commander *cmdp, int size);
void output_cmd_info(struct commander *cmdp, int size);
void sort_wisdom(struct commander *cmdp,  int size);

int main()
{
	// 结构体数组
	struct commander cmd[5];

	input_cmd_info(cmd, ARRAY_SIZE(cmd));
	output_cmd_info(cmd, ARRAY_SIZE(cmd));
	sort_wisdom(cmd, ARRAY_SIZE(cmd));
	output_cmd_info(cmd, ARRAY_SIZE(cmd));

	return 0;
}

void input_cmd_info(struct commander *cmdp, int size)
{
	printf("please input 5 commander informationi<name age wisdom>: \n");
	for(int i = 0; i < size; i++, cmdp++) {
		printf("input: ");
		scanf("%s%d%d", cmdp->name, &cmdp->age, &cmdp->wisdom);
	}
}
void output_cmd_info(struct commander *cmdp, int size)
{
	printf("All commander information is:\n");
	for(int i = 0; i < size; i++) {
		printf("commander %d name:   %s\n", i, cmdp[i].name);
		printf("commander %d age:    %d\n", i, cmdp[i].age);
		printf("commander %d wisdom: %d\n", i, cmdp[i].wisdom);
		printf("---------------------------\n");
	}
}
//按照智商排序
void sort_wisdom(struct commander *cmdp,  int size)
{
	struct commander temp;
	int flag = 0, i, j;
	//bubble sort
	for(i = 0; i < size - 1; i++) {
		for(j = 0, flag = 0; j < size - 1 - i; j++) {
			if(cmdp[j].wisdom < cmdp[j + 1].wisdom) {
				temp = cmdp[j];
				cmdp[j] = cmdp[j + 1];
				cmdp[j + 1] = temp;
				flag  = 1;
			}
		}
		if(!flag) {
			break;
		}
	}
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值