Vị trí
Bài học 3 trên 8 trong khóa học Lớp người dùng, dự án OOP của Coddy.
Thử thách
DễThêm một phương thức có tên info để in thông tin về người dùng, mỗi trường trên một dòng, theo định dạng sau:
Username: <username>
Email: <email>
Location: <location>Phương thức
infokhông trả về gì cả, nó chỉ in ra.
Tự làm thử
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "user.h"
int main() {
User u;
User_init(&u, "", "");
char line[1024];
while (fgets(line, sizeof(line), stdin)) {
line[strcspn(line, "\r\n")] = '\0';
char* cmd = strtok(line, " \t");
if (!cmd) continue;
if (strcmp(cmd, "new") == 0) {
char* un = strtok(NULL, " \t");
char* em = strtok(NULL, " \t");
User_init(&u, un ? un : "", em ? em : "");
}
if (strcmp(cmd, "username") == 0) { printf("%s\n", u.username); }
if (strcmp(cmd, "email") == 0) { printf("%s\n", u.email); }
if (strcmp(cmd, "setLocation") == 0) { char* v = strtok(NULL, " \t"); if (v) User_setLocation(&u, v); }
if (strcmp(cmd, "getLocation") == 0) { printf("%s\n", User_getLocation(&u)); }
if (strcmp(cmd, "info") == 0) { User_info(&u); }
}
return 0;
}
Tất cả bài học trong Lớp người dùng, dự án OOP
Tự luyện tập: Trình biên dịch C online