当前位置:文档之家› 实验九实验报告

实验九实验报告

Press any key to continue.
input three line:
I study very hard.
C language is very interesting.
He is a professfor.
Now,the order is:
C language is very interesting.
printf("Now,the order is:\n");
printf("%s\n%s\n%s\n",str1,str2,str3);
}
void swap(char*p1,char*p2)
{char p[20];
strcpy(p,p1);strcpy(p1,p2);strcpy(p2,p);
}
(2). #include <stdio.h>
两个字符串s1,s2由main函数输入,strcmp函数的返回值也由main函数输出.
三.实验步骤
(1).#include <stdio.h>
void main()
{void swap (int *p1,int *p2);
int n1,n2,n3;
int *p1,*p2,*p3;
printf ("input three integer n1,n2,n3:");
{ int i;
i=0;
while (*(p1+i)==*(p2+i))
if (*(p1+i++)=='\0') return(0);
return (*(p1+i)-*(p2+i));
}
四.试验结果:
(1)input three integer n1,n2,n3:34,21,25
Now,the order is:21,25,34
move(p);
printf("Now,matrix:\n");
for (i=0;i<3;i++)
printf("%d %d %d\n",a[i][0],a[i][1],a[i][2]);
}
void move(int *pointer)
{int i,j,t;
for (i=0;i<3;i++)
for (j=i;j<3;j++)
(2).将一个3*3的矩阵转置,用一函数实现之。在主函数中用scanf函数输入以下矩阵元素:【1 3 5,7 9 11,13 15 19】将数组名作为函数实参,在执行函数的过程中实现矩阵转置,函数调用结束后在主函数中输出已转置的矩阵。
(4),用一个函数实现几个字符串的比较,即自己写一个strcmp函数,函数原型为:
scanf ("%d,%d,%d",&n1,&n2,&n3);
p1=&n1;p2=&n来自;p3=&n3;
if (n1>n2) swap(p1,p2);
if (n1>n3) swap(p1,p3);
if (n2>n3) swap(p2,p3);
printf("Now,the order is:%d,%d,%d\n",n1,n2,n3);
printf("input two strings:\n");
scanf("%s",str1);
scanf("%s",str2);
p1=&str1[0];
p2=&str2[0];
m=strcmp(p1,p2);
printf("result: %d\n",m);
}
int strcmp(char *p1,char *p2)
一.实验目的
(1).通过实验进一步掌握指针的定义,会定义和使用指针变量。
(2).能正确使用数组的指针,和指向数组的指针变量。
(3).能正确使用字符串的指针,和指向字符串的指针变量。
(4).了解指向指针的指针的概念及其使用方法。
二.实验内容
(1).输入三个整数,按由小到大的顺序输出,然后将程序改为:输入三个字符串,按由小到大的顺序输出。
}
void swap(int *p1,int *p2)
{int p;
p=*p1;*p1=*p2;*p2=p;
},
#include <stdio.h>
#include <string.h>
void main()
{void swap(char*,char*);
char str1[20],str2[20],str3[20];
中国矿业大学计算机学院实验报告
课程名称__c语言_实验名称__实验九指针___
班级______信电10-14班___姓名_____缪斯_学号____04101696_______仪器组号____实验日期___2011-4-25___
实验报告要求:1.实验目的2.实验内容3.实验步骤
4.运行结果5.流程图6.实验体会
void main()
{void move (int *pointer);
int a[3][3],*p,i;
printf ("input matrix:\n");
for (i=0;i<3;i++)
scanf("%d %d %d",&a[i][0],&a[i][1],&a[i][2]);
p=&a[0][0];
{t=*(pointer+3*i+j);
*(pointer+3*i+j)=*(pointer+3*j+i);
*(pointer+3*j+i)=t;
}
}
(4). #include <stdio.h>
void main()
{int strcmp(char *p1,char *p2);
int m;
char str1[20],str2[20],*p1,*p2;
result: -32
Press any key to continue
input two strings:
hellow!
hellow!
result: 0
Press any key to continue
六.实验体会:
通过一节课的学习,基本掌握了关于指针的知识点,能够简单的编写一些运用到指针的程序。
He is a professfor.
I study very hard.
(2). input matrix:
1 2 3
4 5 6
7 8 9
Now,matrix:
1 4 7
2 5 8
3 6 9
Press any key to continue
(4). input two strings:
CHINA
Chen
In strcmp(char *p1,char *p2);
设p1指向字符串s1,p2指向字符串s2,要求当s1=s2时,函数返回值为零;如果s1≠s2,返回它们二者第一个不相同字符的ASCII码值(如“BOY”与“BAD”,第一个字母不相同,“O”与“A”之差为79-65=14);如果s1>s2,则输出正值;如s1<s2则输出负值。
printf("input three line:\n");
gets(str1);
gets(str2);
gets(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);
教师评价



及格
不及格
教师签名
日期
相关主题