您的位置:首页 > 其它

结构体指针

2015-07-10 20:42 429 查看
主函数

// main.m

// C9_结构体指针

//

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

// Copyright (c) 2015年 cml. 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 0

int a =10;

#define TEST 20

#else

int a =50;

#define TEST 50

#endif

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

// printf("%ld\n",sizeof(Stu));

//

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

// Stu stu = {18 , 70.5 , 'w',"zhangsan"};

// // 用 . 来访问成员变量

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

//

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

// Stu temp = stu;

// // 常量地址(数组不可以直接赋值)

// int arr[2]={1,2};

// x int arr1[2]=arr;

// 指针变量

// int *p =NULL;

// int a =10;

//// char str[20]="";

//// scanf("%s",str);

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

// p =&a;

// // *取值符

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

//

// int arr[5] ={3,4,6,5,1};

// int *p=arr;

// bubble(p, 5);

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

// printf("%d\n",arr[i]);

// }

// 自定义的数据类型

// Stu stu ={20, 70.5 ,'W',"yanglin"};

// Stu *p =&stu;

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

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

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

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

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

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

// //

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

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

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

//

////

// Cpoint m={8,11};

// Cpoint n={5,7};

// Cpoint *p1=&m;

// Cpoint *p2=&n;

// distance(p1,p2);

//

// Student stu ={1,"l an ou",'m',95.6};

// Student *p=&stu;

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

//

// change( p);

// 结构体数组

// Student stu1 = {20,75.5,'w',"zhangsan"};

// Student stu2 = {30,80.5,'m',"lisi"};

// Student stu3 = {33,90.5,'w',"wangwu"};

// Student stu4 = {23,87,'m',"yanglin"};

// 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->stuName);

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

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

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

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

// }

//

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

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

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

// if (p[i].stuScore<p[i+1].stuScore) {

// Student temp ;

//

// temp=p[j];

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

// p[j+1]=temp;

// }

// }

// }

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

// printf("%g\n",p[i].stuScore);

// }

// 平均成绩和平均年龄

// int ageCount =0;

// float scoreCount =0;

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

// ageCount += (p+i)->stuAge;

// scoreCount += (p+i)->stuScore;

// }

// printf("%d\n",ageCount/4);

// 结构体数组练习

// grade(&stu);

// Student *p =&stu;

//

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

// printf("%g\n",p[i].stuScore);

// }

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

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

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

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

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

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

// while(1){

// int enterCard = 0;

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

// int enterPassWord =0;

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

// Person *p=per;

// int result =checkPassCardAndPassWord(enterCard, enterPassWord, p, 4);

// if (result == 4) {

// printf("错误\n");

// continue ;

// }else {

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

// }

// }

// 宏定义

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

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

// int a =10 ,b =20;

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

// printf("%d\n",MUL(3+4, b));

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

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

return 0;

}

.h 文件

//

// MyFunction.h

// C9_结构体指针

//

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

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

//

#import <Foundation/Foundation.h>

// 声明结构体

// 学生 : 姓名,年龄,性别,成绩,最后用typedef

//

struct student{

int stuAge;

float stuScore;

char stuSex;

char stuName[20];// 成员变量

};

typedef struct student Student;

//struct student{

// long a;

// char c;

//

// char d;

// int b;

//};

//typedef struct student Stu;

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

//void bubble(int *p,int count);

//

//// 开方

//struct cpoint{

// float x;

// float y;

//};

//typedef struct cpoint Cpoint;

//void distance(Cpoint *p1,Cpoint *p2);

//struct stu{

// int num;

// char name[20];

// char sex;

// float score;

//};

//typedef struct stu Student;

//void change(Student *stu);

//void grade(Student *stu);

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

struct person{

char name[30];

int cardNum;

int passWord;

int moneyCount;

};

typedef struct person Person;

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

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

int checkMoney(int postion, Person *p,int getMoney);

.m文件

/ MyFunction.m

// C9_结构体指针

//

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

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

//

#import "MyFunction.h"

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

//

//void bubble(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]) {//*(p+j)=p[j]

// int temp =p[j];

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

// p[j+1]=temp;

// }

// }

// }

//}

//

//void distance(Cpoint *p1,Cpoint *p2){

// // 取地址

// float distance =0;

//

// distance=sqrt(((p1->x)-(p2->x))*((p1->x)-(p2->x))+((p1->y)-(p2->y))*((p1->y)-(p2->y)));

//

// printf("%g\n",distance);

//}

//

//

//

//void change(Student *stu){

//

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

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

// stu->name[0] -= ('a'-'A');

// }

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

// if (stu->name[i]==' ') {

// stu->name[i]='_';

// }

// }

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

//}

//

//void grade(Student *stu){

//

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

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

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

// }

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

// stu[i].stuScore=100;

// }

//

// }

//

//}

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

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

if ((enterCard == p[i].cardNum)&&(passWord==p[i].passWord)) {

return i ;

}

}

return 4;

}

int checkMoney(int postion, Person *p,int getMoney){

while(1){

scanf("%d",&getMoney);

if (getMoney>p[postion].moneyCount) {

printf("余额不足,请重新输入\n");

}else if (getMoney==p[postion].moneyCount){

// 钱正好没了

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

printf("余额为:%d\n",p[postion].moneyCount);

}else{

p[postion].moneyCount -= getMoney;

printf("余额为:%d\n",p[postion].moneyCount);

}

}

return 1;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: