-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathasst5.cpp
More file actions
127 lines (102 loc) · 1.83 KB
/
asst5.cpp
File metadata and controls
127 lines (102 loc) · 1.83 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <iostream>
#include <stdio.h>
#include <fstream>
#include <cstdlib>
#include <string.h>
#include <ctype.h>
using namespace std ;
int main(int argc, char* argv[])
{
int count[26];
int lines = atoi(getenv("LINES")) ;
int cols = atoi(getenv("COLUMNS")) ;
int greatFreq = 0;
int plotAmt = 0;
int countAmt = 0;
FILE *fp = fopen(argv[1],"r");
if (fp == NULL)
{
cout << "empty file \n";
return 0;
}
for (int i = 0; i < 26; ++i)
{
count[i] = 0;
}
while (!feof(fp))
{
int z = fgetc(fp);
if (isalpha(z))
{
if (isupper(z))
{
tolower(z);
}
count[z - 'a']++;
}
}
//horizontal histogram
for (int i =0; i < 26; ++i)
{
cout << "count[" << (char) (i + (int)'a') << "]" << " ";
cout << count[i] << endl;
if (count[i] > greatFreq)
{
greatFreq = count[i];
}
}
cout << endl;
for (int i = 0; i < 26; ++i)
{
countAmt = count[i];
plotAmt = ((cols - 11) * countAmt) / greatFreq;
cout << "count[" << (char) (i + (int)'a') << "]" << " | ";
for (int j = 0; j < plotAmt; ++j)
{
cout << "=";
}
cout << endl;
}
cout << endl;
//vertical histogram
for (int i = 0; i < (lines - 2); ++i)
{
int y = (lines - (lines / 6));
if (i == y + 1)
{
for (int k = 0; k < 52; ++k)
{
cout << "=";
}
cout << endl;
}
if (i > (y+1) && i <= (y+2))
{
for (int k = 0; k < 26; ++k)
{
cout << (char) (k+97);
cout << " ";
}
cout << endl;
}
if (i < y)
{
for (int j = 0; j < 26; ++j)
{
countAmt = count[j];
plotAmt = (y * countAmt) / greatFreq;
if ( (plotAmt / (y - i)) >= 1)
{
cout << "*";
}
else
{
cout << " ";
}
cout << " ";
}
}
cout << endl;
}
return 0;
}