Day17_堆变量、qsort等函数使用

来源:互联网 发布:雅马哈电子琴淘宝便宜 编辑:程序博客网 时间:2024/05/08 03:54


堆中变量的生命周期由程序控制
为了使用堆中变量,需要一组标准函数,为了使用这些标准函数,需要包含stdlib.h文件

malloc可以从堆中分配多个连续的字节
---------------------------------------------------------
  1 #include <stdio.h>
  2 #include <stdlib.h>
  3
  4 int main()
  5 {
  6         int num=0;
  7         int *p_num=(int *)malloc(3*sizeof(int));
  8         if(p_num)
  9         {
 10                 for(;num<=2;num++)
 11                 {
 12                         //printf("%d\n",p_num[num]);
 13                         printf("%d\n",*(p_num + num));
 14                 }
 15                 free(p_num);//释放变量
 16                 p_num= NULL;//消灭野指针
 17         }  
 18         return 0;
 19 }
---------------------------------------------------------
malloc返回值记录了首字节地址
如果分配失败则返回值是NULL
分配好的变量就可以当成数组使用

当结束对堆中变量的使用后必须使用free函数释放这些变量
free函数需要首字节的地址作为参数
任何堆中的变量只能释放一次

malloc分配的地址如果没有在函数中被使用free释放掉,则可以被赋值给返回值
这种时候必须有其它函数负责释放这部分变量 

calloc函数也可以从堆中分配多个变量。分配完成后把所有变量内容清0
calloc函数需要两个参数,一个表示单个变量的大小,另一个表示变量的个数

realloc函数可以调整堆中分配变量的个数
realloc函数需要两个参数,第一个参数表示原来的首字符地址,第二个参数表示调整后的总字节个数
 例:p_str2=(int *)realloc(p_str,10*sizeof(int));
新地址可能和老地址不一样
realloc函数也可能失败,如果失败则返回null;

函数指针用来和函数配对(记录函数地址)
 例:typedef int (*p_func)(int, int);
     p_func p_add=add,p_sub=sub;
可以使用函数指针调用函数
函数名称可以用来表示函数的地址

qsort是一个标准函数,可以用来对多个数据进行顺序调整
---------------------------------------------------------
  1 #include <stdio.h>
  2 #include <stdlib.h>
  3
  4 int comp(const void *p_num,const void *p_num1)
  5 {
  6         const int *p_val=(const int*)p_num;
  7         const int *p_val1=(const int*)p_num1;
  8         if(*p_val > *p_val1)
  9         {
 10                 return 1;
 11         }
 12         else if(*p_val < *p_val1)
 13         {
 14                 return -1;
 15         }
 16         else
 17         {
 18                 return 0;
 19         }
 20 }
 21 int comp1(const void *p_num,const void *p_num1)
 22 {
 23         const int *p_val=(const int*)p_num;
 24         const int *p_val1=(const int*)p_num1;
 25         if(*p_val > *p_val1)
 26         {
 27                 return -1;
 28         }
 29         else if(*p_val < *p_val1)
 30         {
 31                 return 1;
 32         }
 33         else
 34         {
 35                 return 0;
 36         }
 37 }
 38
 39 int main()
 40 {
 41         int arr[]={80,90,10,20,30,40,50,60},num=0;
 42         //qsort(arr,8,sizeof(int),comp);
 43         qsort(arr,8,sizeof(int),comp1);
 44         for(num=0;num<=7;num++)
 45         {
 46                 printf("%d ",arr[num]);
 47         }
 48         printf("\n");
 49         return 0;
 50 }
---------------------------------------------------------
atoi可以把字符串中的整数转换成整数数据
atof可以把字符串中的带小数点的数字转换成double数据
以上两个函数需要包含stdlib.h头文件

sqrt函数可以计算一个数字的平方根
需要包含math.h头文件
编译时需要使用-lm选项

pow函数可以计算一个数字的幂
需要包含math.h头文件
编译时需要使用-lm选项

输出缓冲区显示在屏幕上的条件
1.遇到\n字符
2.函数结束时
3.输出缓冲区满了
4.使用fflush(stdout)语句强制显示

sleep函数可以让当前进程休眠n秒(时间长度由参数决定)
 应该包含unistd.h头文件
时间不精确

sprintf可以把变量内容打印在字符串中
sscanf可以从字符串中读数据并记录在变量中
---------------------------------------------------------
  1 #include <stdio.h>
  2
  3 int main()
  4 {
  5         char ch='a';
  6         int num=19;
  7         float fnum=4.8;
  8         char buf[20]={};
  9         sprintf(buf,"%c %d %g",ch,num,fnum);
 10         printf("%s\n",buf);
 11         ch=0;
 12         num=0;
 13         fnum=0.0f;
 14         sscanf(buf,"%c %d %g",&ch,&num,&fnum);
 15         printf("%d %c %g\n",num,ch,fnum);
 16         return 0;
 17 }
---------------------------------------------------------
fprintf/fscanf可以对文件进行格式化读写操作


预习:
 1.栈
 2.队列
 3.链表
作业:
 1.人员信息管理系统增加删除功能

练习:写一个自己的mystrcpy函数
  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 char *mystrcpy(const char* p_ch)
  4 {
  5         int num=0;
  6         char *p_ret=NULL;
  7         while(*(p_ch+num))//*(p_ch+num) == p_ch[num]
  8         {
  9                 num++;//用来计算字符的长度
 10         }
 11         num++;
 12         p_ret=(char *)malloc(num*sizeof(char));//把从堆中申请到到空间首地址赋予p_ret;
 13         if(p_ret)//如果成功的话p_ret就不为空,执行下面内容
 14         {
 15                 num=0;
 16                 while(*(p_ch+num))
 17                 {
 18                         *(p_ret+num)=*(p_ch+num);//按位把字符赋值给p_ret;
 19                         num++;
 20                 }
 21         }
 22         return p_ret;//返回p_ret;
 23 }
 24
 25 int main()
 26 {
 27         char *p_ch=mystrcpy("abc");//mystrcpy函数返回的p_ret是个指针,把这个赋值给p_ch
 28         if(p_ch)
 29         {
 30                 printf("%s\n",p_ch);
 31                 free(p_ch);//释放
 32                 p_ch=NULL;//消灭野指针
 33         }
 34         return 0;
 35 }



原创粉丝点击