/*利用map映射构造密码本,可以通过密码本加密和解密
* map是一个模板类,使用时需先定义类型
*/
#include <iostream>
#include <map>
#include <string.h>
using namespace std;
map<string, string> words;//定义两个string型参数,类对象为words
void Fun1()
{
char buf[1024] = {'\0',};
char buf1[512] = {'\0',};
char buf2[512] = {'\0',};
int i = 0;
int j = 0;
//打开密码本,将密码读取到words中
string s1 , s2;
int len;
FILE* fp = fopen("secret","r");
if (fp == NULL)
{
perror ("打开失败\n");
exit(0);
}
//将文件按行读取到buf
while (fgets(buf,128,fp) != NULL)
{
len = strlen(buf);
buf[len-1] = '\0';
//将buf \t 之前的内容读出来
for (j = 0;buf[i] !='\t';i++,j++)
{
buf1[j] = buf[i];
}
i++;
buf1[j] = '\0';
s1 = buf1;
//将buf \t 之前的内容读出来
for (j = 0;buf[i] !='\0';i++,j++)
{
buf2[j] = buf[i];
}
i = 0;
buf2[j] = '\0';
s2 = buf2;
//将字符串写入map中,并将buf置空
words[s1] = s2;
buf1[512] = {'\0',};
buf2[512] = {'\0',};
buf[1024] = {'\0',};
}
}
//查找明文函数
void Fun2 (string s)
{
auto it = words.find(s);
if (it != words.end()) {
cout << it->second << endl;
} else {
cout << "没找到" << endl;
}
}
//查找密文函数
void Fun3 (string s)
{
for (auto it=words.begin(); it!=words.end(); it++) {
if (s == it->second ){
cout << it->first << " : " << it->second << endl;
break;
}
if (it == words.end())
{
cout << "没找到" << endl;
}
}
}
//查看密码本函数
void Show ()
{
for (auto it=words.begin(); it!=words.end(); it++) {
cout << it->first << " : " << it->second << endl;
}
}
int main()
{
string t1;
string t2;
Fun1();
// Show();
while(1)
{
cout << "请输入接收到的报文" << endl;
cin >> t1;
Fun2(t1);
cout << "请输入要发送到的报文" << endl;
cin >> t2;
Fun3(t2);
}
}
/*将下面内容保存 为密码本,命名为 secret 不要更改格式
nh 你好 吃了么
lv 我爱你
lc 晚上一起去吃饭
bd 帮忙答个到
gd 这次考试考的很好
wl 太无聊了
*/
* map是一个模板类,使用时需先定义类型
*/
#include <iostream>
#include <map>
#include <string.h>
using namespace std;
map<string, string> words;//定义两个string型参数,类对象为words
void Fun1()
{
char buf[1024] = {'\0',};
char buf1[512] = {'\0',};
char buf2[512] = {'\0',};
int i = 0;
int j = 0;
//打开密码本,将密码读取到words中
string s1 , s2;
int len;
FILE* fp = fopen("secret","r");
if (fp == NULL)
{
perror ("打开失败\n");
exit(0);
}
//将文件按行读取到buf
while (fgets(buf,128,fp) != NULL)
{
len = strlen(buf);
buf[len-1] = '\0';
//将buf \t 之前的内容读出来
for (j = 0;buf[i] !='\t';i++,j++)
{
buf1[j] = buf[i];
}
i++;
buf1[j] = '\0';
s1 = buf1;
//将buf \t 之前的内容读出来
for (j = 0;buf[i] !='\0';i++,j++)
{
buf2[j] = buf[i];
}
i = 0;
buf2[j] = '\0';
s2 = buf2;
//将字符串写入map中,并将buf置空
words[s1] = s2;
buf1[512] = {'\0',};
buf2[512] = {'\0',};
buf[1024] = {'\0',};
}
}
//查找明文函数
void Fun2 (string s)
{
auto it = words.find(s);
if (it != words.end()) {
cout << it->second << endl;
} else {
cout << "没找到" << endl;
}
}
//查找密文函数
void Fun3 (string s)
{
for (auto it=words.begin(); it!=words.end(); it++) {
if (s == it->second ){
cout << it->first << " : " << it->second << endl;
break;
}
if (it == words.end())
{
cout << "没找到" << endl;
}
}
}
//查看密码本函数
void Show ()
{
for (auto it=words.begin(); it!=words.end(); it++) {
cout << it->first << " : " << it->second << endl;
}
}
int main()
{
string t1;
string t2;
Fun1();
// Show();
while(1)
{
cout << "请输入接收到的报文" << endl;
cin >> t1;
Fun2(t1);
cout << "请输入要发送到的报文" << endl;
cin >> t2;
Fun3(t2);
}
}
/*将下面内容保存 为密码本,命名为 secret 不要更改格式
nh 你好 吃了么
lv 我爱你
lc 晚上一起去吃饭
bd 帮忙答个到
gd 这次考试考的很好
wl 太无聊了
*/