Advertisement
Guest User

Untitled

a guest
Nov 12th, 2017
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  1. #include "picture.h"
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5. #include <conio.h>
  6.  
  7. using namespace std;
  8.  
  9. class picture
  10. {
  11. public:
  12. picture(void);
  13. ~picture(void);
  14. friend ostream & operator<<(ostream&, const picture *pic);
  15.  
  16. private:
  17. int sx, sy, color, pixels; // rozmiary w pikselach poziom i pion, kolor ( tylko jedna instancja, nie ma aż takich strat pamięci ), liczba pokolorowanych pikseli
  18. int *dots; // wskaźnik na tablicę pikseli
  19. //tablica ta zawiera w sobie numery pozycji pokolorwanych pikseli, licząc od lewego górnego rogu idąc jak normalnie w prawo i w dół
  20.  
  21. };
  22.  
  23. picture::picture(void)
  24. {
  25. string filename;
  26. cout << "Podaj nazwe pliku z obrazkiem " << endl;
  27. getline(cin, filename);
  28. fstream pfile;
  29. pfile.open(filename, ios::in);
  30. while (pfile.good() != true)
  31. {
  32. cout << "Podano bledna nazwe pliku. Prosze podac poprawna nazwe pliku " << endl;
  33. getline(cin, filename);
  34. pfile.open(filename, ios::in);
  35. }
  36. // otwarcie pliku plik pfile
  37. char character;
  38. pfile.get(character);
  39. this->sx = 0;
  40. this->sy = 0;
  41. int white = 0;
  42. int black = 0;
  43. int linel = 0;
  44. while (!(pfile.eof()))
  45. {
  46. if (character != 10)
  47. {
  48. if (character == 32 || character == 9)white++;
  49. else black++;
  50. linel++;
  51. }
  52. else
  53. {
  54. if (linel > this->sx) this->sx = linel;
  55. this->sy = this->sy + 1;
  56. linel = 0;
  57. }
  58. pfile.get(character);
  59. }
  60. pfile.close();
  61. pfile.open(filename, ios::in);
  62. if (white > black) this->color = 0; // 0 jesli bialy wystepuje czesciej niz czary, to czarny jest tuszem
  63. else this->color = 1; // w przeciwnym wypadku bialy tusz, a wartosc 1
  64. int position = 1;
  65. int line = 1;
  66. int i = 0;
  67. if (this->color == 0)
  68. {
  69. this->dots = new int(black);
  70. this->pixels = black;
  71. while (!(pfile.eof()))
  72. {
  73. pfile.get(character);
  74. if (character == 10) // znak nowej linii
  75. {
  76. line++;
  77. position = (line * (this->sx)) + 1;
  78. }
  79. else if (character == 32 || character == 9) //spacja lub tabulator
  80. {
  81. position++;
  82.  
  83. }
  84. else // inny zapisany w pliku tekstowym znak
  85. {
  86. (this->dots)[i] = position;
  87. i++;
  88. position++;
  89. }
  90. }
  91. }
  92.  
  93. if (this->color == 1)
  94. {
  95. this->dots = new int(white);
  96. this->pixels = white;
  97. while (!(pfile.eof()))
  98. {
  99. pfile.get(character);
  100. if (character == 10) // znak nowej linii
  101. {
  102. line++;
  103. position = (line * (this->sx)) + 1;
  104. }
  105. else if (character == 32 || character == 9) //spacja lub tabulator
  106. {
  107. (this->dots)[i] = position;
  108. i++;
  109. position++;
  110. }
  111. else // inny zapisany w pliku tekstowym znak
  112. {
  113. position++;
  114. }
  115. }
  116. }
  117.  
  118. }
  119.  
  120. picture::~picture(void)
  121. {
  122. delete [] this->dots;
  123. }
  124.  
  125. ostream & operator<<(ostream&, const picture *pic)
  126. {
  127. cout << '\n';
  128. char dot, background;
  129. if (pic->color == 0)
  130. {
  131. background = ' ';
  132. dot = '#';
  133. }
  134. else
  135. {
  136. background = '#';
  137. dot = ' ';
  138. }
  139. int character = 0;
  140. int x, y;
  141. for (y = 0; y <= pic->sy; y++)
  142. {
  143. for (x = 1; x <= pic->sx; x++)
  144. {
  145. if (pic->dots[character] == (((pic->sx)*y) + x))
  146. {
  147. cout << dot;
  148. character++;
  149. }
  150. else cout << background;
  151. }
  152. cout << '\n';
  153. }
  154. return;
  155. }
  156.  
  157. void main()
  158. {
  159. cout << "Uruchomiony\n";
  160. picture testowy;
  161. cout << testowy;
  162.  
  163.  
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement