#include <stdio.h> #include <stdio.h> #include <malloc.h> #define MaxSize 100 typedef struct { char data[MaxSize]; int length; } SqString; void StrAssign(SqString &s, char cstr[]) { int i; for (i = 0; cstr[i] != '\0'; i++) s.data[i] = cstr[i]; s.length = i; } void DispStr(SqString s) { if (s.length > 0) { for (int i = 0; i < s.length; i++) printf("%c", s.data[i]); printf("\n"); } } SqString A, B; SqString EnCrypt(SqString p) { int i = 0, j; SqString q; while (i < p.length) { for (j = 0; p.data[i] != A.data[j] && j <= A.length; j++) if (j >= A.length) q.data[i] = p.data[i]; else q.data[i] = B.data[j]; i++; } q.length = p.length; return q; } SqString UnEncrypt(SqString q) { int i = 0, j; SqString p; while (i < q.length) { for (j = 0; q.data[i] != B.data[j] && j <= B.length; j++); if (j >= B.length) p.data[i] = q.data[i]; else p.data[i] = A.data[j]; i++; } p.length = q.length; return p; } int main() { SqString q, p; int ok = 1; StrAssign(A, "abcdefghijklmnopqrstuvwxyz"); StrAssign(B, "ngzqtcobmuhelkpdawxfyivrsj"); char str[MaxSize]; printf("输入原文串:"); gets(str); StrAssign(p, str); printf("\n加密解密如下:\n"); printf("\n 原文串:"); DispStr(p); q = EnCrypt(p); printf("\n 加密串:"); DispStr(q); p = UnEncrypt(q); printf("\n 解密串:"); DispStr(p); printf("\n"); return 1; }改bug
时间: 2024-04-04 21:33:50 浏览: 47
在 EnCrypt 函数和 UnEncrypt 函数中,SqString 类型的变量 q 和 p 没有初始化,需要在函数内部进行初始化:
SqString EnCrypt(SqString p) {
int i = 0, j;
SqString q;
StrAssign(q, ""); // 初始化为空字符串
while (i < p.length) {
for (j = 0; p.data[i] != A.data[j] && j <= A.length; j++)
if (j >= A.length)
q.data[i] = p.data[i];
else
q.data[i] = B.data[j];
i++;
}
q.length = p.length;
return q;
}
SqString UnEncrypt(SqString q) {
int i = 0, j;
SqString p;
StrAssign(p, ""); // 初始化为空字符串
while (i < q.length) {
for (j = 0; q.data[i] != B.data[j] && j <= B.length; j++);
if (j >= B.length)
p.data[i] = q.data[i];
else
p.data[i] = A.data[j];
i++;
}
p.length = q.length;
return p;
}
阅读全文