#include <iostream> using namespace std; #define STR_LEN 80 #define MAX_LINE 20 //读入一行,长度不超过maxLength void readLine(char str[], int maxLength) { int i = 0; while (i < maxLength) { char ch = cin.get(); if (ch != '\n') { str[i] = ch; } else { break; } i++; } str[i] = '\0'; } //其它函数定义 /********* Begin *********/ void jiemi(char kaisa[],int key) { for(int i=0; kaisa[i]!='\0';i++) { if( (kaisa[i]>='A'+key) && (kaisa[i]<='Z') ) { kaisa[i]=kaisa[i]-key; } else { if( (kaisa[i]>='A') && (kaisa[i]<='A' + key) ) { kaisa[i]=kaisa[i]-key+26; }}}} /********* End *********/ int main() { char cipherText[MAX_LINE][STR_LEN + 1]; //存储密文 /********* Begin *********/ /********* End *********/ return 0; }
时间: 2023-07-01 10:21:02 浏览: 134
在main函数中,需要先读入密文,可以使用readLine函数:
```
for (int i = 0; i < MAX_LINE; i++) {
readLine(cipherText[i], STR_LEN);
}
```
然后对于每一行密文,都需要进行解密操作,可以使用jiemi函数:
```
for (int i = 0; i < MAX_LINE; i++) {
jiemi(cipherText[i], i % 26);
}
```
注意,这里使用了i%26作为密钥,因为密钥的取值范围是0~25,所以需要对i取模。
完整代码如下:
```
#include <iostream>
using namespace std;
#define STR_LEN 80
#define MAX_LINE 20
void readLine(char str[], int maxLength) {
int i = 0;
while (i < maxLength) {
char ch = cin.get();
if (ch != '\n') {
str[i] = ch;
} else {
break;
}
i++;
}
str[i] = '\0';
}
void jiemi(char kaisa[], int key) {
for (int i = 0; kaisa[i] != '\0'; i++) {
if ((kaisa[i] >= 'A' + key) && (kaisa[i] <= 'Z')) {
kaisa[i] = kaisa[i] - key;
} else {
if ((kaisa[i] >= 'A') && (kaisa[i] <= 'A' + key)) {
kaisa[i] = kaisa[i] - key + 26;
}
}
}
}
int main() {
char cipherText[MAX_LINE][STR_LEN + 1];
for (int i = 0; i < MAX_LINE; i++) {
readLine(cipherText[i], STR_LEN);
}
for (int i = 0; i < MAX_LINE; i++) {
jiemi(cipherText[i], i % 26);
cout << cipherText[i] << endl;
}
return 0;
}
```
阅读全文