Advertisement
_br0uh_

this code does not work

Oct 30th, 2022
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.10 KB | None | 0 0
  1. main.cpp
  2. #include "Image.h"
  3. #include "image.cpp"
  4. #include <cstring>
  5. #include "SandPile.h"
  6. #include "SandPile.cpp"
  7.  
  8. struct Arguments{
  9.   int length;
  10.   int width;
  11.   char *input_filepath;
  12.   char *output_filepath;
  13.   uint32_t iterations = 0;
  14.   uint32_t frequency;
  15. };
  16.  
  17. void Output(int length, int width, SandPile ModelOutput){
  18.     Image image(width, length);
  19.     std::cout << ModelOutput.sanding.size() << std::endl;
  20.     for(int i = 0; i < length; i++){
  21.         for (int j = 0; j < width; j++){
  22.             if (ModelOutput.sanding[i][j] == 0){
  23.                 image.SetColor(Color(255, 255, 255), i, j);
  24.             }
  25.             if (ModelOutput.sanding[i][j] == 1){
  26.                 image.SetColor(Color(0, 255, 0), i, j);
  27.             }
  28.             if (ModelOutput.sanding[i][j] == 2){
  29.                 image.SetColor(Color(255, 0, 255), i, j);
  30.             }
  31.             if (ModelOutput.sanding[i][j] == 3){
  32.                 image.SetColor(Color(255, 255, 0), i, j);
  33.             }
  34.             if (ModelOutput.sanding[i][j] > 3){
  35.                 image.SetColor(Color(0, 0, 0), i, j);
  36.             }
  37.         }
  38.  
  39.     }
  40.     image.Export("image.bmp");
  41.     std::cout<<"file created\n";
  42. }
  43.  
  44. void TSVParse(char* path, SandPile& SandCoords){
  45.     std::ifstream infile(path);
  46.     int buf;
  47.     int iter = 0;
  48.     int x;
  49.     int y;
  50.     int val;
  51.     while(infile >> buf) {
  52.         if (iter % 3 == 0) {
  53.             x = buf;
  54.         }
  55.         else if (iter % 3 == 1) {
  56.             y = buf;
  57.         }
  58.         else {
  59.             val = buf;
  60.             SandCoords.sanding[x][y] = val;
  61.         }
  62.         iter++;
  63.     }
  64. }
  65.  
  66. int main(int argc, char* argv[]) {
  67.     Arguments args;
  68.     for (int k = 1; k < argc; k++) {
  69.         std::string opts = argv[k];
  70.         if (strcmp(argv[k], "-l") == 0 || strcmp(argv[k], "--length") == 0) {
  71.             args.length = std::stoi(argv[k + 1]);
  72.         }
  73.         if (strcmp(argv[k], "-w") == 0 || strcmp(argv[k], "--width") == 0) {
  74.             args.width = std::stoi(argv[k + 1]);
  75.         }
  76.         if (strcmp(argv[k], "-i") == 0 || strcmp(argv[k], "--input") == 0) {
  77.             args.input_filepath = argv[k + 1];
  78.  
  79.         }
  80.         if (strcmp(argv[k], "-o") == 0 || strcmp(argv[k], "--output") == 0) {
  81.             args.output_filepath = argv[k + 1];
  82.         }
  83.         if (strcmp(argv[k], "-m") == 0 || strcmp(argv[k], "--max-iter") == 0) {
  84.             args.iterations = std::stoi(argv[k + 1]);
  85.         }
  86.         if (strcmp(argv[k], "-f") == 0 || strcmp(argv[k], "--freq") == 0) {
  87.             args.frequency = std::stoi(argv[k + 1]);
  88.         }
  89.     }
  90.     SandPile Making_Scenes(args.length, args. width);
  91.     TSVParse(args.input_filepath, Making_Scenes);
  92.     for(int i = 0; i < args.iterations; i++){
  93.         Making_Scenes.Piling();
  94.         Output(args.length, args.width, Making_Scenes);
  95.     };
  96.  
  97. }
  98.  
  99. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  100. SandPile.h
  101. #include <iostream>
  102. #include <vector>
  103. #include <string>
  104. #include <fstream>
  105. #ifndef LABWORK3__SANDPILE_H_
  106. #define LABWORK3__SANDPILE_H_
  107.  
  108. class SandPile {
  109.  
  110.   int length_sanding;
  111.   int width_sanding;
  112.  public:
  113.     std::vector<std::vector<int>> sanding;
  114.     SandPile();
  115.     SandPile(int length_sanding, int width_sanding);
  116.     ~SandPile();
  117.   friend void TSVParse(char* path, SandPile& SandCoords);
  118.     void Piling();
  119.     void PrintPile();
  120.  
  121. };
  122. #endif //LABWORK3__SANDPILE_H_
  123. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  124. Sandpile.cpp
  125.  
  126.  
  127. #include "SandPile.h"
  128.  
  129. SandPile::SandPile() {
  130.  
  131.  
  132. }
  133. SandPile::SandPile(int length_sanding, int width_sanding) {
  134.     std::vector<std::vector<int>> sanding;
  135.     this->length_sanding= length_sanding;
  136.     this-> width_sanding = width_sanding;
  137.     sanding.resize(length_sanding);
  138.     for(int i = 0; i < length_sanding; ++i) {
  139.         sanding[i].resize(width_sanding);
  140.     }
  141.     for (int  i = 0; i < sanding.size(); i++){
  142.         for(int j = 0; j < sanding[i].size(); j++){
  143.             int a(0);
  144.             sanding[i][j] = a;
  145.         }
  146.     }
  147. }
  148. SandPile::~SandPile() {}
  149. void SandPile::Piling() {
  150.     for (int i = 0; i < sanding.size(); i++) {
  151.         for (int j = 0; j < sanding[i].size(); j++) {
  152.             if (sanding[i][j] > 3) {
  153.                 if (i + 1 < sanding.size()) {
  154.                     sanding[i + 1][j] += 1;
  155.                 }
  156.  
  157.                 if (j + 1 < sanding[i].size()) {
  158.                     sanding[i][j + 1] += 1;
  159.                 }
  160.  
  161.                 if (i + 1 > 0) {
  162.                     sanding[i - 1][j] += 1;
  163.                 }
  164.  
  165.                 if (i - 1 > 0) {
  166.                     sanding[i + 1][j] += 1;
  167.                 }
  168.  
  169.                 sanding[i][j] -= 4;
  170.                 }
  171.  
  172.             }
  173.  
  174.     }
  175.  
  176.  
  177. }
  178. void SandPile::PrintPile() {
  179.     for(int i = 0; i < sanding.size(); i++){
  180.         std::cout << "\n";
  181.         for (int j = 0; j < sanding[i].size(); j++){
  182.             std::cout << sanding[i][j] << " ";
  183.         }
  184.  
  185.     }
  186.  
  187. }
  188.  
  189.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement