写成代码,含有两三项功能即可
时间: 2024-03-12 16:04:07 浏览: 53
以下是用C语言实现的一个简单的在线学习网站后端示例代码,包括了用户登录、注册和课程管理三个功能:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mysql/mysql.h>
// 用户结构体
typedef struct {
int id;
char username[50];
char password[50];
} User;
// 课程结构体
typedef struct {
int id;
char name[50];
char description[100];
int user_id;
} Course;
// 连接数据库
MYSQL *connect_db() {
MYSQL *conn = mysql_init(NULL);
if (!mysql_real_connect(conn, "localhost", "root", "password", "learning", 0, NULL, 0)) {
printf("Error connecting to database: %s\n", mysql_error(conn));
exit(1);
}
return conn;
}
// 用户登录
User *login(char *username, char *password) {
MYSQL *conn = connect_db();
MYSQL_RES *res;
MYSQL_ROW row;
User *user = NULL;
char query[200];
sprintf(query, "SELECT * FROM users WHERE username='%s' AND password='%s'", username, password);
if (mysql_query(conn, query)) {
printf("Error executing query: %s\n", mysql_error(conn));
exit(1);
}
res = mysql_use_result(conn);
if (res) {
if ((row = mysql_fetch_row(res))) {
user = (User *)malloc(sizeof(User));
user->id = atoi(row[0]);
strcpy(user->username, row[1]);
strcpy(user->password, row[2]);
}
mysql_free_result(res);
}
mysql_close(conn);
return user;
}
// 用户注册
User *register_user(char *username, char *password) {
MYSQL *conn = connect_db();
MYSQL_RES *res;
MYSQL_ROW row;
User *user = NULL;
char query[200];
sprintf(query, "INSERT INTO users (username, password) VALUES ('%s', '%s')", username, password);
if (mysql_query(conn, query)) {
printf("Error executing query: %s\n", mysql_error(conn));
exit(1);
}
sprintf(query, "SELECT * FROM users WHERE username='%s' AND password='%s'", username, password);
if (mysql_query(conn, query)) {
printf("Error executing query: %s\n", mysql_error(conn));
exit(1);
}
res = mysql_use_result(conn);
if (res) {
if ((row = mysql_fetch_row(res))) {
user = (User *)malloc(sizeof(User));
user->id = atoi(row[0]);
strcpy(user->username, row[1]);
strcpy(user->password, row[2]);
}
mysql_free_result(res);
}
mysql_close(conn);
return user;
}
// 添加课程
void add_course(char *name, char *description, int user_id) {
MYSQL *conn = connect_db();
char query[200];
sprintf(query, "INSERT INTO courses (name, description, user_id) VALUES ('%s', '%s', %d)", name, description, user_id);
if (mysql_query(conn, query)) {
printf("Error executing query: %s\n", mysql_error(conn));
exit(1);
}
mysql_close(conn);
}
// 删除课程
void delete_course(int course_id, int user_id) {
MYSQL *conn = connect_db();
char query[200];
sprintf(query, "DELETE FROM courses WHERE id=%d AND user_id=%d", course_id, user_id);
if (mysql_query(conn, query)) {
printf("Error executing query: %s\n", mysql_error(conn));
exit(1);
}
mysql_close(conn);
}
// 获取用户的所有课程
Course **get_courses(int user_id) {
MYSQL *conn = connect_db();
MYSQL_RES *res;
MYSQL_ROW row;
Course **courses = NULL;
int num_courses = 0;
char query[200];
sprintf(query, "SELECT * FROM courses WHERE user_id=%d", user_id);
if (mysql_query(conn, query)) {
printf("Error executing query: %s\n", mysql_error(conn));
exit(1);
}
res = mysql_use_result(conn);
if (res) {
while ((row = mysql_fetch_row(res))) {
courses = (Course **)realloc(courses, sizeof(Course *) * (num_courses + 1));
courses[num_courses] = (Course *)malloc(sizeof(Course));
courses[num_courses]->id = atoi(row[0]);
strcpy(courses[num_courses]->name, row[1]);
strcpy(courses[num_courses]->description, row[2]);
courses[num_courses]->user_id = atoi(row[3]);
num_courses++;
}
mysql_free_result(res);
}
mysql_close(conn);
return courses;
}
int main() {
// 登录
User *user = login("john", "123456");
if (user) {
printf("User %s logged in successfully.\n", user->username);
} else {
printf("Invalid username or password.\n");
}
// 注册
user = register_user("mary", "abcdef");
if (user) {
printf("User %s registered successfully.\n", user->username);
} else {
printf("Registration failed.\n");
}
// 添加课程
add_course("Math", "Learn math from basic arithmetic to calculus.", 1);
add_course("History", "Explore the history of human civilization.", 2);
// 删除课程
delete_course(2, 2);
// 获取用户的所有课程
Course **courses = get_courses(1);
printf("User 1 has %d courses:\n", sizeof(courses));
for (int i = 0; i < sizeof(courses); i++) {
printf("%d. %s: %s\n", courses[i]->id, courses[i]->name, courses[i]->description);
}
return 0;
}
```
以上代码仅为示例,实际实现时需要根据具体需求进行修改和完善。
阅读全文