c语言之指针练习

/************************************************************************/
/*	01.c																*/
/* 用指向函数的指针比较大小                                             */
/************************************************************************/
#include <stdio.h>
int min(int a,int b)
{
	if(a > b)
		return b;
	else
		return a;
}
int main()
{
	int (*p)(int,int);//指向函数的指针变量p,参数必须指明类型,否则出错
	p=min;//p指向min函数
	int a,b,re;
	printf("输入两个整数:\n");
	scanf("%d%d",&a,&b);
	re = (*p)(a,b);
	printf("min = %d\n",re);
	return 0;
}


/************************************************************************/
/* 02.c																	*/
/*	使用返回指针的函数查找最大值                                        */
/************************************************************************/
#include <stdio.h>
#define ARRAYSIZE 10
int *FindMax(int *p,int n)
{
	int i,*max;
	max = p;//max指向传入的数组首地址&a[0]
	for(i=0;i<n;i++)
		if(*(p+i) > *max)
			max = p+i;
	return max;
}
int main()
{
	int a[ARRAYSIZE];
	int *re;
	int i;
	printf("输入%d个整数\n",ARRAYSIZE);
		for(i=0 ;i < ARRAYSIZE;	i++)
			scanf("%d",&a[i]);
	re=FindMax(a,ARRAYSIZE);
	printf("maxNum = %d\n",*re);
	return 0;
}

/************************************************************************/
/* 03.c																	*/
/*	使用指针实现逆序存放数组元素值                                      */
/************************************************************************/
#include <stdio.h>
#define ARRAYSIZE 10

void invert(int *a,int n)
{
	int *first,*last,*mid;
	int temp;
	first = a;//数组首地址
	mid = a+(n-1)/2;//指向中间元素
	last = a+n-1;//数组末尾地址
	for(; first <=mid; first++,last--)
	{
		temp = *first;
		*first = *last;
		*last = temp;	
	}
}
int main()
{
	int a[ARRAYSIZE];
	int i;
	printf("输入%d个整数\n",ARRAYSIZE);
	for (i=0;i<ARRAYSIZE;i++)
	{
		scanf("%d",&a[i]);
	}
	invert(a,ARRAYSIZE);
	for(i=0;i<ARRAYSIZE;i++)
		printf("%d ",a[i]);
	return 0;
}

/************************************************************************/
/* 04.c																	*/
/* 使用指针实现字符串匹配                                               */
/************************************************************************/
#include <stdio.h>
#include <string.h>
//strA为用来匹配的字符串,strB为要进行匹配操作的字符串
//即在strB中寻找strA
int match(char *strB,char *strA)
{
	int i,j,startPos=0;//从0开始
	int lenA = strlen(strA)-1;
	int lenB = strlen(strB)-1;
	int endMatch = lenA;
//比较strA的最后一个字符与strB中字符是否相同,若相同,循环比较strA与strB是否匹配
	for (j = 0; endMatch <= lenB; endMatch++,startPos++)
	{
		if(strB[endMatch] == strA[lenA])
			for (j = 0,i=startPos; j<lenA&&strB[i] == strA[j];)
			{
				i++;
				j++;
			}
			if(j == lenA)//匹配完成
			{
				return (startPos+1);
			}
	}
	if(endMatch > lenB)
	{
		printf("Not Matchable\n");
		return -1;
	}
	return 0;
}
int main()
{
	char A[] = "abaabcac";
	char B[] = "acabaabcaabaabcac";
	int p = match(B,A);
	if(p != -1)
	{
		printf("Matchable\n");
		printf("The start position is %d\n",p);
	}
	return 0;
}

/************************************************************************/
/* 05.c																	*/
/* 使用指针查找最大元素,并将最大元素所在数组下标输出					*/
/************************************************************************/
#include <stdio.h>

#define ARRAYSIZE 10

int *findMax(int *a,int n,int &script)
{
	int *max;
	max = a;
	int i;
	for(i = 0; i<n; i++)
	{
		if(*(a+i) > *max){
			max = (a+i);
			script = i;
		}
	}
	
	return max;
}
int main()
{
	int *max;
	int a[ARRAYSIZE];
	int i,scr;
	printf("输入%d个整数:",ARRAYSIZE);
	for(i = 0; i<ARRAYSIZE; i++)
	{
		scanf("%d",&a[i]);
	}
	max = findMax(a,ARRAYSIZE,scr);
	printf("Max Num is:a[%d]=%d\n",scr,*max);
	return 0;
}

/************************************************************************/
/* 06.c																	*/
/* 寻找两个数组中相同元素                                               */
/************************************************************************/
#include <stdio.h>

#define ARRAYSIZEA 5
#define ARRAYSIZEB 4
//将数组排序
void sort(int *a,int n)
{
	int i,j;
	int temp;
	for(i=0; i<n-1; i++)
		for(j=i+1; j<n; j++)
		{
			if(*(a+i) > *(a+j))
			{
				temp = *(a+i);
				*(a+i) = *(a+j);
				*(a+j) = temp;
			}
		}
}
//查找相同元素
int *find(int *a,int *b,int an,int bn)
{
	int *pta,*ptb;
	pta = a;
	ptb = b;
	while(pta < (a+an) && ptb < (b+bn))
	{
		if(*pta < *ptb)
			pta++;
		else if(*pta > *ptb)
			ptb++;
		else//相等
			return pta;
	}
	return 0;
}

int main()
{
	int i;
	int *re;
	int a[ARRAYSIZEA],b[ARRAYSIZEB];
	printf("输入A数组的%d个整数:",ARRAYSIZEA);
	for(i=0; i<ARRAYSIZEA; i++)
		scanf("%d",&a[i]);
	printf("输入B数组的%d个整数:",ARRAYSIZEB);
	for (i=0; i<ARRAYSIZEB; i++)
		scanf("%d",&b[i]);
	sort(a,ARRAYSIZEA);
	sort(b,ARRAYSIZEB);
	re = find(a,b,ARRAYSIZEA,ARRAYSIZEB);
	printf("same num is %d\n",*re);
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值