Do untwist

 1 #include <iostream>

 2 #include<stdio.h>

 3 #include <string.h>

 4 #pragma warning(disable:4996)

 5 #define MAX 71

 6 using namespace std;

 7 

 8 void untwist(char *str, int k);

 9 int _getstr(char* str);

10 int main()

11 {

12     char str[MAX];

13     int judge = _getstr(str);

14     while (judge)

15     {

16         untwist(str, judge);

17         judge = _getstr(str);

18     }

19     return 0;

20 }

21 

22 

23 

24 //First convert the letters in plaintext to integer codes in plaincode according to the following rule:

25 // '_' = 0, 'a' = 1, 'b' = 2, ..., 'z' = 26, and '.' = 27.

26 //Next, convert each code in plaincode to an encrypted code in ciphercode according to the following formula:

27 // for all i from 0 to n - 1,

28 //ciphercode[i] = (plaincode[ki mod n] - i) mod 28.

29 void untwist(char *str, int k)

30 {

31     int len = strlen(str);

32     char *text = new char[len + 1];

33     int *ciphercode = new int[len];

34     int *plaincode = new int[len];

35     strcpy(text, str);

36     for (int i = 0; i<len; i++)

37     {

38         if (text[i] == '_') ciphercode[i] = 0;

39         else if (text[i] == '.')ciphercode[i] = 27;

40         else if (text[i] >= 'a'&&text[i] <= 'z')

41             ciphercode[i] = (text[i] - 'a') + 1;

42     }

43 

44     for (int i = 0; i<len; i++)

45     {

46         int index = (k*i) % len;

47         int temp = ciphercode[i] + i;

48         while (temp>=28) temp -= 28;

49         while (temp < 0) temp += 28;

50         plaincode[index] = temp;

51     }

52     int j = 0;

53     for (; j<len; j++)

54     {

55         if (plaincode[j] == 0) text[j] = '_';

56         else if (plaincode[j] == 27)text[j] = '.';

57         else if (plaincode[j] >= 1 && plaincode[j] <= 26)

58             text[j] = plaincode[j] + 'a' - 1;

59     }

60     text[j] = '\0';

61     cout << text << endl;

62     delete[]text; delete[]ciphercode; delete[]plaincode;

63 }

64 

65 

66 int _getstr(char* str)//lim 100

67 {

68     int k = 0;

69     int index_of_k = 0;

70     char ck[3];

71     int c = 1;

72     int i = 0;

73     bool space = false;

74     for (; i<100 && !((c = getchar()) == '0'&&i==0)&&c != '\n'; i++)

75     {

76         if (c == ' ') space = true;

77         if (space == false)

78         {

79             ck[index_of_k] = c;

80             index_of_k++;

81         }

82         else if (c != ' ')

83             str[i - index_of_k-1] = c;

84     }

85     if (i == 0)

86         return 0;

87     else

88     {

89         str[i - index_of_k - 1] = '\0';

90         if (index_of_k == 1)

91             k = ck[0] - '0';

92         else if (index_of_k == 2)

93             k = (ck[0]-'0')* 10 + ck[1]-'0';

94         else

95             k = (ck[0]-'0') * 100 + (ck[1]-'0') * 10 + (ck[2]-'0');

96         return k;

97     }

98 }

输入写得太丑了,其实直接用cin就可以了 int 跟 char 正好互相隔开的

你可能感兴趣的:(T)