冒泡排序,swich语句,while循环...基础性的一道综合题初学者可以做一个简单的测试...
這道題是沒有什么太大的難度,但是是比較基礎性的知識點的應用!
對學習OC有一定的幫助.
1. 創建一對文件Student.h ? Student.m
2. 在Student.h中,定義一個Student結構體,成員變量包括:姓名、性別、年齡、學號、分數
?3. 聲明一個打印Student的函數。參數是結構體指針
4. 聲明一個打印數組中所有學生的函數
5. 聲明一個實現學生數組排序的函數,按照姓名升序
6. 聲明一個實現學生數組排序的函數,按照年齡降序
7. 聲明一個實現學生數組排序的函數,按照學號升序
8. 聲明一個實現學生數組排序的函數,按照分數降序
9. 聲明一個輸出學生數組中全部男學生的函數
10. 聲明一個輸出學生數組中全部女學生的函數?
11、從控制臺輸入1~6之間的數字,分別調用上述6個函數,使用switch/case實現。
12、如果輸入1~6以外的數字,輸出“沒有對應的數字,請重新輸入”
13、使用while循環實現反復輸入
14、定義枚舉類型表示1~6之間的數字,switch/case使用枚舉值。
?
?//main函數部分
#import <Foundation/Foundation.h>
#import "Student.h"//注意頭文件
// 聲明枚舉類型,描述1~6之間的數字
enum FunctionName{
? ? SortAscendByName = 1,
? ? SortDescendByAge,
? ? SortAscendByNumber,
? ? SortDescendByScore,
? ? PrintMaleStudent,
? ? PrintFemaleStudent
};
?
int main(int argc, const char * argv[]) {
? ? Student stu1 = {"a", 'm', 19, 5, 90};
? ? Student stu2 = {"c", 'm', 26, 1, 89};
? ? Student stu3 = {"v", 'f', 20, 3, 92};
? ? Student stu4 = {"b", 'm', 24, 4, 97};
? ? Student stu5 = {"x", 'm', 22, 2, 95};
?? ?
? ? Student stus[5] = {stu1, stu2, stu3, stu4, stu5};
?? ?
?? ?
? ? while (YES) {
? ? ? ? printf("\n");
? ? ? ? printf("輸入0:退出程序\n");//這里輸入0退出,輸入字符應該也退出.如果非要實現這個功能可以通過case選用除0~6之外數字
? ? ? ? printf("輸入1:實現按照姓名升序排列\n");
? ? ? ? printf("輸入2:實現按照年齡降序排列\n");
? ? ? ? printf("輸入3:實現按照學號升序排列\n");
? ? ? ? printf("輸入4:實現按照分數降序排列\n");
? ? ? ? printf("輸入5:實現輸出所有男生\n");
? ? ? ? printf("輸入6:實現輸出所有女生\n");
? ? ? ? printf("=========================\n");
? ? ? ? printf("請輸入實現功能對應的數字:");
? ? ? ? int functionNum = 0; // 保存輸入的數字
? ? ? ? scanf("%d", &functionNum);
? ? ? ? getchar();
? ? ? ? printf("\n");
? ? ? ? if (functionNum <= 0) {
? ? ? ? ? ? printf("退出程序");
? ? ? ? ? ? break;
? ? ? ? }
? ? ? ? // 根據輸入的數字,調用對應的函數
? ? ? ? switch (functionNum) {
? ? ? ? ? ? case SortAscendByName: {
? ? ? ? ? ? ? ? sortAscendByName(stus, 5);
? ? ? ? ? ? ? ? printAllStudent(stus, 5);
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? ? ? case SortDescendByAge: {
? ? ? ? ? ? ? ? sortDescendByAge(stus, 5);
? ? ? ? ? ? ? ? printAllStudent(stus, 5);
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? ? ? case SortAscendByNumber: {
?? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? sortAscendByNumber(stus, 5);
? ? ? ? ? ? ? ? printAllStudent(stus, 5);
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? ? ? case SortDescendByScore: {
?? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? sortDescendByScore(stus, 5);
? ? ? ? ? ? ? ? printAllStudent(stus, 5);
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? ? ? case PrintMaleStudent: {
?? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? printMaleStudent(stus, 5);
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? ? ? case PrintFemaleStudent: {
?? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? printFemaleStudent(stus, 5);
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? ? ? default: {
? ? ? ? ? ? ? ? printf("沒有對應的函數,請重新輸入!\n");
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? }
? ? }
?? ?
? ? return 0;
}
?//聲明.h文件部分
// 聲明學生結構體
struct student{
? ? char names[20];
? ? char sex;
? ? int age;
? ? int number;
? ? float score;
};
typedef struct student Student;
?
// 輸出結構體的所有信息。參數:結構體指針
void printStudent(Student * s);
?
// 輸出結構體數組中所有元素的信息
void printAllStudent(Student * stus, int count);
?
// 按照姓名升序排列
void sortAscendByName(Student * stus, int count);
?
// 按照年齡降序排列
void sortDescendByAge(Student * stus, int count);
?
// 按照學號升序排列
void sortAscendByNumber(Student * stus, int count);
?
// 按照分數降序排列
void sortDescendByScore(Student * stus, int count);
?
// 輸出全部男生
void printMaleStudent(Student * stus, int count);
//定義.m部分
// 輸出結構體的所有信息。參數:結構體指針
void printStudent(Student * s){
? ? printf("%-*s? %c? %d? %d? %.1f\n", 10, s->names, s->sex, s->age, s->number, s->score);
}
?
// 輸出結構體數組中所有元素的信息
void printAllStudent(Student * stus, int count){
? ? for (int i = 0; i < count; i++) {
? ? ? ? printStudent(stus+i);
? ? }
}
?
// 按照姓名升序排列
void sortAscendByName(Student * stus, int count){
? ? for (int i = 0; i < count-1; i++) {
? ? ? ? for (int j = 0; j < count-1-i; j++) {
? ? ? ? ? ? if (strcmp(stus[j].names, stus[j+1].names) > 0) {
? ? ? ? ? ? ? ? Student temp = stus[j];
? ? ? ? ? ? ? ? stus[j] = stus[j+1];
? ? ? ? ? ? ? ? stus[j+1] = temp;
? ? ? ? ? ? }
? ? ? ? }
? ? }
}
?
// 按照年齡降序排列
void sortDescendByAge(Student * stus, int count){
? ? for (int i = 0; i < count-1; i++) {
? ? ? ? for (int j = 0; j < count-1-i; j++) {
? ? ? ? ? ? if (stus[j].age < stus[j+1].age) {
? ? ? ? ? ? ? ? Student temp = stus[j];
? ? ? ? ? ? ? ? stus[j] = stus[j+1];
? ? ? ? ? ? ? ? stus[j+1] = temp;
? ? ? ? ? ? }
? ? ? ? }
? ? }
}
?
// 按照學號升序排列
void sortAscendByNumber(Student * stus, int count){
? ? for (int i = 0; i < count-1; i++) {
? ? ? ? for (int j = 0; j < count-1-i; j++) {
? ? ? ? ? ? if (stus[j].number > stus[j+1].number) {
? ? ? ? ? ? ? ? Student temp = stus[j];
? ? ? ? ? ? ? ? stus[j] = stus[j+1];
? ? ? ? ? ? ? ? stus[j+1] = temp;
? ? ? ? ? ? }
? ? ? ? }
? ? }
}
?
// 按照分數降序排列
void sortDescendByScore(Student * stus, int count){
? ? for (int i = 0; i < count-1; i++) {
? ? ? ? for (int j = 0; j < count-1-i; j++) {
? ? ? ? ? ? if (stus[j].score < stus[j+1].score) {
? ? ? ? ? ? ? ? Student temp = stus[j];
? ? ? ? ? ? ? ? stus[j] = stus[j+1];
? ? ? ? ? ? ? ? stus[j+1] = temp;
? ? ? ? ? ? }
? ? ? ? }
? ? }
}
?
// 輸出全部男生
void printMaleStudent(Student * stus, int count){
? ? for (int i = 0; i < count; i++) {
? ? ? ? if (stus[i].sex == 'm') {
? ? ? ? ? ? printStudent(stus+i);
? ? ? ? }
? ? }
}
?
// 輸出全部女生
void printFemaleStudent(Student * stus, int count){
? ? for (int i = 0; i < count; i++) {
? ? ? ? if (stus[i].sex == 'f') {
? ? ? ? ? ? printStudent(stus+i);
? ? ? ? }
? ? }
}
?
?
// 輸出全部女生
void printFemaleStudent(Student * stus, int count);
?
轉載于:https://www.cnblogs.com/lyh1993/p/4819894.html
總結
以上是生活随笔為你收集整理的冒泡排序,swich语句,while循环...基础性的一道综合题初学者可以做一个简单的测试...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux bind-utils
- 下一篇: 用java实现zip压缩