服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - C++实现单词管理系统

C++实现单词管理系统

2022-10-18 13:08一条菜鸟鱼 C/C++

这篇文章主要为大家详细介绍了C++实现单词管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了C++实现单词管理系统的具体代码,供大家参考,具体内容如下

实现功能

  • 退出
  • 添加单词
  • 删除单词
  • 修改单词
  • 查询单词
  • 排序单词
  • 显示单词

简述

单词管理系统使用了C++语言连接MySQL数据库实现常规CRUD操作,界面为控制台窗体+文字显示的方式。

测试环境

使用Win10 + Code::Blocks IDE编写。

运行截图

C++实现单词管理系统

代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#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;
}

数据库代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/*
 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;

总结

代码还是比较简单的,主要是不同编译器,它所对应的驱动方式会有所不同。因此如果想要移植到其它的IDE如: VC6++、VS、DEV 等,可能需要一些处理操作,还要添加数据库连接驱动和库函数。当然,难点也就在于获取ODBC连接,这块还是需要一些时间琢磨的。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/qq_40491534/article/details/120113880

延伸 · 阅读

精彩推荐
  • C/C++C++类结构体与json相互转换

    C++类结构体与json相互转换

    这篇文章主要介绍的是C++类结构体与json相互转换,json字符串一般使用的是开源的类库Newtonsoft.Json,方法十分简洁,下面就随小编一起看下面文章内容吧...

    Yaronzz4562022-01-19
  • C/C++C++操作SQLite简明教程

    C++操作SQLite简明教程

    这篇文章主要介绍了C++操作SQLite简明教程,包含创建表、插入数据、查询数据等常用操作,需要的朋友可以参考下...

    C++教程网9452021-01-20
  • C/C++C实现不定长数组的示例

    C实现不定长数组的示例

    今天小编就为大家分享一篇C实现不定长数组的示例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    蔡松豆4912021-06-28
  • C/C++详解C++ 动态库导出函数名乱码及解决

    详解C++ 动态库导出函数名乱码及解决

    这篇文章主要介绍了C++ 动态库导出函数名乱码及解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们...

    luochenlong10342021-07-25
  • C/C++C++工厂方法之对象创建型模式详解

    C++工厂方法之对象创建型模式详解

    这篇文章主要为大家详细介绍了C++对象创建型模式,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你...

    早睡身体好hh10142022-10-17
  • C/C++C语言员工信息管理系统源代码

    C语言员工信息管理系统源代码

    这篇文章主要为大家详细介绍了C语言员工信息管理系统源代码,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    daisy9972021-10-15
  • C/C++C++实现LeetCode(17.电话号码的字母组合)

    C++实现LeetCode(17.电话号码的字母组合)

    这篇文章主要介绍了C++实现LeetCode(17.电话号码的字母组合),本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考...

    Grandyang9742021-11-24
  • C/C++C语言中关于scanf函数的一些问题详解

    C语言中关于scanf函数的一些问题详解

    这篇文章主要为大家介绍了C语言中关于scanf函数的一些问题,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助...

    风雪宜哉10122022-08-04