Advertisement
Guest User

Untitled

a guest
Nov 13th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 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 &out, 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. picture* next;
  21. };
  22.  
  23. picture::picture(void)
  24. {
  25. string filename;
  26. fstream pfile;
  27.  
  28. {
  29. cout << "Podaj nazwe pliku z obrazkiem " << endl;
  30. getline(cin, filename);
  31. pfile.open(filename, ios::in);
  32. while (pfile.good() != true)
  33. {
  34. cout << "Podano bledna nazwe pliku. Prosze podac poprawna nazwe pliku " << endl;
  35. getline(cin, filename);
  36. pfile.open(filename, ios::in);
  37. }
  38. }
  39.  
  40. // otwarcie pliku plik pfile
  41. char character;
  42. this->sx = 0;
  43. this->sy = 0;
  44. int white = 0;
  45. int black = 0;
  46. int linel = 0;
  47. while (!(pfile.eof()))
  48. {
  49. pfile.get(character);
  50. if (character != 10)
  51. {
  52. if (character == 32 || character == 9)white++;
  53. else black++;
  54. linel++;
  55. }
  56. else
  57. {
  58. if (linel > this->sx) this->sx = linel;
  59. this->sy = this->sy + 1;
  60. linel = 0;
  61. }
  62. }
  63. // pętla sprawdzania pliku - kolor dominujący, wysokość i szerokość
  64. {
  65. pfile.close();
  66. pfile.open(filename, ios::in);
  67. }
  68. //reset pliku - powrót na początek zamknięciem i otworzeniem
  69. if (white > black) this->color = 0; // 0 jesli bialy wystepuje czesciej niz czary, to czarny jest tuszem
  70. else this->color = 1; // w przeciwnym wypadku bialy tusz, a wartosc 1
  71. int positionor = 1;
  72. int lineor = 0;
  73. int ior = 0;
  74. int *position;
  75. int *line;
  76. int *i;
  77. position = &positionor;
  78. line = &lineor;
  79. i = &ior;
  80. if (this->color == 0)
  81. {
  82. this->dots = new int(black);
  83. this->pixels = black;
  84. cout << this->sx << ' ' << this->sy << ' ' << this->pixels << ' ' << this->color << '\n';
  85. while (!(pfile.eof()))
  86. {
  87. pfile.get(character);
  88. if (character == '\n')
  89. {
  90. *line = *line + 1;
  91. *position = *line * this->sx + 1;
  92. }
  93. else if (character == ' ' || character == ' ')
  94. {
  95. (*position)++;
  96. }
  97. else
  98. {
  99. this->dots[(*i)] = *position;
  100. (*i)++;
  101. (*position)++;
  102. }
  103. }
  104. }
  105.  
  106. else if (this->color == 1)
  107. {
  108. this->dots = new int(white);
  109. this->pixels = white;
  110. cout << this->sx << ' ' << this->sy << ' ' << this->pixels << ' ' << this->color << '\n';
  111. while (!(pfile.eof()))
  112. {
  113. pfile.get(character);
  114. if (character == '\n')
  115. {
  116. *line = *line + 1;
  117. *position = *line * this->sx + 1;
  118. }
  119. else if (character == ' ' || character == ' ')
  120. {
  121. this->dots[(*i)] = *position;
  122. (*i)++;
  123. (*position)++;
  124. }
  125. else
  126. {
  127. (*position)++;
  128. }
  129. }
  130. }
  131.  
  132. }
  133.  
  134. picture::~picture(void)
  135. {
  136. delete this->dots;
  137. }
  138.  
  139. ostream & operator<<(ostream &out, const picture & pic)
  140. {
  141. char background;
  142. char dot;
  143. if (pic.color == 0)
  144. {
  145. background = ' ';
  146. dot = '#';
  147. }
  148. else
  149. {
  150. background = '#';
  151. dot = ' ';
  152. }
  153. int x, y, i, position;
  154. i = 0;
  155. for (y = 0; y <= pic.sy; y++)
  156. {
  157. for (x = 1; x <= pic.sx; x++)
  158. {
  159. position = (y * (pic.sx) + x);
  160. if (pic.dots[i] == position)
  161. {
  162. out << dot;
  163. i++;
  164. }
  165. else out << background;
  166. }
  167. out << '\n';
  168. }
  169. return out;
  170. }
  171.  
  172. void main()
  173. {
  174. cout << "Uruchomiony\n";
  175. picture testowy;
  176. cout << testowy;
  177. int j = 5;
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement