结构体

#include <stdio.h>

struct Book
{
    char title[120];
    char author[40];
    float price;
    unsigned int date;
    char publisher[40];
} book;
//这里book是一个声明,还可以有其他的,例如:
//book1, book2等等,后面又详细解释
/*
struct Book
{
    char title[120];
    char author[40];
    float price;
    unsigned int date;
    char publisher[40];
} book1, book2;
*/

int main()
{
    // 在这里定义属于局部变量struct Book book;上面的属于全局变量
    printf("book name:");
    scanf_s("%s", book.title);
    printf("auther name:");
    scanf_s("%s", book.author);
    printf("book price:");
    scanf_s("%f", &book.price);
    printf("book date:");
    scanf_s("%d", &book.date);
    printf("book chubanshe:");
    scanf_s("%s", book.publisher);

    printf("\n=======数据录入完毕==========");

    printf("%s\n", book.title);
    printf("%s\n", book.author);
    printf("%.2f\n", book.price);
    printf("%d\n", book.date);
    printf("%s\n", book.publisher);

    return 0;
}
#include <stdio.h>

struct Date
{
    int year;
    int month;
    int day;
};

struct Book
{
    char title[120];
    char author[40];
    float price;
    struct Date date;
    char publisher[40];
}
book = {
        "《带你学C带你飞》",
        "小甲鱼",
        44.8,
    {2017, 11, 11},
    "清华大学出版社"
};

int main()
{
    //struct Book* pt;
    //pt = &book;

    printf("%s\n", book.title);
    printf("%s\n", book.author);
    printf("%.2f\n", book.price);
    printf("%u-%u-%u\n", book.date.year, book.date.month, book.date.day);
    printf("%s\n", book.publisher);

    /*
        优先级:. > *,所以要加括号
    printf("%s\n", (*pt).title);
    printf("%s\n", (*pt).author);
    printf("%.2f\n", (*pt).price);
    printf("%u-%u-%u\n", (*pt).date.year, (*pt).date.month, (*pt).date.day);
    printf("%s\n", (*pt).publisher);
    */

    /*
    printf("%s\n", pt -> title);
    printf("%s\n", pt -> author);
    printf("%.2f\n", pt -> price);
    printf("%u-%u-%u\n", pt -> date.year, pt -> date.month, pt -> date.day);
    printf("%s\n", pt -> publisher);
    */

    return 0;
}

证明相同类型之间的结构体之间可以相互赋值

#include <stdio.h>

struct A {
    int x;
    int y;
}t1, t2;

int main()
{
    t1.x = 2;
    t1.y = 3;

    t2 = t1;
        //证明相同类型之间的结构体之间可以相互赋值
    printf("%d, %d\n", t2.x, t2.y);
    return 0;
}
#include <stdio.h>
#include <stdlib.h>

struct Date
{
    int year;
    int month;
    int day;
};

struct Book
{
    char tittle[20];
    char author[30];
    float price;
    char publisher;
    struct Date date;
};

void getInput(struct Book *book);
void printBook(struct Book *book);

/*
struct Book getInput(struct Book book);
void printBook(struct Book book);

struct Book getInput(struct Book book)
{
    printf("请输入书名:");
    scanf("%s", book.tittle);
    printf("请输入作者:");
    scanf("%s", book.author);
    printf("请输入售价:");
    scanf("%f", &book.price);
    printf("请输入出版社:");
    scanf("%s", book.publisher);
    printf("请输入出版日期:");
    scanf("%d-%d-%d", &book.date.year, &book.date.month, &book.date.day);

    return book;
}

void printBook(struct Book book)
{
    printf("请输入书名:%s\n", book.tittle);
    printf("请输入作者:%s\n", book.author);
    printf("请输入售价:%f\n", book.price);
    printf("请输入出版社:%s\n", book.publisher);
    printf("请输入出版日期:%d-%d-%d\n", book.date.year,&book.date.month, book.date.day);
}
*/

void getInput(struct Book *book)
{
    printf("请输入书名:");
    scanf("%s", book->tittle);
    printf("请输入作者:");
    scanf("%s", book->author);
    printf("请输入售价:");
    scanf("%f", &book->price);
    printf("请输入出版社:");
    scanf("%s", book->publisher);
    printf("请输入出版日期:");
    scanf("%d-%d-%d", &book->date.year, &book->date.month, &book->date.day);

    return book;
}

void printBook(struct Book *book)
{
    printf("请输入书名:%s\n", book->tittle);
    printf("请输入作者:%s\n", book->author);
    printf("请输入售价:%f\n", book->price);
    printf("请输入出版社:%s\n", book->publisher);
    printf("请输入出版日期:%d-%d-%d\n", book->date.year, &book->date.month, book->date.day);
}

int main()
{
    struct Book *b1, *b2;

    b1 = (struct Book*)malloc(sizeof(struct Book));
    b2 = (struct Book*)malloc(sizeof(struct Book));

    if (b1 = NULL || b2 == NULL)
    {
        printf("分配内存失败!");
        exit(1);
    }

    printf("请输入第一本书的信息:\n");
    getInput(&b1);
    putchar(&#39;\n&#39;);
    printf("请输入第二本书的信息:\n");
    getInput(&b2);

    printf("====书籍信息输入完毕,现在开始进行打印来确定输入正确====");

    printf("打印第一本书的信息:");
    printBook(&b1);
    putchar(&#39;/n&#39;);
    printf("打印第二本书的信息:");
    printBook(&b2);

    /*
    printf("请输入第一本书的信息:\n");
    b1 = getInput(b1);
    putchar(&#39;\n&#39;);
    printf("请输入第二本书的信息:\n");
    b2 = getInput(b2);

    printf("====书籍信息输入完毕,现在开始进行打印来确定输入正确====");

    printf("打印第一本书的信息:");
    printBook(b1);
    putchar(&#39;/n&#39;);
    printf("打印第二本书的信息:");
    printBook(b2);
    */

    free(b1);
    free(b2);

    return 0;
}

单链表

//什么是链表?链表就是结构体变量与结构体变量连接在一起
/*
动态创建一个链表:动态内存申请+模块化设计
1.创建链表(创建一个表头表示整个链表)
2.创建结点
3.插入结点
4.删除结点
5.打印遍历链表(测试)
*/
#include <stdio.h>
#include <stdlib.h>

struct Book {
    char title[30]; // 数据域
    char author[50]; // 数据域
    struct Book* next; // 指针域
};

void addBook(struct Book** library);
void getInput(struct Book* book);
void printLibrary(struct Book* library);
void releaseLibrary(struct Book** library);

//用到了**
void addBook(struct Book** library)
{
        /*尾插法:
        void addBook(struct Book** library)
{
    struct Book* book;
    static struct Book* tail;
    book = (struct Book*)malloc(sizeof(struct Book));
    if (book == NULL)
    {
        printf("分配内存失败了!");
        exit(1);
    }

    getInput(book);

    if (*library != NULL)
    {
        tail->next = book;
        book->next = NULL;
    }
    else
    {
        *library = book;
        book->next = NULL;
    }
    tail = book;
}
        */
    struct Book* book, *temp;
    book = (struct Book*)malloc(sizeof(struct Book));

    if (book == NULL)
    {
        printf("分配内存失败!");
        exit(1);
    }

    getInput(book);

    if (*library == NULL)
    {
        *library = book;
        book->next = NULL;
    }
    else
    {
        temp = *library;
        *library = book;
        book->next = temp;
    }
}

void getInput(struct Book* book)
{
    printf("请输入书名:");
    scanf("%s", book->title);
    printf("请输入作者:");
    scanf("%s", book->author);
}

void printLibrary(struct Book* library)
{
    struct Book* book;
    int count = 1;

    book = library;

    while (book != NULL)
    {
        printf("Book%d\n", count);
        printf("书名:%s\n", book->title);
        printf("作者:%s\n", book->author);
        count++;
        book = book->next;
    }
}

void releaseLibrary(struct Book** library)
{
    struct Book* temp;
    while (*library != NULL)
    {
        temp = *library;
        *library = (*library)->next;
        free(temp);
    }
}

int main(void)
{
    struct Book *library = NULL;
    char ch;

    while (1)
    {
        printf("是否需要录入书籍信息(是y/否n):");
        do {
            ch = getchar();
        } while (ch != &#39;y&#39; && ch != &#39;n&#39;);

        if (ch == &#39;y&#39;)
        {
            addBook(&library);
        }
        else
            break;
    }

    printf("\n\n");

    printf("是否需要打印书籍信息(是y/否n):");

    do {
        ch = getchar();
    } while (ch != &#39;y&#39; && ch != &#39;n&#39;);

    if (ch == &#39;y&#39;)
    {
        printLibrary(library);
    }

    releaseLibrary(&library);

    return 0;
}

编写电话簿

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

//设置了内存池的空间
#define MAX 1024

struct Person {
    char name[40];
    char phone[20];
    struct Person* next;
};

struct Person* pool = NULL;
int count;

void getInput(struct Person* person);
void printPerson(struct Person* person);
void addPerson(struct Person** contacts);
struct Person *findPerson(struct Person* contacts);
void changePerson(struct Person* person);
void delPerson(struct Person** contacts);
void displayContacts(struct Person* contacts);
void releaseContacts(struct Person** contacts);
void releasePool(void);

void getInput(struct Person* person)
{
    printf("请输入姓名:");
    scanf("%s", person->name);
    printf("请输入电话号码:");
    scanf("%s", person->phone);
}

void printPerson(struct Person* person)
{
    printf("姓名:%s\n", person->name);
    printf("电话号码:%s\n", person->phone);
}

void displayContacts(struct Person* contacts)
{
    struct Person* person;
    person = contacts;
    int count = 1;

    while (person != NULL)
    {
        printf("联系人%d\n", count);
        printf("姓名:%s\n", person->name);
        printf("电话号码:%s\n", person->phone);
        count++;
        person = person->next;
    }
}

void addPerson(struct Person** contacts)
{
    struct Person* person;
    struct Person* temp;

    // 如果内存池非空,则直接从里面获取空间
    if (pool != NULL)
    {
        person = pool;
        pool = pool->next;
        count--;
    }
    // 如果内存池为空,则调用malloc函数申请新的空间
    else
    {
        person = (struct Person*)malloc(sizeof(struct Person));
        if (person == NULL)
        {
            printf("分配内存失败!");
            exit(1);
        }
    }

    getInput(person);

    if (*contacts != NULL)
    {
        temp = *contacts;
        *contacts = person;
        person->next = temp;
    }
    else
    {
        *contacts = person;
        person->next = NULL;
    }
}

struct Person* findPerson(struct Person* contacts)
{
    struct Person* person;
    char input[40];

    printf("请输入要查找的联系人:");
    scanf("%s", &input);

    person = contacts;

    while (person != NULL && strcmp(person->name, input))
    {
        person = person->next;
    }
    return person;
}

void changePerson(struct Person* person)
{
    struct Person* temp;

    temp = findPerson(person);
    if (temp == NULL)
        printf("未找到该联系人!");
    else
    {
        printf("请输入新的电话号码:");
        scanf("%s", temp->phone);
    }
}

void delPerson(struct Person** contacts)
{
    struct Person* person;
    struct Person* current;
    struct Person* previous;
    struct Person* temp;

    person = findPerson(*contacts);
    if (person == NULL)
    {
        printf("未找到该联系人!");
    }
    else
    {
        current = *contacts;
        previous = NULL;

        while (current != NULL && current != person)
        {
            previous = current;
            current = current->next;
        }

        if (previous == NULL)
        {
            *contacts = current->next;
        }
        else
        {
            previous->next = current->next;
        }

        // 判断内存池是不是有空位
        if (count < MAX)
        {
            if (pool != NULL)
            {
                temp = pool;
                pool = person;
                person->next = temp;
            }
            else
            {
                pool = person;
                person->next = NULL;
            }
            count++;
        }
        else
        {
            free(person);
        }
    }
}

void releaseContacts(struct Person** contacts)
{
    struct Person* temp;

    while (contacts != NULL)
    {
        temp = *contacts;
        *contacts = (*contacts)->next;
        free(temp);
    }
}

void releasePool(void)
{
    struct Person* temp;

    while (pool != NULL)
    {
        temp = pool;
        pool = pool->next;
        free(temp);
    }
}

int main(void)
{
    struct Person* person;
    struct Person* contacts = NULL;
    int code;

    printf("|欢迎使用电话管理系统!|\n");
    printf("|-----1-添加联系人-----|\n");
    printf("|-----2-寻找联系人-----|\n");
    printf("|-----3-改变联系人-----|\n");
    printf("|-----4-删除联系人-----|\n");
    printf("|-----5-展示联系人-----|\n");
    printf("|-----6-退出本程序-----|\n");

    while (1)
    {
        printf("\n请输入指令:");
        scanf("%d", &code);

        switch (code)
        {
        case 1:addPerson(&contacts); break;
        case 2:
            person = findPerson(contacts);
            if (person == NULL)
            {
                printf("找不到该联系人!");
            }
            else
            {
                printPerson(person);
            }
            break;
        case 3:changePerson(contacts); break;
        case 4:delPerson(&contacts); break;
        case 5:displayContacts(contacts); break;
        case 6: goto END;
        }
    }

END:
    releaseContacts(&contacts);
    releasePool();

    return 0;
}

typedef和宏定义

#include <stdio.h>

typedef int integer;
typedef int* PTRINT;
// 还可以写成这样
// typedef int integer, *PTRINT;
// 但是这里使用宏定义是不对的,只有b才会被定义为指针变量。
// #define PTRINT int*

int main(void)
{
    integer a;
    PTRINT b, c;

    b = &a;
    c = b;

    printf("addr of a = %p", c);

    return 0;
}

typedef和结构体

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

//方法一(不用typedef)
struct Node {
    int value;
};

int main(void)
{
    struct Node* node;

    node = (struct Node*)malloc(sizeof(struct Node));

    if (node == NULL)
    {
        printf("分配内存失败!\n");
        exit(1);
    }

    node->value = 3;

    printf("%d\n", node->value);

    return 0;
}

//方法二
typedef struct Node {
    int value;
} NODE, *PNODE;

int main(void)
{
    struct Node* node;

    node = (PNODE)malloc(sizeof(NODE));

    if (node == NULL)
    {
        printf("分配内存失败!\n");
        exit(1);
    }

    node->value = 3;

    printf("%d\n", node->value);

    return 0;
}

共用体
共用体语法和结构体类似,但是里面所有元素都用一个内存空间(如下)
union data a = {520}; // 初始化第一个成员
union data b = a; // 直接用一个共用体初始化
union data c = {.ch = 'C'}; // C99新增特性,指定初始化成员
优点:可以节约空间的存放多种类型

#include <stdio.h>
#include <string.h>

union Test
{
    int i;
    double pi;
    char str[6];
};

int main()
{
    union Test test;

    test.i = 520;
    test.pi = 3.14;
    strcpy(test.str, "FishC");

    printf("addr of test.i: %p\n", &test.i);
    printf("addr of test.pi: %p\n", &test.pi);
    printf("addr of test.str: %p\n", &test.str);

    printf("test.i: %d\n", test.i);
    printf("test.pi: %.2f\n", test.pi);
    printf("test.str: %s\n", test.str);

    printf("sizeof test: %d\n", sizeof(test));

/*
    addr of test.i: 00AFFCE8
        addr of test.pi : 00AFFCE8
        addr of test.str : 00AFFCE8
        test.i : 1752394054
        test.pi : 3.13
        test.str : FishC
        sizeof test: 8
*/

    return 0;
}

无名联合

union {
    int i;
    float f
}

在程序中可以这样使用:
i = 10;
f = 2.02;
如果实际中这样使用i的值会被冲掉。

#include <string>
#include <iostream>
using namespace std;

class ExamInfo {
public:
    // 三种构造函数,分别用等级、是否通过和百分来初始化
    ExamInfo(string name, char grade)
        : name(name), mode(GRADE), grade(grade) { }
    ExamInfo(string name, bool pass)
        : name(name), mode(PASS), pass(pass) { }
    ExamInfo(string name, int percent)
        : name(name), mode(PERCENTAGE), percent(percent) { }
    void show();

private:
    string name; // 课程名称
    enum { GRADE, PASS, PERCENTAGE } mode;    // 采用何种计分方式
    union {
        char grade;    // 等级制的成绩
        bool pass;    // 是否通过
        int percent; // 百分制的成绩
    };
};

void ExamInfo::show() {
    cout << name << ": ";
    switch (mode) {
    case GRADE:
        cout << grade;
        break;
    case PASS:
        cout << (pass ? "PASS" : "FAIL");
        break;
    case PERCENTAGE:
        cout << percent;
        break;
    }
    cout << endl;
}

int main() {
    ExamInfo course1("English", &#39;B&#39;);
    ExamInfo course2("Calculus", true);
    ExamInfo course3("C++ Programming", 85);
    course1.show();
    course2.show();
    course3.show();
    return 0;
}
全部评论

相关推荐

1 收藏 评论
分享
牛客网
牛客企业服务