这篇文章主要为大家展示了“如何使用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;}
数据库代码
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++实现单词管理系统”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注编程网行业资讯频道!