#import <Foundation/Foundation.h>
#import "Recursion.h"
typedef struct student{
char name[10];
int age;
float score;
}Student;
//输出单个学生信息
void outputStudentInfo(Student stu)
{
printf("%s %d %.2f\n", stu.name, stu.age, stu.score);
}
//输出所有学生的信息
void outputAllStudentInfo(Student stu[], int count)
{
for (int i = 0; i < count; i++) {
// printf("%s %d %.2f\n", stu[i].name, stu[i].age, stu[i].score);
outputStudentInfo(stu[i]);
}
}
//将学生按成绩升序排序
void sortStudentAscByScore(Student stu[], int count)
{
for (int i = 0; i < count - 1; i++) {
for (int j = 0; j < count - 1 - i; j++) {
if (stu[j].score > stu[j + 1].score) {
Student temp = stu[j];
stu[j] = stu[j + 1];
stu[j + 1] = temp;
}
}
}
}
//将学生按年龄降序排序
void sortStudentDescByAge(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 sortStudentAscByName(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;
}
}
}
}
int main(int argc, const char * argv[])
{
Student stu[5] = {
{"xiaoHong", 18, 90},
{"xiaoMong", 26, 100},
{"xiaoWang", 21, 70},
{"xiaoLi", 14, 96},
{"xiaoZhang", 30, 80}
};
outputAllStudentInfo(stu, 5);
printf("\n");
/*
eatApple(10);
reverse(54321);
printf("\n");
positive(54321);
printf("\n");
printf("%d", factorial(5));
*/
/*
//结构体和函数的关系.
Student xiaoMing = {"xiaoHong", 18, 90};
// //输出xiaoMing的信息
// printf("%s %d %.2f\n", xiaoMing.name, xiaoMing.age, xiaoMing.score);
Student xiaoHong = {"xiaoMing", 20, 100};
// //输出xiaoHong的信息
// printf("%s %d %.2f\n", xiaoHong.name, xiaoHong.age, xiaoHong.score);
outputStudentInfo(xiaoMing);
outputStudentInfo(xiaoHong);
*/
//成绩升序
sortStudentAscByScore(stu, 5);
outputAllStudentInfo(stu, 5);
printf("\n");
//年龄降序
sortStudentDescByAge(stu, 5);
outputAllStudentInfo(stu, 5);
printf("\n");
//姓名升序
sortStudentAscByName(stu, 5);
outputAllStudentInfo(stu, 5);
printf("\n");
return 0;
}
**************************************************************************************************************
#import <Foundation/Foundation.h>
//函数模拟吃苹果
void eatApple(int n);
//倒序
void reverse(int number);
//正序
void positive(int number);
//阶乘
int factorial(int number);
#import "Recursion.h"
//递归是调用一个和自己功能完全相同的函数.
//函数模拟吃苹果
void eatApple(int n)
{
//一旦发现苹果个数为0时,通过return结束当前函数执行.
//递归一定要有出口,否则会造成死循环.
if (n == 0) {
return;
}
//如果不为0
//1.留一个苹果
n--;
//2.找下一个人来吃苹果
eatApple(n);
//3.吃自己手里的苹果
printf("第%d个人吃苹果\n", 10 - n);
}
//倒序
void reverse(int number)
{
if (number == 0) {
return;
}
//1.留个数
int n = number % 10;
//2.报数
printf("%d", n);
//3.找下个人报数
reverse(number / 10);
}
//正序
void positive(int number)
{
if (number == 0) {
return;
}
int n = number % 10;
positive(number / 10);
printf("%d", n);
}
//阶乘
int factorial(int number)
{
if (number == 1 || number == 0) {
return 1;
}
return number * factorial(number - 1);
}