当前位置: 首页 > news >正文

c++ code:(4)pointer

/*
下面程序片段的输出结果是 Hello 

*/
#include <iostream>
using namespace std;
int main() {
	char s[] = "Hello";  
	char * p;
	for( p = s; p[0]; ++p	)
		cout << * p ;	
	return 0;
}

*****************************************************************************************************************************

 

/*知识点是函数指针*/
#include <iostream>
using namespace std;

void ForEach(void * a, int width, int num,void (*f)(void *))
{
	for (int i = 0; i < num; ++i)
		f((char*)a + width*i);
}

void PrintSquare(void * p)
{
	int * q = (int*)p;  // 类型强转
	int n = *q;
	cout << n * n << ",";
}
void PrintChar(void * p) {
	char * q = (char*)p;
	cout << *q << ",";
}
int main()
{
	int a[5] = { 1,2,3,4,5 };
	char s[] = "hello!";
	ForEach(a, sizeof(int), 5, PrintSquare);
	cout << endl;
	ForEach(s, sizeof(char), 6, PrintChar);
	return 0;
}

&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& 

 

#include <iostream>
using namespace std;
void Memcpy(char * src, char * dest, int n)
{
	for (int j = 0; j <=n; j++)
		dest[j] = src[j];
}
int Strlen(char * s)
{
	int i;
	for (i = 0; s[i]; ++i);
	return i;
}
int main()
{
	freopen("f:\\freopen.txt", "r", stdin);
	int a;
	char s1[30];
	char s2[30];
	int t;
	cin >> t;
	for (int i = 0; i < t; ++i) {
		cin >> a;
		int b = 99999999;
		Memcpy((char*)&a, (char *)&b, sizeof(int));
		cout << b << endl;
	}
	for (int i = 0; i < t; ++i) {
		cin >> s1;
		Memcpy(s1, s2, Strlen(s1) + 1);
		cout << s2 << endl;
	}
	return 0;
}
/*
Memcpy practice
输入样例:

10
15 25 35 45 55 65 75 85 95 105

输出样例:
15,25,35,45,55,65,75,85,95,105,
1,2,3,4,5,1,2,3,4,5,
123434567
167896789
 
*/
#include <iostream>
using namespace std;
void Memcpy( void * src, void * dest, int size)
{
//your code starts here
	
	char * csrc = (char * ) src;
	char * cdest = (char * ) dest;
	if( src == dest )
		return;
	if( cdest > csrc && cdest < csrc + size) {
		//从后往前拷贝 
		for(int i = size-1; i >= 0; -- i)
			cdest[i] = csrc[i];
	}
	else {
		//从前往后拷贝
		for(int i = 0;i < size; ++i)
			cdest[i] = csrc[i]; 
	}
	
//your code ends here	
}

void Print(int * p,int size)
{
	for(int i = 0;i < size; ++i)
		cout << p[i] << ",";
	cout << endl;
}

int main()
{
	int a[10];
	int n;
	cin >> n;
	for(int i = 0;i < n; ++i)
		cin >> a[i];
	int b[10] = {0};
	Memcpy(a,b,sizeof(a));
	Print(b,n);
	
	int c[10] = {1,2,3,4,5,6,7,8,9,10};
	Memcpy(c,c+5,5*sizeof(int)); //将c的前一半拷贝到后一半 
	Print(c,10);

	char s[10] = "123456789";
	Memcpy(s+2,s+4,5); //将s[2]开始的5个字符拷贝到s[4]开始的地方 
	cout << s << endl;
	
	char s1[10] = "123456789";
	Memcpy(s1+5,s1+1,4); //将s1[5]开始的4个字符拷贝到s1[1]开始的地方 
	cout << s1 << endl;
	
	return 0;
}

 

 ()()()()()()()()()()()()()()()()()()()()()()()

 

#include <iostream>
using namespace std;

void Double(int * p, int n)
{
	for (int i = 0; i < n; ++i)
		p[i] *= 2;
}


int main()
{
	int a[3][4] = { { 1,2,3,4 },{ 5,6,7,8 },
	{ 9,10,11,12 } };

	Double(&(a[1][0]),8);

	for (int i = 0; i < 3; ++i) {
		for (int j = 0; j < 4; ++j)
			cout << a[i][j] << ",";
		cout << endl;
	}

	return 0;
}

 

/*
编写一个 MyMax函数,可以用来求任何数组中的最大值

使得程序按要求输出

输入数据

第一行是测试数据组数 t
对每组数据:
第一行是整数n (1<=n<=10)
第2行是 n个整数
第3行是n个浮点数

输出数据

对每组数据:

先输出n个整数中个位数最大的数(答案保证唯一) 
再输出n个整数中最大的数
再输出n个浮点数中最大的数

输入样例:
2
5
31 20 100 7 8
30.1 100.2 2.5 9.8 48.4
2
1 2
0.1 0.2

输出样例 

8
100
100.2
2
2
0.2


*/

#include <iostream>
using namespace std;
//your code starts here
void * MyMax(void * a,int width,int num, 
			int (*compare)(void * p1,void * p2))
{
	void * result = a;
	for(int i = 1;i < num; ++i) {
		if( compare( result, ((char *) a ) + i * width) < 0)
			result = ((char *) a ) + i * width;
	}
	return result;
}
//your code ends here

int Compare1(void * n1,void * n2)
{
	int * p1 = (int * )n1;
	int * p2 = (int * )n2;
	return ((*p1)%10) - ((*p2)%10);
}
int Compare2(void * n1,void * n2)
{
	int * p1 = (int * )n1;
	int * p2 = (int * )n2;
	return *p1 - *p2;
}
#define eps 1e-6
int	Compare3(void * n1,void * n2)
{
	float * p1 = (float * )n1;
	float * p2 = (float * )n2;
	if( * p1 - * p2 > eps)
		return 1;
	else if(* p2 - * p1 > eps)
		return -1;
	else
		return 0; 
}

int main()
{
	int t;
	int a[10];
	float d[10];
	cin >> t;
	while(t--) {
		int n;
		cin >> n;
		for(int i = 0;i < n; ++i)
			cin >> a[i];
		for(int i = 0;i < n; ++i)
			cin >> d[i];
		int * p = (int *) MyMax(a,sizeof(int),n,Compare1);
		cout << * p << endl;
		p = (int *) MyMax(a,sizeof(int),n,Compare2);
		cout << * p << endl;
		float * pd = (float * )MyMax(d,sizeof(float),n,Compare3);
		cout << * pd << endl;
	}
	return 0;
}

 

//指向指针的指针
#include <iostream>
using namespace std;
int main()
{
	int x, y, z;
	x = 10;
	y = 20;
	z = 30;

	int * a[3] = { &x, &y,&z };
	for ( int ** p=&a[0];p < a + 3; ++p)
		cout << *(*p) << endl;
	return 0;

}

 

/*
填写内存交换函数 SwapMemory,使得程序输出结果是: 


10,20,30,40,50,
1,2,3,4,5,
abcde
12345

 
 
*/
#include <iostream>
using namespace std;
void SwapMemory(void * m1,void * m2, int size)
{
//your code starts here
	char * p1 = (char *)m1;
	char * p2 = (char *)m2;
	for(int i = 0;i < size; ++i) {
		char tmp =  p1[i];
		p1[i] = p2[i];
		p2[i] = tmp;
	}
//your code ends here	
}

void PrintIntArray(int * a,int n)
{
	for(int i = 0;i < n; ++i)
		cout << a[i] << ",";
	cout << endl;
}

int main()
{
	int a[5] = {1,2,3,4,5};
	int b[5] = {10,20,30,40,50};
	SwapMemory(a,b,5 * sizeof(int));
	PrintIntArray(a,5);
	PrintIntArray(b,5);
	char s1[] = "12345";
	char s2[] = "abcde";
	SwapMemory(s1,s2,5);
	cout << s1 << endl;
	cout << s2 << endl;
	return 0;
}

 

相关文章:

  • c++ code:(5)sort
  • code 练习之路
  • 初见kopernio
  • c++ code:(7)basics2
  • c++ code:(10)operator overload
  • c++ code:(11)inheritance
  • c++ code:(12)polymorphism
  • c++ code:(13)io and template
  • 英文论文写作素材
  • 王梓坤院士的学术思想
  • 使用EI、SCI搜索 (使用 websci中的endnote online引用 )
  • 玩转word 和 mathtype 的结合
  • paper 格式
  • 计算思维
  • 程序中的一些思想
  • ----------
  • 《网管员必读——网络组建》(第2版)电子课件下载
  • angular学习第一篇-----环境搭建
  • CSS盒模型深入
  • CSS实用技巧干货
  • es6(二):字符串的扩展
  • JavaScript学习总结——原型
  • Java反射-动态类加载和重新加载
  • js正则,这点儿就够用了
  • Node.js 新计划:使用 V8 snapshot 将启动速度提升 8 倍
  • nodejs实现webservice问题总结
  • October CMS - 快速入门 9 Images And Galleries
  • PaddlePaddle-GitHub的正确打开姿势
  • 闭包--闭包作用之保存(一)
  • 动手做个聊天室,前端工程师百无聊赖的人生
  • 基于web的全景—— Pannellum小试
  • 如何打造100亿SDK累计覆盖量的大数据系统
  • 使用 5W1H 写出高可读的 Git Commit Message
  • 说说动画卡顿的解决方案
  • 微信小程序实战练习(仿五洲到家微信版)
  • 项目管理碎碎念系列之一:干系人管理
  • 新海诚画集[秒速5センチメートル:樱花抄·春]
  • ​决定德拉瓦州地区版图的关键历史事件
  • ​如何使用ArcGIS Pro制作渐变河流效果
  • ​业务双活的数据切换思路设计(下)
  • #pragma once与条件编译
  • (3)Dubbo启动时qos-server can not bind localhost22222错误解决
  • (二)Eureka服务搭建,服务注册,服务发现
  • (二)斐波那契Fabonacci函数
  • (每日持续更新)信息系统项目管理(第四版)(高级项目管理)考试重点整理第3章 信息系统治理(一)
  • (学习日记)2024.03.25:UCOSIII第二十二节:系统启动流程详解
  • (一)基于IDEA的JAVA基础1
  • (原創) 系統分析和系統設計有什麼差別? (OO)
  • .mat 文件的加载与创建 矩阵变图像? ∈ Matlab 使用笔记
  • .NET Core引入性能分析引导优化
  • .net 设置默认首页
  • .NET/ASP.NETMVC 大型站点架构设计—迁移Model元数据设置项(自定义元数据提供程序)...
  • .NET/C# 使窗口永不激活(No Activate 永不获得焦点)
  • [] 与 [[]], -gt 与 > 的比较
  • [20170728]oracle保留字.txt