c++—简单的密码本实现
主要实现功能有增删改查数据,本地读取保存。
数据存储设计 data.h data.cpp
#pragma once #define 添加账户 1 #define 删除账户 2 #define 修改账户 3 #define 查询账户 4 #define 查询所有 5 #define 保存本地 6 #define 退出 7#define NUM 100 extern int g_nNum; //当前密码本容量 extern int oCho; //判断是否保存到本地了//定义账户结构体 typedef struct _ACCOUNT {char website[100];char userName[100];char passWord[100]; }ACCOUNT,*PACCOUNT;
#include "data.h" int g_nNum;
函数方法的实现:
ctrl.h
#pragma once #include "data.h" //增加账户 void insertInfo(ACCOUNT acc[], int* nCount); //删除账户 void deleteInfo(ACCOUNT acc[], int *nCount); //修改账户 void alterInfo(ACCOUNT acc[], int nCount); //查询账户 void searchInfo(ACCOUNT acc[], int nCount); //查询所有账户 void searchALLInfo(ACCOUNT acc[], int nCount); //保存操作 void saveInfo(ACCOUNT acc[], int nCount); //读取数据 void readInfo(PACCOUNT *pAc, int *nCount);
ctrl.cpp
#include <stdio.h> #include <string.h> #include <malloc.h> #include "data.h" #include "ctrl.h" void insertInfo(ACCOUNT acc[], int* nCount) {int nNum = *nCount;if (nNum <0 || nNum > NUM){printf("添加异常请重试!\n");return;}printf("请输入需要保存的站点:\n");scanf_s("%s", acc[nNum].website,100);char ss = getchar();printf("请输入需要保存的用户名:\n");scanf_s("%s", acc[nNum].userName,100);ss = getchar();printf("请输入需要保存的密码:\n");scanf_s("%s", acc[nNum].passWord,100);ss = getchar();(*nCount)++;printf("保存成功!返回主菜单\n");}void deleteInfo(ACCOUNT acc[], int* nCount) {int nNum = *nCount;if (nNum <0 || nNum > NUM){printf("有点小毛病我也不知道咋回事你再试试!\n");return;}char str[100] = {};printf("请输入你想要删除的站点:\n");scanf_s("%s", str, 100);for (int i = 0; i < nNum; i++){if (strcmp(str, acc[i].website) == 0){for (int j=i;j<nNum-1;j++){strcpy_s(acc[j].website, 100, acc[j + 1].website);strcpy_s(acc[j].userName, 100, acc[j + 1].userName);strcpy_s(acc[j].passWord, 100, acc[j + 1].passWord);}printf("删除成功!\n");(*nCount)--;return;}}printf("查无此站点请重试!\n"); }void alterInfo(ACCOUNT acc[], int nCount) {char str[100] = {};printf("请输入你想要修改的站点:\n");scanf_s("%s", str, 100);for (int i = 0; i < nCount; i++){if (strcmp(str, acc[i].website) == 0){printf("请输入修改后的站点:\n");scanf_s("%s", acc[i].website, 100);printf("请输入修改后的用户名:\n");scanf_s("%s", acc[i].userName, 100);printf("请输入修改后的密码:\n");scanf_s("%s", acc[i].passWord, 100);printf("修改成功!\n");return;}}printf("查无此站点请重试!\n"); }void searchInfo(ACCOUNT acc[], int nCount) {char str[100] = {};printf("请输入你想要查询的站点:\n");scanf_s("%s", str, 100);for (int i = 0;i<nCount;i++){if (strcmp(str,acc[i].website) ==0){printf("查询结果为:\n");printf("站 点:%s \n", acc[i].website);printf("用户名:%s \n", acc[i].userName);printf("密 码:%s \n", acc[i].passWord);return;}}printf("暂无搜索结果\n");}void searchALLInfo(ACCOUNT acc[], int nCount) {printf("-------------查询结果----------------\n");if (0==nCount){printf("空\n");return;}for (int i = 0; i< nCount; i++){printf("站 点:%s \n",acc[i].website);printf("用户名:%s \n",acc[i].userName);printf("密 码:%s \n",acc[i].passWord);printf("--------------------------------\n");} }void saveInfo(ACCOUNT acc[], int nCount) {FILE* pFile=NULL;errno_t err = fopen_s(&pFile, "file.txt", "wb");if (err != 0){printf("保存本地失败!\n");return ;}g_nNum = nCount;fwrite(&nCount, sizeof(nCount), 1, pFile);fwrite(&g_nNum, sizeof(g_nNum), 1, pFile);int bytsize = sizeof(ACCOUNT) * g_nNum;char* buf = (char*)malloc(bytsize);memcpy_s(buf, bytsize, acc, bytsize);for (int i = 0; i < bytsize; i++){buf[i] += 2;}fwrite(buf, sizeof(ACCOUNT), g_nNum, pFile);fclose(pFile);free(buf);buf = NULL;printf("已成功保存到本地!\n"); } void readInfo(PACCOUNT *pAc, int *nCount) {FILE* pFile=NULL;errno_t err = fopen_s(&pFile, "file.txt", "rb");if (err != 0){*pAc = (PACCOUNT)malloc(sizeof(ACCOUNT) * NUM);g_nNum = NUM;return ;}fread(nCount, sizeof(nCount), 1, pFile);fread(&g_nNum, sizeof(g_nNum), 1, pFile);if (g_nNum==0){*pAc= (PACCOUNT)malloc(sizeof(ACCOUNT));fclose(pFile);return;}*pAc = (PACCOUNT)malloc(sizeof(ACCOUNT) * g_nNum);memset(*pAc, 0, sizeof(ACCOUNT) * g_nNum);size_t lent = fread(*pAc, sizeof(ACCOUNT), g_nNum, pFile);if (lent!=g_nNum){printf("读取错误!");fclose(pFile);return;}int bytsize = sizeof(ACCOUNT) * g_nNum;char* buf = (char*) *pAc;for (int i = 0; i < bytsize; i++){buf[i] -= 2;}fclose(pFile);}
主界面 menuMain.cpp
#include <stdio.h> #include <malloc.h> #include "data.h" #include "ctrl.h" #include <stdlib.h> int main() {int g_nNum = 0; //当前密码容量int nCount = 0; //当前密码个数PACCOUNT pAc = nullptr;readInfo(&pAc, &nCount);//pAc = (PACCOUNT)malloc(sizeof(ACCOUNT) * NUM);int nOver = 1;int oper;int oCho = 0;printf("欢迎使用尘风密码本\n");while (nOver){printf("======主菜单===========\n");printf("1 添加账户\n");printf("2 删除账户\n");printf("3 修改账户\n");printf("4 查询账户\n");printf("5 查询所有账户\n");printf("6 保存本地\n");printf("7 退出\n");printf("请输入你想要进行的操作序号:\n");scanf_s("%d",&oper);switch (oper){case 添加账户:{insertInfo(pAc, &nCount);oCho += 1;}break;case 删除账户:{deleteInfo(pAc, &nCount);oCho += 1;}break;case 修改账户:{alterInfo(pAc,nCount);oCho += 1;}break;case 查询账户:{searchInfo(pAc, nCount);}break;case 查询所有: {searchALLInfo(pAc, nCount);}break;case 保存本地: {saveInfo(pAc, nCount);oCho = 0;}break;case 退出:{if (oCho!=0){printf("更改是否保存到本地?(y or 其他)\n");char x = getchar();char s;scanf_s("%c", &s,1); if ('y'== s){saveInfo(pAc, nCount);}}printf("谢谢您的使用!");nOver = 0;system("pause");system("cls");}break;default:{printf("请输入正确命令");}break;}}free(pAc);pAc = nullptr;}
转载于:https://www.cnblogs.com/mountainCold/p/11191068.html
c++—简单的密码本实现相关推荐
- html网页设如何置访问密码,利用JS给单页html加上简单访问密码,需要密码才能访问!...
本文最后更新于[ 2019-12-21 22:28:00 ],某些文章具有时效性,若有错误或已失效,请在下方 留言 或联系 小P君 . 今天在搞一个简单的短域名生成网页的时候,以防被滥用,所以想给h ...
- 第二章:简单古典密码(及其五元组)
简单古典密码及其五元组 编制密码的基本原理和基本方法称为密码法.基本的密码法主要有移位(也称为置换),代替,和加减三种(在许多书上只是介绍置换和代替).在密码发展的初级阶段,他们都曾经独立地作为加密算 ...
- 【简单的密码生成器】
简单的密码生成器 用python写了一个简单的密码生成器.生成的密码包含数字.小写字母.大写字母.特殊字符.为了确保这四种类型的密码都存在,最后4次循环分别把四种类型的值插入到了密码的随机位置. 代码 ...
- 入门,前后端实现简单账号密码登录
前后端实现简单账号密码登录 涉及知识点 原理 效果图 前端代码 index.html res.html login.html main.js info.html error.html WZ.html ...
- C语音实现简单的密码登录(简单逻辑)
今天从一个题目中学到不少东西,打算分享出来和大家探讨一下. 现有题为:编写代码实现,模拟用户登录情景,并且只能登陆三次.(只允许输入三次密码,如果密码正确则提示登录成功,如果三次均输入错误,则退出程序 ...
- 简单移位密码——凯撒加密、解密算法
简单移位密码--凯撒加密.解密算法 移位密码是简单的替换密码(simple substitution cipher),即将明文的一个字符用相应的一个密文字符替换. 算法描述:设P=C=K=Z26,对k ...
- 使用爬山法实现简单替换密码的破译
使用爬山法实现简单替换密码的破译 本来我没有这个作业,看到有人在群里问这个作业,就顺手写出来了,感觉收获挺大的,就写成个博文和大家一起交流. 欢迎来我网站:www.xuanworld.top 一.题目 ...
- Python 暴力破解zip文件密码(相对简单的密码)
Python 暴力破解zip文件密码(相对简单的密码) 密码的使用范围很广,首先,我们要明确密码的作用,其次要明白密码的类型. 密码的作用是进行权限的限制,判断用户的合法性,(不 ...
- 密码学入门:几种简单的密码
密码学入门:几种简单的密码 一.维吉尼亚密码 维吉尼亚密码引入了"密钥"的概念,即根据密钥来决定用哪一行的密表来进行替换,以此来对抗字频统计.假如以上面第一行代表明文字 ...
- 搭建图片加密平台,扫码支付后简单获取密码
搭建图片加密平台,扫码支付后简单获取密码 很多人问我,互联网上到底做什么项目是可以赚到钱的?没有基础,不懂技术,不会推广,所以有没有简单一点的,一操作就能上手就能赚钱的?我可以很肯定的告诉你:没有. ...
最新文章
- BitCask 持久化hash存储引擎 原理介绍
- 面了一个 32 岁的程序员,一看就是“卷”出来的
- 近世代数--子环--怎么判断是不是子环?
- uboot中设置MAC地址,重启不丢失
- html视频位置控制器,html5中返回音视频的当前媒体控制器的属性controller
- android Content provider 组件
- send,recv,sendto,recvfrom
- urllib2的用法
- 去除maven父依赖_如何在Maven中从父项目中排除依赖项?
- HDU2019 数列有序!【入门】
- python-opencv中的cv.CreateImage函数的参数问题
- 大数据面试都问些什么?
- Outlook中的Notes的链接打不开,提示错误信息“File does not exit”
- 第四章 MyBatis-SQL映射文件
- eclipse查看代码git历史_Eclipse使用教程 : 利用Eclipse中git的插件来查看项目状态 - Break易站...
- 发那科机器人override指令_上海发那科(FANUC)机器人有限公司内部教材 -
- 使用 spire.xls 免费版 excel 转换成 pdf
- Android开发——项目实例(一)迷你背单词软件
- 10 大话设计模式C++实现之模板方法模式
- 铁流:中国突破半导体新工艺研发