课堂小记
数据越界的危害
1.越界短 会覆盖其他变量的值
2.越界过长 会出现段错误
char buf1[10]=“123”;
char buf[10]=“abc”;
strcat(char* buf1,char* buf2);
//把字符串buf2追加到buf1的尾部,只有buf1被改变.
buf1和buf2都要以’\0’结尾
strcpy(char* buf1,char* buf2);
//将buf2的内容复制并覆盖到buf1中.没被覆盖的内容保留.
strcpy(char* buf1,char* buf2,int 4);
//将buf2的内容的前4位复制并覆盖到buf1中,遇到’\0’会提前结束.
strcmp(char* buf1,char* buf2);
//从左往右比较字符串的ASCII码,直到\0为止.
strcasecmp(char* buf1,char* buf2);
//不区分大小写的从左往右比较字符串的ASCII码,直到\0为止.
strncmp(char* buf1,char* buf2,int n);
//从左往右比较字符串前n个字符的ASCII码.
strlen(buf1);
//获取字符串buf1的长度,不包括’\0’.
a[],b[]
memset(a,0,sizeof(a));
//将数组a的值全部置0;
memcmp(a,b,n);
//将数组b的前n个元素和数组a比较
memcpy(a,b,n);
//将数组b的前n个元素复制并覆盖数组a的前n个元素
sprintf(char* c," ",xxxxx)
将printf的内容给字符串c.
sscanf(char* c," ",xxxxx)
用字符串c的内容代替输入的内容
char c[]=“abcd=efg”
char s1,s2;
sscanf(c,"%[^=]=%s",s1,s1);
将"abcd"和"efg"拆到s1,s2中.
%[^=]的意思扫描到’='停止输入s1.
几种排序
1.选择排序
void sort1(int* arr, int low,int high)
{
int temp;
for (int i = low; i < high; i++)
{
for (int j = i + 1; j <= high; j++)
{
if (arr[j] < arr[i])
{
temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
}
}
}
2.冒泡排序
void sort2(int* arr, int low, int high)
{
int temp;
for (int i = high; i > 0; i--)
{
for (int j = 1; j <= i; j++)
{
if (arr[j] < arr[j - 1])
{
temp = arr[j];
arr[j] = arr[j-1];
arr[j-1] = temp;
}
}
}
}
3.插入排序
void sort3(int* arr, int low, int high)
{
int temp;
for (int i = low+1; i <= high; i++)
{
for(int j=i;arr[j]<arr[j-1];j--)
{
temp = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = temp;
}
}
}
3.快速排序
void sort4(int* arr, int low, int high)
{
int mid = (low + high) / 2;
int key = arr[mid],l=low,r=high;
arr[mid] = arr[l];
if (low < high)
{
while (l < r)
{
while (l < r && arr[r] >= key)r--;
if (l < r)
{
arr[l] = arr[r];
l++;
}
while (l < r && arr[l] < key)l++;
if (l < r && arr[l] >= key)
{
arr[r] = arr[l];
r--;
}
}
arr[l] = key;
sort4(arr, low, l - 1);
sort4(arr, l + 1, high);
}
}