// // main.m // LessonPointerPro //// Copyright (c) 2015年 池海涛. All rights reserved. // #import <Foundation/Foundation.h> #import "Function.h" #define PI 3.1415926 #define kMul(A,B) (A)*(B) ////条件编译 ////形式1 //#ifdef PI //#define kPPPI 0 //#else //#define kPPPPI 1 //#endif // ////形式2 //#ifndef kP // //#define kP 3 // //#else // //#define kP 4 // //#endif // ////形式3 //#if 0 // //#define kRong 0 // //#else // //#define kRight 1 // //#endif //预编译指令 //以#开头的叫预编译指令:预编译时期做一些文本及代码的替换工作 //PI 代表宏名,3.14是预编译时期会被替换的内容 int main(int argc, const char * argv[]) { // //指针,指针变量 // int a = 10; // int b = 6; // change(&a,&b); // //指针与函数 // //练习:交换两个数的值 // printf("a = %d,b = %d",a,b); /** * int a[5] = {0}; int count = sizeof(a) / sizeof(a[0]); getArray(a,count); */ // char strArray[4][20] = {"iPhone", "iPad", "ipod", "iWatch"}; //指针数组 /** * 字符串 char *strPinter[4] = {"iPhone", "iPad", "ipod", "iWatch"}; printStr(strPinter,4); sortStrArray(strPinter,4); printStr(strPinter,4); */ //指针数据类型:Student * //指针变量 :P //初值:&student,存储的是student的地址 //指针变量 p 所占的字节: 8个字节 // // Student student = {"chihaitao",‘m‘,23,100}; // // Student *p = &student; //通过指针访问结构体成员 /** * *p 相当于student结构体变量 */ // printf("%s \n",student.name); // // printf("%s \n",(*p).name); /** * 方式2 CPoint c1 = {4,5}; CPoint c2 = {7,1}; CPoint *p1 = &c1; CPoint *p2 = &c2; printf("%f ", qiujuli(p1,p2)); // printf("%s \n",p->name);//通过指针,直接进行访问, ->指向操作符 //使用->输出结构体变量student中的所有成员变量 */ /** * <#Description#> Student student = {"chihaitao",‘m‘,23,100}; Student stuArray[5] = { {"chichi",‘m‘,25,99}, {"haihai",‘w‘,22,98}, {"taotao",‘w‘,23,97}, {"niuniu",‘w‘,22,98}, {"bibi",‘w‘,23,97}, }; //->就是指针操作 (指针)->(结构体成员) Student *stu = stuArray; //字符串不能直接赋值,字符串指针能赋值 (stu+4)->name = "fengfeng"; (stu+1)->age = 120; printf("%s",(stu + 4)->name); printf("%d",(stu+1)->age); printf("%d",stuArray[4].age); //printf("%s",m1->name); printf("%f",stu[4].score); printf("%p",(stu + 4)); printf("%p",&stuArray[4]); //按学生年龄升序排列 for (int i = 0; i < 5 -1; i++) { for (int j = 0;j < 5 - 1 - i; j++) { if ((stu + j)->age > (stu+j + 1)->age) { Student temp = *(stu + j); *(stu + j) = *(stu + j + 1); *(stu + j + 1)=temp; } } } */ //定义宏 //普通宏 //PI 代表宏名 int mul = kMul(3 + 1, 5); printf("mul = %d\n",mul); /** * 宏与函数的区别 1.宏是在预编译时期进行替换的内容,不进行任何的逻辑检测,只是简单的赋值而已,运行速度比函数快 2.宏定义十不考虑参数的类型 3.参数宏在定义时记得多加括号 4.参数宏在使用时会再目标文件中村在多个副本,会增加目标文件的大小 */ #ifdef PI //如果定义了 PI printf("PI已经定义过了\n"); #else printf("PI没有定义\n"); #endif #ifndef PI //如果没有定义 PI printf("PI这个宏没有定义\n"); #else printf("早就定义过了\n"); #endif #if 1 //和条件判断if else 用法一样 printf("优衣库被查了"); #else printf("你说啥 听不懂\n"); #endif return 0; }
//--------Function.h
// // Function.h // LessonPointerPro // // Created by laouhn on 15/7/27. // Copyright (c) 2015年 池海涛. All rights reserved. // #import <Foundation/Foundation.h> struct student{ char *name; char sex; int age; float score; }; typedef struct student Student; typedef struct{ char *name; char sex; int age; float score; } Student1; struct cpoint{ float x; float y; }; typedef struct cpoint CPoint; void change(int *,int *); void getArray(int *,int); void print(int *,int); void sortArray(int *,int); void printStr(char *[],int); void sortStrArray(char *[],int); float qiujuli(CPoint *,CPoint *);
//Function.m
// // Function.m // LessonPointerPro // // Created by laouhn on 15/7/27. // Copyright (c) 2015年 池海涛. All rights reserved. // #import "Function.h" void change(int *a,int *b) { int temp = *a; *a = *b; *b = temp; } void getArray(int *p,int count) { for (int i = 0; i < count; i++) { *(p + i) = arc4random() % (100 - 10 + 1) + 10; } print(p,count); sortArray(p, count); print(p, count); } void print(int *a,int count) { for (int i = 0; i < count; i++) { printf("%d ",*(a+i)); } printf("\n"); } void sortArray(int *a,int count) { for (int i = 0; i < count - 1; i++) { for (int j = 0; j < count - i -1; j++) { if (*(a + j) > *(a + j + 1)) { int temp = *(a + j); *(a + j) = *(a + j +1); *(a + j + 1) = temp; } } } } void printStr(char *b[],int count) { for (int i = 0; i < count; i++) { printf("%s ",*(b + i)); } } void sortStrArray(char *str[],int count) { for (int i = 0; i < count - 1; i++) { for (int j = 0; j < count - 1 - i; j++) { if (strcmp(str[j],str[j + 1] ) > 0) { char *temp = str[j]; str[j] = str[j + 1]; str[j + 1] = temp; } } } } float qiujuli(CPoint *a,CPoint *b){ float x = fabsf(a->x - b->x); float y = fabsf(a->y - b->y); return sqrtf(x*x + y*y); }
时间: 2024-10-12 17:27:00