zoukankan      html  css  js  c++  java
  • 结构体

    //
    //  main.c
    //  结构体
    //
    //  Created by zhangxueming on 15/6/8.
    //  Copyright (c) 2015年 zhangxueming. All rights reserved.
    //
    
    #include <stdio.h>
    //name score age
    //struct
    //定义结构体的关键字
    
    //struct Student
    //{
    //    char name[20];
    //    int age;
    //    int score;
    //};
    
    //使用.运算符访问结构体变量的成员
    #include <string.h>
    
    //int main(int argc, const char * argv[]) {
    //    struct Student xiaoHua={"小华", 12, 88};
    //    printf("name = %s, age = %d, score = %d
    ", xiaoHua.name, xiaoHua.age, xiaoHua.score);
    //    
    //    struct Student stu[100]={};
    //    strcpy(stu[0].name, "小黄");
    //    stu[0].age = 13;
    //    stu[0].score = 90;
    //    
    //    printf("name = %s, age = %d, score = %d
    ", stu[0].name, stu[0].age, stu[0].score);
    //    
    //    return 0;
    //}
    
    //利用学生的成绩排序
    #include <stdlib.h>
    
    //int main(int argc,const char *argv[])
    //{
    //    struct Student stu[10]={};
    //    
    //    for (int i=0; i<10; i++) {
    //        sprintf(stu[i].name, "name%d", rand()%20+1);
    //        stu[i].age = rand()%20+10;
    //        stu[i].score = rand()%101;
    //    }
    //    //排序前
    //    for (int i=0; i<10; i++) {
    //        printf("name=%s age=%d score=%d
    ",stu[i].name, stu[i].age, stu[i].score );
    //    }
    //    //排序
    //    for (int i=0; i<10-1; i++) {
    //        for (int j=0; j<10-1-i; j++) {
    //            if (stu[j].score > stu[j+1].score) {
    //                struct Student temp = stu[j];
    //                stu[j]=stu[j+1];
    //                stu[j+1]=temp;
    //            }
    //        }
    //    }
    //    printf("排序后:
    ");
    //    for (int i=0; i<10; i++) {
    //        printf("name=%s age=%d score=%d
    ",stu[i].name, stu[i].age, stu[i].score );
    //    }
    //    return 0;
    //}
    
    //利用typedef关键字
    //1. 给基本数据类型取名
    typedef unsigned int uint_32;
    typedef int int_32;
    typedef unsigned char uint_8;
    typedef unsigned long uint_64;
    
    //2.给复杂类型取名
    
    typedef int (* Pint)(int , int);
    
    //int add(int a, int b)
    //{
    //    return a+b;
    //}
    //int main(int argc,const char *argv[])
    //{
    //    Pint pfun=add;//Pint <==> int (*)(int , int);
    //    printf("sum = %d
    ", pfun(3,5));
    //    return 0;
    //}
    
    //3.给结构体重新取名
    
    //写法一
    //typedef struct Student
    //{
    //    char name[20];
    //    int age;
    //    int score;
    //}Student;
    
    //写法二
    typedef struct
    {
        char name[20];
        int age;
        int score;
    }Student, *pStudent;
    
    
    //int main(int argc,const char *argv[])
    //{
    //    //struct Student xiaoHua={"小华", 12, 98};
    //    Student xiaoFei={"小飞",13,88};
    //    printf("name=%s age=%d score=%d
    ", xiaoFei.name, xiaoFei.age, xiaoFei.score);
    //    
    //    return 0;
    //}
    
    //结构体指针
    
    //int main(int argc,const char *argv[])
    //{
    //    Student xiaoHua={"小华", 12, 77};
    //    pStudent ps = &xiaoHua;
    //    printf("name = %s age = %d score = %d
    ", ps->name,ps->age, ps->score);
    //
    //    Student stu[100]={{"小华", 12, 44},{"小黄",13,55},{"小飞",32,99}};
    //    pStudent pstu = stu;
    //    
    //    for (int i=0; i<3; i++) {
    //        printf("name = %s age = %d score = %d
    ", pstu->name, pstu->age, pstu->score);
    //        pstu++;
    //    }
    //    return 0;
    //}
    
    //结构体大小
    //结构体成员内存对齐后的大小
    //结构体成员按照从小到大的排列保证结构体类型变量占用内存空间最小
    
    typedef struct
    {
        char ch;
        char name[15];
        int age;
        long f;
    }Size;
    
    //int main(int argc, const char *argv[])
    //{
    //    printf("%ld
    ", sizeof(Size));
    //    return 0;
    //}
    
    
    //联合体(共用体)
    //union
    //联合体中成员公用同一块内存, 只能初始化第一个成员变量
    
    typedef union {
        float weight;
        int number;
    }Amount;
    
    //int main(int argc,const char *argv[])
    //{
    //    Amount mount={3.56};
    //    
    //    printf("%p
    ", &mount.number);
    //    printf("%p
    ", &mount.weight);
    //    mount.weight = 3.14;
    //    printf("weight = %.2f
    ", mount.weight);
    //    
    //    mount.number = 100;
    //    
    //    printf("weight = %.2f
    ", mount.weight);
    //    return 0;
    //}
    
    typedef struct {
        char name[30];
        float price;
        Amount amount;
        struct Date{
            int year;
            int month;
            int day;
        }date;
    }Goods;
    
    //int main(int argc,const char *argv[])
    //{
    //    Goods cup = {"杯子",5.68,{},{2015,5,21}};
    //    cup.amount.number = 289;
    //    printf("name=%s price=%.2f amount=%d %d年%d月%d日
    ", cup.name, cup.price, cup.amount.number,cup.date.year, cup.date.month, cup.date.day);
    //    Goods rice = {"大米",3.20,{78.5},{2015,5,21}};
    //    
    //    printf("name=%s price=%.2f amount=%.2f %d年%d月%d日
    ", rice.name, rice.price, rice.amount.weight,rice.date.year, rice.date.month, rice.date.day);
    //    
    //    return 0;
    //}
    
    //枚举类型
    //enum
    
    typedef enum
    {
        MON=1,
        TUE,
        WEN,
        THU,
        FRI,
        SAT,
        SUN
    }Weekday;
    
    //int main(int argc,const char *argv[])
    //{
    ////    printf("%ld
    ", sizeof(SUN));
    ////    printf("mon = %d sun = %d", MON, SUN);
    //    Weekday day ;
    //    printf("size = %ld
    ", sizeof(Weekday));
    //    
    //    scanf("%d", &day);
    //    
    //    switch (day) {
    //        case MON:
    //            printf("星期一
    ");
    //            break;
    //        case TUE:
    //            printf("星期二
    ");
    //            break;
    //        case WEN:
    //            printf("星期三
    ");
    //            break;
    //        case THU:
    //            printf("星期四
    ");
    //            break;
    //        case FRI:
    //            printf("星期五
    ");
    //            break;
    //        case SAT:
    //            printf("星期六
    ");
    //            break;
    //        case SUN:
    //            printf("星期天
    ");
    //            break;
    //        default:
    //            printf("输入错误
    ");
    //            break;
    //    }
    //    return 0;
    //}
    
    //位操作
    //& | ~ ^ >> <<
    
    //3&5
    // 0000 0011
    //&0000 0101
    //-----------
    // 0000 0001
    
    //register(8位) 0x38 0011 1000
    
    // 0001 0111
    //&0000 1111
    //------------
    // 0000 0111
    
    //register & 0xf7;
    
    //int main(int argc, const char *argv[])
    //{
    //    char a=3,b=5;//3:0000 0011  5:0000 0101
    //    printf("%d
    ", a&b);
    //    
    //    printf("0x%x
    ", 0x38&0xf7);
    //    //printf("%d
    ", 0x30);
    //    return 0;
    //}
    
    //按位或 |
    // 0000 0011
    //|0000 0101
    //----------
    // 0000 0111
    //register(8位) 0x38 0011 1000   0x38 | 0x02
    // 0011 1000
    //|0000 0010
    //------------
    // 0011 1010
    
    //int main(int argc,const char *argv[])
    //{
    //    int a=3, b=5;
    //    printf("a|b = 0x%x
    ", a|b);
    //    printf("0x%x
    ", 0x38|0x02);
    //    
    //    return 0;
    //}
    
    //按位取反
    // 0x3   ~0x3
    //  0000 0011
    //~ ----------
    //  1111 1100
    
    //int main(int argc,const char *argv[])
    //{
    //    printf("%d
    ", ~0x3);
    //    
    //    return 0;
    //}
    
    //按位异或
    //3^5
    // 0000 0011
    //^0000 0101
    //-----------
    // 0000 0110
    
    //int main(int argc,const char *argv[])
    //{
    //    //printf("0x%x
    ", 3^5);
    //    int a=3, b=5;
    //    a^=b;//a = a^b;
    //    b^=a;
    //    a^=b;
    //    printf("a = %d b= %d
    ", a, b);
    //    
    //    return 0;
    //}
    
    //左移 右移
    //<<   >>
    //0x38<<3  0011 1000
    //1100 0000
    
    //0x39>>3  0011 1001 >>3
    // 0000 0111
    
    //register 0x38    register | (1<<2)
    //0000 0001 << 2 --> 0000 0100
    //int main(int argc,const char *argv[])
    //{
    //    char a = (char)(0x38<<3);
    //    printf("0x%x
    ",a);
    //    
    //    printf("0x%x
    ", 0x39>>3);
    //    
    //    printf("%x
    ", 0x38 | (1<<2));
    //    
    //    return 0;
    //}
    
    int main(int argc,const char *argv[])
    {
        int a=15;
        printf("a=%d
    ", a>>=2);
        printf("a=%d
    ", a>>=1);
        printf("a=%d
    ", a>>=1);
        
        int b=3;
        printf("b = %d
    ", b<<=1);
        printf("b = %d
    ", b<<=1);
        return 0;
    }
  • 相关阅读:
    最权威的 Android Oreo 新特性详解
    【送书福利】第一次送书活动(总共10本)
    【资源篇】Python那么火,你还不知道如何人门?
    不忘初心,方得始终 ,让我们一起学习成长,感谢有你!
    搭建环境篇 | 运行第一个Java Web 项目
    为什么我们需要看技术公众号文章?
    手把手教学APK反编译实现源码阅读
    分享一款 Google Pixel 2 独家动态壁纸
    了解CoordinatorLayout,在项目中运用
    jdk 与 jre的区别
  • 原文地址:https://www.cnblogs.com/0515offer/p/4561512.html
Copyright © 2011-2022 走看看