我正在关注 iTunes 大学:哈佛 CS50 讲座,以学习编程基础知识。我目前正在尝试为文本密码创建一个程序。这是整个程序的代码。
#include<stdio.h>
#include<cs50.h>
#include<stdlib.h>
#include<string.h>
int cyphmain (void);
int decyphmain (void);
int maincalc (int k);
int dmaincalc (int k);
int uppercalc (int u, int k);
int lowercalc (int u, int k);
int duppercalc (int u, int k);
int dlowercalc (int u, int k);
int
main(void) //Main Function begins by asking the user if he would like to Cypher or Decypher his text
{
char h;
printf("Do You Want Cypher(c) or Decypher(d)?:");
scanf("%c",&h);
if (h==99) //Checks ascii of "c"(99) and runs the cypher main function
cyphmain();
else if (h==100) //Checks ascii of "d"(100) and runs the decypher main function
decyphmain();
else
return 0;
}
int cyphmain (void) //Cypher Main Function
{
int k;
printf("What is the Cypher Number:"); //Gets the rot number
scanf("%d",&k);
if (k>25) //Ensures that the User only choses a number from 1-25
{ while (k>25) //Continues to ask user for a number until they input a value <=25
{
printf("Sorry Please Choose A Number from 1-25 only:");
int l=GetInt();
k=l;
}
maincalc(k); //as soon as a valid input is received maincalc function is run
}
else //in this case a valid input has been recieved and maincalc runs
maincalc(k);
return 0;
}
int decyphmain (void) //Decypher Main Function
{
int k;
printf("What is the Cypher Number:"); //Gets the rot number
scanf("%d",&k);
if (k>25) //Ensures that the User only choses a number from 1-25
{ while (k>25) //Continues to ask user for a number until they input a value <=25
{
printf("Sorry Please Choose A Number from 1-25 only:");
int l=GetInt();
k=l;
}
dmaincalc(k); //as soon as a valid input is received maincalc funtion is run
}
else //in this case a valid input has been recieved and maincalc runs
dmaincalc(k);
return 0;
}
int maincalc(int k) //The Calculation Function for Cyphering
{
char *s;
printf("Please enter the phrase that you would like to code:");
scanf("%s",&s);
int i=0;
int n=strlen(s);
while(i<n)
{
if(s[i]>=65&&s[i]<=90) //For Uppercase letters
{
int u=s[i];
int c=uppercalc(u,k); //Hands off the character to a function to cypher Upper Case Letters
printf("%c",c);
}
else if(s[i]>=97&&s[i]<=122) //For Lowercase letters
{
int u=s[i];
int c=lowercalc(u,k); //Hands off the character to a function to cypher Lower Case Letters
printf("%c",c);
}
else
printf("%c",s[i]); //For non letters
i++;
}
printf("\n");
return 0;
}
int uppercalc(int u, int k) //Algorithm used to cypher Upper Case letters
{
if(u+k<=90)
{
int c=u+k;
return (c);
}
else
{
if (((u+k)/26)==3)
{
int c=((u+k)%26)+52;
return (c);
}
else
{
int c=((u+k)%26)+78;
return (c);
}
}
}
int lowercalc(int u, int k) //Algorithms used to Cypher lower case letters
{
if(u+k<=122)
{
int c=u+k;
return (c);
}
else
{
if (((u+k)/26)==4)
{
int c=((u+k)%26)+78;
return (c);
}
else
{
int c=((u+k)%26)+104;
return (c);
}
}
}
int dmaincalc(int k) //The Calculation Function for Decyphering
{
char *s;
printf("Please enter the phrase that you would like to decode:");
scanf("%s",&s);
int i=0;
int n=strlen(s);
while(i<n)
{
if(s[i]>=65&&s[i]<=90) //For Uppercase letters
{
int u=s[i];
int c=duppercalc(u,k);
printf("%c",c);
}
else if(s[i]>=97&&s[i]<=122) //For Lowercase letters
{
int u=s[i];
int c=dlowercalc(u,k);
printf("%c",c);
}
else
printf("%c",s[i]); //For non letters
i++;
}
printf("\n");
return 0;
}
int duppercalc(int u, int k) //Algorithm to decypher Upper Case letters
{
if(u-k>=65)
{
int c=u-k;
return (c);
}
else
{
if (((u-k)/26)==2)
{
int c=((u-k)%26)+78;
return (c);
}
else
{
int c=((u-k)%26)+52;
return (c);
}
}
}
int dlowercalc(int u, int k) //Algorithms to decypher lower case letters
{
if(u-k>=97)
{
int c=u-k;
return (c);
}
else
{
if (((u-k)/26)==3)
{
int c=((u-k)%26)+104;
return (c);
}
else
{
int c=((u-k)%26)+78;
return (c);
}
}
}
该程序首先通过插入字符“c”或“d”来询问用户他是想要 Cypher 还是 Decypher,然后运行 cyphmain(如果是密码)或 dcecyphmain(如果是解密)函数。这工作正常
然后程序会询问用户的 rot 编号,然后要求用户输入一个短语。这也很好用。
但是,在输入单词以(de)cypher 后,程序因分段错误而崩溃,这使我相信错误在于 maincalc/dmaincalc 函数。(请注意,每个函数本质上都有两个副本,一个用于加密,另一个用于解密,除了文本中的一些更改以及加密或解密文本所涉及的实际数学之外,它们完全相同)。
这是一个错误的例子
Do You Want Cypher(c) or Decypher(d)?:d
What is the Cypher Number:2
Please enter the phrase that you would like to decode: Hello
Segmentation fault (core dumped)