Freepascal question!!Help!20pt

2011-12-30 10:18 pm
Please help me to solve the problem!! and make it into a free pascal program!!!!!!!!!!!!!
Encryption /Decryption program
Keyword encryption methode.g. keyword is "computer"
then we arrrange the alphabetic order as follows
computerabdfghijklnqsvwxyz
abcdefghijklmnopqrstuvwxyz
If the plaintect is "Iloveyousomuch", then the ciphertext will be "oqbvgybeubcean"

Help me to write a program to simulate the above progress in both ways, encryption and decryption, as follows:
Enter keyword: Computer
Menu:
1. Encryption
2. Decryption
3.Exit
>1
Enter plaintext: Iloveyousomuch
Ciphertext=oqbvgybeubcean
Do you want to continue(y/n)? y
Menu:
1. Encryption
2. Decryption
3.Exit
>2
Enter ciphertext:oqbvgybeubcean
Plaintext= Iloveyousomuch
Do you want to continue(y/n)?y
Menu:
1. Encryption
2. Decryption
3.Exit
>3

Finally the program can end..... Anyway, anyone can help me plz~? >_<v
Using free pascal, also, the words can be uppercase or lowercase.....
e.g.
like this 'A' or 'a'.
I just cannot find a way to make this complicated program.

Last but not the least, can anyone explain to me clearly, what is for loop, while loop and repeat loop? If so, how can we use it in some complicated way like what we see above?

回答 (2)

2011-12-31 6:24 am
✔ 最佳答案
我先講講loop比你聽,program 方面急唔急,later有時間先試下 ~
for loop 就係將一D會重複做幾次既野 group埋一齊做,例如
for i:= 1 to 10 do
write('*');
就係write10次*
有時我地會用到咁樣:
for i:= 1 to 5 do begin
for j:= 1 to 5 do write('*')
writeln;
end;
咁樣就係
先write 5 次*,再隔一行 <<<呢2個步驟做5次

2011-12-30 19:39:12 補充:
while loop 就係有一個條件,如果符合就做下面既野,如果唔符合就唔做
例如
while x<>1 do write('*')
就係當x 不等於1 ,就會write *
要做複雜小小就要加上begin end;
while x<> 1 do begin
write('*');
writeln;
write(x);
end;

2011-12-30 19:41:49 補充:
repeat loop 就係包住一group野,做左一次之後,再判斷再做唔做
例如
x:=0;
repeat
write('*');
x:=x+1;
until(x>10)
上面就會重複做write('*') 同x:=x+1 而當x>10(姐係11)就唔會再做
(注意,repeat loop 一定會做一次,而 while loop有機會一次都唔做)

2011-12-30 19:43:41 補充:
另外想問一問,您學了 procedure未?

2011-12-30 22:24:19 補充:
以下的program 只供參考,因為太多字(仲要分2次 = ="),唔係意見到發放~
自己睇下明唔明,唔明就問LA~

program password;
var keyword,abc,temp,q:string;
option,a,e:integer;
continue:char;
function password:string;
var j:integer;
i:char;
word:string;
flag:boolean;
begin
password:=keyword;
for i:= 'a' to 'z' do begin
flag:=true;
for j:= 1 to length(keyword) do begin
word:=copy(keyword,j,1);
if i=word then flag:=false;
end;
if flag then password:=password+i;
end;
end;
procedure encryption;
var plaintext,ciphertext,word,temp:string;
e,x,p:integer;
begin
ciphertext:='';
write('Enter plaintext:');
readln(plaintext);
temp:=plaintext;
plaintext:='';
for e:= 1 to length(temp) do begin
q:=copy(temp,e,1);
if (ord(temp[e]) < 97)
then plaintext:=plaintext+chr(ord(temp[e])+32)
else plaintext:=plaintext+q;
end;
for x:= 1 to length(plaintext) do begin
word:=copy(plaintext,x,1);
p:=pos(word,password);
ciphertext:=ciphertext+copy(abc,p,1);
end;
write('Ciphertext=',ciphertext);
writeln;
end;
procedure decryption;
var plaintext,ciphertext,word,temp:string;
b,c,e:integer;
begin
plaintext:='';
write('Enter ciphertext:');
readln(ciphertext);
temp:=ciphertext;
ciphertext:='';
for e:= 1 to length(temp) do begin
q:=copy(temp,e,1);
if (ord(temp[e]) < 97)
then ciphertext:=ciphertext+chr(ord(temp[e])+32)
else ciphertext:=ciphertext+q;
end;
for b:= 1 to length(ciphertext) do begin
word:=copy(ciphertext,b,1);
c:=pos(word,abc);
plaintext:=plaintext+copy(password,c,1);
end;
write('Plaintext=',plaintext);
writeln;
end;
begin
a:=0;
abc:='abcdefghijklmnopqrstuvwxyz';
write('Enter keyword:');
readln(keyword);
temp:=keyword;
keyword:='';
for e:= 1 to length(temp) do begin
q:=copy(temp,e,1);
if (ord(temp[e]) < 97)
then keyword:=keyword+chr(ord(temp[e])+32)
else keyword:=keyword+q;
end;
writeln(keyword);
repeat
a:=0;
writeln('Menu:');
writeln('1.Encryption');
writeln('2.Decryption');
writeln('3.Exit');
write('>');


2011-12-30 22:25:39 補充:
if option=1
then begin
encryption;
write('Do you want to continue(y/n)?');
readln(continue);
if continue='y' then a:=1;
end

2011-12-30 22:25:46 補充:
else if option=2
then begin
decryption;
write('Do you want to continue(y/n)?');
readln(continue);
if continue='y' then a:=1;
end
else exit;
until a<>1;
end.

2011-12-30 22:28:56 補充:
我想補充一點 . 都唔得,話太多字 = ="
,一定要係意見度發佈, sorry~

2011-12-31 19:22:21 補充:
I think the program has no problem since I can run it at home.I will send the file to you later.Anyway, if I know how to slove your problem,i will answer and you may send me a email to ask me~

2011-12-31 20:17:03 補充:
依家放緊假,唔緊要,later 要考試就唔得,考完試再問吧~
加油!
參考: me
2011-12-31 8:34 pm
The program cannot run ar~ it only shows..............Enter the keyword: ( and I enter a word) then the program exit...........

2011-12-31 20:08:26 補充:
OK, thank you very much, hope this won't cause much trouble to you...


收錄日期: 2021-04-13 18:26:24
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20111230000051KK00466

檢視 Wayback Machine 備份