掌握C语言,10个实战案例助你快速上手

发布时间:2025-05-23 00:32:50

1. 打算器顺序

一个简单的打算器顺序可能帮助你懂得C言语的基本语法跟把持构造。以下是一个基本的打算器顺序示例:

#include <stdio.h>

int main() {
    char operator;
    double firstNumber, secondNumber;

    printf("Enter an operator (+, -, *, /): ");
    scanf("%c", &operator);

    printf("Enter two operands: ");
    scanf("%lf %lf", &firstNumber, &secondNumber);

    switch (operator) {
        case '+':
            printf("%.1lf + %.1lf = %.1lf", firstNumber, secondNumber, firstNumber + secondNumber);
            break;
        case '-':
            printf("%.1lf - %.1lf = %.1lf", firstNumber, secondNumber, firstNumber - secondNumber);
            break;
        case '*':
            printf("%.1lf * %.1lf = %.1lf", firstNumber, secondNumber, firstNumber * secondNumber);
            break;
        case '/':
            if (secondNumber != 0.0)
                printf("%.1lf / %.1lf = %.1lf", firstNumber, secondNumber, firstNumber / secondNumber);
            else
                printf("Division by zero is not allowed.");
            break;
        default:
            printf("Error! operator is not correct");
    }

    return 0;
}

2. 字符串处理

利用C言语处理字符串,例照实现字符串反转:

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

void reverseString(char str[]) {
    int len = strlen(str);
    int i;
    char temp;
    for (i = 0; i < len / 2; i++) {
        temp = str[i];
        str[i] = str[len - i - 1];
        str[len - i - 1] = temp;
    }
}

int main() {
    char str[] = "Hello, World!";
    printf("Original String: %s\n", str);
    reverseString(str);
    printf("Reversed String: %s\n", str);
    return 0;
}

3. 行列实现

进修怎样利用C言语实现行列数据构造:

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

#define MAX 100

int queue[MAX];
int front = -1;
int rear = -1;

void enqueue(int item) {
    if (rear == MAX - 1) {
        printf("Queue is full\n");
    } else {
        if (front == -1) {
            front = 0;
        }
        rear++;
        queue[rear] = item;
    }
}

int dequeue() {
    int item;
    if (front == -1) {
        printf("Queue is empty\n");
        return -1;
    } else {
        item = queue[front];
        if (front == rear) {
            front = -1;
            rear = -1;
        } else {
            front++;
        }
        return item;
    }
}

int main() {
    enqueue(1);
    enqueue(2);
    enqueue(3);
    printf("%d dequeued from queue\n", dequeue());
    printf("%d dequeued from queue\n", dequeue());
    return 0;
}

4. 函数递归

递归函数是一个重要的不雅点,以下是一个利用递归打算阶乘的示例:

#include <stdio.h>

long factorial(int n) {
    if (n == 0)
        return 1;
    else
        return n * factorial(n - 1);
}

int main() {
    int number;
    printf("Enter a positive integer: ");
    scanf("%d", &number);
    printf("Factorial of %d = %ld\n", number, factorial(number));
    return 0;
}

5. 排序算法

实现一个排序算法,如冒泡排序,来懂得C言语中的轮回跟比较逻辑:

#include <stdio.h>

void bubbleSort(int arr[], int n) {
    int i, j, temp;
    for (i = 0; i < n-1; i++) {
        for (j = 0; j < n-i-1; j++) {
            if (arr[j] > arr[j+1]) {
                temp = arr[j];
                arr[j] = arr[j+1];
                arr[j+1] = temp;
            }
        }
    }
}

int main() {
    int arr[] = {64, 34, 25, 12, 22, 11, 90};
    int n = sizeof(arr)/sizeof(arr[0]);
    bubbleSort(arr, n);
    printf("Sorted array: \n");
    for (int i=0; i < n; i++)
        printf("%d ", arr[i]);
    printf("\n");
    return 0;
}

6. 链表操纵

利用C言语实现链表数据构造,并停止拔出跟删除操纵:

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

struct Node {
    int data;
    struct Node* next;
};

void push(struct Node** head_ref, int new_data) {
    struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
    new_node->data = new_data;
    new_node->next = (*head_ref);
    (*head_ref) = new_node;
}

void deleteNode(struct Node** head_ref, int key) {
    struct Node *temp = *head_ref, *prev;

    if (temp != NULL && temp->data == key) {
        *head_ref = temp->next;
        free(temp);
        return;
    }

    while (temp != NULL && temp->data != key) {
        prev = temp;
        temp = temp->next;
    }

    if (temp == NULL) return;

    prev->next = temp->next;
    free(temp);
}

void printList(struct Node *node) {
    while (node != NULL) {
        printf("%d ", node->data);
        node = node->next;
    }
    printf("\n");
}

int main() {
    struct Node* head = NULL;

    push(&head, 1);
    push(&head, 3);
    push(&head, 2);

    printf("Created Linked list is: ");
    printList(head);

    deleteNode(&head, 1);
    printf("Linked List after Deletion of 1: ");
    printList(head);

    return 0;
}

7. 文件操纵

进修怎样利用C言语停止文件操纵,比方读取跟写入文件:

#include <stdio.h>

int main() {
    FILE *fp;
    char ch;

    fp = fopen("example.txt", "r");
    if (fp == NULL) {
        printf("Error opening file\n");
        return 0;
    }

    printf("Reading characters from file:\n");
    while ((ch = fgetc(fp)) != EOF) {
        printf("%c", ch);
    }

    fclose(fp);
    return 0;
}

8. 收集编程

利用C言语停止基本的收集编程,比方创建一个简单的TCP效劳器:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>

int main() {
    int server_fd, new_socket;
    struct sockaddr_in address;
    int opt = 1;
    int addrlen = sizeof(address);

    // Creating socket file descriptor
    if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
        perror("socket failed");
        exit(EXIT_FAILURE);
    }

    // Forcefully attaching socket to the port 8080
    if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) {
        perror("setsockopt");
        exit(EXIT_FAILURE);
    }
    address.sin_family = AF_INET;
    address.sin_addr.s_addr = INADDR_ANY;
    address.sin_port = htons(8080);

    // Forcefully attaching socket to the port 8080
    if (bind(server_fd, (struct sockaddr *)&address, sizeof(address))<0) {
        perror("bind failed");
        exit(EXIT_FAILURE);
    }

    if (listen(server_fd, 3) < 0) {
        perror("listen");
        exit(EXIT_FAILURE);
    }

    while ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen))) {
        printf("Connection accepted\n");
        char buffer[1024] = {0};
        read(new_socket, buffer, 1024);
        send(new_socket, "Hello from server", 18, 0);
        close(new_socket);
    }

    if (new_socket < 0) {
        perror("accept");
        exit(EXIT_FAILURE);
    }

    return 0;
}

9. 图形用户界面编程

利用C言语结合GTK库停止图形用户界面编程:

#include <gtk/gtk.h>

static void activate(GtkApplication* app, gpointer user_data) {
    GtkWidget *window;

    window = gtk_application_window_new(app);
    gtk_window_set_title(GTK_WINDOW(window), "GTK Application");
    gtk_window_set_default_size(GTK_WINDOW(window), 300, 200);

    gtk_container_set_border_width(GTK_CONTAINER(window), 10);

    GtkWidget *label = gtk_label_new("Hello, GTK!");
    gtk_window_set_child(GTK_WINDOW(window), label);

    gtk_widget_show(window);
}

int main(int argc, char **argv) {
    GtkApplication *app;
    int status;

    app = gtk_application_new("org.gtk.example", G_APPLICATION_FLAGS_NONE);
    g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
    status = g_application_run(G_APPLICATION(app), argc, argv);
    g_object_unref(app);

    return status;
}

10. 数据库连接

利用C言语连接Oracle数据库,履行SQL语句:

”`c #include #include #include

int main() {

OCIEnv *envhp;
OCIServer *srvhp;
OCISession *seshp;
OCISessionPool *sesp;
OCIError *errhp;
ub4 n = 0;
ub4 r = 0;
ub4 nattr = 0;
ub4 rattr = 0;
ub4 column;
ub4 row;
ub4 colcount = 0;
ub4 rowcount = 0;
ub4 size = 0;
ub4 colsize = 0;
ub4 rowsize = 0;
ub4 len = 0;
ub4 type = 0;
ub4 len2 = 0;
ub4 type2 = 0;
ub4 len3 = 0;
ub4 type3 = 0;
ub4 len4 = 0;
ub4 type4 = 0;
ub4 len5 = 0;
ub4 type5 = 0;
ub4 len6 = 0;
ub4 type6 = 0;
ub4 len7 = 0;
ub4 type7 = 0;
ub4 len8 = 0;
ub4 type8 = 0;
ub4 len9 = 0;
ub4 type9 = 0;
ub4 len10 = 0;
ub4 type10 = 0;
ub4 len11 = 0;
ub4 type11 = 0;
ub4 len12 = 0;
ub4 type12 = 0;
ub4 len13 = 0;
ub4 type13 = 0;
ub4 len14 = 0;
ub4 type14 = 0;
ub4 len15 = 0;
ub4 type15 = 0;
ub4 len16 = 0;
ub4 type16 = 0;
ub4 len17 = 0;
ub4 type17 = 0;
ub4 len18 = 0;
ub4 type18 = 0;
ub4 len19 = 0;
ub4 type19 = 0;
ub4 len20 = 0;
ub4 type20 = 0;
ub4 len21 = 0;
ub4 type21 = 0;
ub4 len22 = 0;
ub4 type22 = 0;
ub4 len23 = 0;
ub4 type23 = 0;
ub4 len24 = 0;
ub4 type24 = 0;
ub4 len25 = 0;
ub4 type25 = 0;
ub4 len26 = 0;
ub4 type26 = 0;
ub4 len27 = 0;
ub4 type27 = 0;
ub4 len28 = 0;
ub4 type28 = 0;
ub4 len29 = 0;
ub4 type29 = 0;
ub4 len30 = 0;
ub4 type30 = 0;
ub4 len31 = 0;
ub4 type31 = 0;
ub4 len32 = 0;
ub4 type32 = 0;
ub4 len33 = 0;
ub4 type33 = 0;
ub4 len34 = 0;
ub4 type34 = 0;
ub4 len35 = 0;
ub4 type35 = 0;
ub4 len36 = 0;
ub4 type36 = 0;
ub4 len37 = 0;
ub4 type37 = 0;
ub4 len38 = 0;
ub4 type38 = 0;
ub4 len39 = 0;
ub4 type39 = 0;
ub4 len40 = 0;
ub4 type40 = 0;
ub4 len41 = 0;
ub4 type41 = 0;
ub4 len42 = 0;
ub4 type42 = 0;
ub4 len43 = 0;
ub4 type43 = 0;
ub4 len44 = 0;
ub4 type44 = 0;
ub4 len45 = 0;
ub4 type45 = 0;
ub4 len46 = 0;
ub4 type46 = 0;
ub4 len47 = 0;
ub4 type47 = 0;
ub4 len48 = 0;
ub4 type48 = 0;
ub4 len49 = 0;
ub4 type49 = 0;
ub4 len50 = 0;
ub4 type50 = 0;
ub4 len51 = 0;
ub4 type51 = 0;
ub4 len52 = 0;
ub4 type52 = 0;
ub4 len53 = 0;
ub4 type53 = 0;
ub4 len54 = 0;
ub4 type54 = 0;
ub4 len55 = 0;
ub4 type55 = 0;
ub4 len56 = 0;
ub4 type56 = 0;
ub4 len57 = 0;
ub4 type57 = 0;
ub4 len58 = 0;
ub4 type58 = 0;
ub4 len59 = 0;
ub4 type59 = 0;
ub4 len60 = 0;
ub4 type60 = 0;
ub4 len61 = 0;
ub4 type61 = 0;
ub4 len62 = 0;
ub4 type62 = 0;
ub4 len63 = 0;
ub4 type63 = 0;
ub4 len64 = 0;
ub4 type64 = 0;
ub4 len65 = 0;
ub4 type65 = 0;
ub4 len66 = 0;
ub4 type66 = 0;
ub4 len67 = 0;
ub4 type67 = 0;
ub4 len68 = 0;
ub4 type68 = 0;
ub4 len69 = 0;
ub4 type69 = 0;
ub4 len70 = 0;
ub4 type70 = 0;
ub4 len71 = 0;
ub4 type71 = 0;
ub4 len72 = 0;
ub4 type72 = 0;
ub4 len73 = 0;
ub4 type73 = 0;
ub4 len74 = 0;
ub4 type74 = 0;
ub4 len75 = 0;
ub4 type75 = 0;
ub4 len76 = 0;
ub4 type76 = 0;
ub4 len77 = 0;
ub4 type77 = 0;
ub4 len78 = 0;
ub4 type78 = 0;
ub4 len79 = 0;
ub4 type79 = 0;
ub4 len80 = 0;
ub4 type80 = 0;
ub4 len81 = 0;
ub4 type81 = 0;
ub4 len82 = 0;
ub4 type82 = 0;
ub4 len83 = 0;
ub4 type83 = 0;
ub4 len84 = 0;
ub4 type84 = 0;
ub4 len85 = 0;
ub4 type85 = 0;
ub4 len86 = 0;
ub4 type86 = 0