-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTable.cpp
More file actions
103 lines (94 loc) · 1.56 KB
/
Copy pathTable.cpp
File metadata and controls
103 lines (94 loc) · 1.56 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
#define _CRT_SECURE_NO_WARNINGS
#include "Table.h"
#include<stdlib.h>
#include<stdio.h>
#define SIZE 256
#define MSG 1000
Table * inizialise()
{
Table *table = (Table*)calloc(SIZE, sizeof(Table));
for (int i = 0; i < SIZE; i++)
{
table[i].letter = i;
table[i].count = 0;
}
return table;
}
void setTable(Table *table, char *file)
{
FILE *stream = fopen(file, "r");
char msg[MSG];
for (int i = 0; i < MSG; i++)
{
msg[i] = fgetc(stream);
}
for (int i = 0; i < MSG; i++)
{
for (int j = 0; j < SIZE; j++)
{
if (table[j].letter == msg[i])
{
table[j].count++;
}
}
}
fclose(stream);
}
void print(Table *t)
{
for (int i = 0; i < SIZE; i++)
{
printf(" %c , -> %d\n", t[i].letter, t[i].count);
}
}
void printOnSize(Table *t, int size)
{
for (int i = 0; i < size; i++)
{
printf(" %c , -> %d\n", t[i].letter, t[i].count);
}
}
int sizeTable(Table * t)
{
int sizeTable = 0;
for (int i = 0; i < SIZE; i++)
{
if (t[i].count != 0)
{
sizeTable++;
}
}
return sizeTable;
}
Table * clearTable(Table *t)
{
int sizeNewTable = sizeTable(t);
//printf("new suize %d \n", sizeNewTable);
Table * table = (Table*)calloc(sizeNewTable, sizeof(Table));
int j = 0;
for (int i = 0; i < SIZE; i++)
{
if (t[i].count != 0)
{
table[j].letter= t[i].letter;
table[j].count = t[i].count;
j++;
}
}
return table;
}
void sort(Table *t, int size)
{
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size - 1; j++)
{
if (t[j].count > t[j + 1].count)
{
Table temp = t[j];
t[j] = t[j + 1];
t[j + 1] = temp;
}
}
}
}