C语言07 -- 结构体多文件管理

来源:互联网 发布:qq主题软件 编辑:程序博客网 时间:2024/06/03 20:38

//
// main.m
// C7 – 结构体多文件
//
// Created by dllo on 15/7/8.
// Copyright (c) 2015年 Gaozi. All rights reserved.
//

import

import “MY Foundation.h”

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

STUDENT STU = {23,"HAHA",90.5};printStu(STU);STUDENT temp =  STU;printStu(temp);// 结构体可以进行直接赋值,但是数组不可以.STUDENT stu1 = {24,"zhangsan",92};STUDENT stu2 = {22,"lisi",93};STUDENT stu3 = {27,"xiaofeng",99};//找到两人中较好成绩/// 条件运算符.

float max = 0;
max = stu1.stuscore > stu2.stuscore ? stu1.stuscore : stu2.stuscore;
printf(“%g\n”,max);
// 找到成绩最好的人.

STUDENT temp = stu1.stuscore > stu2.stuscore ? stu1 : stu2;printStu(temp);// 调用函数.STUDENT temp =goodstu(stu1, stu2, stu3);printStu(temp);printStu(goodstu(stu1, stu2, stu3));

printf(“%ld”,sizeof(STUDENT));

printf(“%ld”,sizeof(TEXT));

// 找平均年龄

average(stu1, stu2, stu3);
// 两个点是否在一个水平线,是否垂直,是否相等.
cpoint point1 = {10,20};
cpoint point2 = {30,20};
if (judgepoint(point1, point2) == 1){
printf(“在一条水平线上\n”);
}else{
printf(“不在一条水平线上\n”);
}

DAYS dayandyear = {};scanf("%d%d%d",&dayandyear.year,&dayandyear.month,&dayandyear.day);dayofyear(dayandyear);printf("第%d天\n",dayofyear(dayandyear));STUDENT student1 = {22,"haha",96,'m'};STUDENT student2 = {23,"xixi",75,'w'};STUDENT student3 = {21,"lala",89,'w'};STUDENT student4 = {22,"aihaihai",69,'m'};STUDENT student5 = {20,"zhangdi",59,'w'};STUDENT student[5] = {student1,student2,student3,student4,student5};// 第二个人姓名. 遍历三个人姓名. printf("%s\n",student[1].stuname);for (int i = 0; i < 3; i++) {    printf("%s\n",student[i].stuname);}for (int i = 0; i < 3 - 1; i++) {    for (int j = 0; j < 3 - i - 1; j++) {        if (student[j].stuscore > student[j+1].stuscore) {            STUDENT temp = student[j];            student[j] = student[j+1];            student[j+1] = temp;        }    }}for (int i = 0; i < 3; i++) {    printf("%s\n",student[i].stuname);}// 用函数写 冒泡函数bubblesortstu(student, 3);passnum(student, 5);initialName(student, 5);namesequence(student, 5);// 结构体的嵌套// 定义一个学生类型的变量STUDENT stu1 = {23,"lalala",96,'w'};CLASSSTU room = {"yanfa1",stu1};printf("%s\n",room.roomName);printf("%s\n",room.stu.stuname);SCHOOL school = {"lanou",room};printf("%s\n",school.cla.stu.stuname);return 0;

}

//
// MY Foundation.h
// C7 – 结构体多文件
//
// Created by dllo on 15/7/8.
// Copyright (c) 2015年 Gaozi. All rights reserved.
//

import

import “MY Foundation.h”

// 打印学生类型变量的所有内容.
void printStu(STUDENT stu1){

printf("年龄 : %d,名字 : %s,分数 : %g \n",stu1.stuage,stu1.stuname,stu1.stuscore);

}
STUDENT goodstu(STUDENT stu1, STUDENT stu2, STUDENT stu3){
STUDENT temp = stu1.stuscore > stu2.stuscore ? stu1 : stu2;
temp = temp.stuscore > stu3.stuscore ? temp : stu3;

return temp;

}
float average(STUDENT stu1, STUDENT stu2, STUDENT stu3){
float temp = (stu1.stuage + stu2.stuage + stu3.stuage)/3.0;
printf(“%0.2f\n”,temp);
return temp;
}
// 判断两个点是否在水平线上/垂直线/相同?.
BOOL judgepoint(cpoint point1, cpoint point2){
if (point1.y == point2.y) {
return YES;
}
return NO;
}
BOOL rightAngles(cpoint p1, cpoint p2){
return p1.x == p2.x;
}
BOOL samepoint(cpoint p1, cpoint p2){
if (p1.x == p2.x && p1.y == p2.y) {
return YES;
}
return NO;
}
//判断第几天
int dayofyear(DAYS ymd){
switch (ymd.month - 1) {
case 11:
ymd.day += 30;
case 10:
ymd.day += 31;
case 9:
ymd.day += 30;
case 8:
ymd.day += 31;
case 7:
ymd.day += 31;
case 6:
ymd.day += 30;
case 5:
ymd.day += 31;
case 4:
ymd.day += 30;
case 3:
ymd.day += 31;
case 2:
if (ymd.year % 400 == 0 || (ymd.year % 4 == 0 && ymd.year % 100 != 0)) {
ymd.day += 29;
}else{
ymd.day += 29;
}
case 1:
ymd.day += 31;
}
return ymd.day;
}
void bubblesortstu(STUDENT student[], int count){
for (int i = 0; i < count - 1; i++) {
for (int j = 0; j < count - i - 1; j++) {
if (student[j].stuscore > student[j+1].stuscore) {
STUDENT temp = student[j];
student[j] = student[j+1];
student[j+1] = temp;
}

    }}for (int i = 0; i < count; i++) {    printf("第%d名 ",i+1);    printf("%s : %g\n",student[i].stuname,student[i].stuscore);}

}
int passnum(STUDENT student[],int count){
int len = 0;
for (int i = 0; i < count ; i++) {
if(student[i].stuscore >= 60){
len++;
}
}
printf(“有%d个人及格\n”,len);
return len;
}
int initialName(STUDENT student[],int count){
int num = 0;
for (int i = 0; i < count; i++) {
if (student[i].stuname[0] == ‘l’) {
num++;
}
}
printf(“%d\n”,num);
return num;
}
void namesequence(STUDENT student[], int count){
for (int i = 0; i < count- 1; i++) {
for (int j = 0; j < count - i - 1; j++) {
if (strcmp(student[j].stuname, student[j+1].stuname) > 0) {
STUDENT temp = student[j];
student[j] = student[j+1];
student[j+1] = temp;
}
}
}
for (int i = 0; i < count; i++) {
printf(“%s\n”,student[i].stuname);
}
}

0 0
原创粉丝点击