请帮忙........... [英] Please help...........
问题描述
我有一个小型C课程,用于大学课程。它旨在加密
并解密小写字母并将空格留作空格。我不能
让它运行正常,因为我认为我在While
构造区域有问题。这让我很生气,试图解决它 - 可以
有人请帮助。
程序是:
/ *
*使用Turbo C随机加密消息的程序
*数字生成器。只有小写字母和空格
*字符可以在消息中使用。
* /
#include< stdio.h>
#include< stdlib.h>
#include< conio.h>
#include< ctype。 h>
#define种子1
unsigned char encrypt(unsigned char);
unsigned char decrypt( unsigned char);
void wait(void);
main()
{
unsigned char c;
字符类型;
/ *
*初始化随机数生成器
* /
srand(SEED);
/ *
*显示用户标题和提示
* /
clrscr();
cprintf(T223加密程序\\\\\\\ n);
cprintf("键入d来解码消息或任何其他要编码的字符
");
type = getch(); / *输入用户选项* /
cprintf(消息中的\\\ nnnn类型,仅使用小写字母
或空格.\\ n与任何其他角色结合。\\\\\\ n是否支持;); / b $ bc =(unsigned char)getchar(); / *读入第一个字符* /
/ *
*主加密/解密循环
*循环,而字符较低大小写字母或空格
* /
while((c> =''a'')&&(c< =''z'')& &(c =''''));
{
if(c =='''')
{
开关(类型)
{
case''64'':
c = decrypt(c);
休息;
默认:
c =加密(c);
休息;
}
}
putchar(''c''); / *显示加密/解密字符* /
c =(unsigned char)getchar(); / *获取下一个字符* /
}
/ *
*主循环结束
* /
等待();
}
/ *
* main()函数结束
* /
/ *
*功能加密()
* ======== ==========
*加密单个字符
* /
unsigned char encrypt(unsigned char letter)
{
unsigned char r;
r =(unsigned char)random(26);
letter = letter + r;
if(letter>''z'')
{
letter = letter - 26;
}
返回(信件);
}
/ *
*加密结束()
* /
/ *
*功能解密()
* ========== ========
*解密单个字符
* /
unsigned char decrypt(unsigned char letter)
{
unsigned char r;
r =(unsigned char)random(26);
letter = lett呃 - r;
if(字母<'''')
{
letter = letter + 26;
}
返回(信件);
}
/ *
*解密结束()
* /
/ *
*功能等待()
* ======== =======
*等待按键
* /
void wait(void)
{
fflush(stdin);
cprintf(" \\ nnPress返回终止");
getchar();
}
/ *
*等待结束()
* /
/ *
*程序结束
* /
史蒂夫克莱写道:
我有一个小型的C程序,用于大学课程。它旨在加密
并解密小写字母并将空格留空。我不能让它正常运行,因为我认为我在While
构造区域有问题。这让我很生气,试图解决它 - 可以请某人帮忙。
程序是:
/ *
*加密程序使用Turbo C随机数字生成器的消息。消息中只能使用小写字母和空格
*字符。
* /
#include< stdio.h>
#include< stdlib.h>
#include< conio.h>
#include< ctype.h>
非标准标题等
#define SEED 1
unsigned char encrypt(unsigned char);
unsigned char decrypt(unsigned char);
void wait(void);
main()
{
unsigned char c;
char类型;
/ *
*初始化随机数发生器
* /
srand(SEED);
/ *
*显示用户标题和提示
* /
clrscr();
cprintf(T223加密程序\\\\\\\ n);
cprintf("键入d来解码消息或任何其他要编码的字符
");
type = getch(); / *输入用户选项* /
cprintf(消息中的\\\ nnNow类型,仅使用小写字母
或space.\\\\
End与任何其他角色。\\\\ n \ n \\ n \\ n; \\ n \\ n; \\ n \\ n; \\ n>
非标准函数等。
c =(unsigned char)的getchar(); / *读入第一个字符* /
如果getchar()返回EOF怎么办?
/ *
*主加密/解密循环
*循环,而字符是小写字母或空格
* /
while((c> =''a'')&&(c< =''z'')& &(c =''''));
c =''''为c分配空格,总是评估为非零(true)和
将c留作空格。
上述声明末尾的假分号。
{
if(c =='''')
{
切换(类型)
{
案例''64'':
''64''是单个字符文字中的两个字符;这是'b $ b实现定义了这意味着什么。为什么不用''d'?
c =解密(c);
休息;
默认:
c =加密(c) ;
休息;
}
}
putchar(''c''); / *显示加密/解密字符* /
你的意思是putchar(c);
c =(unsigned char)getchar(); / *获取下一个字符* /
}
/ *
*主循环结束
* /
wait();
}
/ *
* main()函数结束
* /
/ *
*函数encrypt()
* == ================
*加密单个字符
* /
unsigned char encrypt(unsigned char letter)
{
unsigned char r;
r =(unsigned char)random(26);
letter = letter + r;
if(letter>''z'')
{
letter = letter - 26;
}
返回(来信);
}
/ *
*加密结束()
* /
/ *
*功能解密()
* ==================
*解密一个单个字符
* /
unsigned char decrypt(unsigned char letter)
{
unsigned char r;
r =(unsigned char)random(26);
你的意思是兰德()。
letter = letter - r;
if(letter<'''')
{
letter = letter + 26;
}
返回(来信);
}
/ *
*解密结束()
* /
/ *
*功能等待()
* ===============
*等待按键
* /
无效等待(无效)
{
fflush(stdin);
此处未定义的行为。
cprintf(" \\\\ nnPress返回终止");
getchar();
}
/ *
*等待结束()
* /
/ *
*程序结束
* /
-
拉出一个分裂来回复。
你好史蒂夫,
彼得已经在你的程序中指出了一些问题
(我发现的更多)。我想要添加s.th.
条件:
$ blockquote class =post_quotes> while((c> =''a'')&&(c< =''z'' )&&(c =''''));
是不是
而((c> =''a'')&&(c< =''z'' )||(c ==''''))
a字符''或''空格?
问候
Michael
Michael写道:
史蒂夫,
彼得已经指出在你的程序中有一些问题(我发现的更多)。我只想添加s.th.根据你的条件:
while((c> =''a'')&&(c< =''z'')&& ;(c =''''));
是不是
> while((c> =''a'')&&(c< =''z'')||(c ==''''))
一个字符''或''空间?
嗯,这肯定是一个改进,因为没有字符可以是一封信
/和/ a空间。但是这个:
while(islower((unsigned char)c)||(c ==''''))
是更好的是,因为它不依赖于小写字母
连续且按字母顺序排列。
-
Richard Heathfield: bi****@eton.powernet.co.uk
Usenet是一个奇怪的地方。 - Dennis M Ritchie,1999年7月29日。
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K& R答案,C书等:< a rel =nofollowhref =http://users.powernet.co.uk/etontarget =_ blank> http://users.powernet.co.uk/eton
I have a small C program for a college course. It is meant to encrypt
and decrypt lower case letters and leave spaces as spaces. I can''t
get it to run properly as I think I have a problem in the While
construct area. It is driving me mad trying to sort it out - can
someone please help.
The program is:
/*
* Program to encrypt a message using the Turbo C random
* number generator. Only lower case letters and the space
* character can be used in the message.
*/
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <ctype.h>
#define SEED 1
unsigned char encrypt(unsigned char);
unsigned char decrypt(unsigned char);
void wait(void);
main()
{
unsigned char c;
char type;
/*
* initialise the random number generator
*/
srand(SEED);
/*
* display user headings and prompts
*/
clrscr();
cprintf("T223 encryption program\r\n\r\n");
cprintf("Type d to decode a message or any other character to encode
");
type = getch(); /* input the user option */
cprintf("\r\nNow type in the message, use only lower case letters "
"or space.\r\nEnd with any other character.\r\n\r\n");
c = (unsigned char)getchar(); /* read in first character */
/*
* main encryption/decryption loop
* loop while characters are lower case letters or spaces
*/
while ((c>=''a'') && (c<=''z'') && (c='' ''));
{
if (c=='' '')
{
switch (type)
{
case ''64'':
c = decrypt (c) ;
break;
default:
c = encrypt (c) ;
break;
}
}
putchar(''c''); /* display encrypted/decrypted character */
c = (unsigned char)getchar(); /* get next character */
}
/*
* end of main while loop
*/
wait();
}
/*
* end of main() function
*/
/*
* function encrypt()
* ==================
* encrypts a single character
*/
unsigned char encrypt(unsigned char letter)
{
unsigned char r;
r = (unsigned char)random(26);
letter = letter + r;
if (letter>''z'')
{
letter = letter - 26;
}
return(letter);
}
/*
* end of encrypt()
*/
/*
* function decrypt()
* ==================
* decrypts a single character
*/
unsigned char decrypt(unsigned char letter)
{
unsigned char r;
r = (unsigned char)random(26);
letter = letter - r;
if (letter<''a'')
{
letter = letter + 26;
}
return(letter);
}
/*
* end of decrypt()
*/
/*
* function wait()
* ===============
* waits for a keypress
*/
void wait(void)
{
fflush(stdin);
cprintf("\r\nPress return to terminate");
getchar();
}
/*
* end of wait()
*/
/*
* end of program
*/
Steve Clay wrote:
I have a small C program for a college course. It is meant to encrypt
and decrypt lower case letters and leave spaces as spaces. I can''t
get it to run properly as I think I have a problem in the While
construct area. It is driving me mad trying to sort it out - can
someone please help.
The program is:
/*
* Program to encrypt a message using the Turbo C random
* number generator. Only lower case letters and the space
* character can be used in the message.
*/
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <ctype.h>
Nonstandard headers, etc.
#define SEED 1
unsigned char encrypt(unsigned char);
unsigned char decrypt(unsigned char);
void wait(void);
main()
{
unsigned char c;
char type;
/*
* initialise the random number generator
*/
srand(SEED);
/*
* display user headings and prompts
*/
clrscr();
cprintf("T223 encryption program\r\n\r\n");
cprintf("Type d to decode a message or any other character to encode
");
type = getch(); /* input the user option */
cprintf("\r\nNow type in the message, use only lower case letters "
"or space.\r\nEnd with any other character.\r\n\r\n");
Nonstandard functions, etc.
c = (unsigned char)getchar(); /* read in first character */
What if getchar() returns EOF?
/*
* main encryption/decryption loop
* loop while characters are lower case letters or spaces
*/
while ((c>=''a'') && (c<=''z'') && (c='' ''));
c='' '' assigns space to c, which always evaluates to nonzero (true) and
leaves c as a space.
Spurious semicolon at the end of the above statement.
{
if (c=='' '')
{
switch (type)
{
case ''64'':
''64'' is two characters in a single character literal; it''s
implementation defined what this means. Why not just use ''d''?
c = decrypt (c) ;
break;
default:
c = encrypt (c) ;
break;
}
}
putchar(''c''); /* display encrypted/decrypted character */
You mean putchar(c);
c = (unsigned char)getchar(); /* get next character */
}
/*
* end of main while loop
*/
wait();
}
/*
* end of main() function
*/
/*
* function encrypt()
* ==================
* encrypts a single character
*/
unsigned char encrypt(unsigned char letter)
{
unsigned char r;
r = (unsigned char)random(26);
letter = letter + r;
if (letter>''z'')
{
letter = letter - 26;
}
return(letter);
}
/*
* end of encrypt()
*/
/*
* function decrypt()
* ==================
* decrypts a single character
*/
unsigned char decrypt(unsigned char letter)
{
unsigned char r;
r = (unsigned char)random(26);
You mean rand().
letter = letter - r;
if (letter<''a'')
{
letter = letter + 26;
}
return(letter);
}
/*
* end of decrypt()
*/
/*
* function wait()
* ===============
* waits for a keypress
*/
void wait(void)
{
fflush(stdin);
Undefined behavior here.
cprintf("\r\nPress return to terminate");
getchar();
}
/*
* end of wait()
*/
/*
* end of program
*/
--
Pull out a splinter to reply.
Hi Steve,
Peter already pointed out some problems
in your program (more I ever found). I
just want to add s.th. according your
while condition:
while ((c>=''a'') && (c<=''z'') && (c='' ''));
Isn''t it
while ((c>=''a'') && (c<=''z'') || (c=='' ''))
a character ''or'' space?
Regards
Michael
Michael wrote:
Hi Steve,
Peter already pointed out some problems
in your program (more I ever found). I
just want to add s.th. according your
while condition:while ((c>=''a'') && (c<=''z'') && (c='' ''));
Isn''t it> while ((c>=''a'') && (c<=''z'') || (c=='' ''))
a character ''or'' space?
Well, that''s certainly an improvement, since no character can be a letter
/and/ a space. But this:
while(islower((unsigned char)c) || (c == '' ''))
is better still, since it doesn''t rely on lower case letters being
contiguous and in alphabetical order.
--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
这篇关于请帮忙...........的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!