#include <stdio.h>
#include <stdlib.h>
#define VSTUP "cisla.txt"
#define VYSTUP "vystup.txt"
int nsd(int x,int y){
int delitel = (x < y) ? x : y;
while(x % delitel != 0 || y % delitel != 0)
delitel--;
return delitel;
}
int nsn(int x,int y){
int nasobek = (x > y) ? x : y;
while(nasobek % x != 0 || nasobek % y != 0)
nasobek += (x > y) ? x : y;
return nasobek;
}
int main(int argc, char** argv) {
FILE * vstup;
FILE * vystup;
int c1, c2;
int i = 1, j = 1;
vstup = fopen (VSTUP,"r");
if (vstup == NULL){
printf("Soubor %s nebyl otevren.\n",VSTUP);
return (EXIT_FAILURE);
}
vystup = fopen (VYSTUP,"w");
printf("Vypis cisel ze souboru %s\n-------------------------------- \n",VSTUP);
fprintf(vystup,"Vypis delitelnych cisel ze souboru %s\n--------------------------------------------\n",VYSTUP);
printf("%7s%7s%7s%7s%7s\n","poradi","cislo1","cislo2","nsn","nsd");
fprintf(vystup,"%7s%7s%7s%7s%7s\n","poradi","cislo1","cislo2","nsn","nsd");
while (fscanf(vstup,"%d %d",&c1,&c2) == 2){
printf("%6d.%7d%7d%7d%7d\n",i,c1,c2,nsn(c1,c2),nsd(c1,c2));
if (nsd(c1,c2) != 1){
fprintf(vystup,"%6d.%7d%7d%7d%7d\n",j,c1,c2,nsn(c1,c2),nsd(c1,c2));
j++;
}
i++;
}
printf("\nSoubor %s obsahuje %d dvojic cisel.\n\n",VSTUP,i-1);
fprintf(vystup,"\nSoubor %s obsahuje %d dvojic cisel.\n",VYSTUP,j-1);
if (fclose (vstup) == EOF)
printf("Soubor %s nebyl uzavren.\n",VSTUP);
if (fclose (vystup) == EOF)
printf("Soubor %s se nepovedlo vytvorit.\n",VYSTUP);
else
printf("Byl vytvoren soubor delitelnych cisel %s.\n\n",VYSTUP);
return (EXIT_SUCCESS);
}
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define VSTUP "cisla.txt"
#define VYSTUP "vystup.txt"
int mocnina (int z, int e){
int v = 1;
for(;e > 0;e--)
v *= z;
return v;
}
int prvocislo(int n){
int i;
for(i = 2; i <= sqrt(n); ++i) {
if (n % i == 0)
return 0;
}
return 1;
}
int main(int argc, char** argv) {
FILE * vstup;
FILE * vystup;
int z,e;
int i = 1, j = 1;
vstup = fopen (VSTUP,"r");
if (vstup == NULL){
printf("Soubor %s nebyl otevren.\n",VSTUP);
return (EXIT_FAILURE);
}
vystup = fopen (VYSTUP,"w");
printf("Vystup cisel ze souboru %s\n",VSTUP);
printf("---------------------------------\n");
printf("%6s%9s%9s%9s\n","poradi","zaklad","exponent","mocnina");
fprintf(vystup,"Vystup cisel s prvociselnym zakladem ze souboru %s\n",VYSTUP);
fprintf(vystup,"---------------------------------------------------------\n");
while( fscanf (vstup,"%d %d",&z,&e) == 2){
printf("%5d.%9d%9d%9d\n",i,z,e,mocnina(z,e));
if (prvocislo(z)){
fprintf(vystup,"%5d.%8d%8d%8d\n",j,z,e,mocnina(z,e));
j++;
}
i++;
}
fprintf(vystup,"Soubor %s obsahuje %d dvojic cisel.\n",VYSTUP,j-1);
if (fclose (vstup) == EOF)
printf("Soubor %s nebyl uzavren.\n",VSTUP);
if (fclose (vystup) == EOF)
printf("Soubor %s se nepovedlo vytvorit.\n\n",VYSTUP);
else
printf("\nByl vytvoren soubor cisel %s s poctem dvojic cisel rovnym %d.\n\n",VYSTUP,j-1);
return (EXIT_SUCCESS);
}