任何人都可以帮我解决我的代码问题。它应该做的是当我输入“创建6 5”它应该创建一个带有*的6x5矩形形状,但除非我输入“show”,否则不会显示。
这是我正在处理的代码,它还不正确,还在弄清楚如何去做。
我是编程新手。
char s[100];
char *s1 = "show";
char *s2 = "create";
int main()
{
int zWidth = 0, zHeight = 0, zLoop = 0, aLoop = 0;
do
{
scanf("%s %d %d", &s, &zWidth, &zHeight);
}
while (strcmp(s1, s) != 0);
if(strcmp(s2, s) == 0)
{
//To draw the first horizontal line
for(zLoop = 0; zLoop < zWidth; zLoop++)
printf("*");
printf("\n");
//Drawing the vertical lines
for(zLoop = 2; zLoop < zHeight; zLoop++)
{
printf("*");
for(zLoop = 0; aLoop < zWidth-2; aLoop++)
printf("*");
printf("*\n");
}
//Last horizontal Line
for(zLoop = 0; zLoop < zWidth; zLoop++)
printf("*");
printf("\n");
}
return 0;
}
答案 0 :(得分:0)
#include <stdio.h>
enum command_state {
SHOW, CREATE, QUIT, HELP, INVALID
};
char Command_line[100];
const char *Show = "show";
const char *Create = "create";
const char *Quit = "quit";
const char *Help = "help";
const char *QMark = "?";
enum command_state command(void){
if(fgets(Command_line, sizeof(Command_line), stdin) != NULL){
char operation[16];
sscanf(Command_line, "%15s", operation);
if(!strcmp(operation, Show))
return SHOW;
else if(!strcmp(operation, Create))
return CREATE;
else if(!strcmp(operation, Quit))
return QUIT;
else if(!strcmp(operation, Help) || !strcmp(operation, QMark))
return HELP;
else
return INVALID;
}
return QUIT;//input EOF
}
int main(void){
int rWidth = 0, rHeight = 0, aLoop = 1;
while(aLoop){
switch(command()){
case SHOW :
if(rWidth == 0 || rHeight == 0)
printf("execute create is required before\n");
else {
int h, w;
for(h = 0; h < rHeight; h++) {
for(w = 0; w < rWidth; w++)
printf("*");
printf("\n");
}
}
break;
case CREATE :
if(2!=sscanf(Command_line, "create %d %d", &rWidth, &rHeight) ||
rWidth < 1 || rHeight < 1){
rWidth = rHeight = 0;
printf("invalid create command\n");
}
break;
case QUIT :
aLoop = 0;
break;
case HELP :
printf(
"help or ? : this\n"
"create w h: set to size of rectangle width and height\n"
"show : draw filled rectangle\n"
"quit : end of program\n"
);
break;
default :
printf("invalid command\n");
}
}
return 0;
}