首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >这个程序是用来画房子的。但是这些函数不起作用

问这个程序是用来画房子的。但是这些函数不起作用
EN

Stack Overflow用户
提问于 2014-03-04 01:24:24
回答 1查看 427关注 0票数 0

我正试着用C语言编写这个程序,用星号、破折号或点来画一座房子。它会问用户一组问题(大房子还是小房子)、(使用什么字符?)、(有多少个窗口?)然后它应该画出房子。我还没有为无效响应添加任何代码,因为我想首先检查它是否与有效响应一起工作,但它没有。在第三个问题之后,Cygwin说“分段错误(核心转储)”。有人能告诉我问题出在哪里吗?

代码语言:javascript
复制
#include <stdio.h>
#include <math.h>
#define house 1

char qbs; // for quit big small
char dsd; // for dash star dot
int now; // number of windows
int i, f; // random variables for for loops


main()
{
while(house) {
printf("Do you want a big or small house? (Q: quit, B: big house, S: small house)\n");
scanf("%s", &qbs);
switch(qbs){
    case 'B': 
    case 'b': 
    case 's':
    case 'S': break;
    default : printf("%s is not a valid choice. Please try again.\n", qbs);
              continue;
    }

printf("Which characters do you want for drawing? (DASH(-), STAR(*), or DOT(.))\n");
scanf("%s", &dsd);
printf("Using %c to draw the house.\n", dsd);

printf("How many windows do you want in the house? (0 through 4 for big houses and 0 through 2 for small houses.)\n");
scanf("%d", &now);

switch(dsd){
        case '-': dsd='-';
                  break;

        case '*': dsd='*';
                  break;

        case '.': dsd='.';
                  break;
        }
printf("%c %c %d", qbs, dsd, now);

switch(qbs){
        case 'Q':
        case 'q': printf("QUITTING THE PROGRAM.\n"); break;

        case 'B':
        case 'b': printf("         "); // this line and the next 7 lines draw the BIG roof 
                  for(i=0; i<=42; i++) {printf("%s", dsd);}
                  printf("\n      ");
                  for(i=0; i<=48; i++) {printf("%s", dsd);}
                  printf("\n   ");
                  for(i=0; i<=56; i++) {printf("%s", dsd);}
                  printf("\n");
                  for(i=0; i<=62; i++) {printf("%s", dsd);}
                 switch(now) { 
                    case '0':
                        for(f=0; i<=15; f++) { // the vertical expansion
                            for(i=0; i<=62; i++) {printf("%s", dsd); printf("\n");} // the horizontal expansion
                        }
                        break;

                    case '1':
                        for(i=0; i<=62; i++) {printf("%s", dsd); printf("\n");} // first line with no windows
                        for(f=0; f<=13; f++) { 
                            for(i=0; i<=28; i++) { printf("%s", dsd); } // left side of window
                            for(i=0; i<=6; i++) { printf(" "); } // window
                            for(i=0; i<=28; i++) { printf("%s"); } // right side of window
                        }
                        for(f=0; f<=2; f++) { for(i=0; i<=62; i++) {printf("%s", dsd);}} // bottom two lines with no windows
                        break;

                    case '2': 
                        for(i=0; i<=62; i++) {printf("%s", dsd); printf("\n");} // first line with no windows
                        for(f=0; f<=13; f++) {
                            for(i=0; i<=21; i++) { printf("%s", dsd); } // left side
                            for(i=0; i<=7; i++) {printf(" ");} // first window
                            for(i=0; i<=6; i++) {printf("%s", dsd);} // space between windows
                            for(i=0; i<=7; i++) {printf(" ");} // second window
                            for(i=0; i<=21; i++) { printf("%s", dsd); } // right side
                        }
                        for(f=0; f<=2; f++) { for(i=0; i<=62; i++) {printf("%s", dsd);}} // bottom two lines with no windows
                        break;

                    case '3': 
                        for(i=0; i<=62; i++) {printf("%s", dsd); printf("\n");} // first line with no windows
                        for(f=0; f<=13; f++) {
                            for(i=0; i<=11; i++) { printf("%s", dsd);} // left side
                            for(i=0; i<=6; i++) {printf(" "); } // first window
                            for(i=0; i<=11; i++) { printf("%s", dsd);} // space between window 1 and 2
                            for(i=0; i<=6; i++) {printf(" "); } // second window
                            for(i=0; i<=11; i++) { printf("%s", dsd);} // space between window 2 and 3
                            for(i=0; i<=6; i++) {printf(" "); } // third window
                            for(i=0; i<=11; i++) { printf("%s", dsd);} // right side
                        }
                        for(f=0; f<=2; f++) { for(i=0; i<=62; i++) {printf("%s", dsd);}} // bottom two lines with no windows
                        break;

                    case '4': 
                        for(i=0; i<=62; i++) {printf("%s", dsd); printf("\n");} // first line with no windows
                        for(f=0; f<=13; f++) {
                            for(i=0; i<=6; i++) {printf("%s", dsd);} // left side
                            for(i=0; i<=8; i++) {printf(" ");} // first window
                            for(i=0; i<=6; i++) {printf("%s", dsd);} // space between win1 and win2
                            for(i=0; i<=8; i++) {printf(" ");} // second window
                            for(i=0; i<=6; i++) {printf("%s", dsd);} // space between win2 and win3
                            for(i=0; i<=8; i++) {printf(" ");} // third window
                            for(i=0; i<=6; i++) {printf("%s", dsd);} // space between win3 and win4
                            for(i=0; i<=8; i++) {printf(" ");} // fourth window
                            for(i=0; i<=6; i++) {printf("%s", dsd);} // right side
                        }
                        for(f=0; f<=2; f++) { for(i=0; i<=62; i++) {printf("%s", dsd);}} // bottom two lines with no windows


        case 'S':
        case 's': printf("         "); // this line and the next 7 lines draw the SMALL roof
                  for(i=0; i<=12; i++) {printf("%s", dsd);}
                  printf("\n      ");
                  for(i=0; i<=18; i++) {printf("%s", dsd);}
                  printf("\n   ");
                  for(i=0; i<=24; i++) {printf("%s", dsd);}
                  printf("\n");
                  for(i=0; i<=30; i++) {printf("%s", dsd);}
                  switch(now) { 
                    case '0':
                        for(f=0; i<=6; f++) { // the vertical expansion
                        for(i=0; i<=30; i++) {printf("%s", dsd); printf("\n");} // the horizontal expansion
                        }
                        break;

                    case '1':
                        for(f=0; f<=5; f++) { 
                        for(i=0; i<=12; i++) { printf("%s", dsd); } // left side of window
                        for(i=0; i<=6; i++) { printf(" "); } // window
                        for(i=0; i<=12; i++) { printf("%s"); } // right side of window
                        }
                    for(f=0; f<=1; f++) { for(i=0; i<=30; i++) {printf("%s", dsd);}} // bottom line with no windows
                    break;

                    case '2': 
                        for(f=0; f<=5; f++) {
                            for(i=0; i<=6; i++) { printf("%s", dsd); } // left side
                            for(i=0; i<=6; i++) {printf(" ");} // first window
                            for(i=0; i<=6; i++) {printf("%s", dsd);} // space between windows
                            for(i=0; i<=6; i++) {printf(" ");} // second window
                            for(i=0; i<=6; i++) { printf("%s", dsd); } // right side
                        }
                    for(f=0; f<=1; f++) { for(i=0; i<=62; i++) {printf("%s", dsd);}} // bottom line with no windows
                    break;
                  printf("Small house with %d windows\n", now);
                  break;
}
}
}
}
EN

回答 1

Stack Overflow用户

发布于 2014-03-04 01:32:41

在您的整个代码中,尝试替换

代码语言:javascript
复制
printf("%s", dsd)

通过

代码语言:javascript
复制
printf("%c", dsd)

并对scanf执行相同的操作。您需要的是字符,而不是字符串。

不管怎么说,有趣的是,前一天有人想画同样的房子,这是你学习的练习吗?^^

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22153195

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档