回调函数应用之比较两个对象大小
来源:互联网 发布:淘宝助理怎么上架宝贝 编辑:程序博客网 时间:2024/05/22 07:02
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef int (*cmp)(void const* one,void const* other); //抽象函数,比较两个对象大小
int cmp_int(void const *one,void const *other) //具体函数比较两个整数大小
{
const int *a=(int*)one; //必须转化
const int *b=(int*)other;
if(*a>*b) return 1;
else if(*a==*b) return 0;
else return -1;
}
int cmp_string(void const *one,void const *other) //具体函数比较两个字符串大小
{
const char *str1=(char*)one;
const char *str2=(char*)other;
return strcmp(str1,str2);
}
void const *MaxofTwo(void const *one,void const *other,cmp func) //回调函数,返回两个对象最大的那个
{
if(func(one,other)>=0) return one;
else return other;
}
void InputException()
{
printf("Input Exception!\n");
exit(0);
}
void MemException()
{
printf("Memeory Exception!\n");
exit(0);
}
void inputstring(char *&s,int len) //输入字符串,防止缓冲区溢出,len是最多可输入字符数
{
int i;
if(len<=0) InputException();
s=(char*)malloc(sizeof(len));
if(!s) MemException();
char *p;
char c;
i=0;
p=s;
while((c=getchar())!='\n' && i++<len)
{
*p++=c;
}
if(i<=len) s[i]='\0';
else s[i-1]='\0';
fflush(stdin);
}
int main(int argc,char **argv)
{
int a,b;
printf("a=");
if(scanf("%d",&a)<=0) InputException();
printf("b=");
if(scanf("%d",&b)<=0) InputException();
int *c=(int*)MaxofTwo(&a,&b,cmp_int); //必须转化
printf("The max Value betwwen %d and %d is %d\n",a,b,*c);
getchar(); //接收回车
char *s1,*s2;
printf("The first string is:");
inputstring(s1,30);
printf("The second string is:");
inputstring(s2,30);
char *str=(char*)MaxofTwo(s1,s2,cmp_string);
printf("The Max string is:");
puts(str);
return 0;
}
- 回调函数应用之比较两个对象大小
- 比较两个数大小
- 比较两个数大小
- 两个数比较大小
- 两个数比较大小
- 比较两个数大小
- 两个数比较大小
- 两个数比较大小
- 比较两个数的大小
- 比较两个数的大小
- 比较两个数的大小
- 两个数大小的比较
- 比较两个数的大小
- 输入两个数,比较大小
- 比较两个数的大小
- 比较两个数的大小
- 比较两个数的大小
- 比较两个数的大小
- 判断整除并求和
- 多线程
- EXCEL表中的CONCATENATE函数
- automake同时生成静态链接库和动态链接库
- linux mysql忘记密码
- 回调函数应用之比较两个对象大小
- oracle 如何查看建表时间?
- 飞鸽传书也具有五华石乡特色
- iphone解析XML文件
- java学习:
- excel操作文件描述符号
- Http 1.0 响应码
- 优秀网站源码、编程源码
- 网络连接正常但IP却Ping不通解答