2018年6月

# include <stdio.h>
# include <malloc.h>
# include <stdlib.h>

struct Node
{
    int data; //数据域
    struct Node * pNext; //指针域
};

//函数声明
struct Node * create_list(void);
void traverse_list(struct Node *);

int main(void)
{
    struct Node * pHead = NULL; //等价于 struct Node * pHead = NULL;

    pHead = create_list();  //create_list()功能:创建一个非循环单链表,并将该链表的头结点的地址付给pHead
    traverse_list(pHead);  //遍历输出每个节点
    
    return 0;
}

struct Node * create_list(void)
{
    int len;  //用来存放有效节点的个数
    int i;
    int val; //用来临时存放用户输入的结点的值

    //分配了一个不存放有效数据的头结点
    struct Node * pHead = (struct Node *)malloc(sizeof(struct Node));
    if (NULL == pHead)
    {
        printf("分配失败, 程序终止!\n");
        exit(-1);
    }
    struct Node * pTail = pHead;
    pTail->pNext = NULL;

    printf("请输入您需要生成的链表节点的个数: len = ");
    scanf("%d", &len);
    
    for (i=0; i<len; ++i)
    {
        printf("请输入第%d个节点的值: ", i+1);
        scanf("%d", &val);
        
        struct Node * pNew = (struct Node *)malloc(sizeof(struct Node));
        if (NULL == pNew)
        {
            printf("分配失败, 程序终止!\n");
            exit(-1);  //终止程序
        }
        pNew->data = val;
        pTail->pNext = pNew;
        pNew->pNext = NULL;
        pTail = pNew;
    }
    
    return pHead;
}

void traverse_list(struct Node * pHead)
{
    struct Node * p = pHead->pNext;

    while (NULL != p)
    {
        printf("%d  ", p->data);
        p = p->pNext;
    }
    printf("\n");
    
    return;
}

# include <stdio.h>

enum WeekDay
{
    MonDay, TuesDay, WednesDay, ThursDay, FirDay, SaturDay, SunDay
};   //默认从0开始。

int main(void)
{
    enum WeekDay day = TuesDay;
    printf("%d\n", day);   //  结果为 1

    return 0;
}
# include <stdio.h>

enum WeekDay
{
    MonDay = 2, TuesDay, WednesDay, ThursDay, FirDay, SaturDay, SunDay
};   //第一个枚举常量声明绝对值后,其后边按顺序升序

int main(void)
{
    enum WeekDay day = ThursDay;
    printf("%d\n", day);   //  结果为 5

    return 0;
}
# include <stdio.h>

enum WeekDay
{
    MonDay, TuesDay, WednesDay, ThursDay, FirDay, SaturDay, SunDay
}; 

void f(enum WeekDay i)
{
    printf("%d\n", i);
    switch (i)
    {
    case 0:
        printf("MonDay!\n");
        break;
    case 1:
        printf("TuesDay!\n");
        break;
    case 2:
        printf("WednesDay!\n");
        break;
    case 3:
        printf("ThursDay!\n");
        break;
    case 4:
        printf("FirDay!\n");
        break;
    case 5:
        printf("SaturDay!\n");
        break;
    case 6:
        printf("SunDay!\n");
        break;
    }

}

int main(void)
{
    f(MonDay);

    return 0;
}

# include <stdio.h>

//冒泡排序
void sort(int * a, int len)
{
    int i, j, t;

    for (i=0; i<len-1; ++i)
    {
        for (j=0; j<len-1-i; ++j)
        {
            if (a[j] > a[j+1])  // >表示升序 <表示降序
            {
                t = a[j];
                a[j] = a[j+1];
                a[j+1] = t; 
            }
        }
    }
}

int main(void)
{
    int a[6] = {10, 2, 8, -8, 11, 0};
    int i = 0;

    sort(a, 6);

    for (i=0; i<6; ++i)
    {
        printf("%d ", a[i]);
    }
    printf("\n");

    return 0;
}

# include <stdio.h>
# include <malloc.h>

struct Student
{
    int age;
    float score;
    char name[100];
};

void InputArr(struct Student * Arr, int len);
void SortArr(struct Student * Arr, int len);
void OutputArr(struct Student * pArr, int len);

int main(void)
{
    int len;
    struct Student * pArr;

    //动态的构造一维数组
    printf("请输入学生的个数:\n");
    printf("len = ");
    scanf("%d", &len);
    pArr = (struct Student *)malloc(len * sizeof(struct Student));

    InputArr(pArr, len);  //输入
    SortArr(pArr, len);  //排序
    OutputArr(pArr, len);  //输出

    return 0;
}

void InputArr(struct Student * Arr, int len)
{
    int i;
    for (i = 0; i < len; ++i)
    {
        printf("请输入第%d个学生的信息\n", i + 1);
        printf("age =");
        scanf("%d", &Arr[i].age);
        printf("score =");
        scanf("%f", &Arr[i].score);
        printf("name =");
        scanf("%s", Arr[i].name);

    }
}


void SortArr(struct Student * Arr, int len)
{
    int i, j;
    struct Student t;

    for (i = 0; i < len - 1; ++i)
    {
        for (j = 0; j < len - 1 - i; ++j)
        {
            if (Arr[j].score > Arr[j + 1].score)
            {
                t = Arr[j];
                Arr[j] = Arr[j + 1];
                Arr[j + 1] = t;
            }
        }
    }
    
}


void OutputArr(struct Student * Arr, int len)
{
    int i;
    for (i = 0; i < len; ++i)
    {
        printf("\n\n");
        printf("第%d个学生的信息是\n", i + 1);
        printf("age = %d\n", Arr[i].age);
        printf("score = %.1f\n", Arr[i].score);
        printf("name = %s\n", Arr[i].name);
    }
}