首页 > 代码库 > DAY-10作业
DAY-10作业
(**)写一个函数交换两个结构体变量
(**)有一学生数组写一函数打印出指定分数段的学生信息
(**)有一学生数组,包含5个学生,写一个函数,对学生排序(按学号 从小到大),使用结构体指针操作数组元素
(**)有一学生数组,包含5个学生,写一个函数,对学生排序(按姓名 从小到大),使用结构体指针操作数组元素
(**)有一学生数组,包含5个学生,写一个函数,对学生排序(按分数 从小到大),使用结构体指针操作数组元素
声明部分
struct student {
long number;
char name[20];
int age;
int grade;
};
typedef struct student Student;
//1.
void swap(Student *a, Student *b);
//2.
void printScore(Student *stu, int n);
//3.
void sort_num(Student *stu1);
//4.
void sort_name(Student *stu1);
//5.
void sort_grade(Student *stu1);
函数部分
//1.
void swap(Student *a, Student *b) {
Student c;
c.number = (*a).number;
strcpy(c.name, (*a).name);
c.age = (*a).age;
c.grade = (*a).grade;
(*a).number = (*b).number;
strcpy((*a).name, (*b).name);
(*a).age = (*b).age;
(*a).grade = (*b).grade;
(*b).number = c.number;
strcpy((*b).name, c.name);
(*b).age = c.age;
(*b).grade = c.grade;
}
//2.
void printScore(Student *stu, int n) {
for (int i = 0; i < n; i++) {
if ((stu + i)->grade >= 80 && (*(stu + i)).grade <= 90) {
printf("%ld %s %d %d\n", (stu[i]).number, (stu[i]).name, (stu[i]).age, (stu[i]).grade);
}
}
}
//3.
void sort_num(Student *stu1) {
for (int i = 0; i < 5 - 1; i++) {
for (int j = 0; j < 5 - 1 - i; j++) {
if ((stu1 + j) ->number > (stu1 + j + 1) ->number) {
long temp = (stu1 + j) ->number;
(stu1 + j) ->number = (stu1 + j + 1) ->number;
(stu1 + j + 1) ->number = temp;
}
}
}
for (int i = 0; i < 5; i++) {
printf("%ld %s %d %d\n", (stu1 + i) ->number, (stu1 + i) ->name, (stu1 + i) ->age, (stu1 + i) ->grade);
}
}
//4.
void sort_name(Student *stu1) {
for (int i = 0; i < 5 - 1; i++) {
for (int j = 0; j < 5 - 1 - i; j++) {
if (strcmp((stu1+ j) ->name, (stu1 + j + 1) ->name) > 0) {
char temp[20] = {0};
strcpy(temp, (stu1 + j) ->name);
strcpy((stu1 + j) ->name, (stu1 + j + 1) -> name);
strcpy((stu1 + j + 1) -> name, temp);
}
}
}
for (int i = 0; i < 5; i++) {
printf("%ld %s %d %d\n", (stu1 + i) ->number, (stu1 + i) ->name, (stu1 + i) ->age, (stu1 + i) ->grade);
}
}
//5.
void sort_grade(Student *stu1) {
for (int i = 0; i < 5 - 1; i++) {
for (int j = 0; j < 5 - 1 - i; j++) {
if ((stu1 + j) ->grade > (stu1 + j + 1) ->grade) {
int temp = (stu1 + j) ->grade;
(stu1 + j) ->grade = (stu1 + j + 1) ->grade;
(stu1 + j + 1) ->grade = temp;
}
}
}
for (int i = 0; i < 5; i++) {
printf("%ld %s %d %d\n", (stu1 + i) ->number, (stu1 + i) ->name, (stu1 + i) ->age, (stu1 + i) ->grade);
}
}
main函数部分
//
// main.m
// DAY-10作业
//
// Created by lanouhn on 15/1/23.
// Copyright (c) 2015年 lanouhn. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "fuction.h"
int main(int argc, const char * argv[]) {
//1.
// Student a = {11153023, "wangxuewen", 23, 90};
// Student b = {11153025, "weizijian", 22, 80};
// swap(&a, &b);
// printf("%ld %s %d %d\n", a.number, a.name, a.age, a.grade);
// printf("%ld %s %d %d\n", b.number, b.name,b.age, b.grade);
//2.
// Student stu[] = {
// {11153021, "wangxuewen", 23, 85},
// {11153022, "weizijian", 22, 75},
// {11153022, "wanhao", 20, 80},
// {11153024, "yaochengzheng", 26, 95}
// };
// printScore(stu, 4);
//3.
Student stu1[] = {
{11153022, "weizijian", 22, 75},
{11153022, "wanhao", 20, 80},
{11153021, "wangxuewen", 23, 85},
{11153024, "yaochengzheng", 26, 95},
{11153025, "yangchao", 18, 70}
};
// sort_num(stu1);
//4.
// sort_name(stu1);
//5.
// sort_grade(stu1);
return 0;
}
DAY-10作业