栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

南京邮电大学高级语言程序设计实验五(指针与字符串实验)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

南京邮电大学高级语言程序设计实验五(指针与字符串实验)

实验题目(1)【见实验教材实验六的题目2】:编程exp6_2.c,现有整型变量x,y,调用交换函数以期实现两个值的交换。下表中4种不同的定义及调用分别进行测试并填写表格。
表1 拟实现交换的四种方法
原型声明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);  
	printf("x=%d,y=%dn",x,y);
	return 0;
}

通过修改以上主函数中调用函数的语句,按表1更新对应的函数调用,分别运行程序,填写下表:

函数原型输出结果是否交换原因分析
void swap1( int a, int b );x=1, y=2x, y均为值传递
void swap2( int *a, int b );x=2, y=2x为地址传递,y为值传递
void swap3( int *a, int *b );x=2, y=1x , y均为地址传递
void swap4(int *a, int *b );x=1, y=2x为值传递, y为地址传递

② 利用F10和F11功能键进行单步跟踪,4次运行观察各变量的变化情况,填写下表:

跟踪点(黄色箭头所指行)实参x的值实参y的值跟踪点(黄色箭头所指行)形参a(或*a)的值形参b(或*b)的值
swap1调用行12swap1函数左大括号处a的值: 1b的值: 2
swap1后的printf行12swap1函数右大括号处a的值: 2b的值: 1
swap2调用行12swap2函数左大括号处*a的值: 1b的值: 2
swap2后的printf行22swap2函数右大括号处*a的值: 2b的值: 1
swap3调用行12swap3函数左大号处*a的值: 1*b的值: 2
swap3后的printf行21swap3函数右大括号处*a的值: 2*b的值: 1
swap4调用行12swap4函数左大括号处*a的值: 1*b的值: 2
swap4后的printf行12swap4函数右大括号处*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
#include
#define N 100

int reverse(int* b, int n)//逆置数组
{
	int t, * p = &b[n - 1];
	while (b < p)
	{
		t = *b;
		*b++ = *p;
		*p-- = t;
}
	return *b;
}

void getArr(int str[], int* p)//键入数组
{
	printf("Please enter the number of elements:n");
	scanf_s("%d", p);
	printf("Please enter an array of %d elements:n", *p);
	for (int i = 0; i < *p; i++)
	{
		scanf_s("%d", &str[i]);
	}
}

void printArr(int str[], int n)//打印逆置后的数组
{
	printf("After inversion:");
	for (int i = 0; i < n; i++)
	{
		printf(" %d", str[i]);
	}
	printf("n");
}

int main()
{
	int str[N], n;
	getArr(str, &n);
	reverse(str, n);
	printArr(str, n);
	return 0;
}

运行结果

Please enter the number of elements:
5
Please enter an array of 5 elements:
  1  4 5 3 8  
After inversion: 8 3 5 4 1

实验题目(3)【见实验教材实验七的题目3】:编写程序exp7_3.c,从键盘读入一个字符串,去掉其中所有的空格得到一个新串后再输出(只能定义一个字符数组,不可以定义两个字符数组,可以根据编程需要定义字符指针以及其他变量)。
#include 
#include 
#define N 100

//删除字符串所有空格
void delSpace(char* str)
{
	int i = 0;
	while ('' != str[i])
	{
		if (' ' == str[i])
		{
			for (int j = i; j < strlen(str); j++)
			{
				str[j] = str[j + 1];
			}
			continue;
		}
		i++;
	}
}

int main()
{
	char str[N];
	printf("Please enter a string with spaces:");
	gets_s(str);
	delSpace(str);
	printf("After deleting spaces:%sn", str);
	return 0;
}

测试用例要求输入的原始串输出结果串
串中空格每处只有一个A bcd 12 45 tAbcd1245t
至少有一处有连续多个空格字符A bc 12 tAbc12t
字符串最前面是4个空格A b 12 tAb12t
你自己设计的测试用例12 323 d ddd2 %h否12323dddd2%h否
实验题目(4)【见实验教材实验七的题目2】:: 编写程序exp7_2.c,帮助小红从小明通过网络发送的两串字符串中,提取用户名和密码。
#include
#include
#define N 100

void getID(char* str1)//获取用户名
{
	char str3[N] = "0";
	int i, j = 0;
	for (i = 0; i < strlen(str1); i++)
		if ((str1[i] >= 'a' && str1[i] <= 'z') || (str1[i] >= 'A' && str1[i] <= 'Z'))
		{
			str3[j] = str1[i];
			j++;
		}
	strcpy(str1, str3);
}

void getPassword(char* str2)//获取密码
{
	char str4[N] = "0";
	int i, j = 0;
	for (i = 0; i < strlen(str2); i++)
		if (str2[i] >= '0' && str2[i] <= '9')
		{
			str4[j] = str2[i];
			j++;
		}
	strcpy(str2, str4);
}

void printID(char* str1)//打印ID
{
	printf("The id is:");
	for (int i = 0; i < strlen(str1); i += 2)//提取奇数项
	{
		printf("%c", str1[i]);
	}
	printf("n");
}

void printPassword(char* str2)//打印密码
{
	printf("The password is:");
	for (int i = 1; i < strlen(str2); i += 2)//提取偶数项
	{
		printf("%c", str2[i]);
	}
	printf("n");
}

int main()
{
	char str1[N], str2[N];
	printf("Please enter the first string with some letters:");
	gets_s(str1);
	printf("Please enter a second string with some numbers:");
	gets_s(str2);
	getID(str1);
	getPassword(str2);
	printID(str1);
	printPassword(str2);
	return 0;
}

运行结果

Please enter the first string with some letters: 1h2h1212hhh2gwg221
Please enter a second string with some numbers:12f2g3f1f12hg37gf4hw
The id is:hhhw
The password is:23134
请按任意键继续. . .

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/886774.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号