C语言基础知识

来源:互联网 发布:阿里云备案服务器费用 编辑:程序博客网 时间:2024/05/02 23:40
1.数据类型部分

   1.1简单又简单的数据运算和输出

        /* 定义变量并赋初值 */
        int    a = 5;      
       char   c = 'a';  
       float f = 5.3;    
       doublem = 12.65;
       doubleresult;
        
         /* 同类型数据间进行运算并输出结果 */
         printf("a + c = %d/n", a + c);
         printf("a + c = %c/n", a + c);//按照ASCII数值计算只不过输出形式不一样而已
         printf("f + m = %f/n", f + m);
 
         /* 不同类型数据间进行运算并输出结果 */
         printf("a + m = %f/n", a + m);
         printf("c + f = %f/n", c + f);
 
         /* 将上述四个变量进行混合运算,并输出结果 */
         result = a + c * (f + m);
         printf("double = %f/n", result);
         getch();

   1.2格式化输出一下
         
        /* 换行符'/n',用于输出换行 */
         printf("How are you?/n");
         printf("I am fine./n/n");
 
         /* 横向跳格符'/t',使跳到下一个输出区 */
         printf("How are you?/t");
         printf("I am fine./n/n");
 
         /* 退格符'/b',使当前的输出位置退一格,即输出的起始位置左移一位 */
         printf(" How are you?/n");
         printf(" /bI am fine./n/n");
 
         /* 回车符'/r',使当前输出位置回到本行开头 */
         printf("                I am fine.");
         printf("/rHow are you?/n/n");
 
         /* 多个转义字符的混合运用 */
         printf("note:/n a s/ti/b/bk/rp/n");

  1.3简单的bool运算
   
        /* 定义一个整数类型的变量,用来存放后面算式的值 */
         int logic;   
 
         inta = 1;
         int b = 2;
         int c = 3;
 
         logic = a+b>c&&b<=c;
         printf("logic = %d/n", logic);
 
         logic = a>=b+c||b==c;
         printf("logic = %d/n", logic);
 
         logic = !(a<c)+b!=1&&(a+c)/2;
         printf("logic = %d/n", logic);
         getch();
 
   1.4 ++和--总是不清楚
    
  
         inti, j, k;
         int m, n, p;
 
         i = 8;
         j = 10;
         k = 12;
   
         /* 自增在操作数之前 */
         m = ++i;
        printf("i = %d/n", i);
         printf("m = %d/n", m);
 
         /* 自减在操作数之后 */
         n = j--;
         printf("j = %d/n", j);
         printf("n = %d/n", n);
 
         /* 自增、自减的混合运算 */
         p = (++m)*(n++)+(--k);
         printf("k = %d/n", k);         
         printf("p = %d/n", p);        
         getch();

  1.5 与或非一下
   
         /* 定义了一个无符号字符型变量,此变量只能用来存储无符号数 */
         unsigned char result;
   
         inta, b, c, d;
         a = 2;
         b = 4;
         c = 6;
         d = 8;
 
         /* 对变量进行“按位与”操作 */
         result = a & c;
         printf("result = %d/n", result);
 
         /* 对变量进行“按位或”操作 */
         result = b | d;
         printf("result = %d/n", result);
 
         /* 对变量进行“按位异或”操作 */
         result = a ^ d;
         printf("result = %d/n", result);
 
         /* 对变量进行“按位取反”操作 */
         result = ~a;
         printf("result = %d/n", result);
         getch();
  
   1.6 左右移

    unsigned a, b, c, d;
         intn;
 
         a = 64;
         n = 2;
 
         /* 将操作数a右移(6-n) */
         b = a >> (6-n);
         printf("b = %d/n", b);
 
         /* 将操作数a左移n */
         c = a << n;
         printf("c = %d/n", c);
 
         /* 对操作数a进行的混合位运算 */
         d = (a >> (n-1)) | (a << (n+1));
         printf("d = %d/n", d);
         getch();

  1.7 可恨又可爱的指针
   
        /* 定义整形指针 */
        intbegin=0, end=0;
       
int *p1=0;
        const int* p2=0;
        int* const p3=&begin;
        const int* const p4=&end; 
         
begin = 10;
         /* 让人头晕的赋值 */
         p1 = &begin;
         end = *p1;
         *p2=1;//Error
         *p3=1;//OK
         p2=&end;//ok
         p3=&end;//Error
         *p4=1//Error
         p4=&end;//Error

         printf("begin = %d/n", begin);
         printf("end = %d/n", end);
 
         /* 输出指针中的地址值 */
         printf("p 1= %d/n", p1);
         printf("&begin = %d/n", &begin);
         printf("*p 1= %d/n", *p1);
         getch();

  1.8 大公约和小公倍
     
    intx, y, num1, num2, temp;
         printf("请输入两个正整数:/n");
         scanf("%d %d", &num1, &num2);
 
         if(num1 < num2)
         {
                   temp = num1;
                   num1 = num2;
                   num2 = temp;
         }
         x = num1;
         y = num2;
         while(y != 0)
         {
                   temp = x%y;printf("%d/n", temp);
                   x = y;
                   y = temp;
         }
         printf("它们的最大公约数为:%d/n", x);
         printf("它们的最小公倍数为:%d/n", num1*num2/x);
         getch();

   1.9 矩阵转置
 
    voidconvert(intelement[N][N])
        {
            inti, j, t;
            for(i=0; i<N; i++)
                  for(j=i+1; j<N; j++)
                   {
                            t = element[i][j];
                            element[i][j] = element[j][i];
                            element[j][i] = t;
                   }
         }
 
   1.10 命令行参数

   voidmain(intargc, char* argv[])
        {
           intdisp, count;
 
          if(argc < 2)
          {
                  printf("You must enter the length of the count/n");
                  printf("on the command line. Try again/n");
                   exit(1);    /* 非正常跳出程序 */
          }
 
          if(argc==3 && !strcmp(argv[2], "display"))
                   disp = 1;
          else
                   disp = 0;
          for(count = atoi(argv[1]); count; --count)
                   if(disp)
                            printf("%d/n", count);
 
          putchar('/a');    /* 将产生蜂鸣 */
          printf("Down");
          getch();
          return;
      }
   
  1.11 查找字符串原来是这样
     intfind_substr(char* s1, char* s2)
    {
         registerintt;
         char *p, *p2;
 
        for(t=0; s1[t]; t++)
         {
                   p = &s1[t];
                   p2 = s2;
 
                  while(*p2 && *p2==*p)
                   {
                            p++;
                            p2++;
                   }
                   if(! *p2)
                            returnt;
         }
         return -1;
     }

  1.12 也算年月日
             
      /* 给出年、月、日,计算该日是该年的第几天 */
     # include <stdio.h>
     # include <conio.h>
 
    intsum_day(intmonth, intday);
    intleap(intyear);
 
    voidmain()
    {
         intyear, month, day;
         intdays;
         printf("请输入日期(年,月,日)");
         scanf("%d, %d, %d", &year, &month, &day);
         printf("%d%d%d", year, month, day);
         days = sum_day(month, day);    /* 调用函数sum_day() */
         if(leap(year) && month>=3)    /* 调用函数leap() */
                   days = days + 1;
         printf("是该年的第%d./n", days);
         getch();
   }
 
    /* 定义静态存储变量并赋初值 */
   staticintday_tab[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
 
    intsum_day(intmonth, intday)    /* 计算日期 */
    {
         inti;
         for(i=1; i<month; i++)
                   day = day + day_tab[i];
         returnday;
    }
 
    intleap(intyear)
   {
         intleap;
         leap = (year%4==0&&year%100!=0)||(year%400==0);
         returnleap;
    }

    1.13 插入字符
                              
       # include <stdlib.h>
       # include <string.h>
       # include <stdio.h>
 
      voidmain()
      {
         /* 声明子函数 */
         intbinary(char *ptr[], char *str, intn);    /* 查找函数声明 */
         voidinsert(char *ptr[], char *str, intn, inti);    /* 插入函数声明 */
 
         char *temp, *ptr1[6];
         inti;
         printf("请为字符形指针数组赋初值:/n");
         for (i=0; i<5; i++)
         {
                   ptr1[i] = (char *)malloc(20);    /* 为指针分配地址后 */
                  gets(ptr1[i]);    /* 输入字符串 */
         }
         ptr1[5] = (char *)malloc(20);
         printf("/n");
 
         printf("original string:/n");
         for(i=0; i<5; i++)    /* 输出指针数组各字符串 */
                  printf("%s/n", ptr1[i]);
 
         printf("/ninput search string:/n");
         temp = (char *)malloc(20);
         gets(temp);    /* 输入被插字符串 */
 
         i=binary(ptr1, temp, 5);    /* 寻找插入位置i */
         printf("i = %d/n", i);
 
         insert(ptr1, temp, 5, i);    /* 在插入位置i处插入字符串 */
         printf("output strings:/n");
 
         for(i=0; i<6; i++)    /* 输出指针数组的全部字符串 */
                  printf("%s/n", ptr1[i]);
    }
 
       intbinary(char *ptr[], char *str, intn)
      {
         /* 折半查找插入位置 */
         inthig, low, mid;
         low = 0;
         hig = n-1;
         if(strcmp(str,ptr[0]) < 0)
                   return 0;
        /* 若插入字符串比字符串数组的第0个小,则插入位置为0 */
         if(strcmp(str,ptr[hig]) > 0)
                   returnn;
        /* 若插入字符串比字符串数组的最后一个大,则应插入字符串数组的尾部 */
        while(low <= hig)
         {
                   mid = (low + hig)/2 ;
                   if (strcmp(str,ptr[mid]) < 0)
                            hig = mid - 1;
                   elseif(strcmp(str,ptr[mid]) > 0)
                            low = mid + 1;
                   else
                            returnmid;    /* 插入字符串与字符串数组的某个字符串相同 */
          }
          returnlow;    /* 插入的位置在字符串数组中间 */
     }
 
      voidinsert(char *ptr[], char *str, intn, inti)
      {
         intj;
         for(j=n; j>i; j--)    /* 将插入位置之后的字符串后移 */
                  strcpy(ptr[j], ptr[j-1]);
         strcpy(ptr[i], str);    /* 将被插字符串按字典顺序插入字符串数组 */
      }

 
原创粉丝点击