Bubble sort, Swich statement, while loop ... A basic integrated problem beginner can do a simple test

Source: Internet
Author: User

This problem is not much difficulty, but is a relatively basic point of knowledge application!

To learn OC has certain help.

1. create a pair of files Student.h student.m

2. in Student.h, define a student structure, member variables include: name, gender, age, school number, score

3. Declare a function that prints student. parameter is a struct-body pointer

4. declare a function that prints all the students in the array

5. declare a function that implements the sorting of the student array, ascending by name

6. declare a function that implements the sorting of students ' arrays in descending order of age

7. declare a function that implements the sorting of the student's array, ascending according to the number of learners

8. declare a function that implements the sorting of students ' arrays in descending order of fractions

9. declare a function that outputs all male students in the student array

declaring a function that outputs all female students in the student array

11, from the console input 1~6 between the numbers, respectively, call the above 6 functions, using switch/case implementation.

12. If you enter a number other than 1~6, the output "does not have a corresponding number, please re-enter"

13. Use the while loop for repeated input

14. Define enumeration types to represent numbers between 1~6, Switch/case use enumeration values.

Main function Section

#import <Foundation/Foundation.h>

#import "Student.h"//Note header file

Declares an enumeration type that describes a number between 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 ("Input 0: Exit program \ n");//Enter 0 to exit, and the input character should also exit. If you want to implement this function, you can choose a number other than 0~6

printf ("Input 1: implementation in ascending order by name \ n");

printf ("Input 2: implementation in descending order of age \ n");

printf ("Input 3: implementation is in ascending order of the number of learning numbers \ n");

printf ("Input 4: implementation in descending order of fractions \ n");

printf ("Input 5: Achieve output for all boys \ n");

printf ("Input 6: Implement output for all girls \ \");

printf ("=========================\n");

printf ("Please enter a number corresponding to the implementation function:");

int functionnum = 0; Save the entered number

scanf ("%d", &functionnum);

GetChar ();

printf ("\ n");

if (functionnum <= 0) {

printf ("Exit program");

Break

}

Call the corresponding function according to the number entered

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 ("No corresponding function, please re-enter!") \ n ");

Break

}

}

}

return 0;

}

Declare the. h file section

Declaring student structures

struct student{

Char names[20];

char sex;

int age;

int number;

Float score;

};

typedef struct student student;

All the information for the output structure body. Parameters: struct-body pointers

void Printstudent (Student * s);

Output information for all elements in the struct array

void Printallstudent (Student * stus, int count);

Sort by name Ascending

void Sortascendbyname (Student * stus, int count);

In descending order of age

void Sortdescendbyage (Student * stus, int count);

In ascending order of the school number

void Sortascendbynumber (Student * stus, int count);

Sort in descending order of fractions

void Sortdescendbyscore (Student * stus, int count);

Output All Boys

void Printmalestudent (Student * stus, int count);

Definition. m section

All the information for the output structure body. Parameters: struct-body pointers

void Printstudent (Student * s) {

printf ("%-*s%c%d%d%.1f\n", ten, S->names, S->sex, S->age, S->number, S->score);

}

Output information for all elements in the struct array

void Printallstudent (Student * stus, int count) {

for (int i = 0; i < count; i++) {

Printstudent (Stus+i);

}

}

Sort by name Ascending

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;

}

}

}

}

In descending order of age

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;

}

}

}

}

In ascending order of the school number

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;

}

}

}

}

Sort in descending order of fractions

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;

}

}

}

}

Output All Boys

void Printmalestudent (Student * stus, int count) {

for (int i = 0; i < count; i++) {

if (Stus[i].sex = = ' m ') {

Printstudent (Stus+i);

}

}

}

Output All Girls

void Printfemalestudent (Student * stus, int count) {

for (int i = 0; i < count; i++) {

if (Stus[i].sex = = ' F ') {

Printstudent (Stus+i);

}

}

}

Output All Girls

void Printfemalestudent (Student * stus, int count);

Bubble sort, Swich statement, while loop ... A basic integrated problem beginner can do a simple test

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.