#include #include #include #include #include typedef enum { FALSE, TRUE } boolean; #define Bmax 64 char Datei[15]; char Zeile[80]; int A[20]; FILE *stream, *fid, *sfid; /***** Compute odd fundamental *************/ int oddfund(int coeff) { int x; x = coeff; while ((x % 2) == 0) { x = x / 2; } return (x); } /*************** Bestimmen des ternaeren Werts einer natuerlichen Zahl *************/ void Ternaer(int *O, int *Po, int *Su, int *SN, long Koeff, boolean druck) { int Posi = 0, Ops = 0, Subs=0, SumNof=0, last; boolean Start; int k, i, l, B[Bmax] ; int TotalOps=0,TotalSub=0,TotalPos=0; int pow2; for (l = 0; l < Bmax; l++) B[l] = 0; pow2 = 1; for (l = 0; pow2 <= Koeff; l++) { if (pow2 & Koeff) B[l] = 1; else B[l] = 0; pow2 <<= 1; } Start = FALSE; for (i = 2; i <= l; i++) if (Start) if (B[i] == 0) { B[i] = 1; Start = FALSE; } else B[i] = 0; else { if ((B[i] == 1) && (B[i - 1] == 1) && (B[i - 2] == 1)) { B[i - 2] = -1; B[i - 1] = 0; B[i] = 0; Start = TRUE; } else if ((B[i + 1] == 1) && (B[i] == 0) && (B[i - 1] == 1) && (B[i - 2] == 1)) { B[i - 2] = -1; B[i - 1] = 0; B[i] = 1; } } for (i = l; i >= 2; i--) if ((B[i] == 1) && (B[i - 1] == 0) && (B[i - 2] == -1)) { B[i - 2] = 1; B[i - 1] = 1; B[i] = 0; } for (i = l; i >= 0; i--) { if (B[i] > 0) Posi++; if (B[i] < 0) Subs++; if (B[i] != 0) Ops++; } Start = TRUE; for (k=0;k<20;k++) A[k]=0; k=1;last=0; pow2 = 1; for (i = 0; i <= l; i++) { if (B[i] != 0) if (Start) { A[0] = B[i]*pow2; Start = FALSE; } else { last = B[i]*pow2; if (B[i]>0) A[k]=A[k-1]+pow2; else A[k]=A[k-1]-pow2; k++; } pow2 <<=1; } SumNof=0; if (Ops==1) A[1]=Koeff; for (k=1;k<15;k++) if (A[k]!=0) A[k] = oddfund(abs(A[k])); for (k=1;k<(Ops-1);k++) SumNof += A[k]; /* if (SumNof==0) SumNof++;*/ *O = Ops; *Po = Posi; *Su = Subs; *SN = SumNof; } int main() { int r, i, k, b; int ls[20],lp[20],lo[20],O, Po, Su, SumNof; int TotalOps=0,TotalSub=0,TotalPos=0; long B, W; boolean druck; druck = FALSE; for (b=0;b<20;b++){lo[b]=0;ls[b]=0;lp[b]=0;} printf("Define csd table bit width =\n"); scanf("%d", &b); B = 1<< (b+1); sprintf(Datei, "csd%d.dat", b); stream = fopen(Datei, "w"); printf(" Saving: %s\n", Datei); sprintf(Datei, "posi%d.dat", b); fid = fopen(Datei, "w"); printf(" Saving: %s\n", Datei); sprintf(Datei, "subs%d.dat", b); sfid = fopen(Datei, "w"); printf(" Saving: %s\n", Datei); for (W = 1; W <= B; W++) { Ternaer(&O, &Po, &Su, &SumNof, W, druck); fprintf(stream, "%d %d ", O - 1, SumNof); for (k=1;k<10;k++) fprintf(stream, "%d ", A[k]); fprintf(stream, "\n"); fprintf(fid, "%d\n", Po); fprintf(sfid, "%d\n", Su); lo[O-1]++; lp[Po]++; ls[Su]++; } fclose(stream); fclose(fid); printf("Statistic of CSD elements:\n"); for (k=0;k<20;k++) { if (lo[k]!=0) printf("Ops[%d]=%d;",k,lo[k]); TotalOps+=lo[k]*k; } printf("\n"); for (k=0;k<20;k++) { if (lp[k]!=0) printf("Pos[%d]=%d;",k,lp[k]); TotalPos+=lp[k]*k;} printf("\n"); for (k=0;k<20;k++) { if (ls[k]!=0) printf("Suk[%d]=%d;",k,ls[k]); TotalSub+=ls[k]*k; } printf("\n"); printf("Totals: Ops=%d Pos=%d Sub=%d\n",TotalOps,TotalPos,TotalSub); /********** Test to read file ********/ /*sprintf(Datei, "csd%d.dat", b); stream = fopen(Datei, "r"); if (stream == NULL) { printf("File %s does not exist !!!\n", Datei); exit(1); } i=0;r=1; while (!feof(stream)) { i++; fscanf(stream, "%s", Zeile); k = atoi(Zeile); printf(" %d ",k); if ((i%11)==0) {r++;printf("\n %d:",r);} } fclose(stream);*/ printf("\n Program done\n"); return 0; }