⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 readpla.cpp

📁 Implement the step 2 of two-level logic minimization. Our goal is to find the minimum (exact minim
💻 CPP
字号:
#include "pla.h"main(int argc, char *argv[]){   FILE *fp;   pPla func;   int i,j,mcost,mlit;   char *mpath;   set result;   cublist *cnode,*dnode;   fp=NULL;   if (argc!=2)   // unmatched argument numbers      printf("Usage: findcost input_file\n");   else if ((fp=fopen(argv[1],"r"))==NULL)   // open file failed      printf("File open error!!\n");   else if ((func=readpla(fp))==NULL)   // read input file failed      printf("Read PLA file failed!!\n");   else {      mcost=func->cubes;   // variables for minimum cost      mpath=new char[func->cubes];   // minimum selection, 1: choose, 0: not      for (i=0,mlit=0;i<(func->cubes);i++) {         mlit+=func->lit[i];         mpath[i]=1;      }      findcost(func,mpath,&mcost,&mlit);   // mlit: minimum number of literals      for (i=0;i<(func->cubes);i++) {         if (mpath[i])            print_cube(func->inputs,func->cubarr[i]);      }      printf("cost=(%d,%d)\n",mcost,mlit);      /* free the used memory */      delete mpath;      for (i=0;i<(func->cubes);i++) delete func->cubarr[i];      delete func->cubarr;      delete func->lit;      cnode=func->onset;      while (cnode) {         dnode=cnode;         cnode=cnode->next;         delete dnode->cube;         delete dnode;      }      delete func;   }   if (fp) fclose(fp);   return 0;}pPla readpla(FILE *fp){   int i,j,k,tmp,tmp1,stleng,litmp;   int *litarr;   set itemp,*readin;   pset *ctemp;   pPla ptemp;   char temp[256];   cublist *newnode,*onend;   ptemp=new Pla;   ptemp->inputs=0;   ptemp->wsize=1;   ptemp->cubes=0;   ptemp->cubarr=NULL;   ptemp->onno=0;   ptemp->onset=NULL;   ptemp->lit=NULL;   onend=NULL;   readin=NULL;      /* readin option */   while ((fgets(temp,256,fp))!=NULL) {      stleng=strlen(temp);      for (i=0;i<stleng;i++) if (temp[i]!=' ') break;   // skip space      if (temp[i]=='\n') continue;      if (temp[i]!='.') {   // wrong format         printf("Format Error!!\n");         delete ptemp;         delete readin;         return NULL;      }      else if (temp[i+1]=='i' || temp[i+1]=='I') {   // input no         if (temp[i+2]!=' ') continue;	 for (i+=2;i<stleng;i++) if (temp[i]!=' ') break;   // skip space	 ptemp->inputs=atoi(temp+i);         ptemp->wsize=(ptemp->inputs)*2/BPI+1;         readin=new set[ptemp->wsize];      }      else if (temp[i+1]=='m' || temp[i+1]=='M') {   // minterms         fgets(temp,256,fp);         stleng=strlen(temp);         i=0;         while (i < stleng && temp[i] != '\n') {            for (;i<stleng;i++) if (temp[i]!=' ') break;   // skip space            if (temp[i] >= '0' && temp[i] <= '9') {                tmp=atoi(temp+i);                  /* translate minterm number into cube format */               for (j=0;j<(ptemp->wsize);j++) readin[j]=0;                 for (j=(ptemp->inputs-1),k=1;j>=0;j--,k*=2) {                  tmp1 = tmp&k;                  if (tmp1 == 0) itemp=ZERO;                  else itemp=ONE;                  itemp = itemp << ((2*j)%BPI);                  readin[(2*j)/BPI] = readin[(2*j)/BPI] | itemp;               }                  /* save minterm cubes into link list */               newnode=new cublist;               newnode->cube=new set[ptemp->wsize];               newnode->next=NULL;               for (j=0;j<(ptemp->wsize);j++)                  newnode->cube[j]=readin[j];               if (onend==NULL) ptemp->onset=newnode;               else onend->next=newnode;               onend=newnode;               (ptemp->onno)++;            }            for (;i<stleng;i++) if (temp[i]==' ' || temp[i]=='\n') break;         }      }      else if (temp[i+1]=='d' || temp[i+1]=='D')   // don't care         fgets(temp,256,fp);   // just ignore it      else if (temp[i+1]=='p' || temp[i+1]=='P')   // product terms         break;      else {   // unknown options         printf("Unknown Options!!\n");         delete ptemp;         delete readin;         return NULL;      }   }   if (feof(fp)) {   /* wrong format : no truth table */      printf("Error: No Truth Table!!\n");      delete ptemp;      delete readin;      return NULL;   }      /* readin truth table */   while ((fgets(temp,256,fp))!=NULL) {      stleng=strlen(temp);	 /* skipping the space in head */      for (i=0;i<stleng;i++) if (temp[i]!=' ') break;	 /* checking if the end mark appear */      if (temp[i]=='.' && (temp[i+4]==' ' || temp[i+4]=='\n' || temp[i+4]==0)) {         temp[i+4]=NULL;         if (strcmp(temp+1,"end") || strcmp(temp+1,"END")) // end found            break;         else {   // unknown option            printf("Unknown Options!!\n");            delete ptemp;            delete readin;            return NULL;         }      }      else if (temp[i]=='\n') continue;	 /* read input field */      itemp=0; litmp=0;      for (j=0;j<(ptemp->wsize);j++) readin[j]=0;      for (j=0;j<(ptemp->inputs);j++) {         switch (temp[i+j]) {	    case '0':	       itemp=ZERO;               litmp++;	       break;	    case '1':	       itemp=ONE;               litmp++;	       break;	    case '-':	       itemp=DASH;	       break;	    default:	       itemp=0;	       break;	 }         if (itemp==0) {            printf("Error: Unknown Symbol in Truth Table!!\n");            delete ptemp;            delete readin;            return NULL;         }         else {            itemp = itemp << ((2*j)%BPI);            readin[(2*j)/BPI] = readin[(2*j)/BPI] | itemp;         }      }	 /* add a new cube */      if ((ptemp->cubes)==0) {         ptemp->cubarr=new pset;         ptemp->lit=new int;      }      else {   // create a larger sized array and copy the old contents         ctemp=new pset[ptemp->cubes+1];         litarr=new int[ptemp->cubes+1];            for (i=0;i<(ptemp->cubes);i++) {             ctemp[i]=ptemp->cubarr[i];            litarr[i]=ptemp->lit[i];         }         delete ptemp->cubarr;         delete ptemp->lit;         ptemp->cubarr=ctemp;         ptemp->lit=litarr;      }      ptemp->cubarr[ptemp->cubes]=new set;      for (i=0;i<(ptemp->wsize);i++)         (ptemp->cubarr[ptemp->cubes])[i]=readin[i];      ptemp->lit[ptemp->cubes]=litmp;      (ptemp->cubes)++;	 /* accross output pattern & skipping the space in the end */      for (i+=j;i<stleng;i++) if (temp[i]==' ') break;      for (;i<stleng;i++) if (temp[i]!=' ') break;      if (i!=stleng && temp[i]!='\n') {         /* wrong format, has term after output */         printf("Format Error!!\n");         delete ptemp;	 delete readin;	 return NULL;      }   }   delete readin;   if (temp[i]=='.') return ptemp;   // normal end with .end   else {      delete ptemp;      return NULL;   }}void print_cube(int inputs, pset cube){   int i;   set result;   if (!cube) return;   for (i=0;i<inputs;i++) {      result = (cube[(2*i)/BPI] >> ((2*i)%BPI)) & 3;      if (result==ZERO) printf("0");      else if (result==ONE) printf("1");      else if (result==DASH) printf("-");   }   printf("\n");}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -