wwwwwwww6

#include <stdio.h>
#include "word.h"
#include "line.h"
int main(void) {
    char word[MAX_WORD_LENGTH + 1];
    int word_length;
    clear_line();
    for (;;) {
        word_length = read_word(word, MAX_WORD_LENGTH);
        if (word_length == 0) {
            write_line();
            break;
        }
        if (word_length > space_remaining()) {
            write_line();
            clear_line();
        }
        add_word(word);
    }
    return 0;
}

#include <stdio.h>
#include <string.h>
#include "line.h"
#define MAX_WORD_LENGTH 100
#define MAX_LINE_LENGTH 80
char line[MAX_LINE_LENGTH + 1];
int line_length = 0;
int num_words = 0;
void clear_line(void) {
    line[0] = '\0';
    line_length = 0;
    num_words = 0;
}
void add_word(const char *word) {
    if (num_words > 0) {
        line[line_length] = ' ';
        line[line_length + 1] = '\0';
        line_length++;
    }
    strcat(line, word);
    line_length += strlen(word);
    num_words++;
}
int space_remaining(void) {
    return MAX_LINE_LENGTH - line_length;
}
void write_line(void) {
    int extra_spaces, spaces_to_insert, i, j;
    extra_spaces = MAX_LINE_LENGTH - line_length;
    for (i = 0; i < line_length; i++) {
        if (line[i] != ' ')
            putchar(line[i]);
        else {
            spaces_to_insert = extra_spaces / (num_words - 1);
            for (j = 1; j <= spaces_to_insert + 1; j++)
                putchar(' ');
            extra_spaces -= spaces_to_insert;
            num_words--;
        }
    }
    putchar('\n');
}
void flush_line(void) {
    if (line_length > 0)
        puts(line);
}

#ifndef LINE_H
#define LINE_H
#define MAX_LINE_LENGTH 80
void clear_line(void);
void add_word(const char *word);
int space_remaining(void);
void write_line(void);
void flush_line(void);
#endif

#include <stdio.h>
#include "word.h"
int read_word(char *word, int max_length) {
    int ch, pos = 0;
    while ((ch = getchar()) != EOF && pos < max_length - 1) {
        if (ch == '\n' || ch == ' ') {
            if (pos > 0)
                break;
        } else {
            word[pos++] = (char) ch;
        }
    }
    word[pos] = '\0';
    if (ch == EOF && pos == 0)
        return 0;
    else
        return 1;
}

#ifndef WORD_H
#define WORD_H
#define MAX_WORD_LENGTH 100
int read_word(char *word, int max_length);
#endif

CC = gcc

CFLAGS = -Wall


OBJS = word.o line.o fmt.o


fmt: $(OBJS)

    $(CC) $(CFLAGS) -o fmt $(OBJS)


word.o: word.c word.h

    $(CC) $(CFLAGS) -c word.c


line.o: line.c line.h

    $(CC) $(CFLAGS) -c line.c


fmt.o: fmt.c word.h line.h

    $(CC) $(CFLAGS) -c fmt.c


clean:

    rm -f fmt $(OBJS)

The Very Hungry Caterpillar by Eric Carle In
the
light
of the moon a little egg
lay on a leaf.
One Sunday morning the warm sun came
up….and POP,

out of the egg came a
tiny, very hungry
caterpillar. He started
looking for some
food.

第二天

#include <stdio.h>

#include <stdlib.h>

#include <string.h>


#define MAX_LEN 100


// 用户信息结构体

typedef struct {

    int id;

    char username[MAX_LEN];

    char password[MAX_LEN];

    char phone[MAX_LEN];

    char email[MAX_LEN];

} User;


// 链表节点结构体

typedef struct Node {

    User user;

    struct Node* next;

} Node;


// 读取.csv文件中的用户信息并加载到链表中

Node* loadUsersFromCSV(const char* filename) {

    FILE* file = fopen(filename, "r");

    if (file == NULL) {

        printf("Failed to open file: %s\n", filename);

        return NULL;

    }


    char line[MAX_LEN];

    Node* head = NULL;

    Node* tail = NULL;


    while (fgets(line, sizeof(line), file) != NULL) {

        line[strcspn(line, "\n")] = '\0';  // 去除换行符


        // 解析CSV行

        char* token;

        char* rest = line;

        int field = 0;


        Node* newNode = (Node*)malloc(sizeof(Node));

        newNode->next = NULL;


        while ((token = strtok_r(rest, ",", &rest))) {

            switch (field) {

                case 0:

                    newNode->user.id = atoi(token);

                    break;

                case 1:

                    strcpy(newNode->user.username, token);

                    break;

                case 2:

                    strcpy(newNode->user.password, token);

                    break;

                case 3:

                    strcpy(newNode->user.phone, token);

                    break;

                case 4:

                    strcpy(newNode->user.email, token);

                    break;

                default:

                    break;

            }

            field++;

        }


        if (head == NULL) {

            head = newNode;

            tail = newNode;

        } else {

            tail->next = newNode;

            tail = newNode;

        }

    }


    fclose(file);

    return head;

}


// 在链表中查找指定ID的用户

Node* findUser(Node* head, int id) {

    Node* current = head;

    while (current != NULL) {

        if (current->user.id == id) {

            return current;

        }

        current = current->next;

    }

    return NULL;

}


// 添加用户到链表

void addUser(Node** head, User newUser) {

    Node* newNode = (Node*)malloc(sizeof(Node));

    newNode->user = newUser;

    newNode->next = *head;

    *head = newNode;

    printf("User added successfully.\n");

}


// 从链表中删除指定ID的用户

void deleteUser(Node** head, int id) {

    Node* current = *head;

    Node* prev = NULL;


    while (current != NULL) {

        if (current->user.id == id) {

            if (prev == NULL) {

                *head = current->next;

            } else {

                prev->next = current->next;

            }

            free(current);

            printf("User deleted successfully.\n");

            return;

        }

        prev = current;

        current = current->next;

    }


    printf("User not found.\n");

}


// 修改链表中指定ID的用户信息

void modifyUser(Node* head, int id, User modifiedUser) {

    Node* userNode = findUser(head, id);

    if (userNode!= NULL) {

        userNode->user = modifiedUser;

        printf("User modified successfully.\n");

    } else {

        printf("User not found.\n");

    }

}


// 将链表中的用户信息写入.csv文件

void saveUsersToCSV(const char* filename, Node* head) {

    FILE* file = fopen(filename, "w");

    if (file == NULL) {

        printf("Failed to open file: %s\n", filename);

        return;

    }


    Node* current = head;

    while (current != NULL) {

        if (current->user.id != 0) {

            fprintf(file, "%d,%s,%s,%s,%s\n", current->user.id, current->user.username, current->user.password, current->user.phone, current->user.email);

        }

        current = current->next;

    }


    fclose(file);

    printf("User information saved successfully.\n");

}


// 释放链表内存

void freeList(Node* head) {

    Node* current = head;

    while (current != NULL) {

        Node* temp = current;

        current = current->next;

        free(temp);

    }

}


int main() {

    const char* filename = "usrinfo.csv";

    Node* userList = loadUsersFromCSV(filename);


    if (userList == NULL) {

        printf("No user information found.\n");

        return 0;

    }


    int choice;

    int id;

    User newUser;

    User modifiedUser;


    do {

        printf("\nUser Management System\n");

        printf("1. Add User\n");

        printf("2. Delete User\n");

        printf("3. Modify User\n");

        printf("4. Search User\n");

        printf("5. Save User Information\n");

        printf("6. Quit\n");

        printf("Enter your choice: ");

        scanf("%d", &choice);


        switch (choice) {

            case 1:

                printf("Enter user ID: ");

                scanf("%d", &newUser.id);

                printf("Enter username: ");

                scanf("%s", newUser.username);

                printf("Enter password: ");

                scanf("%s", newUser.password);

                printf("Enter phone: ");

                scanf("%s", newUser.phone);

                printf("Enter email: ");

                scanf("%s", newUser.email);

                addUser(&userList, newUser);

                break;

            case 2:

                printf("Enter user ID to delete: ");

                scanf("%d", &id);

                deleteUser(&userList, id);

                break;

            case 3:

                printf("Enter user ID to modify: ");

                scanf("%d", &id);

                printf("Enter new username: ");

                scanf("%s", modifiedUser.username);

                printf("Enter new password: ");

                scanf("%s", modifiedUser.password);

                printf("Enter new phone: ");

                scanf("%s", modifiedUser.phone);

                printf("Enter new email: ");

                scanf("%s", modifiedUser.email);

                modifyUser(userList, id, modifiedUser);

                break;

            case 4:

                printf("Enter user ID to search: ");

                scanf("%d", &id);

                Node* userNode = findUser(userList, id);

                if (userNode != NULL) {

                    printf("User found:\n");

                    printf("ID: %d\n", userNode->user.id);

                    printf("Username: %s\n", userNode->user.username);

                    printf("Password: %s\n", userNode->user.password);

                    printf("Phone: %s\n", userNode->user.phone);

                    printf("Email: %s\n", userNode->user.email);

                } else {

                    printf("User not found.\n");

                }

                break;

            case 5:

                saveUsersToCSV(filename, userList);

                break;

            case 6:

                printf("Exiting program.\n");

                break;

            default:

                printf("Invalid choice. Please try again.\n");

                break;

        }


    } while (choice != 6);


    freeList(userList);


    return 0;

}

第三天

#include <stdio.h>

#include <stdlib.h>

#include <string.h>


// 用户结构体

typedef struct {

    int id;

    char username[50];

    char password[50];

    char phone[20];

    char email[50];

} User;


// 二叉排序树结点结构体

typedef struct Node {

    User user;

    struct Node* left;

    struct Node* right;

} Node;


// 创建新的结点

Node* createNode(User user) {

    Node* newNode = (Node*)malloc(sizeof(Node));

    newNode->user = user;

    newNode->left = NULL;

    newNode->right = NULL;

    return newNode;

}


// 插入结点到二叉排序树

Node* insertNode(Node* root, User user) {

    if (root == NULL) {

        root = createNode(user);

    } else {

        // 根据用户名比较大小决定插入位置

        if (strcmp(user.username, root->user.username) < 0) {

            root->left = insertNode(root->left, user);

        } else {

            root->right = insertNode(root->right, user);

        }

    }

    return root;

}


// 在二叉排序树中查找结点

Node* searchNode(Node* root, char* username) {

    if (root == NULL || strcmp(username, root->user.username) == 0) {

        return root;

    }


    if (strcmp(username, root->user.username) < 0) {

        return searchNode(root->left, username);

    }


    return searchNode(root->right, username);

}


// 删除二叉排序树中的结点

Node* deleteNode(Node* root, char* username) {

    if (root == NULL) {

        return root;

    }


    if (strcmp(username, root->user.username) < 0) {

        root->left = deleteNode(root->left, username);

    } else if (strcmp(username, root->user.username) > 0) {

        root->right = deleteNode(root->right, username);

    } else {

        // 找到要删除的结点


        // 结点没有子节点或只有一个子节点

        if (root->left == NULL) {

            Node* temp = root->right;

            free(root);

            return temp;

        } else if (root->right == NULL) {

            Node* temp = root->left;

            free(root);

            return temp;

        }


        // 结点有两个子节点

        Node* minNode = root->right;

        while (minNode->left != NULL) {

            minNode = minNode->left;

        }

        root->user = minNode->user;

        root->right = deleteNode(root->right, minNode->user.username);

    }

    return root;

}


// 修改二叉排序树中的结点

void modifyNode(Node* root, char* username, User user) {

    Node* node = searchNode(root, username);

    if (node != NULL) {

        node->user = user;

    }

}


// 中序遍历二叉排序树,打印用户信息

void inorderTraversal(Node* root) {

    if (root != NULL) {

        inorderTraversal(root->left);

        printf("ID: %d, Username: %s, Password: %s, Phone: %s, Email: %s\n",

               root->user.id, root->user.username, root->user.password, root->user.phone, root->user.email);

        inorderTraversal(root->right);

    }

}


// 保存二叉排序树到.csv文件

void saveToFile(FILE* file, Node* root) {

    if (root != NULL) {

        saveToFile(file, root->left);

        fprintf(file, "%d,%s,%s,%s,%s\n", root->user.id, root->user.username, root->user.password, root->user.phone, root->user.email);

        saveToFile(file, root->right);

    }

}


// 从.csv文件中加载用户信息到二叉排序树

Node* loadFromFile(FILE* file, Node* root) {

    char line[256];

    while (fgets(line, sizeof(line), file)) {

        User user;

        sscanf(line, "%d,%[^,],%[^,],%[^,],%[^,\n]", &user.id, user.username, user.password, user.phone, user.email);

        root = insertNode(root, user);

    }

    return root;

}


int main() {

    Node* root = NULL;


    FILE* file = fopen("usrinfo.csv", "r");

    if (file != NULL) {

        root = loadFromFile(file, root);

        fclose(file);

    }


    int choice;

    do {

        printf("\n------ 用户信息管理系统 ------\n");

        printf("1. 查找用户信息\n");

        printf("2. 新增用户信息\n");

        printf("3. 删除用户信息\n");

        printf("4. 修改用户信息\n");
        
        printf("5. 打印用户信息\n");

        printf("6. 保存用户信息到文件\n");

        printf("0. 退出\n");

        printf("请输入您的选择:");

        scanf("%d", &choice);


        switch (choice) {

            case 0:

                printf("感谢使用用户信息管理系统,再见!\n");

                break;

            case 1: {

                char username[50];

                printf("请输入要查找的用户名:");

                scanf("%s", username);


                Node* node = searchNode(root, username);

                if (node != NULL) {

                    printf("用户信息找到:\n");

                    printf("ID: %d, Username: %s, Password: %s, Phone: %s, Email: %s\n",

                           node->user.id, node->user.username, node->user.password, node->user.phone, node->user.email);

                } else {

                    printf("未找到该用户名的用户信息。\n");

                }

                break;

            }

            case 2: {

                User user;

                printf("请输入新用户的ID:");

                scanf("%d", &user.id);

                printf("请输入新用户的用户名:");

                scanf("%s", user.username);

                printf("请输入新用户的密码:");

                scanf("%s", user.password);

                printf("请输入新用户的电话:");

                scanf("%s", user.phone);

                printf("请输入新用户的邮箱:");

                scanf("%s", user.email);


                root = insertNode(root, user);

                printf("用户信息已添加。\n");

                break;

            }

            case 3: {

                char username[50];

                printf("请输入要删除的用户名:");

                scanf("%s", username);


                root = deleteNode(root, username);

                printf("用户信息已删除。\n");

                break;

            }

            case 4: {

                char username[50];

                printf("请输入要修改的用户名:");

               scanf("%s", username);


                Node* node = searchNode(root, username);

                if (node != NULL) {

                    User user;

                    printf("请输入新的ID:");

                    scanf("%d", &user.id);

                    printf("请输入新的用户名:");

                    scanf("%s", user.username);

                    printf("请输入新的密码:");

                    scanf("%s", user.password);

                    printf("请输入新的电话:");

                    scanf("%s", user.phone);

                    printf("请输入新的邮箱:");

                    scanf("%s", user.email);


                    modifyNode(root, username, user);

                    printf("用户信息已修改。\n");

                } else {

                    printf("未找到该用户名的用户信息。\n");

                }

                break;

            }


               case 5:

                printf("所有用户信息:\n");

                inorderTraversal(root);

                break;

            case 6: {

                FILE* file = fopen("usrinfo.csv", "w");

                if (file != NULL) {

                    saveToFile(file, root);

                    fclose(file);

                    printf("用户信息已保存到文件。\n");

                } else {

                    printf("保存文件失败。\n");

                }

                break;

            }

            default:

                printf("无效的选择,请重新输入。\n");

        }

    } while (choice != 0);


    return 0;

}

第四天

#include <stdio.h>

#include <stdlib.h>

#include <sys/types.h>

#include <sys/ipc.h>

#include <sys/shm.h>

#include <sys/sem.h>

#include <unistd.h>


#define BUFFER_SIZE 10


typedef struct {

    int buffer[BUFFER_SIZE];

    int in;

    int out;

    int count;

} Buffer;


int main() {

    int shmid, semid;

    key_t key = ftok(".", 'S');

    Buffer *buffer;


    // 创建共享内存

    shmid = shmget(key, sizeof(Buffer), IPC_CREAT | 0666);

    if (shmid == -1) {

        perror("shmget");

        exit(1);

    }


    // 连接共享内存

    buffer = (Buffer *)shmat(shmid, NULL, 0);

    if (buffer == (Buffer *)(-1)) {

        perror("shmat");

        exit(1);

    }


    //创建信号量集

    semid = semget(key, 1, IPC_CREAT | 0666);

    if (semid == -1) {

        perror("semget");

        exit(1);

    }


    // 初始化信号量

    semctl(semid, 0, SETVAL, 1);


    // 打开 data文件

    FILE *file = fopen("data.txt", "r");

    if (file == NULL) {

        perror("fopen");

        exit(1);

    }


    // 生产数据

    int data;

    while (fscanf(file, "%d", &data) == 1) {

        // 获取互斥信号量

        struct sembuf sem_op;

        sem_op.sem_num = 0;

        sem_op.sem_op = -1;

        sem_op.sem_flg = 0;

        semop(semid, &sem_op, 1);


        // 检查共享内存是否为满

        if (buffer->count < BUFFER_SIZE) {

            // 写入数据

            buffer->buffer[buffer->in] = data;

            buffer->in = (buffer->in + 1) % BUFFER_SIZE;

            buffer->count++;

            printf("Produced: %d\n", data);

        }


        //释放互斥信号量

        sem_op.sem_op = 1;

        semop(semid, &sem_op, 1);


        // 等待一段时间

        sleep(1);

    }


    // 关闭data文件

    fclose(file);


    // 断开共享内存连接

    shmdt(buffer);


    return 0;

}

#include <stdio.h>

#include <stdlib.h>

#include <sys/types.h>

#include <sys/ipc.h>

#include <sys/shm.h>

#include <sys/sem.h>

#include <unistd.h>


#define BUFFER_SIZE 10


typedef struct {

    int buffer[BUFFER_SIZE];

    int in;

    int out;

    int count;

} Buffer;


int main() {

    int shmid, semid;

    key_t key = ftok(".", 'S');

    Buffer *buffer;


    // 创建共享内存

    shmid = shmget(key, sizeof(Buffer), IPC_CREAT | 0666);

    if (shmid == -1) {

        perror("shmget");

        exit(1);

    }


    // 连接共享内存

    buffer = (Buffer *)shmat(shmid, NULL, 0);

    if (buffer == (Buffer *)(-1)) {

        perror("shmat");

        exit(1);

    }


    // 创建信号量集

    semid = semget(key, 1, IPC_CREAT | 0666);

    if (semid == -1) {

        perror("semget");

        exit(1);

    }


    // 消费数据

    while (1) {

        // 获取互斥信号量

        struct sembuf sem_op;

        sem_op.sem_num = 0;

        sem_op.sem_op = -1;

        sem_op.sem_flg = 0;

        semop(semid, &sem_op, 1);


        // 检查缓冲区是否为空

        if (buffer->count > 0) {

            // 取出数据

            int data = buffer->buffer[buffer->out];

            buffer->out = (buffer->out + 1) % BUFFER_SIZE;

            buffer->count--;

            printf("Consumed: %d\n", data);

        }


        // 释放互斥信号量

        sem_op.sem_op = 1;

        semop(semid, &sem_op, 1);


        // 等待一段时间


        sleep(2);

    }


    // 断开共享内存连接

    shmdt(buffer);


    return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值