您的位置:首页 > 移动开发 > IOS开发

ios结构体

2015-07-22 21:44 387 查看
//
//  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\n",stu1.name);
stu1.age = 20;
printf("%d\n",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 \n", stu4.name);
}
else
stu4 = stu3;
printf("%s \n", 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\n", 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 \n", 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;
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: