ios 구조 체

19939 단어 ios
//
//  main.m
//  LessonStruct
////  Copyright (c) 2015     . All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Function.h"
int main(int argc, const char * argv[]) {

    /*
     //     
     struct     {
              ;
     ...
          
     };
        ,        :         ,                      ,                   ,       
     
     
               
     1.                        ,         ,             
     2.                              ,    
     3.      ,    
     */
    /**
     *        
     1.        (    ),        .
     2.        ,     ,             
     */


    Student stu1 = {"angelbaby",'m',18,59.9};
    Student stu2 = {"bibi",'w',23,33.3};
    Student stu3 = {"nobibi",'w',24,34.4};
    Student stu4 = {"shuine",'2',21,44.4};
    Student stu[5] = {
        {"lichen",'m',20,38},
        {"jianrenzeng", 'm', 56, 19},
        {"wenzhang", 'm', 45, 59.9},
        {"bobo", 'w', 18, 66},
        {"zhenzhen",'w',17,83}};
                 
    printf("%s",stu[0].name);
    int count = sizeof(stu) / sizeof(Student);
    sortByAge(stu, count);
    sortByName(stu, count);
    printAllStudent(stu, count);
    Woman woman1 = {"kongkong",'w',28,{"xiaowang",'m',2}};
    printf("%s",woman1.sonl.name);
               
    printf("%s %c %d %f", stu[0].name, stu[0].gender, stu[0].age, stu[0].score);
    printAllStudent(stu,count);
    
    
    stu1 = stu2;//         
     int a[10] = {1}; int b[10] = {3};          
         ,               
    strcpy(stu1.name, "xiaoming");
    printf("%s
",stu1.name); stu1.age = 20; printf("%d
",stu1.age); 3 , , stu1.age > sut2.age ? stu1.age > stu3.age ? stu1.age :stu3.age :stu2.age; char maxname[20]; if (stu1.age > stu2.age) { stu4 = stu1; } else stu4 = stu2; if (stu4.age > stu3.age) { printf("%s
", stu4.name); } else stu4 = stu3; printf("%s
", stu4.name); if (stu1.score > stu2.score) { stu4 = stu1; } else{ stu4 = stu2; } if (stu4.score > stu3.score) { printStudent(stu4); } else{ printStudent(stu3); } return 0; }

//---------------
//
//  Function.h
//  LessonStruct
//
//  Created by laouhn on 15/7/22.
//  Copyright (c) 2015     . All rights reserved.
//

#import <Foundation/Foundation.h>
struct student{
    char name[20];
    char gender;
    int age;
    float score;
};
typedef struct student Student;


struct teacher{
    char name[20];
    char gender;
    int age;
    float score;
};
typedef struct teacher Teacher;

//       
typedef struct dog{
    char name[20];
    char sex;
} Person;
typedef struct miaomiao{
    char name[20];
    char sex;
    int age;
} Miaomiao;
typedef struct son{
    char name[20];
    char sex;
    int age;
} Son;
typedef struct woman{
    char name[20];
    char sex;
    int age;
    Son sonl;
} Woman;
typedef  struct men{
    char name[20];
    char sex;
    int age;
    Woman wife;
} Men;
//      ,   ,   
struct dot {
    float abscissa;//   
    float ordinate;//   
};
void printStudent(Student);
void printAllStudent(Student[],int);
void sortByAge(Student stu[],int count);
void sortByName(Student stu[],int count);

//---------
//
//  Function.m
//  LessonStruct
////  Copyright (c) 2015     . All rights reserved.
//

#import "Function.h"
void printStudent(Student stu)
{
    printf("%s,%c,%d,%.2f
", stu.name, stu.gender, stu.age, stu.score); } void printAllStudent(Student stu[],int n) { //int count = sizeof(stu) / sizeof(Student); for (int i = 0; i < n; i++) { printf("%s %c %d %f
", stu[i].name, stu[i].gender, stu[i].age, stu[i].score); } } void sortByAge(Student stu[],int count) { for (int i = 0; i < count - 1; i++) { for (int j = 0; j < count - 1 - i; j++) { if (stu[j].age > stu[j + 1].age) { Student temp = stu[j]; stu[j] = stu[j + 1]; stu[j + 1] = temp; } } } } void sortByName(Student stu[],int count){ for (int i = 0; i < count - 1; i++) { for (int j = 0; j < count - 1 - i; j++) { if (strcmp(stu[j].name,stu[j+1].name) < 0) { Student temp = stu[j]; stu[j] = stu[j + 1]; stu[j + 1] = temp; } } } }

좋은 웹페이지 즐겨찾기