標籤:
//// 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);}
ios指標第二天