NoMatchFound

Untitled

Feb 25th, 2012
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.26 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #define N 100
  4.  
  5. using namespace std;
  6.  
  7. typedef struct myrec { // record definition
  8.     int item;
  9.     int cnt;
  10. } rec;
  11.  
  12. typedef rec hist[N]; // array of records definition
  13.  
  14. // prototypes
  15. void loadHistogram(hist, int&);
  16. void printHistogram(hist, int);
  17. void printHistToFile(hist, int);
  18. int exist(hist, int, int);
  19.  
  20. int main() {
  21.     int cap;
  22.     hist h;
  23.  
  24.     loadHistogram(h, cap);
  25.     cout << "Print: \n\n";
  26.     printHistogram(h, cap);
  27.  
  28.     printHistToFile(h, cap);
  29.  
  30.     return 0;
  31. }
  32.  
  33. // returns -1 if the element 'element' doesn't exist in the array
  34. // otherwise it returns the position of the element
  35. int exist(hist h, int cap, int element) {
  36.     int i = 0;
  37.     while ((h[i].item != element) && (i < cap))
  38.         i++;
  39.  
  40.     if (i == cap)
  41.         return -1;
  42.     else
  43.         return i;
  44. }
  45.  
  46. void loadHistogram(hist h, int &cap) {
  47.     fstream fin;
  48.     fin.open("fin.txt", ios::in);
  49.  
  50.     if (!fin.is_open()) {
  51.         cout << "File not found...\n";
  52.     } else {
  53.         int newcap = 0; // number of the item without duplicates
  54.         while (!fin.eof()) {
  55.             int e;
  56.             fin >> e; // read an element from file
  57.  
  58.             if (exist(h, newcap, e) == -1) { // if not exists, it is a new item
  59.                 h[newcap].item = e; // save the new item in a new position
  60.                 h[newcap].cnt = 1; // set to 1 the counter
  61.                 newcap++;
  62.             } else // otherwise, if it exists, so just increase the counter
  63.                 h[exist(h, newcap, e)].cnt++;
  64.         }
  65.  
  66.         // set the array cap with the real number of items (without duplicates)
  67.         cap = newcap;
  68.     }
  69.  
  70.     fin.close();
  71. }
  72.  
  73. // print the array of structures on the video stream
  74. void printHistogram(hist h, int cap) {
  75.     for (int i = 0; i < cap; i++) {
  76.         cout << h[i].item << "\t\t" << h[i].cnt << " ";
  77.  
  78.         for (int j = 0; j < h[i].cnt; j++)
  79.             cout << "o";
  80.         cout << endl;
  81.     }
  82. }
  83.  
  84. // print the array of structures on the file stream
  85. void printHistToFile(hist h, int cap) {
  86.     fstream fout;
  87.     fout.open("fout.txt", ios::out);
  88.  
  89.     for (int i = 0; i < cap; i++) {
  90.         fout << h[i].item << "\n";
  91.         fout << h[i].cnt << "\n";
  92.     }
  93.  
  94.     fout.close();
  95. }
Advertisement
Add Comment
Please, Sign In to add comment