因此,我只需要一些有关如何修复此程序的指导,因此,我需要阅读一个标题为“ infile.txt”的文件,该文件内的内容是描述应绘制形状的说明(A单个大写字符,即R,T,D,S,E),然后赋予char形状应填充的字符,然后是int中的行数和列数。 txt文件如下所示:
T & 4
S @ 6
T x 5
R * 5 7
D $ 7
D + 5
R = 4 3
E
现在,我什至不确定我是否可以使用switch语句完成此操作,因为infile是作为字符串读取的。但是后来我对于如何从switch语句更改感到困惑。无论如何,在读取infile后,我必须输出绘制到outfile的形状。希望这是有道理的,我是超级入门级别,几乎不知道我在做什么。所以这是我的代码:
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
void draw_rect (char out_char, int rows, int columns); // Draws a rectangle shape
void draw_square (char out_char, int rows); //Draws a square shape
void draw_triangle (char out_char, int rows);// Draws a triangle shape
void draw_diamond (char out_char, int rows); // Draws a diamond shape
//void dimension_instructions(char value);
int main()
{
ofstream outfile;
ifstream infile;
int row, col;
bool exit = false;
char value;
char code;
infile.open("infile.txt");
outfile.open("outfile.txt");
if(!infile.good())
{
cout << "failed to open\n";
}else
{
string buffer;
while(!infile.eof())
{
getline(infile, buffer);
cout << buffer << endl;
}
while(!exit)
{
cout << "Enter your shape R for rectangle, T for triangle, D for diamond, S for square, and E to exit" << endl;
cin >> code;
switch(code)
{
case 'R':
dimension_instructions(code);
cin >> value >> row >> col;
draw_rect(value, row, col);
break;
case 'T':
dimension_instructions(code);
cin >> value >> row;
draw_triangle(value, row);
break;
case 'D':
dimension_instructions(code);
cin >> value >> row;
draw_diamond(value, row);
break;
case 'S':
dimension_instructions(code);
cin >> value >> row;
draw_square(value, row);
break;
case 'E':
cout << "Exiting";
exit = true;
break;
default:
cout << "Invalid input, try again" << endl;
}
}
infile.close();
}
outfile.close();
return 0;
}
/*void dimension_instructions(char value)
{
if (value == 'R')
{
cout << "Enter your character rows and columns values." << endl;
}else
{
cout << "Enter your character and row values" << endl;
}
}*/
void draw_diamond (char out_char, int rows)
{
int space = 1;
space = rows - 1;
for (int i = 1; i <= rows; i++)
{
for (int k = 1; k <= space; k++)
{
cout << " ";
}
space--;
for( int k = 1; k <= 2*i-1; k++)
{
cout << out_char;
}
cout << endl;
}
space = 1;
for (int i = 1; i <= rows; i++)
{
for(int k = 1; k <= space; k++)
{
cout << " ";
}
space++;
for(int k = 1; k <= 2*(rows-i)-1; k++)
{
cout << out_char;
}
cout << endl;
}
}
void draw_triangle (char out_char, int rows)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j <= i; j++)
{
cout << out_char;
}
cout << endl;
}
}
void draw_square (char out_char, int rows)
{
for (int i = 0; i < rows; i++)
{
for (int i = 0; i < rows; i++)
{
cout << out_char;
}
cout << endl;
}
}
void draw_rect (char out_char, int rows, int columns)
{
for (int i = 0; i < rows; i++)
{
for (int i = 0; i < columns; i++)
{
cout << out_char;
}
cout << endl;
}
}
答案 0 :(得分:1)
好吧,根据您的评论,我看到了您陷入困境的原因。 (如果还没有完成,还需要在draw_square
和draw_rect
中修复循环变量。)
您的主要问题是不了解如何处理每行不同数量的输入。遇到此问题时,您已正确选择getline
并将每一行读入buffer
,但是那又是什么呢?这就是stringstream
与众不同的地方。
为什么?有两个原因,(1)它允许您使用基本iostream buffer
逐字地解析>>
的内容,(2)需要时,它允许您循环播放直至结束-流读取存在的所有(或更少)令牌,在到达行尾时停止(这在文件流本身上无法使用>>
,因为>>
会占用空白,会很高兴地跳过每个'\n'
)
这样一来,您的代码实际上只需要进行一点重构(一个花哨的词来修复混乱的逻辑)。
要开始不要硬编码文件名或在代码中使用幻数。使用main()
的参数将文件名传递给程序,并在需要时声明常量。还要避免使用char
,因为它不会消耗前导空格。 cin >> a_char;
和阅读其他内容一样,很高兴阅读' '
(空格)。
还要适当地设置变量的范围。您不需要声明所有变量,因此它们在main()
中都是可见的。在适当的范围内声明/定义它们。
例如:
...
#include <sstream>
...
int main (int argc, char **argv) { /* don't hardcode filenames */
ifstream infile; /* infile and buffer are the only variables */
string buffer; /* that need to be scoped at main() */
在将文件名作为参数传递时,只需在验证之前验证您的用户是否提供了文件名或向他提供使用信息即可。
if (argc < 2) { /* validate at least 1 argument is provided */
cerr << "error: insufficient input.\n"
"usage: " << argv[0] << " filename.\n";
return 1;
}
您有自己的论点,现在验证您是否打开文件以供阅读:
infile.open (argv[1]); /* open filename provided as 1st argument */
if(!infile.good()) { /* validate file is open for reading */
cerr << "failed to open infile\n";
return 1;
}
现在,对控制读取循环的方式进行了重要更改。 getline
提供您所需的一切。在getline
为buffer
提供良好的输入时,例如
while (getline(infile, buffer)) { /* loop reading each line */
int row, col; /* remaining variables scoped inside */
string value, code; /* your read loop, use strings */
stringstream ss(buffer); /* create stringstream from buffer */
现在您正在阅读每一行,并且已经从stringstream
创建了一个buffer
来解析-中的字符,但 note 除外,value, code
的声明方式是string
而非char
-提供一种简单的方法来跳过仅读取非空白字符的前导空白。然后,您可以轻松访问所需的字符,例如value[0]
。
验证您对code
的理解很好
if (!(ss >> code)) { /* validate code read into string */
cerr << "error: ss >> code.\n";
break;
}
然后,只需重复对所需数据的读取进行相同的验证,然后在每个switch() case:
中调用正确的函数即可,例如
switch (code[0]) /* switch on 1st char of code */
{
case 'R':
if ((ss >> value >> row >> col)) /* validate read */
draw_rect (value[0], row, col); /* draw rect */
else /* or handle error */
cerr << "error: 'R' invalid format '" << buffer << "'\n'";
break;
case 'T':
if ((ss >> value >> row)) /* ditto for rest of shapes */
draw_triangle(value[0], row);
else
cerr << "error: 'T' invalid format '" << buffer << "'\n'";
break;
case 'D':
if ((ss >> value >> row))
draw_diamond(value[0], row);
else
cerr << "error: 'D' invalid format '" << buffer << "'\n'";
break;
case 'S':
if ((ss >> value >> row))
draw_square(value[0], row);
else
cerr << "error: 'S' invalid format '" << buffer << "'\n'";
break;
case 'E':
cout << "Exiting\n";
goto exitE; /* goto to break nested loops / scopes */
break;
default:
cout << "Invalid input, try again" << endl;
}
}
exitE:; /* the lowly goto provides a simple exit */
除了关闭infile
(它会自动发生,但手动显示您对关闭的考虑也无妨)之外。
注意,但是请使用goto
而不是exit
的标志。尽管goto
并没有受到太多关注,但它还有一个不可估量的目的-能够清晰地脱离嵌套循环和范围。不要使用它跳出函数(longjmp
是技术限制),但是它可以大大简化打破嵌套循环并跳几行的逻辑。 (在同一设置中,如果遇到错误情况,跳过通常在循环结束时正常执行的代码也很有用)
因此了解其用途。您可以随意使用标志,但可以在许多设置中找到goto
清洁器。
有了它,您可以将其与以下内容完全合并(暂时忽略outfile
):
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstdlib>
using namespace std;
void draw_rect (char out_char, int rows, int columns); // Draws a rectangle shape
void draw_square (char out_char, int rows); //Draws a square shape
void draw_triangle (char out_char, int rows);// Draws a triangle shape
void draw_diamond (char out_char, int rows); // Draws a diamond shape
int main (int argc, char **argv) { /* don't hardcode filenames */
ifstream infile; /* infile and buffer are the only variables */
string buffer; /* that need to be scoped at main() */
if (argc < 2) { /* validate at least 1 argument is provided */
cerr << "error: insufficient input.\n"
"usage: " << argv[0] << " filename.\n";
return 1;
}
infile.open (argv[1]); /* open filename provided as 1st argument */
if(!infile.good()) { /* validate file is open for reading */
cerr << "failed to open infile\n";
return 1;
}
while (getline(infile, buffer)) { /* loop reading each line */
int row, col; /* remaining variables scoped inside */
string value, code; /* your read loop, use strings */
stringstream ss(buffer); /* create stringstream from buffer */
if (!(ss >> code)) { /* validate code read into string */
cerr << "error: ss >> code.\n";
break;
}
switch (code[0]) /* switch on 1st char of code */
{
case 'R':
if ((ss >> value >> row >> col)) /* validate read */
draw_rect (value[0], row, col); /* draw rect */
else /* or handle error */
cerr << "error: 'R' invalid format '" << buffer << "'\n'";
break;
case 'T':
if ((ss >> value >> row)) /* ditto for rest of shapes */
draw_triangle(value[0], row);
else
cerr << "error: 'T' invalid format '" << buffer << "'\n'";
break;
case 'D':
if ((ss >> value >> row))
draw_diamond(value[0], row);
else
cerr << "error: 'D' invalid format '" << buffer << "'\n'";
break;
case 'S':
if ((ss >> value >> row))
draw_square(value[0], row);
else
cerr << "error: 'S' invalid format '" << buffer << "'\n'";
break;
case 'E':
cout << "Exiting\n";
goto exitE; /* goto to break nested loops / scopes */
break;
default:
cout << "Invalid input, try again" << endl;
}
}
exitE:; /* the lowly goto provides a simple exit */
infile.close();
return 0;
}
void draw_diamond (char out_char, int rows)
{
int space = 1;
space = rows - 1;
for (int i = 1; i <= rows; i++)
{
for (int k = 1; k <= space; k++)
{
cout << " ";
}
space--;
for( int k = 1; k <= 2*i-1; k++)
{
cout << out_char;
}
cout << endl;
}
space = 1;
for (int i = 1; i <= rows; i++)
{
for(int k = 1; k <= space; k++)
{
cout << " ";
}
space++;
for(int k = 1; k <= 2*(rows-i)-1; k++)
{
cout << out_char;
}
cout << endl;
}
}
void draw_triangle (char out_char, int rows)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j <= i; j++)
{
cout << out_char;
}
cout << endl;
}
}
void draw_square (char out_char, int rows)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < rows; j++)
{
cout << out_char;
}
cout << endl;
}
}
void draw_rect (char out_char, int rows, int columns)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
cout << out_char;
}
cout << endl;
}
}
(注意:循环变量i, j
修复了draw_square
和draw_rect
,它们似乎是复制/粘贴错误-除此之外,没有任何更改制作成您的形状函数)
使用/输出示例
$ ./bin/drawshapes dat/drawshapes.txt
&
&&
&&&
&&&&
@@@@@@
@@@@@@
@@@@@@
@@@@@@
@@@@@@
@@@@@@
x
xx
xxx
xxxx
xxxxx
*******
*******
*******
*******
*******
$
$$$
$$$$$
$$$$$$$
$$$$$$$$$
$$$$$$$$$$$
$$$$$$$$$$$$$
$$$$$$$$$$$
$$$$$$$$$
$$$$$$$
$$$$$
$$$
$
+
+++
+++++
+++++++
+++++++++
+++++++
+++++
+++
+
===
===
===
===
Exiting
仔细检查一下,如果还有其他问题,请告诉我。