凯撒密码用C 编写(凯撒密码程序流程图)

2023-03-03 13:18:30 密码用途 思思

#include stdio.h

#include stdlib.h

#include string.h

const int MAX_N=200;

int main(int argc, char *argv[])

{

int i,j,p;

char text[MAX_N];

char alphabet[30];

char op[10];

while(1)

{

printf("1---输入密码表 2---退出\n");

gets(op);

if(strcmp(op,"1")==0)

{

printf("密码表:");

gets(alphabet);

while(1)

{

printf("1---加密 2---解密 3---返回\n");

gets(op);

if(strcmp(op,"1")==0 ||strcmp(op,"2")==0 )

{

printf("输入文本:");

gets(text);

for(i=0;text[i]!='\0';i++)

{

if((text[i]='a'text[i]='z') || (text[i]='A'text[i]='Z') )

{

if(strcmp(op,"1")==0)

{

p=text[i]='a'? (text[i]-'a'):(text[i]-'A');

text[i]=text[i]+ alphabet[p]-(p+'A');

}

else

{

for(j=0;;j++)

if(alphabet[j]==text[i]||alphabet[j]==(text[i]-('a'-'A')))

break;

text[i]= text[i]='a' ? (j+'a') :(j+'A');

}

}

}//for(i)

if(strcmp(op,"1")==0)

printf("加密后的文本为:" );

else

printf("解密后的文本为:");

printf("%s\n\n",text);

}

else if(strcmp(op,"3")==0)

{

printf("\n");

break;

}

else

{

printf("选择有误!请重新选择!\n");

}

}//while(1)

}

else if(strcmp(op,"2")==0)

{

exit(1);

}

else

{

printf("选择有误!请重新选择!\n");

}

}

return 0;

}

/*

输入样例

QWERTYUIOPASDFGHJKLZXCVBNM

Welcome to ZZSY2009!

输出样例

Vtsegdt zg MMLN2009!

*/

请问一下这道C语言编程题怎么做?

按照题目要求编写的用凯撒密码加密的C语言程序如下

#includestdio.h

int main(){

char s[80];

int offset,i;

fgets(s,80,stdin);

scanf("%d",offset);

for(i=0;s[i]!='\0';i++){

if('A'=s[i] s[i]='Z')

if(offset=0)

  s[i]='A'+(s[i]-'A'+offset)%26;

else

  s[i]='A'+(s[i]-'A'+26+offset%26)%26;

else if('a'=s[i] s[i]='z')

if(offset=0)

  s[i]='a'+(s[i]-'a'+offset)%26;

else

  s[i]='a'+(s[i]-'a'+26+offset%26)%26;

}

printf("%s\n", s);

return 0;

}

凯撒密码用VB如何编写程序

这个很简单嘛,用ASC 和 CHR 转换一下嘛

打开Vb,添加2个label控件,2个text控件 1个command按钮

代码如下:

Private Function f(ByVal a As String, k As Integer, n As Integer) As String

If ((Asc(a) = 65 And Asc(a) = 97) Or (Asc(a) = 97 And Asc(a) = 122)) And Len(a) = 1 Then '判断是否为一个字母

'利用公式计算

If Asc(a) = 65 And Asc(a) = 97 Then f = Chr((Asc(a) - 64 + k) Mod n + 64) '当为大写的时候

If Asc(a) = 97 And Asc(a) = 122 Then f = Chr((Asc(a) - 96 + k) Mod n + 96) '当为小写的时候

Else

f = "error" '若不满足要求,则返回错误

End If

End Function

Private Sub Command1_Click()

Dim strold As String

Dim strnew As String

Dim k As Integer

Dim n As Integer

Dim i As Long

Dim tmp As String

k = 3

n = 26

strold = Text1.Text '要加密的字符串

For i = 1 To Len(strold)

tmp = Mid(strold, i, 1)

tmp = f(tmp, k, n)

If tmp "error" Then

strnew = strnew + tmp

Else

MsgBox "字符串中含有非法字符"

Exit Sub

End If

Next i

Text2.Text = strnew

End Sub

Private Sub Form_Load()

Text1.Text = "PROGRAM"

Text2.Text = ""

Command1.Caption = "加密"

Label1.Caption = "源字符串:"

Label2.Caption = "加密字符串:"

End Sub