2016年4月

读取文件内容

//
//  main.c
//  读取文件的内容
//
//  Created by zhang on 16/4/10.
//  Copyright © 2016年 jin. All rights reserved.
//

#include <stdio.h>
/**
 输出文件包含的内容
 */
void test1()
{
    FILE *file = fopen("/Users/zhang/xcode/算法与数据结构/读取文件的内容/test.h", "r");
    //    file = fopen("test.h", "r");
    if (file != NULL) {
        char temp;
        while (fscanf(file, "%c",&temp) != EOF) {
            printf("%c",temp);
        }
        fclose(file);
    }
}

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

    return 0;
}

九宫格字谜

//
//  main.c
//  算法与数据结构
//
//  Created by zhang on 16/4/10.
//  Copyright © 2016年 jin. All rights reserved.
//

#include <stdio.h>
#include <string.h>
void checkStr(const char str[]);
// 单词最大长度
#define WORDMAXLENGTH 10
// 九宫格基础数据
char data[][WORDMAXLENGTH] = {
    {'t','h','i','s'},
    {'w','a','t','s'},
    {'o','a','h','g'},
    {'f','g','d','t'},
};
// 字典,出现在这里面的词语就会
char dictionary[][WORDMAXLENGTH] =
{
    "this",
    "what",
    "fat",
    "good",
    "that"
};
/**
 查找九宫格中出现的单词
 */
void test1()
{
    // 获得最大的x轴和y轴坐标
    unsigned long maxX = strlen(data[0]);
    int maxY = sizeof(data) / sizeof(data[0]);
    // 循环遍历查找
    for (int x = 0; x < maxX; x++) {
        for (int y = 0; y < maxY; y++) {
            // 从左上角的字母开始
            char tempStringX[WORDMAXLENGTH] = {data[y][x]};
            char tempStringXY[WORDMAXLENGTH] = {data[y][x]};
            char tempStringXY2[WORDMAXLENGTH] = {data[y][x]};
            // 当前坐标,横向的所有组合
            for (int offsetX = 1; offsetX < (maxX - x); offsetX++) {
                tempStringX[offsetX] = data[y][x + offsetX];
                checkStr(tempStringX);
//                printf("%s\n",tempStringX);
            }
            // 当前坐标,纵向的所有组合
            char tempStringY[WORDMAXLENGTH] = {data[y][x]};
            for (int offsetY = 1; offsetY < (maxY - y); offsetY++) {
                tempStringY[offsetY] = data[y + offsetY][x];
                checkStr(tempStringY);
//                printf("%s\n",tempStringY);
            }
            // 当前坐标,斜对角的所有组合,从左往右
            for (int offsetXY = 1; offsetXY < (maxX - x); offsetXY++) {
                // 左上方向
                if (offsetXY < (maxY - y)) {
                    tempStringXY[offsetXY] = data[y + offsetXY][x + offsetXY];
                    checkStr(tempStringXY);
//                    printf("%s\n",tempStringXY);
                }
                // 当前坐标,斜对角的所有组合,右上方向
                if (offsetXY <= y) {
                    tempStringXY2[offsetXY] = data[y - offsetXY][x + offsetXY];
                    checkStr(tempStringXY2);
                    //                    printf("%s\n",tempStringXY);
                }
            }
        }
    }
    printf("%i\n",maxY);
}
/**
 翻转字符串
 */
void strReversal(char str[])
{
    char temp[WORDMAXLENGTH] = "";
    unsigned long length = strlen(str);
    for (int i = length; i >= 0; i--) {
//        temp[length - i] = str[i - 1];
        if ((length - i) >= (i - 1)) {
            break;
        }
        str[length - i] = str[i - 1] ^ str[length - i];
        str[i - 1] = str[i - 1] ^ str[length - i];
        str[length - i] = str[i - 1] ^ str[length - i];
    }
    char *tempPointer = temp;
//    return tempPointer;
}
void checkStr(const char str[])
{
    int length = sizeof(dictionary) / sizeof(dictionary[0]);
    for (int i = 0; i < length ; i++) {
        if (strcmp(str, dictionary[i]) == 0) {
            printf("找到一个:%s\n",str);
        }
        char tempStr[WORDMAXLENGTH];
        strcpy(tempStr, str);
        strReversal(tempStr);
        if (strcmp(tempStr, dictionary[i]) == 0) {
            printf("找到一个:%s\n",tempStr);
        }
    }
}
int main(int argc, const char * argv[]) {
    test1();
    return 0;
}

static和extern

//
//  main.c
//  static和extern
//
//  Created by admin on 16/4/1.
//  Copyright © 2016年 jin. All rights reserved.
//

#include <stdio.h>

/**
 static:
    对局部变量
        1.延长变量生命周期,从执行开始到程序退出
        2.定义的这行代码只会执行一次
        3.不会改变变量的作用域
 */
void test1()
{
    static int num = 1;
    num++;
    printf("%i\n",num);
}
int num = 1;
/**
 extern:
    对局部变量
        1.表面当前的变量使用的是全局变量
 */
void test2()
{
    extern int num;
    num++;
    printf("%i\n",num);
}
/**
 全局变量:
    1.外部变量:在所有的文件中都可以被访问
    2.内部变量:只能在当前文件中被访问
 static:声明一个内部变量
 extern:完整声明一个外部变量(全局变量默认就是外部变量),如果使用extern初始化一个变量,则系统不会自动再初始化这个变量,在整个程序中都不能重复定义

 函数:
 1.外部函数:在所有的文件中都可以被访问
 2.内部函数:只能在当前文件中被访问
 static:声明一个内部函数
 extern:完整声明一个外部函数(函数默认就是外部函数),如果使用extern初始化一个变量,则系统不会自动再初始化这个变量,在整个程序中都不能重复定义

 */
int main(int argc, const char * argv[]) {
    /**
    test1();// 2
    test1();// 3
    test1();// 4
    test1();// 5
     */
    test2();// 2
    test2();// 2
    test2();// 2
    test2();// 2
    return 0;
}

编译预处理指令

//
//  main.c
//  编译预处理指令
//
//  Created by admin on 16/4/1.
//  Copyright © 2016年 jin. All rights reserved.
//

#include <stdio.h>
/**
 源码 -> 编译预处理 -> 编译 -> 链接 -> 运行
 编译预处理:在编译前进行解析处理的指令
    1.宏 #define
        1.1.带参数 #define SUM(num1,num2) num1 + num2 ,在出现运算符的时候,因为宏只是做了下替换,所以需要注意下运算符的先后执行顺序
        1.2.不带参数 #define NAME "小芳"
    2.条件编译

    3.包含 #include
 */
#define NAME "小芳"
#define SUM(num1,num2) num1 + num2
int main(int argc, const char * argv[]) {
    // insert code here...
    printf("%s\n",NAME);
    printf("%i\n",SUM(1, 2));
    return 0;
}

条件编译

//
//  main.c
//  条件编译
//
//  Created by admin on 16/4/1.
//  Copyright © 2016年 jin. All rights reserved.
//

#include <stdio.h>
// 满足条件的代码才会被编译
#define MY_TEST_DEBUG 1// 不注释,main里面的会输出内容,否则,什么也不输出
#ifdef MY_TEST_DEBUG// 判断宏是否定义了
#define LOG(formatter,...) printf(formatter,## __VA_ARGS__)
// __VA_ARGS__ 固定写法,表示多参数
// ## 表示可以不戴参数
#else
#define LOG(formatter,...)
#endif
int main(int argc, const char * argv[]) {
    // insert code here...
    LOG("Hello, World!\n");
    return 0;
}

文件包含就不写了。。。

c中结构体和枚举的使用

//
//  main.c
//  c中的结构体
//
//  Created by admin on 16/4/1.
//  Copyright © 2016年 jin. All rights reserved.
//

#include <stdio.h>

// 结构体的基本使用
void test1()
{
    // 定义结构体
    struct Person
    {
        char *name;
        int age;
        char sex;
    };
    // 初始化一个结构体变量
    struct Person p;
    // 给结构体变量赋值
    p.name = "xiaofang";
    p.age = 18;
    p.sex = 'w';
    printf("%s今年就%i岁了,性别%c\n",p.name,p.age,p.sex);
}
// 结构体复制的多种方式
void test2()
{
    // 定义结构体
    struct Person
    {
        char *name;
        int age;
        char sex;
    };
    // 定义一个结构体变量
    struct Person p;
    // 逐个给结构体变量赋值
    p.name = "xiaofang";
    p.age = 18;
    p.sex = 'w';
    printf("%s今年就%i岁了,性别%c\n",p.name,p.age,p.sex);

    // 定义一个结构体变量
    struct Person p1;
    // 整体给结构体赋值,顺序要和结构体定义时的成员顺序一致,再强转
    p1 = (struct Person){"小明",18,'m'};
    printf("%s今年就%i岁了,性别%c\n",p1.name,p1.age,p1.sex);

    // 定义一个结构体变量
    struct Person p2;
    // 使用有个已经定义好的结构体变量来赋值
    p2 = p;
    printf("%s今年就%i岁了,性别%c\n",p2.name,p2.age,p2.sex);
}
// C编译器将按照n个字节对齐。下面输出Person的内存占用的时候会输出14
#pragma pack(2)
// 结构体内存
void test3()
{
    // 定义结构体
    struct Person
    {
        char *name;// 8
        int age;
        char sex;
    };
    // 默认会是占内存空间最大的成员的内存的倍数
    printf("%lu\n",sizeof(struct Person));// 16
}
struct Person
{
    char *name;
    int age;
};
// 结构体数组
void test4()
{
    int length = 3;
    struct Person persons[length];
    // 逐个赋值,没有初始化的数组项会被初始化为0
    persons[0] = (struct Person){"小芳",18};
    persons[1] = (struct Person){"小明",18};
    for (int i = 0; i < length; i++) {
        printf("%s今年%i岁了\n",persons[i].name,persons[i].age);
    }

    // 定义数组的同时完成赋值
    struct Person persons1[] = {{"小芳",18},{"小明",18},{"小芳2",18}};
    // 逐个赋值,没有初始化的数组项存放着垃圾值
    for (int i = 0; i < length; i++) {
        printf("%s今年%i岁了\n",persons1[i].name,persons1[i].age);
    }
}
// 结构体指针
void test5()
{
    struct Person person = (struct Person){"小芳",18};
    // 定义结构体指针
    struct Person *personPointer = &person;
    // 通过指针访问结构体
    printf("%s今年%i岁了\n",(*personPointer).name,(*personPointer).age);
    // 这样也可以访问
    printf("%s今年%i岁了\n",personPointer->name,personPointer->age);
}
struct Data
{
    int year;
    int month;
    int day;
};
// 这个结构体有个成员是结构体
struct Employee
{
    char *name;
    char sex;
    struct Data birthday;
    struct Employee *employee;// 结构体不能嵌套自己,但是可以嵌套自己类型的指针
};
// 结构体嵌套
void test6()
{
    // 初始化
    struct Employee employee = {"小芳",'w',{1992,11,11}};
    printf("雇员:%s,%c,生日是%i-%i-%i\n",employee.name,employee.sex,employee.birthday.year,employee.birthday.month,employee.birthday.day);
    // 对指向自己类型的成员赋值
    employee.employee = &((struct Employee){"小明",'m',{1992,12,12}});
    printf("雇员:%s,%c,生日是%i-%i-%i,他的下属:%s,%c,生日是%i-%i-%i\n",employee.name,employee.sex,employee.birthday.year,employee.birthday.month,employee.birthday.day,employee.employee->name,employee.employee->sex,employee.employee->birthday.year,employee.employee->birthday.month,employee.employee->birthday.day);
}
// 结构体函数
// 结构体作为参数是值传递
void test7(struct Person person)
{
    person.age = 19;
}
// 传递结构体的地址
void test8(struct Person *person)
{
    person->age = 19;
}
int main(int argc, const char * argv[]) {
//    test6();
    struct Person p = (struct Person){"小芳",18};
    printf("%s今年%i岁了\n",p.name,p.age);
    test8(&p);
    printf("%s今年%i岁了\n",p.name,p.age);
    return 0;
}

枚举

enum Sex
{
    Man,Woman
};
int main(int argc, const char * argv[]) {

    enum Sex sex = Man;

    switch (sex) {
        case Man:
            printf("boy\n");
            break;
        case Woman:
            printf("girl\n");
            break;
        default:
            printf("Are you kidding me?\n");
            break;
    }

    return 0;
}