700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > 预编译宏定义

预编译宏定义

时间:2022-06-12 05:09:38

相关推荐

预编译宏定义

总结: 1.结构体指针如何人访问成员 2.结构体数组与指针如何访问某个元素的成员 3.条件编译如何进行条件编译 #import <Foundation/Foundation.h> //预编译

//宏定义

#define PI 3.1415926//#define 的第一种形式

#define S(a,b) (a*b)//#define 的第二种形式

//注意:1推荐用大写 驼峰命名法 k+PI 或 PI

// 2宏不是变量

//pragma

#pragma mark add函数

void add (int a,int b){

}

#pragma mark sayHi函数

void sayHi(){

}

#pragma mark main 函数

//条件编译

//条件编译的第一种形式

#ifdef PI

int a = 10;

#else

int a = 20;

#endif

//条件编译的第二种形式

#ifndef wangnima

//没有定义宏定义过,执行代码段一

int b = 30;

#else

int b = 40;

#endif

//条件编译的第三种形式

#if 1

int c = 50;

#else

int c = 60;

#endif

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

printf("%d\n",a); printf("%d\n",b); printf("%d\n",c); return 0; } #import <Foundation/Foundation.h>

typedef struct CPoint{

float X;

float y;

}CPoint;

typedef struct student{

char name[20];

char gender;

float score;

}Student;

typedef struct person{

char name[20];

int age;

}person;

//打印person的name

void printName(person p){

printf("%s\n",p.name);

}

//打印数组的成员的name

void printArrName(person arr[],int n){//(person *arr[],int n)

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

printf("%s\n",(arr + i)->name);

}

}

void scoreName(Student arr[],int n){

Student *p = arr;

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

if ((p+i)->gender == 'm') {

(p+i)->score += 10;

if ((p+i)->score > 100) {

(p+i)->score = 100;

}

}

}

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

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

}

}

void ScoreName(Student *p,int n){

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

if ((p+i)->gender == 'm') {

(p+i)->score += 10;

if ((p+i)->score > 100) {

(p+i)->score = 100;

}

}

}

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

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

} } int main(int argc, const char * argv[]) { Student students[4] = {{"tangmaru",'m',91},{"wangnima",'m',96},{"ruhua",'f',80},{"zhangquandan",'m',90}}; Student *ps = students; for (int i = 0; i < 4; i++) { if ((ps+i)->gender == 'm') { (ps+i)->score += 10; if ((ps+i)->score > 100) { (ps+i)->score = 100; }}} for (int j = 0; j < 4; j++) { printf("%s %c %.2f\n",(ps+j)->name,(ps+j)->gender,(ps+j)->score);} Student students[4] = {{"liubei",'m',98},{"zhangfei",'m',97},{"gaunyu",'m',98},{"zhaoyun",'m',99}}; scoreName(students, 4); ScoreName(students, 4); // person p1 = {"wangnima",16}; // person p2 = {"tangmaru",18};

// person p3 = {"ruhua",20};

// person arr[3] = {p1,p1,p3};

// printArrName(arr,3);

// printName(p1);

//函数操作结构体数组时需要传入两个参数

//1数组元素的首地址(结构体数组名)2结构体数组元素的个数 //结构体指针 // CPoint arr = {10,20};

// CPoint *p1= &arr;

// p1 = &arr;

// // arr.X 用点取出结构体变量的成员变量

// printf("%.2f\n",(*p1).X); //指针取结构体变量的成员变量 用*取值,然后再. //练习:定义一个学生的结构体,里面有两个成员变量 1 姓名 2 年龄 用指针打印成员变量

// Student student = {"袁新峰",78};

// Student student1 = {"刘振艺",18};

// Student student2 = {"王明伟",17};

// Student student3 = {"赵成浩",16};

// Student *ps = &student,*ps1 = &student1,*ps2 = &student2,*ps3 = &student3;

// ps = &student;

// ps1 = &student1;

// ps2 = &student2;

// ps3 = &student3;

// printf("%s %d \n",(*ps).name,(*ps).age);

// printf("%s %d \n",(*ps1).name,(*ps1).age);

// printf("%s %d \n",(*ps2).name,(*ps2).age);

// printf("%s %d \n",(*ps3).name,(*ps3).age);

// printf("%s %d \n",ps->name,ps->age);

//结构体指针访问成员变量,还可以用箭头这个符号

// CPoint num1 = {10,30};

// CPoint num2 = {10,5};

// CPoint *pn1 = &num1;

// CPoint *pn2 = &num2;

// pn1 = &num1;

// pn2 = &num2;

// float num = 0;

// num = sqrtf(((pn1->X-pn2->X)*(pn1->X-pn2->X))+((pn1->y-pn2->y)*(pn1->y-pn2->y)));

// printf("%.2f\n",num);

// Student stu = {1, "lan ou",'m',89};

// Student *ps = &stu;

// ps = &stu;

ps->name[0] -= 32;

// ps->name[0] ^= 32;//大小写互相转换用^=32

ps->name[0]= 'L';

char a1 = ps->name[0] - 32;

ps->name[0] = a1;

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

// //结构体数组数组名就是数组首元素的地址 也就是这个结构体数组的指针

// Student students[5] = {{1, "lan ou",'m',89},{2, "lan ou",'m',89},{3, "lan ou",'m',89},{4, "lan ou",'m',89},{5, "lan ou",'m',89}};

// Student *pst = students; // students[0]->number; // pst->number; // printf("%d\n",pst->number);

// printf("%d\n",(pst+1)->number);

// printf("%d\n",(pst+2)->number);

// printf("%d\n",(pst+3)->number);

// printf("%d\n",(*(pst+4)).number);

// printf("%d\n",(*pst).number);

// //*(p+i)等价于p[i] // for (int i = 0; i < 5; i++) { // printf("%d %s %c %f\n",(pst+i)->number,(pst+i)->name,(pst+i)->gender,(pst+i)->score); // }

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

// //结构体指针也是一个指针,他所占字节只与操作系统有关

// //64位系统占8个字节 32位系统占4个字节 // printf("%lu\n",sizeof(pst));

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。