Day10_c语言_高级指针练习

主函数部分:

//

//  main.m

//  Day10_高级指针

//

//  Created by 王佳兴 on 14-10-27.

//  Copyright (c) 2014 lanou3g.com 蓝鸥科技. All rights reserved.

//


#import <Foundation/Foundation.h>

#import "MYFunction.h"

第六题

#define RoundArea(A) ((A) * (A) * (3.14))



第七题

#define avg(A, B) (((A) + (B)) / 2)



int main(int argc, const char * argv[]) {

1. (**)写一个函数交换两个结构体变量

    MYExchanges a = {3, 5};

    MYExchanges b = {4, 6};

    MYExchanges *p = &a;

    exchanges(p, &b);

    printf("%d", (&a)->width);

    

    

    

2. (**)有一学生数组写一函数打印出指定分数段的学生信息

    MYStudent studentInfo[5] = {{"王佳兴", 100}, {"蛋头", 59}, {"伙夫", 90}, {"杨亭", 0}, {"快跑", 69}};

    printfInfo(studentInfo, 0, 100, 5);

    

    

    

3. (**)有一学生数组,包含5个学生,写一个函数,对学生排序(按学号 从小到大),使用结构体指针操作数组元素

     MYStudent studentInfo[5] = {{"王佳兴", 100}, {"蛋头", 59}, {"伙夫", 90}, {"老孟", 0}, {"快跑", 69}};

    studentSort(studentInfo, 5);

    

    

    

4. (**)有一学生数组,包含5个学生,写一个函数,对学生排序(按姓名 从小到大),使用结构体指针操作数组元素

     MYStudent studentInfo[5] = {{"wangjiaxing", 100}, {"dantou", 59}, {"huofu", 90}, {"laomeng", 0}, {"kuaipao", 69}};

    studentNameSort(studentInfo, 5);

    

    

    

5. (**)有一学生数组,包含5个学生,写一个函数,对学生排序(按分数 从小到大),使用结构体指针操作数组元素

    MYStudent studentInfo[5] = {{"王佳兴", 100}, {"蛋头", 59}, {"伙夫", 90}, {"老孟", 0}, {"快跑", 69}};

    studentSort(studentInfo, 5);


    

    

    6. (**)定义一个求圆面积的宏

    printf("%.2f", RoundArea(2));

    

    

    

    7. (**)定义一个求2个数平均数的宏

    printf("%.1f", avg(5.0, 8.0));

    

    return 0;

}


声明部分:

//

//  MYFunction.h

//  Day10_高级指针

//

//  Created by 王佳兴 on 14-10-27.

//  Copyright (c) 2014 lanou3g.com 蓝鸥科技. All rights reserved.

//


#import <Foundation/Foundation.h>

第一题

struct exchangeVariable{

    int width;

    int height;

};

typedef struct exchangeVariable MYExchanges;


void exchanges(MYExchanges *p1, MYExchanges *p2);


第二题


struct student{

    char name[20];

    int score;

};

typedef struct student MYStudent;


void printfInfo(MYStudent *p, int count1, int count2, int size);


第三题

struct student{

    char name[20];

    int score;

};

typedef struct student MYStudent;


void studentSort(MYStudent *p, int size);


第四题

struct student{

    char name[20];

    int score;

};

typedef struct student MYStudent;


void studentNameSort(MYStudent *p, int size);


第五题

struct student{

    char name[20];

    int score;

};

typedef struct student MYStudent;


void printfInfo(MYStudent *p, int count1, int count2, int size);


函数部分:

//

//  MYFunction.m

//  Day10_高级指针

//

//  Created by 王佳兴 on 14-10-27.

//  Copyright (c) 2014 lanou3g.com 蓝鸥科技. All rights reserved.

//


#import "MYFunction.h"

第一题

void exchanges(MYExchanges *p1, MYExchanges *p2)

{

    MYExchanges temp = *p1;

    *p1 = *p2;

    *p2 = temp;

}


第二题

void printfInfo(MYStudent *p, int count1, int count2, int size)

{

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

        if ((p + i)->score >= count1 && (p + i)->score <= count2) {

            printf("%s %d\n", (p + i)->name, (p + i)->score);

        }

    }


}


第三题

void studentSort(MYStudent *p, int size)

{

    for (int i = 0; i < size - 1; i++) {

        for (int j = 0; j < size - i - 1; j++) {

            if (((p + j)->score) > ((p + j + 1)->score)) {

                int temp1 = (p + j)->score;

                (p + j)->score = (p + j + 1)->score;

                (p + j + 1)->score = temp1;


                char temp2[20];

                strcpy(temp2, (p + j)->name);

                strcpy((p + j)->name, (p + j + 1)->name);

                strcpy((p + j + 1)->name, temp2);


            }

        }

    }

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

        printf("%s %d\n", (p + i)->name, (p + i)->score);

    }

}



第四题

void studentNameSort(MYStudent *p, int size)

{

    for (int i = 0; i < size - 1; i++) {

        for (int j = 0; j < size - i - 1; j++) {

            if (strcmp((p + j)->name, (p + j + 1)->name) > 0) {

                char temp1[20];

                strcpy(temp1, (p + j)->name);

                strcpy((p + j)->name, (p + j + 1)->name);

                strcpy((p + j + 1)->name, temp1);

                

                int temp2 = (p + j)->score;

                (p + j)->score = (p + j + 1)->score;

                (p + j + 1)->score = temp2;



            }

        }

    }


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

        printf("%s %d\n", (p + i)->name, (p + i)->score);

    }


}



第五题

void printfInfo(MYStudent *p, int count1, int count2, int size)

{

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

        if ((p + i)->score >= count1 && (p + i)->score <= count2) {

            printf("%s %d\n", (p + i)->name, (p + i)->score);

        }

    }


}


















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值