欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++實(shí)現(xiàn)單詞管理系統(tǒng)

 更新時(shí)間:2022年03月12日 13:58:25   作者:一條菜鳥魚  
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)單詞管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(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í)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論