第九周c++作业(第六章)之例题

例题1:

/***************************************************
****    6_1通过本例来理解指针的几种表示形式    ****
****************************************************/

# include<iostream>
using namespace std;

int main()
{
	int a = 10;
	int *p;
	p = &a;
	cout<<"a = "<<a<<endl;
	cout<<"p = "<<p<<endl;
	cout<<"&a = "<<&a<<endl;
	cout<<"*p = "<<*p<<endl;
	cout<<"&p = "<<&p<<endl;
	cout<<endl;
	*p = 15;
	cout<<"a = "<<a<<endl;
	cout<<"p = "<<p<<endl;
	cout<<"&a = "<<&a<<endl;
	cout<<"*p = "<<*p<<endl;
	cout<<"&p = "<<&p<<endl;


	return 0;
}

例题2:

/*************************************************
****    6_2输入两个整数由大到小输出    ****
**************************************************/

# include<iostream>
using namespace std;

int main()
{
	int a, b, temp;
	int *p = &a, *q = &b;
	cout<<"please input a and b:";
	cin>>a>>b;
	cout<<"初始的两个数为:"<<a<<"   "<<b<<endl;
	if(a < b)                   //如果a小于b,则交换*p和*q,实质上交换的是a和b的值
	{
		temp = *p; *p = *q; *q = temp;
	}
	cout<<"从大到小排序后的数为:"<<*p<<"   "<<*q<<endl;

	return 0;
}

例题3:

/***********************************************************
****    6_3将两个整数按由大到小的顺序输出_2    ****
************************************************************/
# include<iostream>
using namespace std;

int main()
{
	int a, b, temp;
	int *p = &a, *q = &b;
	cout<<"please input a and b:";
	cin>>a>>b;
	cout<<"初始两个数为:"<<a<<"  "<<b<<endl;
	if(a < b)
	{
		temp = *p; *p = *q; *q = temp;
	}
	cout<<"从大到小排序后的数为:"<<*p<<"  "<<*q<<endl;

	return 0;
}


例题4:

/****************************************************
****    6_4指针的关系运算    **** 
*****************************************************/

# include<iostream>
using namespace std;

int main()
{
	int a = 10, b = 10, *ptr1, *ptr2;
	ptr1 = &a;
	ptr2 = &b;
	cout<<boolalpha<<(*ptr1 == *ptr2)<<endl;
	//上述语句实质上比较的是变量a和b的值是否相等
	cout<<boolalpha<<(ptr1 == ptr2)<<endl;
	//上述语句比较指针变量ptr1和ptr2的值是否相等

	return 0;
}


例题5:

/*****************************************************************
****    6_5使用指针处理数组输出数组全部元素的三种方法    ****
******************************************************************/

# include<iostream>
# include<iomanip>
using namespace std;

//方法一:下标法
int main()
{
	int a[10];
	for(int i = 0; i < 10; i++)        //为数组元素赋值
		a[i] = 2 * (i + 1);
	for(i = 0; i < 10; i++)            //输出数组元素
		cout<<setw(4)<<a[i];
	cout<<endl;

	return 0;
}

//方法二,通过数组名计算数组元素地址,找出元素的值
...
for(i = 0; i < 10; i++)            //为数组元素赋值
	*(a + i) = 2 * (i + 1);
for(i = 0; i < 10; i++)            //输出数组元素
	cout<<setw(4)<<*(a + i);
...

//方法三,用指针变量指向数组元素
...
for(p = a; p < a + 10; p++)        //为数组元素赋值
	*p = 2 * (i + 1);
for(p = a; p < a + 10; p++)         //输出数组元素
    cout<<setw(4)<<*p;
...

例题6:

/*****************************************************************
****    6_6使用字符指针变量实现将字符串a赋值到字符串b中    ****
******************************************************************/

# include<iostream>
using namespace std;

int main()
{
	char a[] = "Hello world", b[20], *p, *q;
	p = a, q = b;
	for(; *p != '\0'; p++, q++)
		*q = *p;
	*q = '\0';
	cout<<"stringl is:";
	cout<<a<<endl;
	cout<<"sting2 is:";
	cout<<b<<endl;

	return 0;
}

例题7:

/********************************************************
****    6_7通过改变指针变量的值输出下面的字符串    ****
*********************************************************/

# include<iostream>
using namespace std;

int main()
{
	char *p = "student";
	for(; *p != '\0'; p++)
		cout<<p<<endl;

	return 0;
}

例题8:

/**********************************************************
****    6_8用下标形式引用字符串中的字符    ****
***********************************************************/

# include<iostream>
using namespace std;

int main()
{
	char *p = "student";
	for(int i = 0; p[i] != '\0'; i++)
		cout<<p[i];
	cout<<endl;

	return 0;
}

例题9:

/********************************************************
****    6_9利用指针相减的运算,计算字符串的长度    ****
*********************************************************/

# include<iostream>
using namespace std;

int main()
{
	char s[50];
	char *p;
	cout<<"please input a string:";
	cin.get(s, 50);
	p = s;           //p储存的是数组s的首个元素地址
	for( ; *p != '\0'; )
		p++;
	cout<<"the length is: "<<p-s<<endl;

	return 0;
}


 例题10:

/*********************************************
****    6_10多级指针举例    ****
**********************************************/

# include<iostream>
using namespace std;

int main()
{
	int i = 5, * p, ** pp;
	p = &i;
	pp = &p;
	cout<<&i<<'\t'<<i<<endl;
	cout<<&p<<'\t'<<p<<'\t'<<*p<<endl;
	cout<<&pp<<'\t'<<pp<<'\t'<<*pp<<'\t'<<**pp<<endl;

	return 0;
}

例题11:

/*******************************************
****    6_11指针数组举例    ****
********************************************/

# include<iostream>
using namespace std;

int main()
{
	int a[5] = {1, 2, 3, 4, 5};
	int * p[5] = {&a[0], &a[1], &a[2], &a[3], &a[4]};
	for(int i = 0; i < 5; i++)
		cout<<* p[i]<<'\t';
	cout<<endl;

	return 0;
}

例题12:

/*****************************************************
****    6_12利用字符指针数组处理多个字符串    ****
******************************************************/

# include<iostream>
using namespace std;

int main()
{
	char a[] = "computer system", b[] = "hardware", c[] = "software";
	char * p[4];
	p[0] = a;
	p[1] = b;
	p[2] = c;
	p[3] = NULL;
	for(int i = 0; p[i] != NULL; i++)
		cout<<p[i]<<endl;

	return 0;
}

例题13:

/************************************************
****    6_13用字符串初始化字符指针数组    ****
*************************************************/

# include<iostream>
using namespace std;

int main()
{
	char * weekname[] = {"Sunday", "Monday", "Tuesday", "wednesday", "Thursday", "Fraday", "Saturday"};
	int i;
	while(1)
	{
		cout<<"please input week No.:";
		cin>>i;
		if(i < 0 || i > 6)
			break;
		cout<<"week N0."<<i<<"-->"<<weekname[i]<<endl;
	}

	return 0;
}

例题14:

/*************************************************************
****    6_14将若干字符串按英文字典排序(由小到大)    ****
**************************************************************/

# include<iostream>
using namespace std;

int main()
{
	char * p[] = {"Rebacca", "Heaven", "Michael", "Lingda Tsai"};      //定义字符指针数组
	char * temp;
	int i, j, n = 4;
	for(i = 0; i < n - 1; i++)     //冒泡排序法
	for(j = 0; j < n - 1; j++)
		if(strcmp(p[j], p[j+1]) > 0)
		{
			temp = p[j]; p[j] = p[j+1]; p[j+1] = temp;
		}
	for(i = 0; i < n; i++)
		cout<<p[i]<<endl;

	return 0;
}

例题15:

/******************************************
****    6_15用指针数组处理二维数组    ****
*******************************************/

# include<iostream>
using namespace std;

int main()
{
	int a[2][3], *p[2];
	int i, j;
	p[0] = a[0];
	p[1] = a[1];
	for(i = 0; i < 2; i++)          //为二维数组元素赋值
	for(j = 0; j < 3; j++)
		a[i][j] = j + i;
	for(i = 0; i < 2; i++)
	for(j = 0; j < 3; j++)
	{
		cout<<"a["<<i<<"][" <<j<<"]:";
		cout<<*(p[i] + j)<<endl;
	}

	return 0;
}

例题16:

/**************************************************
****    利用二级指针处理二维数组    ****
***************************************************/

# include<iostream>
using namespace std;

int main()
{
	int a[2][3], * p[2], **pp;
	int i, j;
	p[0] = &a[0][0];
	p[1] = &a[1][0];
	pp = p;
	for(i = 0; i < 2; i++)             //为数组元素赋值
	for(j = 0; j < 3; j++)
		pp[i][j] = j + i;
	for(i = 0; i < 2; i++)              //输出数组元素
	for(j = 0; j < 3; j++)
	{
		cout<<"a["<<i<<"]["<<j<<"]";
		cout<<*(*(pp+i)+j)<<endl;
	}

	return 0;
}

例题17:

/*****************************************************
****    6_17用二级指针处理多个字符串    ****
******************************************************/

# include<iostream>
using namespace std;

int main()
{
	int i = 0;
	char * a[] = {"Rebacca", "Heaven", "Michael", "Michael Chang", NULL};
	char ** pp;
	pp = a;
	while(* pp != NULL)
	{
		cout<<*pp++<<endl;
		i++;
	}
	cout<<i<<endl;

	return 0;
}

例题18:

/************************************************
****    6_18利用数组指针处理二维数组    ****
*************************************************/

# include<iostream>
# include<iomanip>
using namespace std;

int main()
{
	int a[2][3] = {1, 2, 3, 4, 5, 6};
	int (* p)[3];
	int i, j;
	p = a;
	for(i = 0; i < 2; i++)
	{
		for(j = 0; j < 3; j++)
			cout<<setw(3)<<( *p)[j];
		p++;
	}
	cout<<endl;

	return 0;
}

例题19:

/**************************************************
****    6_19指针作为函数参数的示例    ****
***************************************************/

# include<iostream>
using namespace std;

void display(int *, int *);

int main()
{
	int a = 5, b = 10;
	int * pa = &a;               //将变量a的地址赋给指针pa,从而使指针pa指向变量a
	int * pb = &b;
	display(pa, pb);

	return 0;
}
void display(int * p1, int * p2)
{
	cout<<"parameter 1 is "<<* p1<<endl;
	cout<<"parameter 2 is "<<* p2<<endl;
}

例题20:

/***********************************************************************
****    6_20将键盘输入的一个大写字母转换为小写字母并显示出来    ****
************************************************************************/

# include<iostream>
using namespace std;

void lower(char * c);

int main()
{
	char ch;
	cout<<"input a uppercase character ";
	cin>>ch;
	lower(&ch);
	cout<<"converted character: ";
	cout<<ch<<endl;


	return 0;
}

void lower(char * pch)
{
	if(* pch >= 'A' && * pch <= 'Z')
		* pch += 'a' - 'A';    //将大写字母转换为小写字母,等价于* pch += 32;
}

例题22:

/******************************************************
****    6_22输入一组数,求最小值并输出    ****
*******************************************************/

# include<iostream>
using namespace std;

void input(int * s, int n);
int min(int * s, int n);

int main()
{
	int a[10], small;
	input(a, 10);
	small = min(a, 10);
	cout<<"the minimm is: "<<small<<endl;

	return 0;
}

void input(int * s, int n)          //输入数组元素
{
	cout<<"please input "<<n<<"inergers: "<<endl;
	for(int i = 0; i < n; i++)
		cin>>s[i];
}

int min(int * s, int n)          //求数组中的最小元素值
{
	int min, i;
	min = *s;
	for(i = 1; i < n; i++)
		if(s[i] < min)
			min = s[i];
	return min;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值