這篇文章主要為大家展示了“如何使用C++實現單詞管理系統”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“如何使用C++實現單詞管理系統”這篇文章吧。
具體內容如下
退出
添加單詞
刪除單詞
修改單詞
查詢單詞
排序單詞
顯示單詞
單詞管理系統使用了C++語言連接MySQL數據庫實現常規CRUD操作,界面為控制臺窗體+文字顯示的方式。
使用Win10 + Code::Blocks IDE編寫。
運行截圖
#include <stdio.h> #include <winsock2.h> //進行網絡連接 #include <mysql.h> //MySQL C API訪問mysql數據庫 #pragma comment (lib, "libmysql.lib") #define N 50 typedef struct Dictionary { char id[50]; char eng[100]; char chi[100]; } Dictionary; //變量配置 MYSQL *conn; //數據庫連接句柄 MYSQL_RES *res; //執行數據庫語言結果 MYSQL_ROW row; //存放一個數據記錄 char* server = "localhost";//本地連接 char* user = "root";// char* password = "yan19991001";//mysql密碼 char* database = "dictionary";//數據庫名 int t,rc; char query[N]; //需要查詢的語句 void readEng(); void addEng(); void delEng(); void modEng(); void seaEng(); void sort(); void sort(){ char id[N],eng[N],chi[N]; sprintf(query,"select * from test order by eng"); printf("查詢語句:%s\n",query); rc = mysql_query(conn,query); if (0 != rc) { printf("mysql_real_query(): %s\n", mysql_error(conn)); return -1; }else{ printf("查詢結果:\n"); res = mysql_use_result(conn); //獲取結果 if (res) { while ((row = mysql_fetch_row(res)) != NULL) { //printf("num=%d\n",mysql_num_fields(res));//列數 for (t = 0; t < mysql_num_fields(res); t++) printf("%8s ", row[t]); printf("\n"); } } mysql_free_result(res); } } void addEng() { char id[N],eng[N],chi[N]; printf("請輸入要增加的詞典信息:\n"); printf("編號:\n"); scanf("%s",id); printf("單詞:\n"); scanf("%s",eng); printf("中文釋義:\n"); scanf("%s",chi); sprintf(query,"insert into test values('%s','%s','%s')",id,eng,chi); printf("%s",query); rc = mysql_query(conn,query); if (0 != rc) { printf("mysql_real_query(): %s\n", mysql_error(conn)); return -1; }else{ printf("添加成功!\n"); } //mysql_close(conn); //斷開數據庫 } void delEng(){ char id[N]; printf("請輸入你要刪除的單詞編號:"); scanf("%s",id); sprintf(query,"delete from test where id = '%s'",id); printf("查詢語句:%s\n",query); rc = mysql_query(conn,query); if (0 != rc) { printf("mysql_real_query(): %s\n", mysql_error(conn)); return -1; }else{ printf("刪除成功!\n"); } } void modEng(){ char id[N],eng[N],chi[N]; printf("請輸入你要修改的單詞編號:"); scanf("%s",id); printf("單詞:\n"); scanf("%s",eng); printf("中文釋義:\n"); scanf("%s",chi); sprintf(query,"update test set eng = '%s',chi = '%s' where id = '%s'",eng,chi,id); printf("查詢語句:%s\n",query); rc = mysql_query(conn,query); if (0 != rc) { printf("mysql_real_query(): %s\n", mysql_error(conn)); return -1; }else{ printf("修改成功!\n"); } } void seaEng(){ char id[N],eng[N],chi[N]; printf("請輸入你要查詢的單詞編號:"); scanf("%s",id); sprintf(query,"select * from test where id = '%s'",id); printf("查詢語句:%s\n",query); rc = mysql_query(conn,query); if (0 != rc) { printf("mysql_real_query(): %s\n", mysql_error(conn)); return -1; }else{ printf("查詢結果:\n"); res = mysql_use_result(conn); //獲取結果 if (res) { while ((row = mysql_fetch_row(res)) != NULL) { //printf("num=%d\n",mysql_num_fields(res));//列數 for (t = 0; t < mysql_num_fields(res); t++) printf("%8s ", row[t]); printf("\n"); } } mysql_free_result(res); } } void init() { conn = mysql_init(NULL); //句柄初始化 if (!mysql_real_connect(conn, server, user, password, database, 3306, NULL, 0)) //判斷是否連接成功 { printf("Error connecting to database:%s\n", mysql_error(conn)); } else { printf("Connected...\n"); } //字符編碼,解決亂碼 if (!mysql_set_character_set(conn, "gbk")) { printf("New client character set: %s\n", mysql_character_set_name(conn)); } } void readEng() { char * query = "select * from test"; //需要查詢的語句 if (mysql_query(conn, query)) { printf("錯誤信息:%s\n", mysql_error(conn)); } else { printf("查詢結果:\n"); res = mysql_use_result(conn); //獲取結果 if (res) { while ((row = mysql_fetch_row(res)) != NULL) { //printf("num=%d\n",mysql_num_fields(res));//列數 for (t = 0; t < mysql_num_fields(res); t++) printf("%8s ", row[t]); printf("\n"); } } mysql_free_result(res); } } void menu() { int choice; char id[20]; do { printf("------------------------------\n"); printf("0、退出\n"); printf("1、添加單詞\n"); printf("2、刪除單詞\n"); printf("3、修改單詞\n"); printf("4、查詢單詞\n"); printf("5、排序單詞\n"); printf("6、顯示單詞\n"); printf("------------------------------\n"); printf("請輸入選擇:"); scanf("%d",&choice); //根據choice的值選取功能 switch(choice) { case 0: exit(0); break; case 1: addEng(); break; case 2: delEng(); break; case 3: modEng(); break; case 4: seaEng(); break; case 5: sort(); break; case 6: readEng(); break; default: printf("輸入錯誤!"); } system("pause"); system("cls"); } while(choice != 0); } int main() { init(); menu(); return 0; }
數據庫代碼
/* Navicat MySQL Data Transfer Source Server : localhost_3306 Source Server Type : MySQL Source Server Version : 50725 Source Host : localhost:3306 Source Schema : dictionary Target Server Type : MySQL Target Server Version : 50725 File Encoding : 65001 Date: 28/06/2021 13:44:35 */ SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0; -- ---------------------------- -- Table structure for test -- ---------------------------- DROP TABLE IF EXISTS `test`; CREATE TABLE `test` ( `id` varchar(50) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL, `eng` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL, `chi` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_bin ROW_FORMAT = Dynamic; -- ---------------------------- -- Records of test -- ---------------------------- INSERT INTO `test` VALUES ('1', 'adopt', '領養'); INSERT INTO `test` VALUES ('2', 'pen', '鋼筆'); INSERT INTO `test` VALUES ('3', 'apple', '蘋果'); INSERT INTO `test` VALUES ('4', 'borrow', '借閱'); INSERT INTO `test` VALUES ('5', 'electric', '電力'); SET FOREIGN_KEY_CHECKS = 1;
以上是“如何使用C++實現單詞管理系統”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。