Skip to content

Commit 84aaf3c

Browse files
committed
Add 2017 sep 1
1 parent 280593a commit 84aaf3c

1 file changed

Lines changed: 125 additions & 0 deletions

File tree

2017/2017_sep_1.c

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
#include <ctype.h>
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
#include <string.h>
5+
6+
#define N 21
7+
#define PROV_MEM(p) if (!p) printf("Neuspesna alokacija\n"), exit(1)
8+
9+
/*
10+
// Unos kada je unapred poznat broj reči
11+
char **unos(int *pm)
12+
{
13+
printf("Uneti broj reci: ");
14+
scanf("%d", pm);
15+
16+
char **mat = calloc(*pm, sizeof *mat); // Isto kao: sizeof(char*)
17+
PROV_MEM(mat);
18+
19+
for (int i = 0; i < *pm; i++) {
20+
mat[i] = calloc(N, sizeof **mat); // Isto kao: sizeof(char)
21+
PROV_MEM(mat[i]);
22+
23+
// Čitanje celog reda u string mat[i]
24+
// fgets(mat[i], N, stdin);
25+
// mat[i][strlen(mat[i])-1] = '\0'; // Izbacuje \n
26+
27+
// Čitanje celog reda u string mat[i]
28+
scanf("%[^\n]%*c", str); // %*c proguta \n
29+
}
30+
31+
return mat;
32+
}
33+
*/
34+
35+
/*
36+
// Dinamički unos bez skraćivanja
37+
char **unos(int *pm)
38+
{
39+
int vel = 16;
40+
char **mat = calloc(vel, sizeof *mat);
41+
PROV_MEM(mat);
42+
43+
printf("Uneti reci (prazan red za kraj):\n");
44+
for (int i = 0; 1; i++) {
45+
char str[N] = "";
46+
scanf("%[^\n]%*c", str);
47+
if (strlen(str) == 0)
48+
break;
49+
50+
if (i == vel) {
51+
mat = realloc(mat, ++vel * sizeof *mat);
52+
PROV_MEM(mat);
53+
}
54+
55+
mat[i] = calloc(N, sizeof **mat);
56+
strcpy(mat[i], str);
57+
}
58+
59+
*pm = vel;
60+
return mat;
61+
}
62+
*/
63+
64+
// Dinamički unos sa skraćivanjem
65+
char **unos(int *pm)
66+
{
67+
int vel = 16;
68+
char **mat = calloc(vel, sizeof *mat);
69+
PROV_MEM(mat);
70+
71+
printf("Uneti reci (prazan red za kraj):\n");
72+
int i;
73+
for (i = 0; 1; i++) {
74+
char str[N] = "";
75+
scanf("%[^\n]%*c", str);
76+
if (strlen(str) == 0)
77+
break;
78+
79+
if (i == vel) {
80+
vel *= 2;
81+
mat = realloc(mat, vel * sizeof *mat);
82+
PROV_MEM(mat);
83+
}
84+
85+
mat[i] = calloc(N, sizeof **mat);
86+
strcpy(mat[i], str);
87+
}
88+
89+
// i je stvarna veličina matrice
90+
vel = i;
91+
mat = realloc(mat, vel * sizeof *mat);
92+
// Ne mora da se proveri alokacija, jer skraćivanje uvek uspeva
93+
94+
*pm = vel;
95+
return mat;
96+
}
97+
98+
void obrada(char **mat, int m)
99+
{
100+
char br_poj[128] = { 0 };
101+
102+
for (int i = 0; i < m; i++)
103+
for (int j = 0; j < strlen(mat[i]); j++) {
104+
char c = tolower(mat[i][j]); // Mala i velika se gledaju isto
105+
br_poj[c]++;
106+
}
107+
108+
int maks = br_poj[1]; // Broj pojavljivanja \0 se ne gleda
109+
for (int i = 2; i < 128; i++)
110+
if (br_poj[i] > maks)
111+
maks = br_poj[i];
112+
113+
printf("Slovo/a sa najvise pojavljivanja:\n");
114+
for (int i = 1; i < 128; i++)
115+
if (br_poj[i] == maks)
116+
printf("%c ", i);
117+
}
118+
119+
int main(void)
120+
{
121+
int m;
122+
char **mat = unos(&m);
123+
obrada(mat, m);
124+
return 0;
125+
}

0 commit comments

Comments
 (0)