0
点赞
收藏
分享

微信扫一扫

桂 林 理 工 大 学实 验 报 告实验名称实验7 指针

炽凤亮尧 2022-01-26 阅读 62

桂 林 理 工 大 学

实  验  报  告

实验名称实验7 指针

一、实验目的:

  1. 掌握指针和间接访问的概念,会定义和使用指针变量
  2. 能正确使用数组的指针 指向数组 的指针变量

二、实验环境:

    PC + Windows +Visual C++6.0  

三、实验内容:

(主要内容的文字及贴图)

实验内容:

  1.  输入3个整数,按从小到大的顺序输出,然后将程序改为输入3个字符串,按由小到大输出(习题8。1和习题8.2)

第一种方法:

#include <stdio.h>

int main()

{

int *p1,*p2,*p3,*p,a,b,c;

printf("please enter three integer numbers:");

scanf("%d%d%d",&a,&b,&c);

p1=&a;

p2=&b;

p3=&c;

if(a>b)

{p=p1;p1=p2;p2=p;}

if(a>c)

{p=p1;p1=p3;p3=p;}

if(b>c)

{p=p2;p2=p3;p3=p;}

printf("a=%d,b=%d,c=%d\n",a,b,c);

printf("%d %d %d\n",*p1,*p2,*p3);

return 0;

}

第二种方法:

#include <stdio.h>

int main()

{

void swap(int *m,int *n);

int a,b,c;

scanf("%d %d %d",&a,&b,&c);

if(a>b)

swap(&a,&b);

if(a>c)

swap(&a,&c);

if(b>c)

swap(&b,&c);

printf("%d %d %d",a,b,c);

}

void swap(int *m,int*n)

{

int t;

t=*m;*m=*n;*n=t;

}

#include <stdio.h>

#include <string.h>

int main()

{

void swap(char *p1,char *p2);

char str1[20],str2[20],str3[30];

printf("please enter three line:\n");

scanf("%s %s %s",str1,str2,str3);

if(strcmp(str1,str2)>0)

swap(str1,str2);

if(strcmp(str1,str3)>0)

swap(str1,str3);

if(strcmp(str2,str3)>0)

swap(str2,str3);

printf("%s %s %s",str1,str2,str3);

return 0;

}

void swap(char *p1,char *p2)

{

char p[20];

strcpy(p,p1);strcpy(p1,p2);strcpy(p2,p);

}

  1. 将n个数按输入时的顺序逆序排列。习题8.13

#include <stdio.h>

int main()

{

void sort(int *p,int m);

int i,a[10],n,*p;

scanf("%d",&n);

for(i=0;i<n;i++)

{

scanf("%d",&a[i]);

}

p=a;

sort(p,n);

for(i=0;i<n;i++)

printf("%d ",a[i]);

return 0;

}

void sort(int*p,int m)

{

int t,i;

for(i=0;i<m/2;i++)

{

t=*(p+i);*(p+i)=*(p+(m-i-1));*(p+(m-i-1))=t;

}

}

3 阅读以下程序 思考程序的运行结果:

4. 阅读以下程序 思考程序的运行结果:

四、心得体会(150字以上)

通过本次实验,熟悉掌握掌握指针和间接访问的概念,会定义和使用指针变量能正确使用数组的指针.

举报

相关推荐

0 条评论