C语言的综合例题

查找数组值的函数

typedef int elemtype;
typedef enum
{
	nullpoint = 1,
	outofrance = 2,
	success = 3,
	fault = 4
}tagtype;
typedef struct
{
	tagtype utype;
	union{
		int index;
		elemtype value;  //可以节约4字节空间,同时typedef的应用可以在需要时转换为别的类型
	};
}status;
status findvalue(const int *br, const int n, const int val)
{
	status res = { fault, -1 };
	if (NULL == br)
	{
		res.utype = nullpoint; 
		return res;
	}
	if (n < 1)
	{
		res.utype = outofrance;
		return res;
	}
	for (int i = 0; i < n; ++i)
	{
		if (val == br[i])
		{
			res.utype = success;
		    res.index = i;
		    break;
		}	
	}
	return res;
}
status getitem(const int *ar,int n,int index)
{
	status res = { fault };
	if (NULL == ar)
	{
		res.utype = nullpoint;
		return res;
	}
	if (index < 0||index>=n)
	{
		res.utype = outofrance;
		return res;
	}
	res.value = ar[index];
	return res;
}
void main()
{
	while (1)
	{
		system("cls");
		int ar[] = { 12, 34, 56, 78, 23, 45, 67, 89, 100, 80 };
		int n = sizeof(ar) / sizeof(ar[0]);
		int val;
		cin >> val;
		int index;
		cin >> index;
		cout << getitem(ar, n, index).value;
		cout << "退出 n" << endl;
		char x;
		cin >> x;
		if (x == 'n')
			break;
	}
}

my_meset()

void my_meset(void *vp, int c, int n,int x)
{
	if (NULL == vp || n < 1)return;
	char *p = (char*)vp;
	for (int i = 0; i < n; i++)
	{
		*p = c;
		p++;
	}

}
void main()
{
	char str[10];
	int ar[10];
	double dr[10];
	my_meset(str, 10, sizeof(str),sizeof(str[0]));
	cout << str[0]<<endl;
	my_meset(ar, 10, sizeof(ar), sizeof(ar[0]));
	my_meset(dr, 10, sizeof(dr), sizeof(dr[0]));

}

写一个my_memcpy

struct node
{
	int a;
	char b;
};
#defne DEBUG//这是已定义状态,会进行if判空
enum status {point1error,point2error};
void *my_memcpy(const void *p1, void *p2,  unsigned int n)//参数表的类型应该为无类型,n表示字节数这样即使在调用结构体或者数组是依旧可以使用,另外之所以在P1前加const是为了避免当复制对象是常量时,指针只能为常指针才能成功调用
{                                                         //另外之所以返回时是一个泛型指针,为的是个strcpy一样可以多次复制更加方便
#ifdef DEBUG
	if ((p1 == NULL) || (p2 == NULL))  //判空是十分重要的
	{
		return NULL;
	}
#endif
	char *p3 = (char*)p1;//之所以强转是为了达到每一个字节都复制的效果
	char *p4 = (char*)p2;//这里就是为了实现链式表达的作用从而达到多是使用
	while (n--)
	{
		*p4++ = *p3++;
	}
	return p2;
}
void main()
{
	node a = { 3, 'm' };
	node b;
	node c;
	my_memcpy(&a, &b, sizeof(node));
	my_memcpy(my_memcpy(&a, &b, sizeof(node)), &c, sizeof(node));
}


//上面的#define DEBUG
#ifdef DEBUG
#endif 
这是另一种宏定义的方式,当我们给DEBUG进行#define的宏定义时那么程序中的判断指针是否为NULL就会起作用,若不进行定义则在编译的过程中会把判空函数给自动取消编译。
也可以这么操作
#ifdef  YHP(宏名)                     #ifndef  YHP(和前面的功能相反)后面是未定义宏名就进行之下内容
。。。                                  。。。
。。。                                  。。。
#else                                   #else
。。。                                  。。。
。。。
#endif                                  #endif  


还有一种做法
#if 0                          #if 1      //这两种情况和上面的#ifdef不同在使用这种方式是宏名要有宏值  1或0,而上面的那种的执行和宏值无关
.。。                          。。。
#else                          #else
。。                           。。。
#endif                         #endif

除了这种手写的通过宏定义的方式来确定是否判空之外还可以使用系统自带的函数assert

#include<assert.h>
assert(p1!=NULL||p2!=NULL);

memove和memcpy他们的作用是一样的,唯一的区别是,当内存发生局部重叠的时候,memmove保证拷贝的结果是正确的,memcpy不保证拷贝的结果的正确。

猜你喜欢

转载自blog.csdn.net/qq_40738945/article/details/84678396