C语言 【总结1】

1.程序 = 数据结构 + 算法

2.常量

     整型常量 : 1 2 

     实型常量: 1.2 4.3

     字符串常量 : 'a' 'b'

3.符号常量

     用一个标识符表示一个常量,称之为符号常量.

     #define N 10 后面没有分号

4.变量

5.%c %d %f %s

 6.字符变量加密

 

7.getche与getchar的区别

   getche()只能获取后直接输出

  getchar()获取后按Enter才会输出

8.%md m为输出宽度

   %m.nf m输出宽度,n为保留小数点的个数

9.条件运算符的结合方向“自右向左”

   a>b?a:b>c?b:c ==>  a>b?a:(b>c?b:c);

 

10

#include <stdio.h>
#include <string.h>
#define N 9
main(){
    int i,j;
    for(i=1;i<=N;i++)
    {
loop: for(j=1;j<=i;j++)
      {
        printf("%2d * %2d = %2d",i,j,i*j);
      }
      printf("\n");
      goto loop;
    }


}

13.数组

#include <stdio.h>

main()
{

    //如果不做初始化赋值,则必须是说明长度
    char c[6] = {'H','e','l','l','o'};
    char b[] = {"Hello"};
    char d[] = "Hello";
    char f[20];
    //当用scanf函数输入字符串时候,输入将以空格和回车作为串的结束符
    printf("请输入字符串:");
    scanf("%s",&f);

    printf("输入的字符串位:%s\n",f); 
    printf("%s\n%s\n%s\n",c,b,d);


}

14.#include<string.h>

#include<stdio.h>
#include<string.h>

main()
{   int result;
    char a[20] ={"Hello"};
    char b[10] = {"World"};
    char c[10] = {"nestle"};
    char d[10];
    //连接strcat(字符串数组1,字符串数组2),数组1足够大
    strcat(a,b);
    printf("%s\n",a);

    //复制strcpy(字符串数组1,字符串数组2),数组1足够大
    strcpy(d,c);
    printf("%s\n",d);

    //比较strcmp(字符串数组1,字符串数组2),字符中比较不能用'==',必须用strcmp

    result = strcmp(a,b);
    printf("%d\n",result);   //-1 b>a

    //字符串长度strlen()
    //大小写转换strlwr(),strupr()
    printf("%d\n",strlen(a));

}

 15.指针位移

#include <stdio.h>

main()
{
 int i;
 int *p;
 printf("请输入一个整形数:");
 scanf("%d",&i);
 p=&i;
 printf("%ld\n",p);
 p++;
printf("%ld%3d\n",p,sizeof(int));

}

 16.通过指针引用数组元素

#include <stdio.h>

main()
{
    int a[3][4]={1,2,3,4,5,6,7,8,9,10,11,12};
    int (*p)[4];
    int i,j;
    p=a;
    for(i=0;i<3;i++)
    {
     for(j=0;j<4;j++)
         printf("%4d",*(*(p+i)+j)); //*(p+i) === a[i] ,*(*(p+i)+j) === a[i][j]
     printf("\n");
    }


}

 16.指向指针的指针

#include <stdio.h>

main()
{
int i;
char **p;
char *month[12] = {"A","B","C","D","E","F","G","H","I","J","K","L"};
for(i=0;i<12;i++)
{
  p = (month+i);
  printf("%s\n",*p);
}

}

 

#include <stdio.h>

main()
{
int a[10],*p1,**p2,i,n=0;
printf("10个证书:");
for(p1=a;p1-a<10;p1++)
{
 p2 = &p1;
 scanf("%d",*p2);

}
printf("满足条件的:");
for(p1=a;p1-a<10;p1++)
{
 p2 =&p1;
 if(**p2%2==0)
 {
  printf("%5d",**p2);
  n++;
 }
}
printf("\n");
printf("满足条件的有%d个\n",n);
}

 17.内存分配

#include <stdio.h>
#include <stdlib.h>
main()
{   int i;
    int *ppInt = (int *)calloc(3,sizeof(int));
    //void *malloc(unsigned int size)
    int *pInt = (int *)malloc(sizeof(int));
    *pInt =100;  //赋值
    printf("%d\n",*pInt);
    
    
    //void *calloc(unsigned n,unsigned size)分配n个长度位size的连续存储单元
    
    for(i=0;i<4;i++)
    {
     *ppInt = 10*i;
     printf("动态分配%d个存储单元的数值为:%d\n",i,*ppInt);
     ppInt++;
    }

    //void free(void *ptr)释放有指针ptr所指内存区域

    free(pInt);
    printf("%d\n",pInt);
    
}

 

=============【需要学习】==========
1.快速排序
  冒泡排序
  直接插入排序
  堆排序
2.  
  1.结构体==================================================
            struct '结构体名'               typedef struct 
             {                               {
              //成员变量                       //成员变量
                int a,b;
             } 变量名;                        } 结构体名;


 声明变量:  struct '结构体名' '变量名';       结构体名 '变量名'
  ================================================

  2.共同体========================================================
                union '共同体名'         
                {
                  //共同体变量
                  
                } 变量名;

  声明变量:   union '共同体名' '变量名'


  =================================================================


  3.枚举==============================================================
       enum 枚举名 {枚举元素....}

       【例子】
          enum DAY                              enum 
          {                                     {
            MON =1,TUE,WED,THU,FRI,SAT,SUN      //枚举元素 

          };                                    } day;
          enum DAY day;

          int main()
             {
                // 遍历枚举元素
                for (day = MON; day <= SUN; day++) {
                    printf("枚举元素:%d \n", day);
                }
            }

  ===================================================================
  4.位域==============================================================
    struct 
    {
    type [member_name] :width;  //width位

    } 变量名;

  =====================================================================
  5.文件的读写========================================================
    
    FILE *p;  //定义一个文件指针

    fp = fopen('filename','读取方式')  //打开文件
      ......写入
    fclose(fp)    //关闭文件
  
    【写入方法】

        fprintf(fp,'文字\n');
        fputs('文字',fp);
        fwrite()
        fseek(FILE *stream,offset,whence);      stream文档流,offset为查找字符个数,whence是查找位置  SEEK_SET 文件的头位置       【重新定位】
                                                                                                    SEEK_CUR 文件指针的当前位置
                                                                                                    SEEK_END 文件的末尾
    【读取文件】
       fread();

    (1)size_t fread ( void * ptr, size_t size, size_t count, FILE * stream );

     其中,ptr:指向保存结果的指针;
                size:每个数据类型的大小;
                count:数据的个数;
                stream:文件指针函数返回读取数据的个数。

  (2)size_t fwrite ( const void * ptr, size_t size, size_t count, FILE * stream );

       其中,ptr:指向保存数据的指针;
                 size:每个数据类型的大小;
                 count:数据的个数;
                 stream:文件指针函数返回写入数据的个数。






  ======================================================================






===============【知识点】=========
1.
    double  size(double)   8字节

    int     size(int)      4字节

    char    size(char)     1字节



2.字符串的输入不能用scanf('%s',s),应该使用gets(s);

3.只有在使用才占用内存单元的变量
   auto 和 register

4.结构化程序设计包括:
   
   1.自顶向下
   2.模块化
   3.逐步求精
   

===================================================【文件读写】=======

1.fseek(FILE *stream ,offset , whence) 【改变指针在文件中的位置】

    FILE *stream  -------------   文件的指针
    offset       - -------------  指针的偏移量
    whence      ----------------  指针的初始位置  SEEK_SET(文件开头) SEEK_CUR(当前的指针位置) SEEK_END(文件的末尾)

2.fputs(str,FILE *stream)    【写入文件】

     str  -------------------    写入文件的内容
     FILE *stream  ------------ 文件的指针

3.fprintf(FILe *stream,str);   【写入文件】

     str  -------------------    写入文件的内容
     FILE *stream  ------------ 文件的指针

4.size_t fread ( void * ptr, size_t size, size_t count, FILE * stream );

     其中,ptr:指向保存结果的指针;
                size:每个数据类型的大小;
                count:数据的个数;
                stream:文件指针函数返回读取数据的个数。

5.size_t fwrite ( const void * ptr, size_t size, size_t count, FILE * stream );

       其中,ptr:指向保存数据的指针;
                 size:每个数据类型的大小;
                 count:数据的个数;
                 stream:文件指针函数返回写入数据的个数。


=============================================题===============================
float x =1.5; int a =1,b = 3,c = 2;

switch(a+b)
{
    
  case 1:printf("**");

  case c:printf("****");

}

1.队列与栈都是线性结构
2.层次型,网状型和关系型数据库划分原则:
   数据之间的联系方式
3.数据库系统的三级模式是:
    1.概念模式:数据库系统中全局数据逻辑结构的描述
    2.外模式:也称子模式或用户模式,它是用户的数据视图,给出了每个用户的局部数据描述。
    3.内模式:又称物理模式,它给出了数据库物理存储结构与物理存取方法。

4.字符串常量应该用双引号表示

5.文件是由数据序列组成,可以构成二进制文件或文本文件

 

  1 =======================================================预处理=====================================================================================
  2 
  3 #undo File  // 取消已定义的File 
  4 
  5 
  6 #ifndef MESSAGE
  7    #define MESSAGE 'Your wish'
  8 #endif                  //MESSAGE未定义的时候才定义MESSAGE
  9 
 10 【预定义】
 11 
 12      #define    定义宏
 13 
 14      #include    包含一个源代码文件
 15 
 16      #undef     取消已定义的宏
 17 
 18      #ifdef     如果宏已经定义,则返回真
 19 
 20      #if        如果给定条件为真,则返回为真
 21 
 22      #else      #if的替代方案
 23 
 24      #elif      如果前面的#if给定条件不为真,当前条件为真,则编译下面的代码
 25 
 26 
 27      #endif     结束一个#if....#else条件编译块
 28 
 29      #error     当遇到标准错误时,输出错误消息
 30 
 31      #pragma    使用标准化方法,向编译器发布特殊的命令到编译器中
 32 
 33 
 34 
 35 
 36 
 37 
 38 
 39 
 40 【预定义宏】     【描述】
 41  
 42 __DATE__       当前日期,一个以 "MMM DD YYYY" 格式表示的字符常量。
 43 
 44 __TIME__       当前时间,一个以 "HH:MM:SS" 格式表示的字符常量。
 45 
 46 __FILE__        这会包含当前文件名,一个字符串常量。
 47 
 48 __LINE__        这会包含当前行号,一个十进制常量。
 49 
 50 __STDC__        当编译器以 ANSI 标准编译时,则定义为 1 51 
 52 【例子】
 53         #include <stdio.h>
 54 
 55         main()
 56         {
 57            printf("File :%s\n", __FILE__ );
 58            printf("Date :%s\n", __DATE__ );
 59            printf("Time :%s\n", __TIME__ );
 60            printf("Line :%d\n", __LINE__ );
 61            printf("ANSI :%d\n", __STDC__ );
 62 
 63         }
 64 
 65 
 66 【预处理器运算符】
 67    
 68    1.宏延续运算符(\)
 69 
 70       一个宏通常写在一个单行上,但是如果太长,则使用宏延续运算符(\)    
 71 
 72    2.字符串常量化运算符(#)   
 73 
 74       在宏定义中当需要把参数转换为字符串常量时,则使用字符串常量化运算(#)
 75 
 76   【例子】
 77 
 78         #include <stdio.h>
 79         /*宏延续运算符    字符串常量化运算符*/
 80         #define  message_for(a, b)  \                   
 81             printf(#a " and " #b ": We love you!\n")
 82 
 83         int main(void)
 84         {
 85            message_for(Carole, Debra);
 86            return 0;
 87         }        
 88 
 89     3.标记粘贴运算符(##)
 90 
 91        宏定义内的标记粘贴运算符(##)会合并两个参数。它允许在宏定义中两个独立的标记被合并为一个标记。
 92 
 93    
 94     【例子】 
 95        #include <stdio.h>
 96 
 97         #define tokenpaster(n) printf ("token" #n " = %d", token##n)
 98 
 99         int main(void)
100         {
101            int token34 = 40;
102            
103            tokenpaster(34);
104            return 0;
105         }   
106     
107     4.define()运算符
108       如果指定的标识符已定义,则值为真(非零)。如果指定的标识符未定义,则值为假(零)。
109 
110    【例子】
111 
112       #include <stdio.h>
113 
114         #if !defined (MESSAGE)
115            #define MESSAGE "You wish!"
116         #endif
117 
118         int main(void)
119         {
120            printf("Here is the message: %s\n", MESSAGE);  
121            return 0;
122         }   
123 
124 
125 【参数化的宏】
126     
127     //#define square(x) ((x) * (x))
128 
129     在使用带有参数的宏之前,必须使用 #define 指令定义。参数列表是括在圆括号内,且必须紧跟在宏名称的后边。宏名称和左圆括号之间不允许有空格。
130 
131  【例子】
132 
133    #include <stdio.h>
134 
135     #define MAX(x,y) ((x) > (y) ? (x) : (y))
136 
137     int main(void)
138     {
139        printf("Max between 20 and 10 is %d\n", MAX(10, 20));  
140        return 0;
141     }
142 
143   【有条件引用】
144     
145     有时候需要从多个不同的头文件中选择一个引用到程序中,例如,需要指定在不同的操作系统上使用的配置参数,您可以通过一系列条件来实现:
146 
147    【例如】
148    
149      #if SYSTEM_1
150         #include "system_1.h"
151 
152      #elif SYSTEM_2
153          #include "system_2.h"
154 
155      #elif SYSTEM_3
156           .....
157      #endif
158 
159      但是如果头文件比较多的时候,这么做是很不妥当的,预处理器使用宏来定义头文件的名称。这就是所谓的有条件引用。它不是用头文件的名称作为 #include 的直接参数,您只需要使用宏名称代替即可:
160 
161      #define SYSTEM_H "system_1.h"
162 
163      ....
164 
165      #include SYSTEM_H
166 
167      SYSTEM_H 会扩展,预处理器会查找 system_1.h,就像 #include 最初编写的那样。SYSTEM_H 可通过 -D 选项被您的 Makefile 定义。
168 
169    【多个.h文件】
170        
171     在有多个 .h 文件和多个 .c 文件的时候,往往我们会用一个 global.h 的头文件来包括所有的 .h 文件,然后在除 global.h 文件外的头文件中 包含 global.h 就可以实现所有头文件的包含,同时不会乱。方便在各个文件里面调用其他文件的函数或者变量。
172 
173     #ifndef _GLOBAL_H
174     #define _GLOBAL_H
175     #include <fstream>
176     #include <iostream>
177     #include <math.h>
178     #include <Config.h>
179 
180 
181     ====================================================================================================================================================

 

posted @ 2018-09-13 21:12  Justice-V  阅读(185)  评论(0)    收藏  举报