count函数

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstring>
using namespace std;

struct student{
char name[32];
int age;
int classnid;
bool sex;
};
/*
写出函数,用函数重载的方式编写程序,统计出一个以上述结构为基准的结构体数组中:
1、名字为X的出现了多少次
2、年龄为X的出现了多少次
3、班级为X的出现了多少次
4、性别的X的出现了多少次
例如:名字为X的可以是这样的声明:
int countName(student * src, int n, char * name);
tip:可以考虑用缺省参数的方式,或者回调函数的方式实现。
*/

bool cmpEql(student a, student b)
{
	return a.age == b.age;
}

bool cmpEqlname(student a, student b)
{
	return strcmp(a.name, b.name) ? false : true;
}

bool cmpEqlsex(student a, student b)
{
	return !(a.sex ^ b.sex);
}
int countS(student *st, int n,student value,bool(*cmp)(student,student)=cmpEql)
{
	int i;
	int count = 0;
	for (i = 0; i < n; i++)
	{
		if (cmp(st[i], value))
		{
			count++;
		}
	}
	return count;
}
int test2()
{
	student st[] = { { "caixikun", 18, 1, true },
					{ "qiaobiluo", 18, 1, false },
					{ "wuyifan", 20, 2, false },
					{ "luhan", 22, 2, true },
					{ "lubenwei", 22, 2, true },
					{ "yangchaoyue", 20, 1, false },
					{ "caixikun", 18, 1, true } 
	};
		

	student test = { "caixikun", 18, 1, true };
	cout << countS(st, 7, test, cmpEqlname) << endl;
	return 0;
}```

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值