C++實(shí)現(xiàn)單詞管理系統(tǒng)
本文實(shí)例為大家分享了C++實(shí)現(xiàn)單詞管理系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
實(shí)現(xiàn)功能
- 退出
- 添加單詞
- 刪除單詞
- 修改單詞
- 查詢單詞
- 排序單詞
- 顯示單詞
簡述
單詞管理系統(tǒng)使用了C++語言連接MySQL數(shù)據(jù)庫實(shí)現(xiàn)常規(guī)CRUD操作,界面為控制臺(tái)窗體+文字顯示的方式。
測試環(huán)境
使用Win10 + Code::Blocks IDE編寫。
運(yùn)行截圖
代碼
#include <stdio.h> #include <winsock2.h> //進(jìn)行網(wǎng)絡(luò)連接 #include <mysql.h> ? ?//MySQL C API訪問mysql數(shù)據(jù)庫 #pragma comment (lib, "libmysql.lib") #define N 50 ? typedef struct Dictionary { ? ? char id[50]; ? ? char eng[100]; ? ? char chi[100]; } Dictionary; ? //變量配置 MYSQL *conn; //數(shù)據(jù)庫連接句柄 MYSQL_RES *res; //執(zhí)行數(shù)據(jù)庫語言結(jié)果 MYSQL_ROW row; //存放一個(gè)數(shù)據(jù)記錄 char* server = "localhost";//本地連接 char* user = "root";// char* password = "yan19991001";//mysql密碼 char* database = "dictionary";//數(shù)據(jù)庫名 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("查詢結(jié)果:\n"); ? ? ? ? res = mysql_use_result(conn);?? ?//獲取結(jié)果 ? ? ? ? if (res) ? ? ? ? { ? ? ? ? ? ? while ((row = mysql_fetch_row(res)) != NULL) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? //printf("num=%d\n",mysql_num_fields(res));//列數(shù) ? ? ? ? ? ? ? ? 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("編號(hào):\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); //斷開數(shù)據(jù)庫 } ? void delEng(){ ? ? char id[N]; ? ? printf("請輸入你要?jiǎng)h除的單詞編號(hào):"); ? ? 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("請輸入你要修改的單詞編號(hào):"); ? ? 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("請輸入你要查詢的單詞編號(hào):"); ? ? 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("查詢結(jié)果:\n"); ? ? ? ? res = mysql_use_result(conn);?? ?//獲取結(jié)果 ? ? ? ? if (res) ? ? ? ? { ? ? ? ? ? ? while ((row = mysql_fetch_row(res)) != NULL) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? //printf("num=%d\n",mysql_num_fields(res));//列數(shù) ? ? ? ? ? ? ? ? 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("錯(cuò)誤信息:%s\n", mysql_error(conn)); ? ? } ? ? else ? ? { ? ? ? ? printf("查詢結(jié)果:\n"); ? ? ? ? res = mysql_use_result(conn);?? ?//獲取結(jié)果 ? ? ? ? if (res) ? ? ? ? { ? ? ? ? ? ? while ((row = mysql_fetch_row(res)) != NULL) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? //printf("num=%d\n",mysql_num_fields(res));//列數(shù) ? ? ? ? ? ? ? ? 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); ? ? ? ?//根據(jù)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("輸入錯(cuò)誤!"); ? ? ? ? } ? ? ? ? system("pause"); ? ? ? ? system("cls"); ? ? } ? ? while(choice != 0); } ? int main() { ? ? init(); ? ? menu(); ? ? return 0; }
數(shù)據(jù)庫代碼
/* ?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', '領(lǐng)養(yǎng)'); 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;
總結(jié)
代碼還是比較簡單的,主要是不同編譯器,它所對應(yīng)的驅(qū)動(dòng)方式會(huì)有所不同。因此如果想要移植到其它的IDE如: VC6++、VS、DEV 等,可能需要一些處理操作,還要添加數(shù)據(jù)庫連接驅(qū)動(dòng)和庫函數(shù)。當(dāng)然,難點(diǎn)也就在于獲取ODBC連接,這塊還是需要一些時(shí)間琢磨的。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- C++實(shí)現(xiàn)簡單的圖書管理系統(tǒng)
- C++實(shí)現(xiàn)簡單職工信息管理系統(tǒng)
- C++實(shí)現(xiàn)簡單的學(xué)生管理系統(tǒng)
- C++學(xué)生信息管理系統(tǒng)
- C++實(shí)現(xiàn)停車場管理系統(tǒng)
- C++實(shí)現(xiàn)簡單的信息管理系統(tǒng)
- C++實(shí)現(xiàn)企業(yè)職工工資管理系統(tǒng)
- 學(xué)生成績管理系統(tǒng)C++實(shí)現(xiàn)代碼
- C++基礎(chǔ)學(xué)生管理系統(tǒng)
- C++實(shí)現(xiàn)簡單職工管理系統(tǒng)
相關(guān)文章
C++中實(shí)現(xiàn)把表的數(shù)據(jù)導(dǎo)出到EXCEL并打印實(shí)例代碼
這篇文章主要介紹了實(shí)現(xiàn)把表的數(shù)據(jù)導(dǎo)出到EXCEL并打印實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-04-04Visual Studio 2022 的安裝和創(chuàng)建C++項(xiàng)目(圖文教程)
本文主要介紹了Visual Studio 2022 的安裝和創(chuàng)建C++項(xiàng)目,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05Qt實(shí)現(xiàn)繪制網(wǎng)格背景的示例代碼
這篇文章主要介紹了Qt如何實(shí)現(xiàn)繪制網(wǎng)格背景,并且能實(shí)現(xiàn)窗口大小調(diào)整時(shí)網(wǎng)格背景也自動(dòng)調(diào)整重繪,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2022-06-06如何實(shí)現(xiàn)在C++中調(diào)用C函數(shù)
這篇文章主要介紹了如何實(shí)現(xiàn)在C++中調(diào)用C函數(shù)問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08使用kendynet構(gòu)建異步redis訪問服務(wù)
這篇文章主要介紹了在kendynet上寫的一個(gè)簡單的redis異步訪問接口,大家參考使用吧2014-01-01C語言實(shí)現(xiàn)字符轉(zhuǎn)unix時(shí)間戳的簡單實(shí)例
下面小編就為大家?guī)硪黄狢語言實(shí)現(xiàn)字符轉(zhuǎn)unix時(shí)間戳的簡單實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-06-06