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