還沒弄完

#include <stdio.h>
#include <string.h>
#include <limits.h>    	// 整型數的最大最小值定義在該文件下
#include <float.h>    	// 浮點數數的最大最小值定義在該文件下

/*數據類型*/
typedef int data;
const char* cDateType = "int";
/*數據類型*/

int swap1(data *m, data *n)
{
	if(!m || !n)	// 對輸入參數的合法性進行判斷
	{
		printf("[%s][%s][%d] parameter error\n", __FILE__, __FUNCTION__, __LINE__);
		return -1;
	}
	data t;			// 引用臨時變量存儲
	t = *m;
	*m = *n;
	*n = t;
	return 0;
}

int swap2(data *m, data *n)
{
	if(!m || !n)	// 對輸入參數的合法性進行判斷
	{
		printf("[%s][%s][%d] parameter error\n", __FILE__, __FUNCTION__, __LINE__);
		return -1;
	}
	if((unsigned long)(*m + *n) > DataMax(cDateType))	// 是否越界
	{
		printf("[%s][%s][%d] parameter error\n", __FILE__, __FUNCTION__, __LINE__);
		return -1;
	}
	data t = *m + *n;
	*m = t - *n;
	*n = t - *m;
	return 0;
}

unsigned long DataMax(const char* type)
{
	unsigned long max;
	printf("%d\n", __LINE__);
	if(!type)
	{
		printf("[%s][%s][%d] parameter error\n", __FILE__, __FUNCTION__, __LINE__);
		return -1;
	}

	// 判斷數據類型
	if(strcasecmp("char", type) == 0)	// windows是stricmp,linux是strcasecmp.不區分大小寫比較字符串
	{
		max = CHAR_MAX;
	}
	else if(strcasecmp("unsigned char", type) == 0)
	{
		max = CHAR_MAX;
	}
	else if(strcasecmp("short", type) == 0)
	{
		max = SHRT_MAX;
	}
	else if(strcasecmp("unsigned short", type) == 0)
	{
		max = USHRT_MAX;
	}
	else if(strcasecmp("int", type) == 0)
	{
		max = INT_MAX;
	}
	else if(strcasecmp("long", type) == 0)
	{
		max = LONG_MAX;
	}
	else if(strcasecmp("unsigned long", type) == 0)
	{
		max = ULONG_MAX;
	}
	else	// 懶,剩下的不想寫了,最小值一個道理,只是需要注意下需要用合適的數據保存值
	{
		printf("[%s][%s][%d] parameter error\n", __FILE__, __FUNCTION__, __LINE__);
		return -1;
	}
	return max;
}

// 主函數入口
int main(int argc, char const *argv[])
{
	data a = 8;
	data b = 50;
	printf("%d %d\n", a, b);
	
	swap1(&a, &b);
	printf("%d %d\n", a, b);

	swap2(&a, &b);
	printf("%d %d\n", a, b);


	printf("int max:%lu\n", DataMax(cDateType));
	
	
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章