Background: The first project is designed to demonstrate the following areas of
ID: 670385 • Letter: B
Question
Background: The first project is designed to demonstrate the following areas of knowledge within File access > Multi-dimensional vectors Function design > Flow control > Decision making The difficult part is intended to be the input and to correctly draw the resulting vector Basics: The first thing the program will do is ask the user for a file name to read in as input. The program will then read in this file, and parse the commands from it. The program will, based on those commands, generate a two-dimensional vector of characters. The program will then output that vector to the console, and then repeat so long as there are more commands left in the input file. Input Format: Input to the program will consist of raw text in a series of words, numbers, or symbols. The commands used will be: # rows-Generate the two-dimensional vector with the specified number of rows (note that "#" will be replaced with an integer, 1-9) # columns-Generate the two-dimensional vector with the specified number of columns (note that"" will be replaced with an integer 1-9) all c- Use the specified character ("c" will be replaced with a non-space,Explanation / Answer
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void printTriangle(int rows, char ch = '*', bool alpha = false, bool outer = false){
if (alpha)
ch = 'A';
for (int i = 0; i < rows; ++i){
for (int j = 0; j < rows; ++j){
if (outer){
if (i == 0 || i == rows - 1 || j == 0 || j == rows - 1 || i == j){
if (i >= j){
cout << ch << " ";
if (alpha)
ch++;
}
}
else{
cout << " ";
}
}
else{
if (i >= j){
cout << ch << " ";
if (alpha)
ch++;
}
}
}
cout << endl;
}
}
void printGrid(int rows, int cols, char ch = '*', bool alpha = false, bool outer = false){
if (alpha)
ch = 'A';
for (int i = 0; i < rows; ++i){
for (int j = 0; j < cols; ++j){
if (outer){
if (i == 0 || i == rows - 1 || j == 0 || j == cols - 1){
cout << ch << " ";
if (alpha)
ch++;
}
else{
cout << " ";
}
}
else{
cout << ch << " ";
if (alpha)
ch++;
}
}
cout << endl;
}
}
void setWords(vector<string> &words, string line){
words.clear();
string temp = "";
for (int i = 0; i < line.size(); ++i){
if (line[i] == ' ' || line[i] == ' ' || i == line.size() - 1){
if (temp != "")
words.push_back(temp);
temp = "";
}
else{
temp += line[i];
}
}
}
int getIndex(vector<string> &words, string str){
for (int i = 0; i < words.size(); ++i){
if (words[i] == str){
return i;
}
}
return -1;
}
int main(){
string fileName, line;
ifstream in;
cout << "Enter file name: ";
cin >> fileName;
in.open(fileName.c_str());
vector<string> words;
if (in.is_open()){
while (getline(in, line)){
setWords(words, line);
char ch = '*';
bool alpha = false, outer = false;
int rows = 0, cols = 0;
int ind = getIndex(words, "all");
if (ind != -1){
ch = words[ind + 1][0];
}
ind = getIndex(words, "rows");
if (ind != -1){
rows = atoi(words[ind - 1].c_str());
}
ind = getIndex(words, "columns");
if (ind != -1){
cols = atoi(words[ind - 1].c_str());
}
if (getIndex(words, "alphabetical") != -1)
alpha = true;
if (getIndex(words, "outer") != -1)
outer = true;
if (getIndex(words, "triangular") != -1)
printTriangle(rows, ch, alpha, outer);
else
printGrid(rows, cols, ch, alpha, outer);
cout << endl << endl;
}
}
else{
cout << "Can not open " << fileName << endl;
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.