-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyUtil.cpp
103 lines (81 loc) · 2.3 KB
/
MyUtil.cpp
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
#include "MyUtil.h"
void printIntVet(int *vet, int n){
for(int i = 0; i<n; i++){
printf("%d ", vet[i]);
}
ln();
}
void cls(int time) {
//sleep(time);
#ifdef _WIN32 // For Windows
system("cls");
#else // For UNIX-like systems (Linux, macOS, etc.)
system("clear");
#endif
}
void ln() {
printf("\n");
}
int randRange(int min, int max) {
return (rand() % (max - min + 1)) + min;
}
int* randIntVet(int max, int min, int n){
int *vet = (int*)malloc(n * sizeof(int));
for(int i = 0; i < n; i++){
vet[i] = randRange(min, max);
}
return vet;
}
int ask2op(const char* question) {
printf("%s (y/n): ", question);
char response;
do {
scanf(" %c", &response);
if (response == 'y' || response == 'Y') {
return 1; // Yes
} else if (response == 'n' || response == 'N') {
return 0; // No
} else {
printf("Invalid input. Please enter 'y' for Yes or 'n' for No: ");
}
} while (1); // Keep asking until a valid response is entered
}
/**!Pace o str como referencia ou seja, passe o seu indereço Ex.: &minhaStr*/
void capStr(char** str) {
if (str == NULL || *str == NULL) {
return; // Handle NULL string pointer or empty string
}
for (int i = 0; (*str)[i] != '\0'; i++) {
(*str)[i] = toupper((*str)[i]);
}
}
void fillFileWithRandomIntegers(const char* filename, int min, int max, int size) {
FILE* file = fopen(filename, "w");
if (!file) {
printf("Failed to open the file.\n");
return;
}
int* vector = randIntVet(max, min, size);
if (!vector) {
fclose(file);
return;
}
for (int i = 0; i < size; i++) {
fprintf(file, "%d ", vector[i]); // Write the integer to the file
}
fclose(file);
free(vector);
}
int* loadIntegersFromFile(const char* filename, int* size, int maxSize){
FILE* fp = fopen(filename, "r");
if(!fp) exit(0);
int *inp = (int*)malloc(sizeof(int)*maxSize);
int num, i = 0;
while(fscanf(fp, "%d ", &num) == 1){
if(i >= maxSize) break;
inp[i++] = num;
}
fclose(fp);
*size = i;
return inp;
}