mian.m
#import <Foundation/Foundation.h>
#import "Function.h"
#define kSqureOfCircle(R) ((3.1415926)*(R)*(R))
#define kAverageOfTwoValue(A,B) (((A)+(B))/(2))
int main(int argc, const char * argv[]) {
// 2.(***)在主函数中输入6个字符串(二维数组),对他们按从小到大的顺 序,然后输出这 6 个已经排好序的字符串。要求使用指针数组进行处理。
// char array[6][10] = {"apple","iphone","ipad","iWatch","macBook","iphone6"};
// rankString(array);
// 1. (**)写一个函数交换两个结构体变量
// STUDENT S1 = {"zhangsan",'m',20,80};
// STUDENT S2 = {"lisi",'f',22,68};
// STUDENT TEMP = {};
// swapStruct(&S1, &S2,&TEMP);
// 2. (**)有一学生数组写一函数打印出指定分数段的学生信息
// STUDENT stus[3] = {
// {"zhangsan", 'M', 25, 98.2},
// {"lisi", 'F', 21, 85.7},
// {"wangwu", 'M', 18, 93.6},
// };
// _printStudents(stus,3);
// 3. (**)有一学生数组,包含5个学生,写一个函数,对学生排序(按学号 从小到大),使用结构体指针操作数组元素
// STU stus[5] = {
// {"zhangsan", 7220, 25, 98.2},
// {"lisi", 7209, 21, 85.7},
// {"wangwu", 7230, 18, 93.6},
// {"zhengchao",7211,29,80},
// {"sunli",7215,23,78}
// };
// orderByNum(stus,sizeof(stus)/sizeof(STU));
// for (int i = 0; i < sizeof(stus)/sizeof(STU); i++) {
// printf("%s,%d,%d,%f\n",(stus + i)->name,(stus + i)->num,(stus + i)->age,(stus + i)->score);
// }
//4. (**)有一学生数组,包含5个学生,写一个函数,对学生排序(按姓名 从小到大),使用结构体指针操作数组元素
// STU stus[5] = {
// {"zhangsan", 7208, 25, 98.2},
// {"lisi", 7209, 21, 85.7},
// {"wangwu", 7210, 18, 93.6},
// {"zhengchao",7211,29,80},
// {"sunli",7220,23,78}
// };
// orderByName(stus,sizeof(stus)/sizeof(STU));
// for (int i = 0; i < sizeof(stus)/sizeof(STU); i++) {
// printf("%s,%d,%d,%f\n",(stus + i)->name,(stus + i)->num,(stus + i)->age,(stus + i)->score);
// }
// 5. (**)有一学生数组,包含5个学生,写一个函数,对学生排序(按分数 从小到大),使用结构体指针操作数组元素
// STUDENT stus[5] = {
// {"zhangsan", 'M', 25, 98.2},
// {"lisi", 'F', 21, 85.7},
// {"wangwu", 'M', 18, 93.6},
// {"zhengchao",'m',29,80},
// {"sunli",'f',23,78}
// };
// orderStudent(stus, sizeof(stus)/sizeof(STUDENT));
// for (int i = 0; i < sizeof(stus)/sizeof(STUDENT); i++) {
// printf("%s,%c,%d,%f\n",(stus+ i)->name,(stus + i)->sex,(stus + i)->age,(stus + i)->score);
// }
// 6. (**)定义一个求圆面积的宏
// printf("%5.2f\n",kSqureOfCircle(3));
// 7. (**)定义一个求2个数平均数的宏
printf("%5.2f",kAverageOfTwoValue(4.3,5.3));
return 0;
}
#import <Foundation/Foundation.h>
@interface Function : NSObject
typedef struct STudent{
char name[20];
int num;
int age;
float score;
}STU;
//2.(***)在主函数中输入 6 个字符串(二维数组),对他们按从小到大的顺 序,然后输出这 6 个已经排好序的字符串。要求使用指针数组进行处理。
//void rankString(char (*array)[10]);
//1. (**)写一个函数交换两个结构体变量
typedef struct Student{
char name[20];
char sex;
int age;
float score;
}STUDENT;
void swapStruct(STUDENT *str1,STUDENT *str2,STUDENT *TEMP);
//2. (**)有一学生数组写一函数打印出指定分数段的学生信息
//void _printStudents(STUDENT *stus,int count);
//3.
//void orderByNum(STU *stus,int count);
//4.
//void orderByName(STU *stus,int count);
//5. (**)有一学生数组,包含5个学生,写一个函数,对学生排序(按分数 从小到大),使用结构体指针操作数组元素
//void orderStudent(STU *stus,int count);
@end
function.m中
#import "Function.h"
@implementation Function
//2.(***)在主函数中输入 6 个字符串(二维数组),对他们按从小到大的顺 序,然后输出这 6 个已经排好序的字符串。要求使用指针数组进行处理。
//void rankString(char (*array)[10]){
// for (int i = 0; i < 6; i ++) {
// for (int j = 0; j < 10; j ++) {
// if (strcmp(*(array + j), *(array + j +1)) > 0) {
// char temp[10] = {};
// strcpy(temp, array[i]);
// strcpy(array[i], array[i + 1]);
// strcpy(array[i + 1], temp);
// }
// }
// }
// for (int i = 0; i < 6; i++) {
// printf("%s ",*(array + i));
// }
//}
//1. (**)写一个函数交换两个结构体变量
void swapStruct(STUDENT *str1,STUDENT *str2,STUDENT *TEMP){
strcpy (TEMP->name,str1->name);
strcpy(str1->name, str2->name);
strcpy(str2->name, TEMP->name);
TEMP->sex = str1->sex;
str1->sex = str2->sex;
str2->sex = TEMP->sex;
TEMP->age = str1->age;
str1->age = str2->age;
str2->age = TEMP->age;
TEMP->score = str1->score;
str1->score = str2->score;
str2->score = TEMP->score;
printf("%s,%c,%d,%f\n",str1->name,str1->sex,str1->age,str1->score);
printf("%s,%c,%d,%f",str2->name,str2->sex,str2->age,str2->score);
}
//2. (**)有一学生数组写一函数打印出指定分数段的学生信息
//void _printStudents(STUDENT *stus,int count){
// for (int i = 0; i < count; i++) {
// if ((*(stus + i)) . score > 90) {
// printf("%s,%d,%c,%f\n",(stus+ i)->name,(stus + i)->age,(stus + i)->sex,(stus + i)->score);
// }
// }
//}
//3
//void orderByNum(STU *stus,int count){
// BOOL needNext = YES;
// for (int i = 0; i < count - 1 && needNext; i ++) {
// for (int j = 0; j < count - 1 - i ; j ++) {
// if ((stus + j)->num > (stus + j + 1) ->num) {
// STU temp = *(stus + j);
// *(stus + j) = *(stus + j +1);
// *(stus + j + 1) = temp;
// needNext = YES;
// }
// }
// }
//}
//4
//void orderByName(STU *stus,int count){
// BOOL needAgain = YES;
// for (int i = 0; i < count - 1&& needAgain ; i ++) {
// needAgain = NO;
// for (int j = 0; j < count - 1 - i; j ++) {
// if (strcmp((stus + j)->name, (stus + j + 1)->name) > 0) {
// STU temp = *(stus + j);
// *(stus + j) = *(stus + j + 1);
// *(stus + j + 1) =temp;
// needAgain = YES;
// }
// }
// }
//}
//5. (**)有一学生数组,包含5个学生,写一个函数,对学生排序(按分数从小到大),使用结构体指针操作数组元素
//void orderStudent(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;
// }
// }
//}
//
//}
@end