标签:结果 get 实验报告 元素 变量 lease 一个 字符 bre
实验项目:
8.3.1、8.3.2、8.3.3、8.3.4
姓名:陈佳媛
实验地点:学校
实验时间:2020.6.2
1、掌握指针的概念和定义方法
2、掌握指针的操作符和指针的运算
3、掌握指针与数组的关系
4、掌握指针与字符串的关系
5、熟悉指针作为函数的参数及返回指针的函数
6、了解函数指针
8.3.1
(1)定义一个整型指针变量p,使它指向一个整型变量a,?定义一个浮点型指针q,使它指向一个浮点型变量b,同时定义另外一个整型变量c并赋初值3。
(2?)使用指针变量,调用scanf()函数分别输入a和b的值。
(?3?)通过指针间接访问并输出a、b的值。
(4)按十六进制方式输出p、q的值及a、b的地址。
(5)将p指向c,通过p间接访问c的值并输出。
(6)输出p的值及C的地址,并与上面的结果进行比较。
#include<stdio.h>
int main()
{
	int *p,a,c=3;
	float *q,b;
	p=&a;
	q=&b;
	printf("Please Input the Value of a,b:");
	scanf("%d%f",p,q);
	printf("Result:\n");
	printf("%d,%f\n",a,b);
	printf("%d,%f\n",*p,*q);
	printf("The Address of a,b:%p,%p\n",&a,&b);
	printf("The Address of a,b:%p,%p\n",p,q);
	p=&c;
	printf("c=%d\n",*p);
	printf("The Address of c:%x,%x\n",p,&c);
	return 0;
}
无
8.3.2
(?1?)定义两个函数,分别为void?swap1(int?a,?int?b)和void?swap2(int?*a,?int?*b),用于交换a、b的值。
(2)从主函数中分别输入两个整型变量a、b。
(3)从主函数中分别调用上述两个交换函数,并打印输出交换后a、b的结果。
#include<stdio.h>
int swap1(int x,int y);
int swap2(int *x,int *y);
int main()
{
	int a,b;
	printf("Please Input a=:");
	scanf("%d",&a);
	printf("\nb=:");
	scanf("%d",&b);
	swap1(a,b);
	printf("\nAfter Call swap1:a=%d b=%d\n",a,b);
	swap2(&a,&b);
	printf("\nAfter Call swap2:a=%d b=%d\n",a,b);
	return 0;
}
int swap1(int x,int y)
{
	int temp;
	temp=x;
	x=y;
	y=temp;
}
int swap2(int *x,int *y)
{
	int temp;
	temp=*x;
	*x=*y;
	*y=temp;
}
代码细节容易出错,以后写代码时要注意。
8.3.3
(1)定义两个字符指针,通过gets()函数输入两个字符串。
(2)定义一个函数char?*reverse(char?*str),?通过指针移动方式将字符串反转。
(3)定义一个函数char?*Iink(char?*strl,?char?*str2),通过指针移动方式将两个字符串连接起来。
(4)从主函数中分别调用上述函数,输人字符串并打印输出结果。
#include<stdio.h>
char *reverse(char *str);
char *link(char *str1,char *str2);
int main()
{
	char str[30],str1[30],*str2;
	printf("Input Reversing Character String:");
	gets(str);
	str2=reverse(str);
	printf("\nOutput Reversed Character String:");
	puts(str2);
	printf("Input String1:");
	gets(str);
	printf("Input String2:");
	gets(str1);
	printf("Link String1 and String2:");
	str2=link(str,str1);
	puts(str2);
	return 0;
}
char *reverse(char *str)
{
	char *p,*q,temp;
	p=str,q=str;
	while(*p!=‘\0‘)
		p++;
	p--;
	while(q<p)
	{
		temp=*q;
		*q=*p;
		*p=temp;
		q++;
		p--;
	}
	return str;
}
char *link(char *str1,char *str2)
{
	char *p=str1,*q=str2;
	while(*p!=‘\0‘)
		p++;
	while(*q!=‘\0‘)
	{
		*p=*q;
		p++;
		q++;
	}
	*p=‘\0‘; 
	return str1;
}
无
8.3.4
(1)定义一个整型一维数组,任意输入数组的元素,其中包含奇数和偶数。
(2)定义一个函数,实现数组元素奇数在左、偶数在右的排列。
(3)在上述定义的函数中,不允许再增加新的数组。
(4)从主函数中分别调用上述函数,打印输出结果。
#include<stdio.h>
#define N 10
int arrsort(int a[],int n);
int main()
{
	int a[N],i;
	for(i=0;i<N;i++)
		scanf("%d",&a[i]);
	arrsort(a,N);
	for(i=0;i<N;i++)
	printf("%d ",a[i]);
}
int arrsort(int a[],int n)
{
	int *p,*q,temp;
	p=a;
	q=a+n-1;
	while(p<q)
	{
		while(*p%2!=0)
			p++;
		while(*q%2==0)
			q--;
		if(p>q)
			break;
		temp=*p;
		*p=*q;
		*q=temp;
		p++;
		q--;
	}
}
打印输出的结果排序后元素的个数与输入的个数不符,指针相向移动由 p- -; q++;改为 p++;q- -;
指针部分相对较难,包含的知识细节也较多,需要牢记和灵活运用。还有数组与指针的区别,不要弄混了。还要写代码时注意小细节,不要因小失大。
标签:结果 get 实验报告 元素 变量 lease 一个 字符 bre
原文地址:https://www.cnblogs.com/Cri-y/p/13062838.html