您的位置:首页 > 数据库 > MySQL

skip-name-resolve参数解决客户端连接mysql服务器巨慢的问题

2012-08-10 09:05 781 查看
      在C语言中,可以使用malloc函数进行动态数组的创建。所谓的动态数组是指可以由用户动态指定数组的大小,而不必一开始就把数组长度定死。

      由于代码是在VS2010中运行的,刚开始保存源文件的时候,保存的是".c"格式,编译(在VS2010中为生成)时老是出错,提示bool标识符错误。查了好久,终于找到了原因,原来VS2010中不支持C99标准,而bool类型是在C99中才有的,所以编译的时候通不过。这种错误只要将源文件的".c"格式改为".cpp"格式就可以编译通过。

      下面是源代码,里面有详细的注释说明:

#include<stdio.h>
#include<malloc.h>		//包含了malloc函数
#include<stdlib.h>		//包含了exit函数
//定义了一个数据类型,该数据类型的名字为struct Array
struct Array
{
int * pBase;		//存储的是数组中第一个元素的地址
int len;			//数组所能容纳的最大元素个数
int cnt;			//当前数组有效元素的个数
};
//函数声明
void initArr(struct Array * pArr, int length);						//初始化
bool appendElement(struct Array * pArr, int value);					//追加元素
bool insertElement(struct Array * pArr, int position, int value);	//插入元素,position从1开始
bool deleteElement(struct Array * pArr, int position, int * pValue);//删除
int getElement(struct Array * pArr, int position);					//获取相应位置元素,positon从1开始
bool isEmpty(struct Array * pArr);									//是否为空
bool isFull(struct Array * pArr);									//是否已满
void sortArr(struct Array * pArr);									//冒泡排序
void showArr(struct Array * pArr);									//显示内容
void inversionArr(struct Array * pArr);								//倒置

int main(void)
{
//定义一个数组类型的变量
struct Array arr;
//删除时,返回删除的元素
int value;
//调用初始化数组的函数
initArr(&arr, 6);
//调用显示数组元素的函数
showArr(&arr);
//调用向数组中添加元素的函数
appendElement(&arr, 56);
appendElement(&arr, 100);
appendElement(&arr, 23);
appendElement(&arr, 12);
appendElement(&arr, 72);
//调用获取指定位置处元素的方法,并输出
printf("第2个元素的值为:%d\n", getElement(&arr, 2));
//调用插入元素的函数
insertElement(&arr, 2, 99);
//调用删除元素的函数
if(deleteElement(&arr, 1, &value))
{
printf("删除成功\n");
printf("您删除的元素为: %d\n", value);
}
else
printf("删除失败!\n");
showArr(&arr);
//调用倒置数组的函数
inversionArr(&arr);
printf("倒置后数组元素为:\n");
showArr(&arr);
//调用冒泡排序的函数
sortArr(&arr);
printf("排序后的数组元素为:\n");
showArr(&arr);
getchar();
return 0;
}
/*
*初始化数组的函数
*/
void initArr(struct Array * pArr, int length)
{
pArr->pBase = (int *)malloc(sizeof(int) * length );
if(NULL == pArr->pBase)				//如果内存分配失败
{
printf("动态内存分配失败!\n");
exit(-1);						//终止整个程序
}
else								//如果内存分配成功,就初始化数组
{
pArr->len = length;
pArr->cnt = 0;
}
return;
}
/*
*输出数组中的所有元素的函数
*/
void showArr(struct Array * pArr)
{
if(isEmpty(pArr))			//如果数组为空
{
printf("数组为空!\n");
}
else						//如果数组不为空
{	int i;
for(i=0;i<pArr->cnt;i++)
printf("%d ", pArr->pBase[i]);
printf("\n");
}
}
/*
*判断数组是否为空的函数
*/
bool isEmpty(struct Array * pArr)
{
//判断元素是否为0
if(0 == pArr->cnt)
return true;
else
return false;
}
/*
*判断数组是否已满的函数
*/
bool isFull(struct Array * pArr)
{
if(pArr->cnt == pArr->len)
return true;
else
return false;
}
/*
*在数组末尾追加元素的函数
*/
bool appendElement(struct Array * pArr, int value)
{
if(isFull(pArr))			//满时返回false
return false;
//不满时追加
pArr->pBase[pArr->cnt] = value;
pArr->cnt++;
return true;
}
/*
*向数组中插入元素的
*/
bool insertElement(struct Array * pArr, int position, int value)
{
if(isFull(pArr))
return false;
if(position < 1 || position > pArr->cnt+1)
return false;
int i;
for(i=pArr->cnt-1;i>=position-1;i--)
{
pArr->pBase[i+1] = pArr->pBase[i];
}
//插入值
pArr->pBase[position-1] = value;
pArr->cnt++;
return true;
}
/*
*删除数组中指定位置处的元素
*/
bool deleteElement(struct Array * pArr, int position, int * pValue)
{
int i;
if(isEmpty(pArr))								//数组为空不能删除
return false;
if(position < 1 || position > pArr->cnt)		//要删除的元素的位置不合法
return false;
//将要删除的元素赋给pValue
*pValue = pArr->pBase[position-1];
//删除元素,并将后面的元素往前移
for(i=position;i<pArr->cnt;i++)
{
pArr->pBase[i-1] = pArr->pBase[i];
}
pArr->cnt--;
return true;
}
/*
*将数组元素倒置的函数
*/
void inversionArr(struct Array * pArr)
{
int i = 0;
int j = pArr->cnt-1;
int t;
while(i < j)
{
t = pArr->pBase[i];
pArr->pBase[i] = pArr->pBase[j];
pArr->pBase[j] = t;
i++;
j--;
}
return;
}
/*
*冒泡排序的函数
*/
void sortArr(struct Array * pArr)
{
int i,j,t;
for(i=0;i<pArr->cnt;i++)
{
for(j=i+1;j<pArr->cnt;j++)
{
if(pArr->pBase[i] > pArr->pBase[j])
{
t = pArr->pBase[i];
pArr->pBase[i] = pArr->pBase[j];
pArr->pBase[j] = t;
}
}
}
}
/*
*获取指定位置元素的函数,positon从1开始
*/
int getElement(struct Array * pArr, int position)
{
return pArr->pBase[position - 1];
}

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐