Description
This assignment asks you to finish the implementation of a program that assesses the level of infection in a tissue sample. You are given data representing a rectangular tissue sample, overlaid with a grid. Certain portions of the tissue are infected; others are not. Your goal is to help assess the extent of the infection by writing a program that, given the coordinates of a colony of infection, can determine its size. A typical use of the program follows. The user interacts with the program only through command-line arguments. The user supplies to the program a data filename and the coordinates of a cell in the grid. The coordinates are specified by row and then column, both starting at zero. The program calculates the extent of infection at that coordinate and outputs a two-dimensional representation of the tissue sample. Figure 1 depicts the execution of the program.
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
#include <iostream> #include <fstream> Using namespace std; #include "grid.h" // You do not need to alter function indexof. Int grid::indexof (int row, int col) const { Return row*cols+col; } // You do not need to alter function infection. Bool grid::infected(int row, int col) const { Return (area->operator[](indexof(row, col)) == INFECTED); } // You may need to alter the constructor Grid::grid (string file) { Ifstream grid_file; Grid_file.open (file.c_str()); Grid_file >> rows; Grid_file >> cols; Area = new vector<bool>(rows*cols, NOT_INFECTED); Note = new vector<string>(rows*cols, "0 "); While (true) { Int blob_row; Int blob_col; Grid_file >> blob_row; Grid_file >> blob_col; If (grid_file.eof()) { Break; } Area->operator[](indexof(blob_row,blob_col)) = INFECTED; Note->operator[](indexof(blob_row,blob_col)) = "1 "; } Grid_file.close(); } // You may need to alter the destructor Grid::~grid () { Delete area; Delete note; } // You will need to alter this function to display the // plus signs (+) next to the cells that belong to // a counted colony. Ostream &operator<<(ostream &stream, const grid& ob) { For (int row=0; row < ob.rows; row++) { For (int col=0; col < ob.cols; col++) { //stream << ob.area->operator[](ob.indexof(row, col)); Stream << ob.note->operator[](ob.indexof(row, col)); } Stream << endl; } Stream << endl; Return stream; } // Replace the return statement in this function with your // recursive implementation of this method */ Int grid::count (int row, int col) { If(0<=row&&row < rows&&0<=col&&col < cols) { If(infected( row, col)&¬e->operator[](indexof(row,col)) =="1 ") { Note->operator[](indexof( row,col)) ="1+ "; Return 1+count (row+1, col+1)+count (row+1, col)+ Count (row+1, col-1)+count (row, col-1)+ Count (row , col+1)+count (row-1, col+1)+ Count (row-1, col )+count (row-1, col-1); } Else Return 0; } Else return 0; } /*string grid::noteplus(int row,int col) const{ If(note->operator[](indexof(row, col)) == INFECTED){ Return "+ "; } Else Return " "; } */ |
This article has been printed on copyright and is protected by copyright law. It must not be reproduced without permission.If you need to reprint, please contact the author or visit the copyright to obtain the authorization. If you feel that this article is useful to you, you can click on the "sponsored author" below to reward the author!
Reprinted Note Source: Baiyuan's Blog>>https://wangbaiyuan.cn/en/data-structure-infectious-diseases-2.html
No Comment