12天的圣诞节C程序功能

时间:2013-08-27 08:36:41

标签: c

一个C程序,其中包含一个输出传统圣诞歌曲“圣诞节的十二天”歌词的功能。不要手动打印整个歌词。

所以我制作了一个代码并且有错误,但我终于修复了它。我的十二天圣诞歌曲与循环打印得很好。

但我有另一个问题。我的代码是否可以作为函数分离或解析?

指令说:“你的函数只会在main()函数中调用,不会返回任何东西。”所以我想我会使用无效的?以什么方式?

#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; }

尝试执行此操作并正常打印。你对我的代码有任何建议吗?遇到功能问题。

4 个答案:

答案 0 :(得分:3)

这只意味着所有的工作都应该在一个void函数中完成,然后在main函数中调用:

void doTheWork(void); // function declaration

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

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

答案 1 :(得分: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

答案 2 :(得分: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;
}

使用live example

答案 3 :(得分:0)

在你的程序中,你需要压制所有幻数(cf: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();
    }