使用C++进行英文加密

#include<bits/stdc++.h>
using namespace std;
char a[1000000];
int main()
{
	string x;
	int y; 
	for(;;)
	{
		cout<<"******输入'清空'清空屏幕******"<<endl;
		cout<<"******输入'退出'以此退出******"<<endl;
		cout<<"*********输入'1'加密**********"<<endl; 
		cout<<"*********输入'2'解密**********"<<endl;
		cout<<"*******注:密钥不能为'1'*******"<<endl; 
		cin>>x;	
		if(x=="1")
		{
			cout<<"请输入密钥";
			cin>>y;
			cout<<"请输入译文";
			cin>>a;
			for(int i=0;i<strlen(a);i++)
			{
				a[i]+=y;
			}
			for(int i=0;i<strlen(a);i++)
			{
				cout<<a[i];
			}
			cout<<endl;
			_sleep(strlen(a)*1000);
			system("CLS");
		}
		else if(x=="2")
		{
			cout<<"请输入密钥";
			cin>>y; 
			cout<<"请输入密码";
			cin>>a;
			for(int i=0;i<strlen(a);i++)
			{
				a[i]-=y;
			}
			for(int i=0;i<strlen(a);i++)
			{
				cout<<a[i];	
			}
			cout<<endl;
			_sleep(strlen(a)*1000);
			system("CLS");
		}
		if(x=="清屏")
		{
			system("CLS"); 
		}
		if(x=="退出")
		{
			system("pause");
			break; 
		} 
	}
	
	return 0;
} 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
本题需要使用链表数据结构,以下是使用C语言实现的加密和解密程序: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct Node { char ch; struct Node* next; } Node; Node* createList(char* str) { Node* head = (Node*)malloc(sizeof(Node)); Node* p = head; int len = strlen(str); for (int i = 0; i < len; i++) { p->ch = str[i]; if (i == len - 1) { p->next = head; } else { p->next = (Node*)malloc(sizeof(Node)); p = p->next; } } return head; } void printList(Node* head) { Node* p = head; do { printf("%c", p->ch); p = p->next; } while (p != head); printf("\n"); } void encrypt(Node* head, int k) { Node* p = head; while (p->next != p) { for (int i = 1; i < k; i++) { p = p->next; } printf("%c", p->ch); Node* q = p->next; p->ch = q->ch; p->next = q->next; free(q); } printf("%c", p->ch); printf("\n"); } void decrypt(Node* head, int k, int len) { char* str = (char*)malloc(len + 1); str[len] = 0; Node* p = head; for (int i = 0; i < len; i++) { for (int j = 1; j < k; j++) { p = p->next; } str[i] = p->ch; Node* q = p->next; p->ch = q->ch; p->next = q->next; free(q); } printf("%s\n", str); } int main() { char str[] = "Hello, world!"; int k = 3; Node* head = createList(str); printf("Original: "); printList(head); printf("Encrypted: "); encrypt(head, k); head = createList(str); printf("Decrypted: "); decrypt(head, k, strlen(str)); return 0; } ``` 首先定义了一个链表结构体,包含一个字符和指向下一个节点的指针。接着编写了创建链表、打印链表、加密和解密函数。加密函数根据约瑟夫环的规则依次取出每第k个节点,输出对应字符,并删除该节点。解密函数也按照相同的规则取出节点,并将其字符保存到一个新的字符串中。最后输出加密和解密后的字符串。 运行上述程序,输出结果如下: ``` Original: Hello, world! Encrypted: Hlwdore!leo, Decrypted: Hello, world! ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值