Commit 0fd01664 by trkall

sum stuff

parent a389045d
47
40
3
89
54
12
53
11
67
45
30
77
93
19
38
47
89
53
67
77
93
38
40
3
54
12
11
45
30
19
#include <stdio.h>
#include <stdlib.h>
int count = 0;
char *file[] = {"file1.txt", "file2.txt",
"res1.txt", "res2.txt"};
/* display the contents of the file */
int showData(char *filename) {
FILE *fp;
int val, i = 0;
fp = fopen(filename, "r");
while (fscanf(fp, "%d", &val) != EOF) {
printf("%4d ", val);
i++;
if (i && i % 8 == 0)
printf("\n");
}
return (i);
}
/*
* Write half of the data in input file to "file1.txt"
* and remaining half to "file2.txt"
*/
void splitData(char *filename) {
FILE *fp[3];
int i, val;
fp[2] = fopen(filename, "r");
if (!fp[2]) {
printf("splitData-fopen failed- %s\n", filename);
exit(0);
}
fp[0] = fopen(file[0], "w");
fp[1] = fopen(file[1], "w");
if (!fp[0] || !fp[1]) {
fcloseall();
printf("splitData - fopen failed\n");
exit(1);
}
for (i = 0; i < count/2; i++) {
fscanf(fp[2], "%d", &val);
fprintf(fp[0], "%d ", val);
}
for (i = count/2; i < count; i++) {
fscanf(fp[2], "%d", &val);
fprintf(fp[1], "%d ", val);
}
fcloseall();
return;
}
/* Perform External Sorting */
void sortData(char *filename) {
FILE *fp[4];
int i, flg1, flg2, count1, count2, val, val1, val2, n = 1;
while (1) {
for (i = 0; i < 4; i++) {
/* file[0] & file[1] are read only files */
if (i < 2) {
fp[i] = fopen(file[i], "r");
if (fp[i]) {
if (fscanf(fp[i], "%d", &val) == EOF)
goto out;
else
rewind(fp[i]);
}
} else {
/* file[2] & file[3] - open in write mode */
fp[i] = fopen(file[i], "w");
}
if (!fp[i]) {
fcloseall();
printf("sortData-fopen(%s) failed\n", file[i]);
exit(0);
}
}
i = 2;
flg1 = flg2 = 1;
count1 = count2 = 0;
for (;;) {
if (flg1) {
/*
* if file[0] reached EOF, then write the contents
* other file file[2] to the output file
*/
if (fscanf(fp[0], "%d", &val1) == EOF) {
while (fscanf(fp[1], "%d", &val2) != EOF) {
fprintf(fp[i], "%d ", val2);
}
break;
}
}
if (flg2) {
/*
* if file[1] reached EOF, then write the contents of
* file[0] to the output file
*/
if (fscanf(fp[1], "%d", &val2) == EOF) {
while (fscanf(fp[0], "%d", &val1) != EOF) {
fprintf(fp[i], "%d ", val1);
}
break;
}
}
/*
* if val1 from file[0] is greater than the value val2,
* then write val2 to output file. Otherwise, write val1
* to output file.
*/
if (val1 > val2) {
flg1 = 0;
flg2 = 1;
count2++;
fprintf(fp[i], "%d ", val2);
} else {
flg1 = 1;
flg2 = 0;
count1++;
fprintf(fp[i], "%d ", val1);
}
/*
* n is an incrementer. If count1 equals n, then write val2
* to output file, increment count2 (written value from file[1]
* to the output file). Value of i is changed inorder to
* use other file (file[1]/file[2]) for next iteration.
*/
if (count1 == n) {
fprintf(fp[i], "%d ", val2);
count2++;
while (count2 < n) {
if (fscanf(fp[1], "%d", &val2) != EOF)
fprintf(fp[i], "%d ", val2);
count2++;
}
flg1 = flg2 = 1;
count1 = count2 = 0;
i = (i == 2) ? 3 : 2;
}
if (count2 == n) {
fprintf(fp[i], "%d ", val1);
count1 = count1 + 1;
while (count1 < n) {
if (fscanf(fp[0], "%d", &val1) != EOF)
fprintf(fp[i], "%d ", val1);
count1++;
}
flg1 = flg2 = 1;
count1 = count2 = 0;
i = (i == 2) ? 3 : 2;
}
}
fcloseall();
unlink(file[0]);
unlink(file[1]);
/* move contents of file[2] to file[0] */
rename(file[2], file[0]);
/* move contents of file[3] to file[1] */
rename(file[3], file[1]);
n = n * 2;
}
out:
fcloseall();
unlink(filename);
/* move contents of file[0] to output file */
rename(file[0], filename);
unlink(file[1]);
}
int main(int argc, char **argv) {
printf("Data before sorting:\n");
count = showData(argv[1]);
printf("\nNo of elements in file: %d\n\n", count);
printf("Data after split operation:\n");
splitData(argv[1]);
printf("Data in file 1:\n");
showData(file[0]);
printf("\n");
printf("Data in file 2:\n");
showData(file[1]);
printf("\n");
sortData(argv[1]);
printf("\nAfter Sorting:\n");
showData(argv[1]);
printf("\n");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE *F0;
FILE *F1;
FILE *F2;
FILE *F3;
FILE *F4;
int *nr;
int i = 0;
int pikkus;
int lastfile;
F0 = fopen("F0.txt", "r");
while(!feof(F0))
{
fscanf(F0, "%d", (nr+i));
///printf("%d ", *(nr+i));
i++;
}
pikkus = i-1;
fclose(F0);
F1 = fopen("F1.txt", "w");
F2 = fopen("F2.txt", "w");
fprintf(F1, "%d\n", *(nr+0));
lastfile = 1;
for(i=1;i<pikkus;i++)
{
if(*(nr+i) > *(nr+(i-1)))
{
if(lastfile == 1)
{
fprintf(F1, "%d\n", *(nr+i));
} else
{
fprintf(F2, "%d\n", *(nr+i));
}
} else
{
if(lastfile == 2)
{
fprintf(F1, "%d\n", *(nr+i));
///lastfile = 2;
} else
{
fprintf(F2, "%d\n", *(nr+i));
///lastfile = 1;
}
}
}
fclose(F1);
fclose(F2);
return 0;
}
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
srand( (unsigned)time( NULL ) );
char name[60];
char* filename = name;
int amount, min, max, i;
FILE *F;
printf("Faili nimi:\n");
scanf("%s", filename);
printf("Mitu nr:\n");
scanf("%d", &amount);
printf("Min nr:\n");
scanf("%d", &min);
printf("Max nr:\n");
scanf("%d", &max);
strcat(filename, ".txt");
F = fopen(filename, "w");
for(i=0;i<amount;i++)
{
fprintf(F, "%d\n", rand() % max + min);
}
fclose(F);
return 0;
}
No preview for this file type
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct // https://www.tutorialspoint.com/cprogramming/c_structures.htm
{
......@@ -31,3 +33,27 @@ int chkFile(FILE *andmed)
}
return 1;
}
char *kontroll(char *filename)
{
printf("Sisestage sorteeritava faili nimi:\n");
scanf("%s", filename);
int pikkus;
int i;
int j = 4;
int cmp;
char str[5];
char str1[5] = ".txt";
pikkus = strlen(filename);
for(i=0;i<5;i++)
{
str[j] = *(filename+(pikkus-i));
j--;
}
cmp = strcmp(str1, str);
if (cmp != 0)
{
strcat(filename, str1);
}
return filename;
}
12.87 14.78
-45.78 48.14
-87.41 28.47
42.41 -9.47
-1.78 -4.78
17.11 -41.72
-88.11 58.41
42.41 9.26
5.88 4.18
-14.21 44.12
12.87 14.78
-45.78 48.14
-87.41 28.47
42.41 -9.47
-1.78 -4.78
17.11 -41.72
-88.11 58.41
42.41 9.26
5.88 4.18
-14.21 44.12
......@@ -16,9 +16,9 @@ int chkFile(FILE *andmed);
int main(void)
{
Co A[10];
Co B[10];
Co C[10];
Co A[MAX];
Co B[MAX];
Co C[MAX];
FILE *S;
int i = 0;
float *pindala;
......@@ -34,7 +34,7 @@ int main(void)
return 0;
}
int sisse(Co X[], int param)
int sisse(Co X[MAX], int param)
{
FILE *andmed;
int chk = 0;
......@@ -60,7 +60,7 @@ int sisse(Co X[], int param)
while(i<MAX)
{
fscanf(andmed, "%lf %lf", &X[i].x, &X[i].y);
printf("%lf\n", X[i].x);
printf("X:%lf\nY:%lf\n", X[i].x, X[i].y);
i++;
}
fclose(andmed);
......
#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
//#include <math.h>
typedef struct{
char *Ax;
char *Ay;
float Bx;
float By;
float Cx;
float Cy;
}Co;
int MAX = 10;
int main(void)
{
Co *tri;
FILE *A;
FILE *B;
FILE *C;
int i = 0;
A = fopen("aAndmed.txt", "r");
while(i<MAX)
{
fscanf(A, "%s %s", (tri+i)->Ax, (tri+i)->Ay);
printf("X%d:%s\nY%d:%s\n", i, *(tri+i)->Ax, i, *(tri+i)->Ay);
i++;
}
fclose(A);
i=0;
B = fopen("bAndmed.txt", "r");
while(i<MAX)
{
fscanf(B, "%lf%lf", &(tri+i)->Bx, &(tri+i)->By);
i++;
}
fclose(B);
i=0;
C = fopen("cAndmed.txt", "r");
while(i<MAX)
{
fscanf(C, "%lf%lf", &(tri+i)->Cx, &(tri+i)->Cy);
i++;
}
fclose(C);
i=0;
return 0;
}
///Kodut66 kirje
typedef struct // https://www.tutorialspoint.com/cprogramming/c_structures.htm
{
char Nimi[50];
char Liik[50];
int Vanus;
}Ryhm;
///faili lugemine kirjesse
void faili_lugemine(int pikkus, char *str, FILE *F2, Ryhm *n)
{
F2 = fopen("F2.txt", "a");
int i;
int compare;
for(i=0;i<pikkus;i++)
{
compare = strcmp((n+i)->Liik, str);
if(compare == 0)
{
fprintf(F2, "%s %s %d\n", (n+i)->Nimi, (n+i)->Liik, (n+i)->Vanus);
}
}
fclose(F2);
}
///faili kontroll
int chkFile(FILE *andmed)
{
if(andmed == NULL)
{
return 0;
}
return 1;
}
///.txt lisamine faili nime l6ppu
char *kontroll(char *filename)
{
printf("Sisestage sorteeritava faili nimi:\n");
scanf("%s", filename);
int pikkus;
int i;
int j = 4;
int cmp;
char str[5];
char str1[5] = ".txt";
pikkus = strlen(filename);
for(i=0;i<5;i++)
{
str[j] = *(filename+(pikkus-i));
j--;
}
cmp = strcmp(str1, str);
if (cmp != 0)
{
strcat(filename, str1);
}
return filename;
}
///massiivi sisselugemine failist
int *arrsisse(FILE *F, char *name)
{
int i = 1;
F = fopen(name, "r");
int chk = chkFile(F);
static int arr[256] = {};
if (chk != 1)
{
return 0;
}
while(!feof(F))
{
fscanf(F, "%d", &arr[i]);
i++;
}
arr[0] = i-1;
fclose(F);
return arr;
}
///massiivi faili lugemine
void arrvljastus(FILE *X, int pikkus, int *nr)
{
int i;
X = fopen("sorted.txt", "w");
for(i=0;i<pikkus;i++)
{
fprintf(X, "%d\n", *(nr+i));
}
}
///kahe arvu asukoha vahetus
void swap2(int *x,int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}
/// kahe arvu keskmise valimine
int choose_midnr(int i,int j )
{
return((i+j) /2);
}
/// quicksort algoritm
void quicksorter(int *nr, int m, int n, int pikkus)
{
int key,i,j,k;
int list[256];
for(i=0;i<pikkus;i++)
{
list[i]=*(nr+i);
}
if( m < n)
{
k = choose_midnr(m,n);
swap2(&list[m],&list[k]);
key = list[m];
i = m+1;
j = n;
while(i <= j)
{
while((i <= n) && (list[i] <= key))
i++;
while((j >= m) && (list[j] > key))
j--;
if( i < j)
swap2(&list[i],&list[j]);
}
swap2(&list[m],&list[j]);
quicksorter(list,m,j-1, pikkus);
quicksorter(list,j+1,n, pikkus);
for(i=0;i<pikkus;i++){
*(nr+i) = list[i];
}
}
}
Tee rnggen.exe kasutades omale numbrite fail
sort1.exe pane selle faili nimi
tekib fail nimega sorted.txt kus on sorteeritud massiiv
\ No newline at end of file
8
9
2
4
4
7
3
9
862
934
61
203
971
613
849
840
395
282
396
465
417
300
573
161
435
122
496
795
857
695
584
877
222
417
438
70
159
133
302
80
425
542
370
448
232
310
745
565
495
218
288
442
431
683
161
751
519
759
918
479
865
959
547
892
777
11
491
169
996
681
487
794
989
788
223
868
741
325
272
832
271
823
90
350
80
484
734
265
786
796
310
563
549
964
373
651
705
284
120
701
859
573
181
761
647
998
652
170
985
211
575
400
446
525
114
451
376
460
823
118
191
166
665
865
654
475
972
424
590
879
997
242
225
179
77
708
217
519
412
484
970
144
128
453
668
379
596
231
660
192
679
947
212
430
302
828
852
680
378
308
943
262
726
990
94
90
189
588
441
124
187
764
429
124
327
998
512
64
102
802
796
289
84
421
455
973
266
722
937
991
351
623
603
524
30
570
995
329
858
977
386
808
381
193
621
977
695
471
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "functions.c"
#include "quicksort.c"
int main(void)
{
char name[256];
char *filename = name;
int *sorted;
FILE *F;
FILE *X;
int *nr;
int i;
int pikkus;
filename = kontroll(filename);
nr = arrsisse(F, filename);
if (nr == 0)
{
printf("ERROR!");
return 0;
}
pikkus = nr[0];
for(i=0;i<pikkus;i++)
{
*(nr+i) = *(nr+(i+1));
}
pikkus -= 1;
quicksorter(nr, 0, pikkus-1, pikkus);
arrvljastus(X, pikkus, nr);
return 0;
}
File added
11
30
61
64
70
77
80
80
84
90
90
94
102
114
118
120
122
124
124
128
133
144
159
161
161
166
169
170
179
181
187
189
191
192
193
203
211
212
217
218
222
223
225
231
232
242
262
265
266
271
272
282
284
288
289
300
302
302
308
310
310
325
327
329
350
351
370
373
376
378
379
381
386
395
396
400
412
417
417
421
424
425
429
430
431
435
438
441
442
446
448
451
453
455
460
465
471
475
479
484
484
487
491
495
496
512
519
519
524
525
542
547
549
563
565
570
573
573
575
584
588
590
596
603
613
621
623
647
651
652
654
660
665
668
679
680
681
683
695
695
701
705
708
722
726
734
741
745
751
759
761
764
777
786
788
794
795
796
796
802
808
823
823
828
832
840
849
852
857
858
859
862
865
865
868
877
879
892
918
934
937
943
947
959
964
970
971
972
973
977
977
985
989
990
991
995
996
997
998
998
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment