12天的圣诞节C计划

时间:2013-08-26 14:09:58

标签: c++ c

所以我自己尝试了12天的圣诞节编码。我还没有完成歌词,我还在努力弄明白。但我不明白为什么我的圣诞节“第一天”加倍并与另一份礼物合作,并且在第12天,没有礼物出现。我检查了我的开关盒,他们似乎是对的。是否有可能减少我的代码打印出完整的歌词?

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

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

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

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

        switch(counter){
            case 1:
                printf("1st");
                break;  
            case 2: 
                printf("2nd");
                break;
            case 3: 
                printf("3rd");
                break;
            case 4: 
                printf("4th");
                break;
            case 5: 
                printf("5th");
                break;      
            case 6: 
                printf("6th");
                break;
            case 7: 
                printf("7th");
                break;
            case 8: 
                printf("8th");
                break;  
            case 9: 
                printf("9th");
                break;
            case 10:    
                printf("10th");
                break;  
            case 11:    
                printf("11th");
                break;  
            case 12:    
                printf("12th");
                break;                                                                                      
                default: 
                printf("1st", num);
                break;
            }
            printf(" day of Christmas my true love sent to me\n");

        }   

        getch();
        return 0;
    }

指令说:“你的函数只会在main()函数中调用,不会返回任何内容”那么这是否意味着我不会创建更多的函数?或者我应该只将所有代码放在main函数中?或者创建单独的代码?

9 个答案:

答案 0 :(得分:6)

再次查看switch语句,并记住中断不是完全必要的。

switch (day) {
  case 2: printf("two turtle doves ");
  case 1: printf("and a partridge in a pear tree");
}

这将是“第二天”从“两只乌龟鸽子”开始,落到“和梨树上的鹧”中。

与此同时,“第1天”开始是“和梨树中的鹧”。

答案 1 :(得分:6)

我实际上并不认为开关是这里的最佳选择。我们有一个要显示的项目列表,我们可以轻松地将其放入数组和订单中,并且每次都必须显示这些项目的不同列表。虽然这里的切换语法很好(正如问题的评论中所提到的,而不是在提问者的代码本身中使用的),但我认为简单的循环比不断要求程序执行切换跳转更合适。

这是我建议的解决方案:

#include <stdio.h>

const char
  *presents[] = {
    "\t\tA Partridge in a Pear Tree\n", "\t\tTwo Turtle Doves\n",
    "\t\tThree French Hens\n", "\t\tFour Calling Birds \n",
    "\t\tFive Golden Rings\n", "\t\tSix Geese a Laying\n",
    "\t\tSeven Swans a Swimming\n", "\t\tEight Maids a Milking\n",
    "\t\tNine Ladies Dancing\n", "\t\tTen Lords a Leaping\n",
    "\t\tEleven Pipers Piping\n", "\t\tTwelve Drummers Drumming\n"},
  *days[] = {
    "1st", "2nd", "3rd", "4th", "5th", "6th",
    "7th", "8th", "9th", "10th", "11th", "12th"};

void printTwelveDaysSong(void)
{
  int i, j;

  for (i = 0; i < 12; ++i) {
    printf("\n\tOn the %s day of Christmas my true love sent to me\n", days[i]);
    for (j = i; j > 0; --j) fputs(presents[j], stdout);
    if (i > 0) fputs("\t\tand\n", stdout);
    fputs(presents[0], stdout);
  }
}

/* Example main function */
int main(void)
{
  printTwelveDaysSong();
  return 0;
}

我把核心代码放在一个不同的功能中,因为它听起来就像是需要的,所以我也表明了这一点,没有什么特别之处。

在代码中我只使用C标准库函数进行输出,我没有尝试实现交互式程序,如果愿意,可以自己添加。

答案 2 :(得分:1)

您的for循环将counter设置为零,但您的switch语句会处理大于零的情况。

答案 3 :(得分:1)

好吧,首先你将counter设置为零,第一个开关没有打印,第二个打印“在第一个......”。

然后你将反击设置为一个......

另外,在您说出礼物的哪一天之前,您还要奇怪地打印出礼物。

要减少代码,你应该考虑1到12,只有第1,第2和第3不会在th中结束,而且还要记住你可以通过开关中的情况(不是特别好的风格,但适用于这种事情。)

答案 4 :(得分:0)

for条件应为:

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

答案 5 :(得分:0)

您从counter=0启动for循环。这导致第一个switch无法打印,第二个switch转到default打印“1st”。这可能就是为什么你的第一天打印两次了!

将您的for循环更改为for (counter = 1; counter <= 12; counter++)

答案 6 :(得分:0)

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

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

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

for (counter=1; counter<=13; counter++)
{   
    printf("\n\tOn the ");

    switch(counter){
        case 1:
            printf("1st");
            break;  
        case 2: 
            printf("2nd");
            break;
        case 3: 
            printf("3rd");
            break;                                                             
        default: 
            printf(counter + "th");
            break;
        }

    printf(" day of Christmas my true love sent to me\n");
    for (int x= counter; x > 0; x--)
    {
        switch(x)
        {
            case 1: printf("\t\t");if (counter > 1 ) printf("And ");printf("A Partridge in a Pear Tree\n");break; // Day 12
            case 2: printf("\t\tTwo Turtle Doves\n"); break;
            case 3: printf("\t\tThree French Hens\n"); break;
            case 4: printf("\t\tFour Calling Birds \n"); break;
            case 5: printf("\t\tFive Golden Rings\n"); break;
            case 6: printf("\t\tSix Geese a Laying\n"); break;
            case 7: printf("\t\tSeven Swans a Swimming\n"); break;
            case 8: printf("\t\tEight Maids a Milking\n"); break;
            case 9: printf("\t\tNine Ladies Dancing\n"); break;
            case 10: printf("\t\tTen Lords a Leaping\n"); break;
            case 11: printf("\t\tEleven Pipers Piping\n"); break;
            case 12: printf("\t\tTwelve Drummers Drumming\n"); break; // Day 1 
        }
    }

}           
    getch();
    return 0;
}

答案 7 :(得分:0)

我假设你教授的目标是教你如何使用switch语句(可能要求你使用多个switch语句?)。显然,除了教学之外,这个课程没有任何实际用途。所以所有人都建议你采取各种“创造性”的方式(比如使用数组和其他东西)会误解你的情况。你犯了一些错误 - 并且在那里有一些不必要的东西。但是你走在正确的轨道上,只需要几次审查(在评论中来自SuvP的特别好的通知)。

答案 8 :(得分:0)

#include <iostream>
#include <string>

using std::cout;

const char *num_day[] = {"-", "First", "Second", "Third",
         "Fourth", "Fifth", "Sixth", "Seventh",
         "Eighth", "Ninth", "Tenth", "Eleventh", "Twelfth"};
int main()
{

    for (int day = 1; day <= 12; ++day) {
    cout << "On the " << num_day[day]
         << " day of Christmas, my true love gave to me:\n";
    switch (day) {
        case 12: cout << "Twelve Drummers Drumming\n";
        case 11: cout << "Eleven Pipers Piping\n";
        case 10: cout << "Ten Lords a-Leaping\n";
        case 9: cout << "Nine Ladies Dancing\n";
        case 8: cout << "Eight Maids a-Milking\n";
        case 7: cout << "Seven Swans a-Swimming\n";
        case 6: cout << "Six Geese a-Laying\n";
        case 5: cout << "Five Gold Rings\n";
        case 4: cout << "Four Calling Birds\n";
        case 3: cout << "Three French Hens\n";
        case 2: cout << "Two Turtle Doves, and\n";
        case 1: cout << "A Partridge in a Pear Tree\n\n";
    }
}
return 0;

}