2018-10-29 08:12

# C++萌新请大佬帮忙解答

1）用户输入房间的长度和宽度，门、窗、家具的位置。放置规则：窗户和门必须在墙上且不能再拐角，窗户不能紧靠门，家具必须在室内且不能堵门。不合规则要提示重新输入；
2）根据上述参数绘制房间图样，墙用'#'表示，门用空格表示，窗户用'|'或'-'表示（前者在竖墙上，后者在横墙上），家具用'*'表示。
###-######

# *

##########

• 点赞
• 写回答
• 关注问题
• 收藏
• 邀请回答

#### 1条回答默认 最新

• Italink 2018-10-29 08:19
已采纳

``````#include<iostream>
using namespace std;
int main(){
int length, width;
cout << "输入房间长度和宽度（以空格分隔，下同）：";
cin >> length >> width;
char room[100][100];
for (int i = 0; i < width; i++) {
for (int j = 0; j < length; j++) {
if (i == 0 || i == width - 1 || j == 0 || j == length - 1)
room[i][j] = '#';
else
room[i][j] = ' ';
}
}
int x, y;
cout << "输入门的坐标：";
cin >> x >> y;
while (room[y][x]!='#' || ((x == 0 || x == length - 1) && (y == 0 || y == width - 1))) {
cout << "门必须在墙上且不能再拐角,请重新输入：";
cin >> x >> y;
}
int doorx = x, doory = y;
room[y][x] = ' ';
cout << "输入窗户的坐标：";
cin >> x >> y;
while (room[y][x] != '#' || ((x == 0 || x == length - 1) && (y == 0 || y == width - 1)) ||((x==0||x==length-1)&&(room[y-1][x]==' '||room[y+1][x]==' '))|| (y == 0 || y == width - 1) && (room[y][x+1] == ' ' || room[y][x-1] == ' ')) {
cout << "窗户必须在墙上且不能再拐角，且不能紧靠门,请重新输入：";
cin >> x >> y;
}
if (x == 0 || length == length - 1)
room[y][x] = '|';
else
room[y][x] = '-';

cout << "输入家具的坐标：";
cin >> x >> y;
while (!(0<x&&x<length&&0<y&&y<width)||(y==doory&&abs(x-doorx)==1)||(x==doorx&&abs(y-doory)==1)) {
cout << "家具必须在室内，且不能堵门,请重新输入：";
cin >> x >> y;
}
room[y][x] = '*';

//显示房间
for (int i = 0; i < width; i++) {
for (int j = 0; j < length; j++)
cout << room[i][j];
cout << endl;
}
return 0;
}

``````
点赞 评论