Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define SIZE 3
- #define cog 16
- #define nom 12
- typedef struct {
- char cognome[cog];
- char nome[nom];
- int stip; } dipendente;
- void letturastampa(FILE *fp); //mostra contenuto del file
- void putvet(FILE *fp, dipendente *dipendenti1, dipendente *dipendenti2, int *n, int *m);
- void stampapari(dipendente *dipendenti1, int n);
- void stampadispari(dipendente *dipendenti2, int m);
- void fondi(dipendente *a, dipendente *b, dipendente *f, int n, int m, int *p);
- void stampafuso(dipendente *dipendenti3, int p);
- int main(int argc, char *argv[])
- {
- dipendente dipendenti1[SIZE];
- dipendente dipendenti2[SIZE];
- dipendente dipendenti3[SIZE];
- FILE *fp;
- int n, m, p;
- //mostra il contenuto del file
- fp=fopen("anagrafe.txt", "r");
- letturastampa(fp);
- fclose(fp);
- //1
- fp=fopen("anagrafe.txt", "r");
- putvet(fp, dipendenti1, dipendenti2, &n, &m);
- fclose(fp);
- //2
- printf("\n\nSTAMPA DIPENDENTI CON STIPENDIO PARI:\n\n");
- stampapari(dipendenti1, n);
- //3
- printf("\n\nSTAMPA DIPENDENTI CON STIPENDIO DISPARI:\n\n");
- stampadispari(dipendenti2, m);
- //4
- printf("\n\nFONDI:\n\n");
- fondi(dipendenti1, dipendenti2, dipendenti3, n, m, &p);
- //5
- stampafuso(dipendenti3, p);
- printf("\n\n");
- system("PAUSE");
- return 0;
- }
- void letturastampa(FILE *fp) {
- char c;
- while(!feof(fp)) {
- c = fgetc(fp);
- putchar(c); }
- }
- void putvet(FILE *fp, dipendente *dipendenti1, dipendente *dipendenti2, int *n, int *m) {
- char c;
- int i=0, j=0;
- dipendente TEMP;
- while(!feof(fp) && i<SIZE && j<SIZE) {
- fscanf(fp, "%s %s %d", TEMP.cognome, TEMP.nome, &TEMP.stip);
- if(TEMP.stip%2==0) { strcpy(dipendenti1[i].cognome, TEMP.cognome);
- strcpy(dipendenti1[i].nome, TEMP.nome);
- dipendenti1[i].stip = TEMP.stip;
- i++; }
- else if(TEMP.stip%2!=0) { strcpy(dipendenti2[j].cognome, TEMP.cognome);
- strcpy(dipendenti2[j].nome, TEMP.nome);
- dipendenti2[j].stip = TEMP.stip;
- j++; } }
- *n = i;
- *m = j;
- }
- void stampapari(dipendente *dipendenti1, int n) {
- int i;
- for(i=0; i<n; i++) printf("%s %s %d\n", dipendenti1[i].cognome, dipendenti1[i].nome, dipendenti1[i].stip);
- }
- void stampadispari(dipendente *dipendenti2, int m) {
- int i;
- for(i=0; i<m; i++) printf("%s %s %d\n", dipendenti2[i].cognome, dipendenti2[i].nome, dipendenti2[i].stip);
- }
- void fondi(dipendente *a, dipendente *b, dipendente *f, int n, int m, int *p) {
- int i=0, j=0, k=0;
- while (i<n && j<m) {
- if(a[i].stip < b[j].stip) {
- strcpy(f[k].nome, a[i].nome);
- strcpy(f[k].cognome, a[i].cognome);
- f[k].stip = a[i].stip;
- i++; }
- else {
- strcpy(f[k].nome, b[j].nome);
- strcpy(f[k].cognome, b[j].cognome);
- f[k].stip=b[j].stip;
- j++; }
- k++; }
- while(i<n) {
- strcpy(f[k].nome, a[i].nome);
- strcpy(f[k].cognome, a[i].cognome);
- f[k].stip = a[i].stip;
- i++;
- k++; }
- while(j<m) {
- strcpy(f[k].nome, b[j].nome);
- strcpy(f[k].cognome, b[j].cognome);
- f[k].stip = b[j].stip;
- j++;
- k++; }
- *p = k;
- }
- void stampafuso(dipendente *dipendenti3, int p) {
- int i;
- for(i=0; i<p; i++) printf("%s %s %d\n", dipendenti3[i].cognome, dipendenti3[i].nome, dipendenti3[i].stip);
- }
Add Comment
Please, Sign In to add comment