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.
代码
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 infected. 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 " "; } */ |

关注我的微信,获取文章更新
如果你觉得这篇文章对你有用,可以点击下面的“赞助作者”打赏作者!
转载注明原文出处:王柏元的博客>>https://wangbaiyuan.cn/data-structure-infectious-diseases.html
可以评论了