结构体

//

//  main.m

//  C7_结构体

//

//  Created by dllo on 15/7/8.

//  Copyright (c) 2015年 cml. All rights reserved.

// 

#import <Foundation/Foundation.h>

//

struct student{

    // 成员变量

    int stuAge;      // 学生的年龄

    float stuScore;  // 学生的成绩

    char stuSex;     // 学生的性别

    char stuName[20];    // 学生的姓名

};

typedef struct student Student;

 

// typedef 的第二种写法

//typedef struct student {

//    int stuAge;

//    float stuScore;

//}Student;

typedef int ML;

// 定义一个数的类型

// 书名,价格,页数,作者,出版商

struct book{

    char bookName[20];

    float bookMoney;

    int bookPage;

    char bookWriter[20];

    char bookMan[20];

};

typedef struct book Book;

 

// 定义一个car 类型的结构体

// 车的品牌,车胎数,颜色,价格

struct car{

    char carBrand[20];

    int  carTyre;

    char carColor[20];

    int  carprice;

};

typedef struct car Car;

 enum season{

    spring,

    summer

};

 int main(int argc, const char * argv[]) {

//    int arr[7] = {1,2,4,5,8,3,6};

    // 冒泡排序

//    for (int i =0; i <6; i++) {

//        for (int j =0; j<6-i; j++) {

//            if (arr[j]>arr[j+1]) {

//                int  temp =0;

//                temp =arr[j];

//                arr[j]=arr[j+1];

//                arr[j+1]=temp;

//            }

//        }

//    }

    

//    for (int i =0; i<7; i++) {

//        printf("%d\n",arr[i]);

//    }

    

    

    // 结构体

//    int arr[3] = {1,2,3};

//    char str[20] = "111";

    // 一维数组元素必须相同

    

    

    // 把结构体看成是一个自定义的数据类型,可以定义结构体类型所对应的变量

    

    // 定义一个学生类型的变量

    struct student stu ={20 , 99.5, 'w',"zhangsan" };

     stu.stuAge =100;

    printf("%d\n",stu.stuAge);

    

   // 书的类型所对应的变量

    struct book book1 = { "iOS", 33.3, 100 , "zhuanjia", "dalian"};

    printf("%g\n",book1.bookMoney);

        

   //  如果想要改变字符串里面的内容,不能使用直接=的方式,还是用strcpy

    strcpy(book1.bookName, "sanguo");

    printf("%s\n",book1.bookName);

    // 只要是操作字符串,要用字符串的方法,比如strcpy等

     struct car car1 ={"dazhong",4,"white",290000};

    printf("%d\n",car1.carprice);

     int a = 10;

    ML  b =20;

    printf("%d\n",b);

    // 用新的变量名来定义一个变量

    Student stu1 = {20 , 50.5, 'w',"lisi"};

    printf("%c\n",stu1.stuSex);

posted @ 2015-07-08 17:41  天涯ml  阅读(146)  评论(0编辑  收藏  举报