实验五:指针与字符串实验
实验题目(1)
【见实验教材实验六的题目2】:编程exp6_2.c,现有整型变量x,y,调用交换函数以期实现两个值的交换。下表中4种不同的定义及调用分别进行测试并填写表格。
原型声明 | void swap1( int , int ); | void swap2( int *, int ); | void swap3( int *, int *); | void swap4( int *, int *); |
---|---|---|---|---|
调用语句 | swap1( x , y ); | swap2( &x , y ); | swap3( &x , &y ); | swap4( &x , &y ); |
函数定义 | void swap1(int a, int b ) { int temp= a; a = b; b = temp;} | void swap2(int *a, int b ) {int temp= *a; *a = b; b = temp; } | void swap3(int *a, int *b ) { int temp = *a; *a = *b; *b = temp; } | void swap4(int *a, int *b ) { int *temp = a; a=b; b=temp;} |
实验解答:
① 主函数代码如下:
int main()
{
int x=1,y=2;
swap1(x,y); /此处依次换成另外3种调用进行观察,其余代码不变/
printf(“x=%d,y=%d\n”,x,y);
return 0;
}
函数原型 | 输出结果 | 是否交换 | 原因分析 |
---|---|---|---|
void swap1( int a, int b ); | x=1,y=2 | 否 | 函数只交换了形参的值,形参在函数调用结束后被销毁 |
void swap2( int *a, int b ); | x=2,y=2 | 否 | 用指针将y的值赋值给了x,但y仍然只在函数内作为形参改变了值 |
void swap3( int *a, int *b ); | x=2,y=1 | 是 | 用指针将x、y原地址的值交换 |
void swap4(int *a, int *b ); | x=1,y=2 | 否 | 被调函数修改的是指针的指向,没有改变指针指向的值 |
② 利用F10和F11功能键进行单步跟踪,4次运行观察各变量的变化情况,填写下表:
跟踪点(黄色箭头所指行) | 实参x的值 | 实参y的值 | 跟踪点(黄色箭头所指行) | 形参a(或*a)的值 | 形参b(或*b)的值 |
---|---|---|---|---|---|
swap1调用行 | 1 | 2 | swap1函数左大括号处 | a的值:1 | b的值:2 |
swap1后的printf行 | 1 | 2 | swap1函数右大括号处 | a的值:2 | b的值:1 |
swap2调用行 | 1 | 2 | swap2函数左大括号处 | *a的值:1 | b的值:2 |
swap2后的printf行 | 2 | 2 | swap2函数右大括号处 | *a的值:2 | b的值:1 |
swap3调用行 | 1 | 2 | swap3函数左大括号处 | *a的值:1 | *b的值:1 |
swap3后的printf行 | 2 | 1 | swap3函数右大括号处 | *a的值:2 | *b的值:2 |
swap4调用行 | 1 | 2 | swap4函数左大括号处 | *a的值:1 | *b的值:2 |
swap4后的printf行 | 1 | 2 | swap4函数右大括号处 | *a的值:2 | *b的值:1 |
实验题目(2)
【见实验教材实验六的题目4】: 编写程序exp6_4.c,实现数组的逆置。如:int a[6]={1,3,4,5,6,7};逆置后int a[6]={7,6,5,4,3,1}
①写出完整的源程序代码并做适当注释:
#include<stdio.h>
#define swap(a,b){int temp=(a);(a)=(b);(b)=temp;}
#define maxn 20
int main()
{
int n,str[maxn];
do
{
scanf("%d",&n);
}while(n<1||n>maxn);
getchar();
for (int i=0;i<n;++i){
scanf("%d",&str[i]);
} //读入数组元素
for (int i=0;i<n/2;++i){
int j=n-i-1;
swap(str[i],str[j]);
}//逆序数组元素
for (int i=0;i<n;++i){
printf("%d ",str[i]);
}//打印逆序后数组
return 0;
}
②运行一次程序,记录程序运行的结果:
4
156 213 324 7
7 324 213 156
实验题目(3)
【见实验教材实验七的题目3】:编写程序exp7_3.c,从键盘读入一个字符串,去掉其中所有的空格得到一个新串后再输出(只能定义一个字符数组,不可以定义两个字符数组,可以根据编程需要定义字符指针以及其他变量)。
快慢指针,这里直接定义两个int变量i,j,移动str[i]和str[j]也可以
#include<stdio.h>
#define maxn 30
int main()
{
char str[maxn];
char *fast,*slow;
gets(str);
fast=slow=str;
while((*fast)!='\0'){
if((*fast)!=' ')
*slow++=*fast++;
else fast++;
}
*slow='\0';
puts(str);
return 0;
}
测试用例要求 | 输入的原始串 | 输出结果串 |
---|---|---|
串中空格每处只有一个 | A bcd 12 45 t | Abcd1245t |
至少有一处有连续多个空格字符 | A bc 12 t | Abc12t |
字符串最前面是4个空格 | A b 12 t | Ab12t |
你自己设计的测试用例 | Sfas 2 o134 df | Sfas2o134df |
实验题目(4)
【见实验教材实验七的题目2】:: 编写程序exp7_2.c,帮助小红从小明通过网络发送的两串字符串中,提取用户名和密码。
①写出完整的源程序代码并做适当注释:
利用getchar在读入字符的同时判断是否符合条件,从而避免定义多个字符数组占用空间
#include<stdio.h>
#define maxn 40
int main()
{
char username[maxn],password[maxn],ch;
ch=getchar();
int i=0,j=0,odd=0,even=0;
while(ch != '\n'){
if(ch>='A'&&ch<='z'){
if((++odd)%2==1)
username[i++]=ch;
}
ch=getchar();
}\\读入用户名字符串并提取
username[i]='\0';
ch=getchar();
while(ch != '\n'){
if(ch>='0'&&ch<='9'){
if((++even)%2==0)
password[j++]=ch;
}
ch=getchar();
}\\读入密码字符串并提取
password[j]='\0';
printf("username:%s\n",username);
printf("password:%s\n",password);
return 0;
}
②记录程序运行结果,观察用户名和密码字符串的具体输出结果:
aduiwqy81334
saio3142ior33
username:auwy
password:123