2013-02-19 7 views
-1

프로그래밍의 기본 사항을 배우기 위해 iTunes University : Harvard CS50 강의를 진행하고 있습니다. 저는 현재 텍스트 사이퍼 (text cypher)를위한 프로그램을 만들고자합니다. 다음은 전체 프로그램 코드입니다.C : 별도의 인스턴스에서 다르게 실행되는 기능

#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"와 실행 중 하나 cyphmain 또는 dcecyphmain (decphering 경우) (cyphering 경우) 기능 중 하나를 삽입하여 그 사이퍼 싶어 여부를 사용자 또는 Decypher 요청에 의해 시작됩니다. 이 작동합니다.

프로그램은 사용자에게 부패 번호를 묻고 사용자에게 구를 입력하도록 요청합니다. 이것은 또한 잘 작동합니다.

그러나 (de) 사이퍼에 단어를 입력하면 프로그램이 세그먼트 오류로 중단되어 오류가 maincalc/dmaincalc 기능에 있다고 생각하게됩니다. (본질적으로 cyphering을위한 함수와 deciphering을위한 각 함수의 사본이 두 개 있다는 점에 유의하십시오. 텍스트의 일부 변경 사항과 텍스트의 cyphering 또는 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) 
+0

Valgrind를 사용하여 충돌이 발생한 줄 번호를 찾습니다. –

+0

문자열이나 힙에 공간을 할당하지 않았습니다. – cha0site

+0

그리고'scanf '에 대한 인수는'% s' 변환이있는'char *'이어야하므로'scanf ("% s", & s)'대신에'scanf ("% s", s)' 할당). –

답변

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)으로 전화해야한다는 것을 잊지 마십시오.

+0

좋아,이 작동하지만 문자열은 한 단어 만 기록합니다. "Hello World"와 같은 구문은 Hello 만 코드하지만 World는 코딩하지 않습니다. 문자열이 첫 단어뿐만 아니라 모든 단어를 읽도록하려면 어떻게해야합니까? – shea