// main.m
// Training_15-8-05
//
// Created by lanou3g on 15/8/5.
// Copyright (c) 2015年 lanou.3g.com. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Function.h"
struct Person{
char _name[20];
char sex;
int age;
float height;
};
//取别名
typedef struct Animal{
char _name[30];
char _sex;
int _age;
}Animal;
int main(int argc, const char * argv[]) {
//结构体声明
//关键字 ---struct
//第一种--在main函数外面声明
//定义一个结构体变量
// BOOL a=258;
// printf("%d\n",a);
// BOOL类型的空间分配---很有趣
//结构体的内存分配也有趣
struct Person person={"yibella",'f',20,1.65};
printf("姓名:%s\t性别:%c\t年龄:%d\t身高:%.2f\n",person._name,person.sex,person.age,person.height);
struct Person person0={"jack",'m',22,1.87};
printf("姓名:%s\t性别:%c\t年龄:%d\t身高:%.2f\n",person0._name,person0.sex,person0.age,person.height);
//访问结构体成员
//结构体成员变量的赋值
//1、初始化时赋值
//2、单个成员变量赋值
//3、结构体变量之间直接整体赋值
//修改其中一个成员变量的值person的身高
person.height=1.76;
printf("姓名:%s\t性别:%c\t年龄:%d\t身高:%.2f\n",person._name,person.sex,person.age,person.height);
//把person0的值全部赋值给person
person=person0;
printf("姓名:%s\t性别:%c\t年龄:%d\t身高:%.2f\n",person._name,person.sex,person.age,person.height);
printf("姓名:%s\t性别:%c\t年龄:%d\t身高:%.2f\n",person0._name,person.sex,person.age,person.height);
//typedef给结构体取别名--有两种方式
typedef struct Person P;
//用别名来定义结构体变量
P personOne={"heby",'m',23,1.76};
printf("姓名:%s\t性别:%c\t年龄:%d\t身高:%.2f\n",personOne._name,personOne.sex,personOne.age,personOne.height);
Animal monkey={"MONKEY",'f',10};
printf("种类:%s\t性别:%c\t年龄:%d\n",monkey._name,monkey._sex,monkey._age);
//结构体作为 函数参数
student student0={"shanshan",3,89};
int scoreofshanshan=returnScore(student0);
printf("%s的成绩:%d\n",student0._name,scoreofshanshan);
//函数在外部文件中声明定义
//以一个结构体作为另一个结构体的成员变量
Doctor doc[5]={
{26,{"jessca",4,98}},
{27,{"pippy",5,90}},
{4,{"hahaha",4,99}},
{14,{"yibella",5,95}},
{13,{"bella",1,94}}
};
sortAscendByPagesOfPaper(doc, 5);
//结构体数组作为 函数参数
student stu[3]={
{"yibella",39,99},
{"salary",23,95},
{"summer",12,87}
};
sortAscendByScore(stu, 3);
sortAscendByNumber(stu, 3);
return 0;
}
Function.h文件:
//
// Function.h
// Training_15-8-05
//
// Created by lanou3g on 15/8/5.
// Copyright (c) 2015年 lanou.3g.com. All rights reserved.
//
#import <Foundation/Foundation.h>
typedef struct Student{
char _name[20];
int _number;
int _score;
}student;
typedef struct Doctor{
int pagesOfPaper;
student stu0;
}Doctor;
//
void sortAscendByPagesOfPaper(Doctor doc[],int n);
//返回成绩
int returnScore(student stu);
//按成绩升序排列
void sortAscendByScore(student stu[],int n);
void sortAscendByNumber(student stu[],int n);
Function.m文件
//
// Function.m
// Training_15-8-05
//
// Created by lanou3g on 15/8/5.
// Copyright (c) 2015年 lanou.3g.com. All rights reserved.
//
#import "Function.h"
//
void sortAscendByPagesOfPaper(Doctor doc[],int n){
int pages=0;
Doctor doc0={0};
for (int i=0; i<n; i++) {
for (int j=0; j<n-i-1; j++) {
if (doc[j].pagesOfPaper>doc[j+1].pagesOfPaper) {
pages=doc[j].pagesOfPaper;
doc0=doc[j];
doc[j].pagesOfPaper=doc[j+1].pagesOfPaper;
doc[j]=doc[j+1];
doc[j+1].pagesOfPaper=pages;
doc[j+1]=doc0;
}
}
}
printf("按论文数量升序排列:\n");
for (int i=0; i<n ; i++) {
printf("姓名:%s\t学号:%d\t成绩:%d\t论文数:%d\n",doc[i].stu0._name,doc[i].stu0._number,doc[i].stu0._score,doc[i].pagesOfPaper);
}
}
//返回成绩
int returnScore(student stu){
int score=0;
score=stu._score;
return score;
}
//按学号
void sortAscendByNumber(student stu[],int n){
int tempNumber=0;
student tempStudent={0};
for (int i=0; i<n ; i++) {
for (int j=0; j<n-i-1; j++) {
if (stu[j]._number>stu[j+1]._number) {
tempNumber=stu[j]._number;
tempStudent=stu[j];
stu[j]._number=stu[j+1]._number;
stu[j]=stu[j+1];
stu[j+1]._number=tempNumber;
stu[j+1]=tempStudent;
}
}
}
printf("按学号排序:\n");
for (int i=0; i<n ; i++) {
printf("姓名:%s\t学号:%d\t成绩:%d\n",stu[i]._name,stu[i]._number,stu[i]._score);
}
}
//按成绩
void sortAscendByScore(student stu[],int n){
int tempScore=0;
student tempStudent={0};
for (int i=0; i<n ; i++) {
for (int j=0; j<n-i-1; j++) {
if (stu[j]._score>stu[j+1]._score) {
tempScore=stu[j]._score;
tempStudent=stu[j];
stu[j]._score=stu[j+1]._score;
stu[j]=stu[j+1];
stu[j+1]._score=tempScore;
stu[j+1]=tempStudent;
}
}
}
printf("按成绩升序排列:\n");
for (int i=0; i<n ; i++) {
printf("姓名:%s\t学号:%d\t成绩:%d\n",stu[i]._name,stu[i]._number,stu[i]._score);
}
}