C:函数在单独的实例中以不同的方式运行

时间:2013-02-19 18:20:13

标签: c string caesar-cipher cs50

我正在关注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);
        }
     }
}

本程序首先通过插入char“c”或“d”询问用户是否想要Cypher或Decypher,然后运行cyphmain(如果是cyphering)或dcecyphmain(如果是decphering)函数。这很好用

程序然后询问用户腐烂编号,然后要求用户输入短语。这也很好。

然而,在输入(de)cypher的单词后,程序崩溃并出现分段错误,这让我相信错误在于maincalc / dmaincalc函数。 (请注意,每个函数基本上有两个副本用于加密,另一个用于decyphering,它们完全相同,除了文本中的一些更改以及对文本进行加密或解密的实际数学时)。

这是故障的一个例子

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)

1 个答案:

答案 0 :(得分:0)

您的错误在这里:

char *s;
printf("Please enter the phrase that you would like to code:");  
scanf("%s",&s);

这不会为字符串分配空间。您可以使用malloc来分配空间,也可以将字符串放在堆栈中。

char *s = malloc(sizeof(char) * 50); /* option 1*/
char s[50]; /* option 2 */

对于选项1,请注意sizeof(char)始终为1,我只是为了完整性而将其放在那里。另外,请不要忘记在完成字符串后必须调用free(s)