我有这个简单的加密/解密程序,它工作得很好。但是我们的老师让我们计时,所以我用了计时器。我的问题是,当我实现计时器时,它不会结束程序。当我调试它时,它确实到达了最后一个大括号,但从不退出程序。怎么了?这是我的代码:
#include<stdio.h>
#include<time.h>
#include<conio.h>
#include<string.h>
#include<alloc.h>
void encrypt(char *crypt,char *plaintext,char *encryption,int size)
{
int i,j,k,flags;
for(i=0;i<size;i++)
{
j=1;flags=0;
while(j<53 && flags==0)
{
if(plaintext[i]==crypt[j])
{
encryption[i]=crypt[j-1];
flags=1;
}
k=j+2;
j=k;
}
}
}
void decrypt(char *crypt,char *ciphertext,char *decryption,int s)
{
int m,i,j,k,flag;
for(i=0;i<s;i++)
{
j=0;flag=0;
while(j<52 && flag==0)
{
if(ciphertext[i]==crypt[j])
{
decryption[i]=crypt[j+1];
flag=1;
}
k=j+2;
j=k;
}
}
}
main()
{
char c,*from, *to;
int size;
char crypt[53]="afbicddeelftgyhbirjakvlnmgncohpjqkrmsotpuqvswuxwyxzz";
clock_t begin, end;
double time_spent;
clrscr();
printf("press e to encrypt and d to decrypt: ");
scanf("%c",&c);
if(c=='e')
{
scanf("%s",from);
size=strlen(from);
begin=clock();
encrypt(crypt,from,to,size);
end=clock();
time_spent=(double)(end-begin)/CLK_TCK;
}
else
{
scanf("%s",from);
size=strlen(from);
begin=clock();
decrypt(crypt,from,to,size);
end=clock();
time_spent=(double)(end-begin)/CLK_TCK;
}
printf("%s",to);
printf ("%e",time_spent);
free(from); from=NULL;
free(to); to=NULL;
}
我试过删除计时器,它再次运行良好。但我真的需要计时器。请帮忙。谢谢你。