码迷,mamicode.com
首页 > 其他好文 > 详细

C9_结构体指针

时间:2015-07-13 22:13:55      阅读:93      评论:0      收藏:0      [点我收藏+]

标签:

.h

//

//  MyFunction.h

//  C9_结构体指针

//

//  Created by dllo on 15/7/10.

//  Copyright (c) 2015年 zhozhicheng. All rights reserved.

//

 

#import <Foundation/Foundation.h>

// 结构体

// 学生:姓名,年龄,性别,成绩

struct student{

    char stuName[20];

    int  stuAge;

    char stuSex;

    float stuScore;  //成员变量

};

typedef struct student Student;

 

// 用指针来进行冒泡排序

void bubbleSort(int *p,int count);

// 声明CPoint类型的结构体

typedef struct cpoint{

    float x;

    float y;

}CPoint;

void distant(CPoint *p1,CPoint *p2);

//struct student {

//    int stuNum;

//    char school[20];

//    char stuSex;

//    float stuScore;

//};

//typedef struct student Student;

//void changeName(Student *stu)

void changeScore(Student *stu,int count);

// 姓名,账号,密码,余额

struct person{

    char name[30];

    int cardNum;

    int passWord;

    int moneyCount;

};

typedef struct person Person;

 

int checkCardAndPassWord(int enterCard,int passWord,Person *p,int count);

// 如果钱正好被取走,余额为0,在姓名后面拼接一个 "00",并且把取走之后的钱数修改并打印,还需要判断钱数够不够

void changeMoney(int money,Person *p, int index );

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 .m

//

//  MyFunction.m

//  C9_结构体指针

//

//  Created by dllo on 15/7/10.

//  Copyright (c) 2015年 zhozhicheng. All rights reserved.

//

 

#import "MyFunction.h"

void bubbleSort(int *p,int count){

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

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

            if (p[j]>p[j+1]) {

                int temp=0;

                temp=p[j];

                p[j]=p[j+1];

                p[j+1]=temp;

            }

        }

    }

}

 

void distant(CPoint *p1,CPoint *p2){

    printf("%g\n",p1->x);

    float result=sqrtf((p1->x - p2 ->x )*(p1->x - p2 ->x )+(p1->y - p2->y)*(p1->y - p2->y));

    printf("%g\n",result);

}

 

 

//void changeName(Student *stu){

//    // 判断首字母是不是小写

//    if (stu->school[0]>=‘a‘ && stu->school[0] <= ‘z‘ ) {

//        stu->school[0] -= 32;

//        

//    }for (int i=0 ; i<strlen(stu->school); i++) {

//        if (stu->school[i] == ‘ ‘ ) {

//            stu->school[i] =‘_‘;

//        }

//    }printf("%s\n",stu->school);

//    

//    

//}

 

//void changeScore(Student *stu,int count){

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

//        if (stu[i].stuSex==‘m‘) {

//            stu[i].stuScore += 10;

//            if (stu[i].stuScore >100) {

//                stu[i].stuScore = 100;

//            }

//        }

//    }

//    

//}

 

 

int checkCardAndPassWord(int enterCard,int passWord,Person *p,int count){

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

        if ((p+i)->cardNum == enterCard && passWord == (p+i)->passWord) {

        return i;

            

            

        }

    }return 4;

}

 

void changeMoney(int money,Person *p, int index ){

    //判断钱够不够

    if (p[index].moneyCount < money) {

        printf("大哥你钱不够\n");

    }else{

        p[index].moneyCount -= money;

        if (p[index].moneyCount == 0) {

            // 钱正好没了,对姓名进行拼接

            strcat(p[index].name, "00");

        }

        printf("%d\n",p[index].moneyCount);

    }

    

    

    

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 main

//

//  main.m

//  C9_结构体指针

//

//  Created by dllo on 15/7/10.

//  Copyright (c) 2015年 zhozhicheng. All rights reserved.

//

 

#import <Foundation/Foundation.h>

#import"MyFunction.h"

//宏是以个替换过程,把后面内容进行替换

#define PI 3.14

// 带参数的宏

 

// 计算两个数的成绩

#define MUL(A,B) (A)*(B)

// 要把括号加上,防止这种因为算术符号优先的问题影响

 

 

#define MAXVALUE(A,B) A>B?A:B

 

//#ifdef PI

//#define TEST 20

//#else

//#define TEST 50

//#endif

 

//第二种

//#ifndef PI

//#define TEST 20

//#else

//#define TEST 50

//#endif

 

//第三种

// 条件两种 一种0 一种1

#if 1

int a=10;

#define TEST 20

#else

int a=50;

#define TEST 50

#endif

 

 

 

 

 

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

 // 定义一个结构体类型的变量

//    Student stu={"zhangsan",17,‘w‘,95};

//    printf("%s\n",stu.stuName);

//    // 结构体可以进行直接赋值

//    Student temp =stu;

//    // 数组名称为常量地址

//

    

    

    // 指针变量

//    int *p = NULL;

//    int a =10;

//    char str[20]="";

//    scanf("%d",&a);

//    

//    printf("%p\n",&a);

//    p=&a;

//    // *取值符

//    printf("%d\n",*p);

    

    

    // 自定义的数据类型

//    Student stu={"yanglin",17,‘m‘,95};

//    Student *p=&stu;

//    printf("%p\n",p);

//    printf("%p\n",&stu);

//    // p保存的是结构体的地址,*p取到的结果相当于地质所对应的结构体的变量,之后的用法和结构体变量一样

//    printf("%s\n",(*p).stuName);

//    strcpy((*p).stuName, "lishanshan");

//    printf("%s\n",(*p).stuName);

//    printf("%p\n",&stu.stuAge);

//    printf("%p\n",&stu.stuScore);

//    printf("%s\n",p->stuName);

    

    

//    CPoint m={5.3 ,6};

//    CPoint n={8.3 ,10};

//    // 取地址

//    CPoint *p1= &m;

//    CPoint *p2= &n;

//    distant(p1, p2);

    

  

    

//    Student stu={1,"lan ou",‘m‘,95.6};

//    Student *p=&stu;

//    printf("%c\n",p->school[0]);

//    

    

    

//    //结构体数组

//    Student stu1={"zhangsan",17,‘w‘,95};

//    Student stu2={"lisi",19,‘m‘,83};

//    Student stu3={"wangwu",27,‘m‘,85};

//    Student stu4={"shenliu",18,‘w‘,75};

//    Student stu[4]={stu1,stu2,stu3,stu4};

////    printf("%p\n",stu);

////    printf("%p\n",&stu[0]);

//    Student *p = stu;

    // stu[i]和p[i]是一样的,取出来的是结构体,所以用.来找成员变量

    // (p+1)是根据指针来操作,用->来访问成员变量

//    printf("%s\n",(p+1)->stuName);

//    // 用指针的方式来遍历所有学生的姓名

//    for (int i=0; i<4; i++) {

//        printf("%s\n",(p+i)->stuName);

//    }

 

    // 根据学生的成绩,用指针对他进行排序,从小到大

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

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

//            if ((p+j)->stuScore > (p+j+1)->stuScore) {           //有问题

//                

//                Student temp = *(p+j);

//                *(p+j)=*(p+j+1);

//                *(p+j+1)=temp;

//            }

//        }

//    }for (int i=0; i<4; i++) {

//        printf("%s\n",(p+i)->stuName);

//    }

//    

    

//    

//    Person per1 ={"yanglin",111,123,100};

//    Person per2={"liushanshan",222,234,200};

//    Person per3={"shangshuai",333,345,300};

//    Person per4={"zhoushengmin",444,456,400};

//    Person per[4]={per1,per2,per3,per4};

//    // 输入账号密码,正确返回下标,不正确返回4

//    int enterCard=0;

//    scanf("%d",&enterCard);

//    int enterPassWord=0;

//    scanf("%d",&enterPassWord);

//    int result=checkCardAndPassWord(enterCard, enterPassWord, per, 4);

//    if (result == 4) {

//        printf("账号或者密码错误\n");

//        

//    }else{

//        printf("登陆成功\n");

//    }

   

    

    

  // 宏定义

  //定义一个宏,命名要不全大写,要不k+驼峰的方式

    

    printf("%g\n",PI+3);

    

    int a=10,b=20;

    printf("%d\n",MAXVALUE(a , b));

    

    

    printf("%d\n",a);

    

    

    

    

    

    return 0;

}

 

C9_结构体指针

标签:

原文地址:http://www.cnblogs.com/zhaozhicheng/p/4644000.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!