ETH官方钱包

前往
大廳
主題

[C程設] 期末考模擬練習,恩,好像不少都比前面的簡單很多呢

テリ君(桃夫模式) | 2023-01-08 17:03:00 | 巴幣 12 | 人氣 267

1. 字串反轉:讀取 input.txt 的字串,然後整個反轉後寫入 output.txt 裡面

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

#define MAX 256

int main(){
    FILE *ip, *op;
    
    ip = fopen("input.txt", "r");
    op = fopen("output.txt", "w");
    
    char line[MAX];
    int N = 0;
    
    while(fgets(line, MAX, ip) != NULL){
    }
    
    N = strlen(line);
    
    for(int i = N - 1; i >= 0; i--){ // N must - 1 while line[N] == NULL;
        fprintf(op,"%c", line[i]);
    }
    
    fclose(ip);
    fclose(op);
    
    return 0;
}

2. 尋找因數: 用一迴圈尋找任意整數的因數

#include <stdio.h>

int main(){
    int num;
    scanf("%d", &num);
    printf("The divisors of %d are: ", num);
    for(int i = 1; i <= num; i++){
        if(num % i == 0) printf("%d ", i);
    }
    return 0;
}

3. 承上題: 尋找兩數的最大公因數HCF


#include <stdio.h>

int main(){
    int T; // T for n lines;
    int m, n; // m, n for two ints;
    int hcf = 0; // hcf for hcf
    
    scanf("%d", &T);
    
    for(int i = 0; i < T; i++){
        scanf("%d %d", &m, &n);
        if(m > n){
            for(int j = 1; j <= n; j++){
                if(m % j == 0 && n % j == 0){
                    hcf = j;
                }
            }
        }
        else{
            for(int j = 1; j <= m; j++){
                if(m % j == 0 && n % j == 0){
                    hcf = j;
                }
            }
        }
        printf("%d\n", hcf);
    }
    
    return 0;
}

4. 多項式的linked list: 定一多項式任一項,之後把多項式A每個係數輸入後,最後print出該多項式。

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

#define max 256

typedef struct poly_node{
    int coef;
    int exp;
    struct poly_node *link;
}Node;

int main(){
    
    Node *header = NULL, *cur = NULL; // declare struct pointer
    int m[max], n[max];
    int T, first = 1, temp; // T for cases, first for the first check, temp for bubble sort.
    
    while(scanf("%d %d", &m[T], &n[T])){
        if(m[T] == 0 && n[T] == 0) break;
        T++;
    }
    
    for(int i = 0; i < T - 1; i++){
        for(int j = 0; j < T - 1 - i; j++){
            if(n[j] < n[j + 1]){
                temp = n[j];
                n[j] = n[j + 1];
                n[j + 1] = temp;
                    
                temp = m[j];
                m[j] = m[j + 1];
                m[j + 1] = temp;
            }
        }
    }
    // bubble sort
    
    
    for(int i = 0; i < T; i++){
    
        if(i == 0){
            header = (Node*)malloc(sizeof(Node));
            header -> coef = m[i];
            header -> exp = n[i];
            header -> link = NULL;
            cur = header;
            continue;
        }
        cur -> link = (Node*)malloc(sizeof(Node));
        cur = cur -> link;
        cur -> coef = m[i];
        cur -> exp = n[i];
        cur -> link = NULL;
        
    }
    // put m and n into the linked list.
    
    printf("A = ");
    while(header != NULL){
        if(first == 1){
            first = 0;
            printf("%dX^%d", header -> coef, header -> exp);
        }
        else printf(" + %dX^%d", header -> coef, header -> exp);
        header = header -> link;
    }
    
    return 0;
}

5.級數的和: (1 + 2) + (2 + 4) + (3 + 6) + ... +(n + 2* n) 的和,阿其實就是 (1 + 2 + ... + n)*3

#include <stdio.h>

int main(){
    int T;
    // T for test cases;
    int num;
    // num for case;
    int sum = 0;
    // sum for sum of case;
    
    scanf("%d", &T);
    
    for(int i = 0; i < T; i++){
        scanf("%d", &num);
        for(int j = 0; j <= num; j++){
            sum = sum + (j + j * 2);
        }
        printf("%d\n", sum);
        sum = 0;
    }
    
    return 0;
}

6.密碼鎖: int password = 48763, pw; scanf("%d", &pw); if pw == password 過了

#include <stdio.h>

#define password 1688

int main(){
    int pw;
    while(printf("Please Enter the Number: ")){
        scanf("%d", &pw);
        if(pw == password) break;
    }
    printf("Log In Successfully! ");
    // I use English instead of Chinese while there is compiling problem with Chinese words.
    
    return 0;
}

7.矩陣加總: 恩就矩陣加總

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

#define ROW 3
#define COL 3

int main(){
    int *a, *b, *sum;
    int space = 0;
    space = ROW * COL;
    
    a = (int *)malloc(sizeof(int) * space);
    b = (int *)malloc(sizeof(int) * space);
    sum = (int *)malloc(sizeof(int) * space);
    
    printf("\nEnter all data of array A :", space);
    
    for(int i = 0; i < space; i++){
        scanf("%d", a + i);
    }
    
    printf("\nEnter all data of array B :", space);
    
    for(int i = 0; i < space; i++){
        scanf("%d", b + i);
    }
    
    for(int i = 0; i < space; i++){
        *(sum + i) = *(a + i) + *(b + i);
    }
    
    printf("\nThe result is:\n");
    printf("Array A:\n");
    for(int i = 0; i < space; i++){
        if((i + 1 ) % ROW == 0){
            printf("%d\n", *(a + i));
        }
        else{
            printf("%d, ", *(a + i));
        }
    }
    
    printf("+\n");
    printf("Array B:\n");
    
    for(int i = 0; i < space; i++){
        if((i + 1 ) % ROW == 0){
            printf("%d\n", *(b + i));
        }
        else{
            printf("%d, ", *(b + i));
        }
    }
    
    printf("=\n");
    
    for(int i = 0; i < space; i++){
        if((i + 1 ) % ROW == 0){
            printf("%d\n", *(sum + i));
        }
        else{
            printf("%d, ", *(sum + i));
        }
    }
    
    return 0;
}

8.季節判斷: 輸入幾月就哪個季節安捏

#include <stdio.h>

int main(){
    int month;
    printf("Please enter the month: ");
    scanf("%d", &month);
    if(month > 12 || month < 1) return 0;
    else if(month == 12 || month == 1 || month == 2) printf("The season now is Winter");
    else if(month == 3 || month == 4 || month == 5) printf("The season now is Spring");
    else if(month == 6 || month == 7 || month == 8) printf("The season now is Summer");
    else printf("The season now is Fall");

    return 0;
}

9.指標、陣列運用: 宣告一個函式void addTwoArrays(int *, int *)可以加總兩個二維陣列
#include <stdio.h>
#include <stdlib.h>

#define ROW 3
#define COL 3

int addTwoArrays(int *m, int *n){
    int sum = 0;
    sum = *m + *n;
    *n = sum;
}

int main(){
    int *a, *b, *sum;
    int space = 0;
    space = ROW * COL;
    int temp = 0;
    
    a = (int *)malloc(sizeof(int) * space);
    b = (int *)malloc(sizeof(int) * space);
    sum = (int *)malloc(sizeof(int) * space);
    
    printf("\nEnter all data of array A :", space);
    
    for(int i = 0; i < space; i++){
        scanf("%d", a + i);
    }
    
    printf("\nEnter all data of array B :", space);
    
    for(int i = 0; i < space; i++){
        scanf("%d", b + i);
    }
    
    printf("\nThe original arrays are:\n");
    printf("Array A:\n");
    for(int i = 0; i < space; i++){
        if((i + 1 ) % ROW == 0){
            printf("%d\n", *(a + i));
        }
        else{
            printf("%d, ", *(a + i));
        }
    }
    
    printf("and\n");
    printf("Array B:\n");
    
    for(int i = 0; i < space; i++){
        if((i + 1 ) % ROW == 0){
            printf("%d\n", *(b + i));
        }
        else{
            printf("%d, ", *(b + i));
        }
    }
    
    for(int i = 0; i < space; i++){
        addTwoArrays(a + i, b + i);
    }
    
    printf("\nThe operated arrays are:\n");
    printf("Array A:\n");
    for(int i = 0; i < space; i++){
        if((i + 1 ) % ROW == 0){
            printf("%d\n", *(a + i));
        }
        else{
            printf("%d, ", *(a + i));
        }
    }
    
    printf("and\n");
    printf("Array B:\n");
    
    for(int i = 0; i < space; i++){
        if((i + 1 ) % ROW == 0){
            printf("%d\n", *(b + i));
        }
        else{
            printf("%d, ", *(b + i));
        }
    }
    
    

    
    return 0;
}


10. 判斷兩個正整數是否互質

#include <stdio.h>

int main(){
    int T; // T for n lines;
    int m, n; // m, n for two ints;
    int hcf = 0; // hcf for hcf
    
    scanf("%d", &T);
    
    for(int i = 0; i < T; i++){
        scanf("%d %d", &m, &n);
        if(m > n){
            for(int j = 1; j <= n; j++){
                if(m % j == 0 && n % j == 0){
                    hcf = j;
                }
            }
        }
        else{
            for(int j = 1; j <= m; j++){
                if(m % j == 0 && n % j == 0){
                    hcf = j;
                }
            }
        }
        if(hcf == 1) printf("Yes\n");
        else printf("%d\n", hcf);
    }
    
    return 0;
}


創作回應

更多創作