3-3

作者: henry_bin | 来源:发表于2017-03-03 16:52 被阅读0次

    函数:strcpy(t,s);
    把s中的字符串拷贝到t中。s可以是一个数组名,也可以是有个字符串常量。成功,返回第一个参数的值。
    例:

    include <stdio.h>

    include <string.h>

    void main()
    {
    char t[50];
    //char s[]="i love beijing";
    //strcpy(t,s);
    strcpy(t,"i love beijing");
    puts(t);
    // puts(s);
    }
    练习:mystrcpy();

    include <stdio.h>

    //#include <string.h>
    void mystrcpy(char *a,char *b)
    {
    int i=0;
    while(b[i]!='\0')
    {
    a[i]=b[i];
    i++;
    }
    a[i]='\0';
    }
    void main()
    {
    char a[50];
    char b[]="abcdefg";
    mystrcpy(a,b);
    puts(a);
    }

    include <stdio.h>

    include <string.h>

    void main()
    {
    char a[50]="abcdefg";
    char b[]="hijklmn";
    char *p=strcpy(a+2,b);
    //strcpy成功,返回第一个参数的值。
    puts(a);
    puts(p);
    }

    strncpy(a,b,n):把b中的n个字符拷贝到a中。

    include <stdio.h>

    include <string.h>

    void main()
    {
    char a[10]="abcdefg";
    char b[20]="bbbbbbbbnmnmnmnm";
    char *p=strncpy(a+3,b,5);
    //成功,返回第一个参数的值。
    puts(a);
    puts(p);
    }

    strcat(a,b);把b中的字符串接在a中的最后。作为一个整体的字符串存在a中。所以a数组的大小应足够容纳得下最终的字符串。
    有返回值,返回的是第一个参数的值。
    例:

    include <stdio.h>

    include <string.h>

    void main()
    {
    char a[50]="abcd";
    char b[]="efgh";
    char *p=strcat(a,b);
    puts(a);
    puts(b);
    puts(p);

    }

    strncat(a,b,n):把b中的n个字符接在a的后面,并把最终的字符串存在a中。有返回值,返回第一个参数的值。

    include <stdio.h>

    include <string.h>

    void main()
    {
    char a[50]="abcd";
    char b[]="efgh";
    char *p=strncat(a,b,3);
    puts(a);
    puts(b);
    puts(p);
    }

    strcmp(str1,str2); 比较字符串str1和str2是否相等(一致),相等,返回0;否则,str1大于str2,返回整数;str1小于str2,返回负数。
    例:

    include <stdio.h>

    include <string.h>

    void main()
    {
    char a[50]="abdd";
    char b[]="abcd";
    int n=strcmp(a,b);
    printf("%d\n",n);
    }
    2个字符串比大小:一个字符一个字符的比,比较第一个不相同字符,哪一个ascii值大,则这个字符串大。
    abcde
    abcfe => d<f 所以下面的字符串大。

    strncmp(str1,str2,n):比较str1和str2前n个字符是否一致。一致返回0,

    include <stdio.h>

    include <string.h>

    void main()
    {
    char *list[]={"wtl is nb","wtl is tall","wtl is smart","wtl is beautiful","wtl is wise"};
    int i;
    for(i=0;i<=4;i++)
    {
    if(strncmp(list[i],"wtl is a good boy",6)==0)
    printf("%s\n",list[i]);
    }

    }

    int a=1;
    char c='a';
    printf("this is %d %c",a,c);
    sprintf(b,"this is %d %c",a,c);
    sprintf():把printf本该输出到屏幕上的字符串输出到一个字符数组中去了。
    例:

    include <stdio.h>

    include <string.h>

    void main()
    {
    int a=1;
    char b='b';
    char c[30];
    sprintf(c,"this is %d %c",a,b);
    printf("%s\n",c);
    }
    //strchr() strrchr()

    学生:
    年龄:int
    性别:char
    姓名:char []
    分数:float

    自定义一个数据类型,用来接受学生的个人信息
    struct student
    {
    int age;
    char sex;
    char name[30];
    flaot score;
    };
    //用该自定义的数据类型定义一个变量stu,用来接受学生的个人信息。
    struct student stu; //int a;

    例:

    include <stdio.h>

    //定义一个变量,来接受一个学生的个人信息。
    //自定义一个相应的数据类型
    struct student
    {
    int num;
    char name[30];
    char sex;
    float score;
    };//分号不能漏
    void main()
    {//用该数据类型定义的变量就可以接受学生信息 类型名+变量名
    struct student stu1={13,"xxx",'m',78.9};
    //数据类型本身不占内存,但是用类型定义的变量会给变量分配空间。
    printf("num:%d name:%s sex:%c score:%.1f\n",stu1.num,stu1.name,stu1.sex,stu1.score);

    }
    /*
    1.结构体类型定义通常会放在头文件中,如果不放在头文件中,通常会放在程序#include的后面
    2.定义好结构体类型后,当要用该类型定义变量时,结构体类型名为:struct +结构体名 +变量名。如:struct student stu1; struct关键字不能省略
    3.结构体名可以和成员变量名同名:
    例:
    struct test
    {
    int a;
    int test;
    };
    二者互不影响
    4.一个结构体中的成员可以是另一个结构体类型定义的变量
    例:
    struct data
    {
    int year;
    int month;
    int day;
    };
    struct student
    {
    int num;
    char name[20];
    struct data bithday;
    float score;
    };
    访问结构体成员: 结构体变量名.成员名
    定义一个学生: struct student a;
    访问它的学号: a.num;
    访问他的出生year: a.birthday.year;
    5.结构体中的成员名,可以和其他变量名同名,也互不影响
    6.定义结构体类型的时候,它的数据成员不能是自己的结构体类型定义的变量。
    例:
    struct test
    {
    int a;
    struct test b; 错
    };
    7.结构体变量除了在初始化的时候可以整体赋值外,其他时候不能整体访问,只能通过访问它的成员变量来访问它。
    8.用一个结构体类型定义一个变量,必须在定义变量之前得存在这个结构体类型的定义,否则出错。
    */

    定义一个结构体变量:
    1.先定义结构体类型,再定义结构体变量
    例:
    struct student
    {
    int num;
    char sex;
    };
    struct student stu1;
    2.在定义结构体类型的同时定义结构体变量
    struct student
    {
    int num;
    char sex;
    }stu1,stu2; 定义了2个结构体变量stu1,stu2.
    struct student stu3;

    3.定义无名结构体
    struct
    {
    int num;
    char sex;
    }stu1,stu2;
    这总无名结构体类型只能用一次,所以只能在它定义类型的同时定义变量。后面用不了了。

    include <stdio.h>

    struct student
    {
    int num;
    char sex;
    };
    void main()
    {
    struct student stu1={1,'w'},stu2;
    stu2=stu1;//对的
    // if(stu2==stu1)错的
    // printf("xxx\n");
    printf("%d %c\n",stu2.num,stu2.sex);
    }
    /*
    注:不能整体操作一个结构体变量,但是在同种类型定义的结构体变量之间可以相互赋值。只有这个意外。
    */

    例:

    include <stdio.h>

    struct student
    {
    int num;
    char sex;
    };
    void main()
    {
    struct student a={1,'m'};
    struct student p=&a;
    printf("%d %c\n",a.num,a.sex);
    printf("%d %c\n",(
    p).num,(*p).sex);
    printf("%d %c\n",p->num,p->sex);
    }

    定义一个结构体类型数组:

    struct student
    {
    int num;
    char sex;
    }stu[30];

    struct student
    {
    int num;
    char sex;
    };
    struct student stu[30];
    3
    struct
    {
    int num;
    char sex;
    }stu[30];

    结构体数组的赋值:
    1.初始化赋值
    a.
    struct student
    {
    int num;
    char sex;
    }stu[2]={1,'m',2,'w'};

    例:

    include <stdio.h>

    struct student
    {
    int num;
    char sex;
    }stu[2]={{1,'m'},{2,'w'}};
    //={1,'m',2,'w'};
    void main()
    {
    printf("%d %c\n",stu[0].num,stu[0].sex);
    printf("%d %c\n",stu[1].num,stu[1].sex);
    }

    b.
    struct student
    {
    int num;
    char sex;
    };
    struct student stu[2]={1,'m',2,'w'};

    c.
    struct
    {
    int num;
    char sex;
    }stu[2]={1,'m',2,'w'};

    2.先定义再赋值。一个一个赋值
    struct student
    {
    int num;
    char sex;
    };
    struct student stu[2];
    stu[0].num=1;
    stu[0].sex='m';
    stu[1].num=2;
    stu[1].sex='w';

    练习:定义一个结构体数组,输入3个学生的个人信息并输出。

    include <stdio.h>

    struct student
    {
    int num;
    char name[30];
    char sex;
    };
    void main()
    {
    struct student stu[3];
    int i;
    for(i=0;i<3;i++)
    {
    printf("please input data:");
    scanf("%d %s %c",&stu[i].num,stu[i].name,&stu[i].sex);
    }
    for(i=0;i<3;i++)
    {
    printf("%d %s %c\n",stu[i].num,stu[i].name,stu[i].sex);
    }
    }

    include <stdio.h>

    struct student
    {
    int num;
    char name[30];
    char sex;
    };
    void main()
    {
    struct student stu[3];
    struct student *p=stu;
    for(;p<=stu+2;p++)
    {
    printf("please input data:");
    scanf("%d %s %c",&p->num,p->name,&p->sex);
    }
    for(p=stu;p<=stu+2;p++)
    {
    printf("%d %s %c\n",p->num,p->name,p->sex);
    }
    }

    练习:定义一个表示平面上的点的数据类型,然后定义函数,给定一个点和半径,在这个函数中判断,该点是不是在以原点为中心,给定的半径的圆内。sqrt(xx+yy) math.h
    gcc 1.c -lm

    include <stdio.h>

    include <math.h>

    //定义一个描述点(x,y)的数据类型
    struct point
    {
    double x;
    double y;
    };

    int Cycle(struct point p,double l)
    {
    if(sqrt(p.xp.x+p.yp.y)<=l)
    return 1;
    else
    return 0;

    }
    void main()
    {
    //定义一个点
    struct point p;
    printf("输入点的位置:");
    scanf("%lf %lf",&p.x,&p.y);
    printf("输入半径的长度:");
    double l;
    scanf("%lf",&l);
    if(Cycle(p,l))
    printf("在圆内!\n");
    else
    printf("在圆外!\n");
    }

    include <stdio.h>

    typedef struct student
    {
    int num;
    char sex;
    }Stu;
    typedef float X;
    void main()
    {
    Stu stu1;
    Stu stu2;
    X a=1.2;
    printf("%.1f\n",a);
    }/*
    typedef:类型重命名
    格式:typedef 类型名 新类型名;
    例: typedef float X; 以后X就表示float.
    */

    共用体/联合体

    include <stdio.h>

    //定义了一个共用体类型,关键字union
    union data
    {
    int a;
    int b;
    char c;
    };
    void main()
    {
    //用共用体类型定义变量
    union data p;
    p.a=356;
    printf("%d\n",p.b);
    printf("%d\n",p.c);
    }
    /*
    注:共用体的所有成员公用一块存储区域,它的大小是成员最大的那个成员的大小。
    */

    include <stdio.h>

    //自定义一个枚举类型
    enum weekday{sunday=5,monday,tursday};
    void main()
    { enum weekday day;
    day=sunday;
    printf("%d\n",day);
    printf("%d %d %d\n",sunday,monday,tursday);
    }/*
    1.枚举类型,把用枚举类型定义的变量的所有可能的取值都放在定义它的大括号中
    2.枚举类型中列出的项都是一个常量,不能给它赋值,它本身对应有一个标号值。默认第一项从0开始,后面的项依次递加。
    3.可以在定义枚举类型的同时给第一项赋值,改变对应标号的初始值。
    */

    相关文章

      网友评论

          本文标题:3-3

          本文链接:https://www.haomeiwen.com/subject/qudrgttx.html