41223108 cp2023

  • Home
    • SMap
    • reveal
    • blog
  • About
  • 倉儲維護
    • s.cycu.org維護
    • windows維護
  • weekly progress
    • w1~4
    • w5
    • w6
    • w7
    • w9
    • w12
      • helloworld
      • GD繪圖程式1
    • W13
    • w15
  • ANSIC
    • 1.
    • 2.
    • 3.
    • 4.
    • 5.
    • 6.
    • 7.
    • 8.
    • 9.
    • 10.
  • NOTE
    • ssh
    • puttygen
  • 國旗練習
    • Thai
    • Laos
    • Bangladesh
    • vietnam
    • Italian
    • Russia
    • Belgium
    • Ukraine
    • Korea
  • 課程評分
  • Brython
  • setup
  • C_lib
  • replit
  • MSD
  • Final
w7 << Previous Next >> w12

w9

2b w8 作業: 請根據 cad2023_2b_w8.txt 檔案中的內容, 透過程式的讀取與篩選, 按照時間先後, 列出 2b 修課學員中已經登入 s1511.cycu.org 的學號. (修課學員名單 2a: 0838, 2b: 0851)

假如在近端處理, 必須同時儲存兩個檔案後, 進行資料選取:

get_stud_num_from_last_data.py (若採 Brython 編寫, 可以直接列出結果, 依照登入時間先後排序, 其中 41123227 為管理者最早測試時登入, 若採用 ANSI 編寫: c_parse_last_cp2023.7z)

read_last_final1.c 原始碼:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <stdio.h>
#include <string.h>
 
int main() {
    // Open the user file for reading
    FILE* user_file = fopen("2b_user_list.txt", "r");
    if (user_file == NULL) {
        perror("Error opening user file");
        return 1;
    }
 
    char line[100]; // Assuming a maximum line length of 100 characters
 
    char valid_users[100][20]; // Assuming a maximum of 100 valid users with a length of 20 characters each
    int valid_user_count = 0;
 
    // Read and store the student numbers from the user file
    while (fgets(line, sizeof(line), user_file)) {
        line[strcspn(line, "\n")] = '\0'; // Remove the newline character
        strcpy(valid_users[valid_user_count], line);
        valid_user_count++;
    }
 
    // Close the user file
    fclose(user_file);
 
    // Open the CAD file for reading
    FILE* cad_file = fopen("cad2023_2b_w8.txt", "r");
    if (cad_file == NULL) {
        perror("Error opening CAD file");
        return 1;
    }
 
    char unique_users[100][20]; // Assuming a maximum of 100 unique users with a length of 20 characters each
    int unique_user_count = 0;
 
    // Read the CAD file line by line
    while (fgets(line, sizeof(line), cad_file)) {
        char* token = strtok(line, " "); // Split the line by space
        if (token != NULL && strstr(token, "cad") == token) {
            // Extract the student number (skip "cad")
            char student_number[20]; // Assuming a maximum length of 20 characters for a student number
            strcpy(student_number, token + 3); // Skip "cad"
             
            // Check if the student number is in the list of valid users and not a duplicate
            int is_valid = 0;
            for (int i = 0; i < valid_user_count; i++) {
                if (strcmp(valid_users[i], student_number) == 0) {
                    is_valid = 1;
                    break;
                }
            }
             
            // Add the student number to the unique_users list if it's valid and not a duplicate
            if (is_valid) {
                int is_duplicate = 0;
                for (int i = 0; i < unique_user_count; i++) {
                    if (strcmp(unique_users[i], student_number) == 0) {
                        is_duplicate = 1;
                        break;
                    }
                }
                 
                if (!is_duplicate) {
                    strcpy(unique_users[unique_user_count], student_number);
                    unique_user_count++;
                }
            }
        }
    }
 
    // Reverse the order of the unique student numbers
    for (int i = 0; i < unique_user_count / 2; i++) {
        char temp[20];
        strcpy(temp, unique_users[i]);
        strcpy(unique_users[i], unique_users[unique_user_count - 1 - i]);
        strcpy(unique_users[unique_user_count - 1 - i], temp);
    }
 
    // Print the unique student numbers in reverse order
    for (int i = 0; i < unique_user_count; i++) {
        printf("%s\n", unique_users[i]);
    }
 
    // Close the CAD file
    fclose(cad_file);
 
    return 0;
}

若採用 Python 編寫:

read_last.py 原始碼

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# 讀取學號檔案
with open("2b_user_list.txt", 'r') as user_file:
    user_lines = user_file.read().splitlines()
  
# 讀取 last 指令轉出的檔案, 以 last -w > cad2023_2b_w8.txt 建立檔案
with open("cad2023_2b_w8.txt", 'r') as cad_file:
    # 以下是利用跳行符號, 將每一行區隔成數列
    cad_lines = cad_file.read().splitlines()
#print(cad_lines)
  
# 從 cad_lines 建立所有登入使用者數列
login_users = []
for i in cad_lines:
    line_list = i.split(" ")
    login_users.append(line_list[0])
#print(login_users)
  
# 根據 https://stackoverflow.com/questions/480214/how-do-i-remove-duplicates-from-a-list-while-preserving-order
# 數列去除重複元素但仍保持原始次序
login_users = list(dict.fromkeys(login_users))
#print(login_users)
  
# 建立數列存放符合條件的使用者
valid_users = []
  
# 取出符合條件的使用者
for line in login_users:
    if "cad" in line:
        # 將 cad 字串去除
        user_number = line.replace("cad", "")
        if user_number in user_lines:
            valid_users.append(user_number)
              
# 利用 reverse() 將 valid_users 反向排序
valid_users.reverse()
  
# , 最早登入者列在最前面
for user in valid_users:
    print(user)

w7 << Previous Next >> w12

Copyright © All rights reserved | This template is made with by Colorlib