-1

プログラミングの基礎を学ぶために、iTunes University: Harvard 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 (暗号化の場合) または dcecyphmain (復号化の場合) 関数のいずれかを実行します。これはうまくいきます

次に、プログラムはユーザーに腐敗番号を尋ね、次にユーザーにフレーズを入力するように求めます。これもうまくいきます。

ただし、(解読) する単語を入力すると、プログラムがセグメンテーション違反でクラッシュするため、エラーは maincalc/dmaincalc 関数にあると思われます。(基本的に、各関数には 2 つのコピーがあり、1 つは暗号化用で、もう 1 つは復号化用です。テキストのいくつかの変更と、テキストの暗号化または復号化に関連する実際の計算を除いて、それらはまったく同じです)。

失敗例はこちら

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)
4

1 に答える 1

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 */

sizeof(char)オプション 1 については、常にであることに注意してください1。完全を期すために、ここに記載しています。また、文字列を使い終わったら呼び出す必要があることも忘れないでくださいfree(s)

于 2013-02-19T18:37:11.410 に答える