需要帮助使用C中的递归用星号填充用户定义的形状 [英] Need help filling a user-defined shape with asterisks using recursion in C

查看:97
本文介绍了需要帮助使用C中的递归用星号填充用户定义的形状的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在C中编写一个程序,该程序从用户处获得用星号概述的形状,并返回填充星号的形状。它还将获得开始填充的形状内的点的坐标。我需要在这里使用递归。例如,要明确:


输入:(忽略该行,将其视为空格)

*****

* ___ *

*****

坐标是(2,1)/ *它是一个数组所以编号将从0 * /

输出

*****

*****

*****


我的程序编译但由于某种原因,形状和过去边界的所有东西都充满了星号。它被填充到我设置数组的最大尺寸,即使形状不是那么大。当它填满星号的边界时它完全忽略了它。


这里是我写的程序,它相当长......

展开 | 选择 | Wrap | 行号

解决方案


if(arr) [row] [col]!=''''|| row> = 20 || col> = 20 || row< 0 || col< 0)



如果你不想超越阵列的界限,那么最好转过这个测试:

展开 | 选择 | < span class =codeLinkonclick =WordWrap(this);> Wrap | 行号



如果你不想超越,那么最好转过这个测试数组的边界:

展开 | 选择 | Wrap | 行号


它仍无效。我有太多的回复电话吗?我应该在填充函数的if语句中有什么内容吗?我很确定问题在于填充功能本身。我真的需要帮助这些家伙,我真的很努力地做到这一点。



基本上我现在得到几行不同长度的星号,然后是一个20X20的星号填充。


I''m trying to write a program in C that gets a shape outlined with asterisks from the user, and returns that shape filled with asterisks. It will also get the coordinates of a point inside the shape from which to start filling. I need to use recursion here. for example, to be clear:

input: (ignore the line, think of that as blank space)
*****
*___*
*****
coordinates are (2,1) /*its an array so numbering will start from 0*/

output
*****
*****
*****

my program compiles but for some reason everything in the shape and past its boundaries get filled with asterisks. it gets filled to the maximum size i''ve set the array to, even if the shape isn''t that big. its like it completely ignores when it gets to a boundary of asterisks when it''s filling.

here is the program i''ve written, its rather long...

Expand|Select|Wrap|Line Numbers

解决方案

if(arr[row][col]!='' ''|| row>=20 || col>=20 || row<0 || col<0)

Better turn that test around if you don''t want to ''look'' beyond the bounds of your array:

Expand|Select|Wrap|Line Numbers


Better turn that test around if you don''t want to ''look'' beyond the bounds of your array:

Expand|Select|Wrap|Line Numbers


its still not working. do i have too many recusive calls? should i have something in my if statement of the fill function? i''m pretty sure the problem lies in the fill function itself. i really need help on this guys, i''ve really tried hard to do this myself.

basically now i get as the output a few lines of different lengths of asterisks and then after that a filled 20X20 box of asterisks.


这篇关于需要帮助使用C中的递归用星号填充用户定义的形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆