2013-08-27 58 views
3

一個C程序,它包含一個輸出傳統聖誕歌曲「十二日聖誕節」的歌詞的功能。不要手動輸出整個歌詞。12天的聖誕節C功能的程序

所以我做了一個代碼,有錯誤,但我終於修好了。我的十二天聖誕歌曲循環播放效果很好。

但我有另一個問題。我的代碼是否可以作爲函數分離或解析?

指令說:「你的函數只會在main()函數中被調用,並且不會返回任何東西。」所以我想我會使用void?以什麼方式?

#include <stdio.h> 
#include <conio.h> 

int main() // Main Function 
{ 
int days, counter, num; 

printf("\n\t\t* * * TWELVE DAYS OF CHRISTMAS * * *\n"); 
printf("\t\t_____________________________________\n\n\n"); 

for (counter=1; counter<=12; counter++) { 

printf("\tOn the "); 

switch(counter){ 
     case 1: 
      printf("1st"); 
      break; 
     case 2: 
      printf("2nd"); 
      break; 
     case 3: 
      printf("3rd"); 
      break; 
     default: 
      printf("%dth", counter); 
      break; 
    } 
printf(" day of Christmas my true love sent to me\n\n"); 

switch(counter) { 

    case 12: printf("\t\tTwelve Drummers Drumming\n\n"); 
    case 11: printf("\t\tEleven Pipers Piping\n\n"); 
    case 10: printf("\t\tTen Lords a Leaping\n\n"); 
    case 9: printf("\t\tNine Ladies Dancing\n\n"); 
    case 8: printf("\t\tEight Maids a Milking\n\n"); 
    case 7: printf("\t\tSeven Swans a Swimming\n\n"); 
    case 6: printf("\t\tSix Geese a Laying\n\n"); 
    case 5: printf("\t\tFive Golden Rings\n\n"); 
    case 4: printf("\t\tFour Calling Birds \n\n"); 
    case 3: printf("\t\tThree French Hens\n\n"); 
    case 2: printf("\t\tTwo Turtle Doves\n\n"); 
    case 1: printf("\t\t");if (counter > 1) printf("And ");printf("A Partridge in a Pear Tree\n\n"); 
    // case 1: printf("\t\tA Partridge in a Pear Tree\n\n"); 



} 

} 

getchar(); return 0; } 

試圖執行此操作,並與打印工作正常。你有任何建議,以imporove我的代碼?遇到功能問題。

+0

你一直由C混淆比賽的啓發,整個項目在三條線上? – matcheek

+1

我已經刪除了C++標記,因爲代碼中沒有C++標記。 –

+0

你的第二個'switch'語句沒有'break',這是一個錯誤。它是如何打印你想要的? – Thanushan

回答

0

這意味着你應該創建一個void功能做的工作:

#include <stdio.h> 

// Function Implementation 
void yourFunction(void) 
{ 
    int days, counter, num; 

    printf("\n\t\t* * * TWELVE DAYS OF CHRISTMAS * * *\n"); 
    printf("\t\t_____________________________________\n\n\n"); 

    for (counter=1; counter<=12; counter++) { 

     printf("\tOn the "); 

     switch(counter){ 
     case 1: 
      printf("1st"); 
      break; 
     case 2: 
      printf("2nd"); 
      break; 
     case 3: 
      printf("3rd"); 
      break; 
     default: 
      printf("%dth", counter); 
      break; 
     } 
     printf(" day of Christmas my true love sent to me\n\n"); 

     switch(counter) { 

     case 12: printf("\t\tTwelve Drummers Drumming\n\n"); 
     case 11: printf("\t\tEleven Pipers Piping\n\n"); 
     case 10: printf("\t\tTen Lords a Leaping\n\n"); 
     case 9: printf("\t\tNine Ladies Dancing\n\n"); 
     case 8: printf("\t\tEight Maids a Milking\n\n"); 
     case 7: printf("\t\tSeven Swans a Swimming\n\n"); 
     case 6: printf("\t\tSix Geese a Laying\n\n"); 
     case 5: printf("\t\tFive Golden Rings\n\n"); 
     case 4: printf("\t\tFour Calling Birds \n\n"); 
     case 3: printf("\t\tThree French Hens\n\n"); 
     case 2: printf("\t\tTwo Turtle Doves\n\n"); 
     case 1: printf("\t\t");if (counter > 1) printf("And ");printf("A Partridge in a Pear Tree\n\n"); 
     // case 1: printf("\t\tA Partridge in a Pear Tree\n\n"); 
     } 
    } 
} 

int main(void) 
{ 
    yourFunction(); // Call your function 

    getchar(); 
    return 0; 
} 

With live example

2

使用void函數。

void print12DaysOfChristmas(void) 
{ 
    // Paste all your code here 
} 

int main(void) // Main Function 
{ 
    print12DaysOfChristmas(); 
    return 0; 
} 

注:

  1. 關於在C函數簽名,見this answer
  2. 如果該函數未在main之前定義,則需要使用forward declaration
+0

嘗試過,但發生錯誤說「print12DaysOfChristmas」沒有在範圍中聲明。 @Daniel Daranas – JustAStudent

+3

你粘貼了我寫的確切代碼嗎?在int main(void)之前定義print12DaysOfChristmas,或者使用[this answer](http://stackoverflow.com/a/18460793/96780)中的前向聲明。 –

3

它只是意味着所有工作必須在一個void函數,然後將其稱爲主要完成:

void doTheWork(void); // function declaration 

int main(void) 
{ 
    doTheWork(); 
    return 0; 
} 

#include <stdio.h> 
// function definition 
void doTheWork(void) 
{ 
    // put the implementation here 
} 
+0

我嘗試過,但發生錯誤,例如dothework沒有在範圍中聲明。什麼範圍? @juanchopanza – JustAStudent

+2

@JennicaPrincess好的,我在打電話給工作函數時有一個錯字。 'doWorkWork()'應該是'doTheWork()'。 – juanchopanza

0

在你的程序,你需要抑制所有幻數(參見:http://en.wikipedia.org/wiki/Magic_number_%28programming%29

而且,我優化了一下你的代碼如下:

#include <stdio.h> 
    #include <string.h> 

    #define FIRST_DAY "1st" 
    #define SECOND_DAY "2nd" 
    #define THIRD_DAY "3rd" 
    #define N_DAY  "th" 

    #define ONLY_ONE_SENTENCE 0 
    #define LAST_DAY   1 
    #define NUMBER_OF_DAYS 12 

    #define BUFFER_SIZE  5 

    #define FIRST_ANNOUNCE "\tOn the %s day of Christmas my true love sent to me\n\n" 

    const char *sentences[] = { 
     "\t\tA Partridge in a Pear Tree\n\n", 
     "\t\tAnd A Partridge in a Pear Tree\n\n", 
     "\t\tTwo Turtle Doves\n\n", 
     "\t\tThree French Hens\n\n", 
     "\t\tFour Calling Birds \n\n", 
     "\t\tFive Golden Rings\n\n", 
     "\t\tSix Geese a Laying\n\n", 
     "\t\tSeven Swans a Swimming\n\n", 
     "\t\tEight Maids a Milking\n\n", 
     "\t\tNine Ladies Dancing\n\n", 
     "\t\tTen Lords a Leaping\n\n", 
     "\t\tEleven Pipers Piping\n\n", 
     "\t\tTwelve Drummers Drumming\n\n"}; 

    void print12DaysOfChristmas(); 

    int main() { // Main Function 
     print12DaysOfChristmas(); 
     return 0; 
    } 


    void print12DaysOfChristmas() { 
     int days, counter, num, index; 
     char buffer[BUFFER_SIZE]; 
     char *day; 

     printf("\n\t\t* * * TWELVE DAYS OF CHRISTMAS * * *\n"); 
     printf("\t\t_____________________________________\n\n\n"); 

     for (counter=1; counter <= NUMBER_OF_DAYS; counter++) { 
      switch (counter) { 
       case 1: 
        day=FIRST_DAY; 
        break; 
       case 2: 
        day=SECOND_DAY; 
        break; 
       case 3: 
        day=THIRD_DAY; 
        break; 
       default: 
        snprintf(buffer, BUFFER_SIZE,"%d",counter); 
        day=strcat(buffer, N_DAY); 
        break; 
       } 

      printf(FIRST_ANNOUNCE, day); 

      for (index=counter; index > 0; index--) { 
       // If there is only one sentence 
       if (counter == LAST_DAY) { 
        printf(sentences[ONLY_ONE_SENTENCE]); 
       } else { 
        printf(sentences[index]); 
       } 
      } 
     } 

     getchar(); 
    }