#include <stdio.h> #define MaxSize 100 typedf struct { char data[MaxSize]; int length; } SqString; void StrAssign(SqString &s, char cstr[]) { int i; for (i = 0; sctr[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.data = p.data; retrun q; } SqString UnEncrypt(SqSring q) { int i = 0, j; SqString q; while (i < p.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 = A.data[j]; ' i++; } p.length = q.length; return p; } int main() { SqString p, q; int ok = 1; StrAssign(A, "abcdefghigklmnopqrstuvwxyz"); StrAssign(B, "ngzqtcobmuhelkpdawxfyivrsj"); char str[MaxSize]; printf("\n"); printf("输入原文串:"); gets(str); StrAssign(p, str); printf("\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-03-13 09:48:25 浏览: 220
在代码中有两个错误:
1. 在函数EnCrypt中,SqString类型的变量q被定义了两次,应该改为SqString类型的变量res。
2. 在函数UnEncrypt中,赋值语句中的单引号是多余的,应该将其删除。
修正后的代码如下:
```c
#include <stdio.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 res;
while (i < p.length) {
for (j = 0; p.data[i] != A.data[j] && j < A.length; j++);
if (j >= A.length) {
res.data[i] = p.data[i];
}
else {
res.data[i] = B.data[j];
}
i++;
}
res.length = p.length;
return res;
}
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 p, q;
int ok = 1;
StrAssign(A, "abcdefghigklmnopqrstuvwxyz");
StrAssign(B, "ngzqtcobmuhelkpdawxfyivrsj");
char str[MaxSize];
printf("\n");
printf("输入原文串:");
gets(str);
StrAssign(p, str);
printf("\n加密解密如下:");
printf("\n原文串:");
DispStr(p);
q = EnCrypt(p);
printf("\n加密串:");
DispStr(q);
p = UnEncrypt(q);
printf("\n解密串:");
DispStr(p);
printf("\n");
return 1;
}
```
阅读全文