这个东东是两年前写的,当时是为了做一个模拟集成电路的辅助分析系统,分析电路呢就是要推导公式,什么电阻R,电容C,电感L,还有晶体管CMOS等等元器件的满天飞,印象特别深刻,为了推导一个仅有10来个晶体管的电路频率特性,要花上半天时间,写满几张草稿纸。当时就特别渴望如果电脑能帮助自己解决该有多好啊,因此就开始动手写了个所谓的电路分析系统,其实就是想偷懒而已啦,这个代数运算引擎呢,就是为了让电脑帮我推导一堆代数公式用的,额滴神啊,真不敢相信这是自己做的,虽然现在看来程序写的有点幼稚,不过这个程序一直都让自己觉得最有成就感,赞一个吧,哈哈。
- #include "c.h"
- #include "analysis.h"
- struct table_affix{
- struct list_head list;
- char *lval;
- struct list_head head;
- };
- // -Cgs(Kua-Pdx+Cia-Dif)UlsFka(-Pds-Ajk+Ufd-Pof)Fua
- //提取括号内各项,并提取出来添加到链表中
- void analysis_exp_inside(struct list_head *head,char *pfrom,char *pto)
- {
- char *p=pfrom;
- if(ismp(*p))p++;
- char *q=pfrom;
- while(p<=pto){
- if(ismp(*p)){
- list_add_node(head,q,p-pto?p-1:p);
- q=p;
- }
- p++;
- }
- list_add_node(head,q,p-1);
- }
- //返回从pstart开始的下一个单元块的地址
- char *get_block_exp(char *pstart)
- {
- int flag=0;
- char *p=pstart;
- if(*p=='='||*p==0)return 0;
- while(*p&&*
- if(*p=='(')flag++;
- else if(*p==')')flag--;
- else if(ismp(*p)&&!flag&&p!=pstart)
- return p-1;
- p++;
- }
- return p-1;
- }
- // -Cgs(Kua-Pdx+Cia-Dif)UlsFka(-Pds-Ajk+Ufd-Pof)Fua
- //对一个带有括号的单元块进行链表化
- struct list_head *set_block_list(char *pstart,char *pto)
- {
- struct list_head *head=newp(head);
- init_list_head(head);
- char *p=pstart,*q=p;
- while(p<=pto){
- if(*q=='(')q++;
- if((*p=='('&&p>q)||*p==')'||p==pto){
- struct node *tnode=node_new((int)sizeof(struct list_head));
- init_list_head((struct list_head*)(tnode->data));
- list_add_tail(&tnode->list,head);
- analysis_exp_inside((struct list_head*)(tnode->data),q,p-pto||*p==')'?p-1:p);
- q=p+1;
- }
- p++;
- }
- return head;
- }
- //对整个表达式建立链表
- struct list_head *set_exp_list(char *buf)
- {
- char *p=buf,*q;
- struct list_head *head=newp(head);
- init_list_head(head);
- while(1){
- q=get_block_exp(p);
- if(q){
- struct list_head *thead=set_block_list(p,q);
- struct node *tnode=node_new(0);
- list_add_tail(&tnode->list,head);
- tnode->data=thead;
- p=q+1;
- }
- else break;
- }
- return head;
- }
- //对单项式进行括号展开
- void _extend_exp(struct list_head *head)
- {
- while(!list_is_singular(head)){
- struct node *nodea=list_entry_node(head->next);list_del(&nodea->list);
- struct node *nodeb=list_entry_node(head->next);list_del(&nodeb->list);
- struct node *nodec=node_new(sizeof(struct list_head));
- init_list_head((struct list_head*)nodec->data);
- list_add(&nodec->list,head);
- struct node *noded,*nodee;
- list_for_each_entry(noded,(struct list_head*)nodea->data,list)
- list_for_each_entry(nodee,(struct list_head*)nodeb->data,list)
- unite_node_str((struct list_head*)nodec->data,noded->data,nodee->data);
- destroy_node(nodea);
- destroy_node(nodeb);
- }
- }
- //对整个表达式进行括号展开
- struct list_head* extend_exp(char *buf)
- {
- struct list_head *head=set_exp_list(buf);
- struct node *tnode;
- list_for_each_entry(tnode,head,list)
- _extend_exp((struct list_head*)tnode->data);
- return head;
- }
- void table_add_affix(struct list_head *affxhead,struct node *tnode)
- {
- char *buf=(char*)tnode->data;
- char *lval=strchr_reverse(buf,'V');
- myassert(lval!=0);
- int len=lval-buf+1;
- char *rval=new(len);
- strncpy(rval,buf,len-1);
- table_insert_value(affxhead,lval,rval);
- }
- struct list_head *set_affix_table(struct list_head *head)
- {
- struct list_head *affxhead=newp(affxhead);
- init_list_head(affxhead);
- struct node *nodea;
- list_for_each_entry(nodea,head,list)
- table_add_affix(affxhead,nodea);
- return affxhead;
- }
- struct list_head *_exp_mul_extend(struct list_head *nodelista,struct list_head *nodelistb)
- {
- struct list_head *head=newp(head);
- init_list_head(head);
- struct node *nodea,*nodeb;
- list_for_each_entry(nodea,nodelista,list)
- list_for_each_entry(nodeb,nodelistb,list)
- unite_node_str(head,(char*)nodea->data,(char*)nodeb->data);
- return head;
- }
- struct list_head *set_list_negative(struct list_head *nodelist)
- {
- struct list_head head;
- init_list_head(&head);
- char *buf="-";
- int len=strlen(buf);
- analysis_exp_inside(&head,buf,buf+len-1);
- return _exp_mul_extend(nodelist,&head);
- }
- struct list_head*exp_mul_extend(struct list_head *affixhead,struct list_head *nodehead)
- {
- struct list_head *taffix=affix_chain_copy(affixhead);
- struct table_affix *affixtable;
- list_for_each_entry(affixtable,taffix,list)
- {
- struct list_head *head=_exp_mul_extend(&affixtable->head,nodehead);
- list_kill_unite(&affixtable->head,head);
- }
- return taffix;
- }
- struct list_head* set_affix_negative(struct list_head *affixhead)
- {
- struct list_head head;
- init_list_head(&head);
- char *buf="-";
- int len=strlen(buf);
- analysis_exp_inside(&head,buf,buf+len-1);
- return exp_mul_extend(affixhead,&head);
- }
- struct list_head* affix_table_unite(struct list_head *affixheada,struct list_head *affixheadb)
- {
- struct list_head *new_affixheadb=set_affix_negative(affixheadb);
- struct list_head *new_affixheada=affix_chain_copy(affixheada);
- struct table_affix *affixtable;
- list_for_each_entry(affixtable,new_affixheadb,list){
- char *lval=affixtable->lval;
- struct table_affix *taffix=table_search_lval(new_affixheada,lval);
- if(!taffix)taffix=table_new_lval(new_affixheada,lval);
- list_put_unite(&taffix->head,&affixtable->head);
- }
- return new_affixheada;
- }
- struct list_head *affix_table_mul(struct list_head *affixheada,struct list_head *affixheadb,char *lval)
- {
- struct table_affix *affixtablea=table_search_lval(affixheada,lval);
- struct table_affix *affixtableb=table_search_lval(affixheadb,lval);
- if(!affixtablea||!affixtableb)return 0;
- struct list_head *new_affixheada=exp_mul_extend(affixheada,&affixtableb->head);
- struct list_head *new_affixheadb=exp_mul_extend(affixheadb,&affixtablea->head);
- affixtablea=table_search_lval(new_affixheada,lval);
- affixtableb=table_search_lval(new_affixheadb,lval);
- list_del(&affixtablea->list);
- list_del(&affixtableb->list);
- struct list_head *affixhead=newp(affixhead);
- init_list_head(affixhead);
- return affix_table_unite(new_affixheada,new_affixheadb);
- }
- struct list_expchain *setup_lval_analysis_affixchain(struct list_head *upchainhead,char *lval)
- {
- struct list_expchain *expchain=newp(expchain);
- LIST_HEAD_INIT(expchain,lval_on);
- LIST_HEAD_INIT(expchain,lval_off);
- while(!list_empty_careful(upchainhead)){
- struct node *tnode=list_entry_node(upchainhead->next);
- list_del_init(&tnode->list);
- if(table_search_lval(tnode->data,lval))
- list_add_tail(&tnode->list,&expchain->lval_on);
- else list_add_tail(&tnode->list,&expchain->lval_off);
- }
- return expchain;
- }
- struct list_head *cal_expchain2_affixchain(struct list_expchain *expchain,char *lval)
- {
- struct list_head *affixhead=newp(affixhead);
- init_list_head(affixhead);
- list_kill_unite(affixhead,&expchain->lval_off);
- // fprintf(fop,"\n\nshow A:\n");
- // show_global_affix_table(affixhead);
- // fprintf(fop,"\n\nlval_off (%s):\n",lval);
- // show_global_affix_table(affixhead);
- // fprintf(fop,"\n\nlval_on (%s):\n",lval);
- // show_global_affix_table(&expchain->lval_on);
-
- struct node *tnode;
- struct list_head *lval_on=&expchain->lval_on;
-
- struct node *pnode=list_entry_node(lval_on->next);
- // fprintf(fop,"\n\nshow_affix_table B:\n");
- // show_affix_table((struct list_head*)pnode->data);
-
- list_del_init(&pnode->list);
-
- // fprintf(fop,"\n\nlval_on A(%s):\n",lval);
- // show_global_affix_table(&expchain->lval_on);
-
- // fprintf(fop,"\n\nlast show_affix_table:\n");
-
- list_for_each_entry(tnode,lval_on,list){
- struct list_head *affix_new=affix_table_mul(pnode->data,tnode->data,lval);
- // show_affix_table(affix_new);
- // fprintf(fop,"\n\n");
- struct node *unode=node_new(0);
- unode->data=affix_new;
- list_add_tail(&unode->list,affixhead);
- }
- #if 0
- #endif
- // fprintf(fop,"\n\nshow B:\n");
- // show_global_affix_table(affixhead);
- return affixhead;
- }
- struct list_head *cal_affix_lval(struct list_head *upchainhead,char *lval)
- {
- return cal_expchain2_affixchain(setup_lval_analysis_affixchain(upchainhead,lval),lval);
- }
- struct list_head *cal_affix(struct list_head *head)
- {
- struct list_head *lvallist=setup_lval_list(head);
- int i=0;
- while(!list_empty_careful(lvallist)){
- struct node *tnode=list_entry_node(lvallist->next);
- list_del_init(&tnode->list);
- struct list_head *thead=cal_affix_lval(head,tnode->data);
- fprintf(fop,"\n\nCalculate start %d::%s",++i,tnode->data);
- show_global_affix_table(thead);
- head=thead;
- }
- return head;
- }
- void global_cal_circuit()
- {
- read_netlist();
- set_global_affix();
- show_global_affix_table(&g_affixhead);
- struct list_head *head=cal_affix(&g_affixhead);
-
- struct node *tnode=list_entry_node(head->next);
- struct list_head *affixhead=tnode->data;
- struct table_affix *affix_vi=table_search_lval(affixhead,"VIN");
- struct table_affix *affix_vo=table_search_lval(affixhead,"VOUT");
- myassert(affix_vi&&affix_vo);
-
-
- struct list_head *vi_list=set_list_negative(&affix_vi->head);
- struct list_head *vo_list=&affix_vo->head;
-
-
- struct list_head *vi_adjust=adjust_res_list(vi_list);
- struct list_head *vo_adjust=adjust_res_list(vo_list);
-
-
- struct list_head *vi_by_s=adjust_by_s(vi_adjust);
- struct list_head *vo_by_s=adjust_by_s(vo_adjust);
- fprintf(fop,"\n\nVo/Vi =====\n");
- show_s_list(vi_by_s);
- fprintf(fop,"\n________________________________________________________________________________");
- show_s_list(vo_by_s);
- }
- #include "c.h"
- #include "analysis.h"
- #include "table.h"
- struct list_head g_exphead;
- struct list_head g_affixhead;
- struct list_head g_lvalhead;
- FILE *fop;
- void read_netlist()
- {
- init_list_head(&g_exphead);
- FILE *fp=fopen(FILE_PATH,"r");
- fop=fopen(OUT_PUT_PATH,"w");
-
- myassert(fp&&fop);
- struct node *tnode;
- char *buf;
- while(!feof(fp)){
- tnode=node_new(0);
- buf=new(128);
- fscanf(fp,"%s",buf);
- fprintf(fop,"\n%s",buf);
- tnode->data=buf;
- list_add_tail(&tnode->list,&g_exphead);
- }
- fprintf(fop,"\n\n");
- }
- void set_global_affix()
- {
- init_list_head(&g_affixhead);
- struct node *tnode;
- list_for_each_entry(tnode,&g_exphead,list)
- {
- struct list_head *head=list_unite(extend_exp((char*)tnode->data));
- struct list_head *affxhead=set_affix_table(head);
- struct node *nodea=node_new(0);
- nodea->data=affxhead;
- list_add_tail(&nodea->list,&g_affixhead);
- }
- }
- struct node *node_new(int size)
- {
- struct node *tnode=newp(tnode);
- LIST_HEAD_INIT(tnode,list);
- tnode->data=new(size);
- return tnode;
- }
- struct node* list_add_node(struct list_head *head,char *pfrom,char *pto)
- {
- int len=pto-pfrom+2;
- struct node *tnode=node_new(len);
- strncpy(tnode->data,pfrom,len-1);
- list_add_tail(&tnode->list,head);
- // printf("\nlist_add_node::%s",tnode->data);
- return tnode;
- }
- struct node *_list_add_node(struct list_head *head,char *buf)
- {
- list_add_node(head,buf,buf+strlen(buf)-1);
- }
- void show_sub_list(struct list_head *head)
- {
- // fprintf(fop,"\n\nshow_sub_list:\n");
- struct node *tnode;
- if(!fop)fop=stdout;
- fprintf(fop,"\n");
- list_for_each_entry(tnode,head,list)
- fprintf(fop,"%s\t",(char*)tnode->data);
- }
- void show_list(struct list_head *head)
- {
- struct node *tnode;
- // fprintf(fop,"\nA new round:\n");
- list_for_each_entry(tnode,head,list)
- {
- show_sub_list((struct list_head*)tnode->data);
- fprintf(fop,"\n");
- }
- }
- void show_exp_list(struct list_head *head)
- {
- struct node *tnode;
- list_for_each_entry(tnode,head,list)
- show_list((struct list_head*)tnode->data);
- }
- void show_string(char *pstart,char *pto)
- {
- fprintf(fop,"\nstring:::");
- while(pstart<=pto)
- fprintf(fop,"%c",*pstart++);
- fprintf(fop,"\n");
- }
- void unite_node_str(struct list_head *head,char *stra,char *strb)
- {
- int lena=strlen(stra);
- int lenb=strlen(strb);
- int lenc=lena+lenb+1;
- int affix=1;
- if(*stra=='-')affix*=-1;
- if(*strb=='-')affix*=-1;
-
- struct node *tnode=node_new(lenc);
- char *buf=tnode->data;
- if(affix==1)buf[0]='+';
- else if(affix==-1)buf[0]='-';
- strcpy(buf+1,stra+ismp(*stra));
- strcat(buf+1,strb+ismp(*strb));
- list_add_tail(&tnode->list,head);
- }
- void destroy_node(struct node *tnode)
- {
-
- }
- //+sCm1go2Gm1sCc2
- char *next_div(char *buf)
- {
- myassert(buf&&*buf);
- char *str[]={"sC","gm","go","G"};
- int size=sizeof(str)/sizeof(str[0]);
- // printf("\nbuf is ::%s",buf);
- int i,len=strlen(buf);
- // printf("\nbuf size is ::%d",len);
- char *pmin=buf+len;
- // printf("\n\n\nmin is ::%s",pmin);
- for(i=0;i<size;i++)
- {
- // printf("\n\n\nmin is ::%s, buf is ::%s, str[i] is ::%s",pmin,buf,str[i]);
- char *t=strstr(buf,str[i]);
- if(!t)continue;
- // printf("\nt is ::%s",t);
- pmin=min(pmin,t);
- // printf("\nmin res is ::%s",pmin);
- }
- return pmin;
- }
- //char *buf="+sCm2sCm1gm3sCm1go2Gm1sCc2";
- struct list_head *analysis_buf(char *buf)
- {
- int len=strlen(buf)+1;
- // printf("buf len:%d",len);
- char *tbuf=new(len);
-
- strcpy(tbuf,buf);
- char *p=tbuf,*q;
- struct list_head *head=newp(head);
- init_list_head(head);
- if(ismp(*p)){
- list_add_node(head,p,p);
- p++;
- }
- char *str[]={"sC","gm","go","G"};
- int size=sizeof(str)/sizeof(str[0]);
- int i;
- for(i=0;i<size;i++){
- // int j=0;
- do{
- // if(j++>6)break;
- q=strstr(p,str[i]);
- // if(q)printf("\n\n\nres-->q::%s",q);
- // else printf("\nres-->q::null");
- if(!q)break;
- char *t=next_div(q+1);
- // if(*t)printf("\nres-->t::%s",t);
- // else printf("\nres-->t::null");
- list_add_node(head,q,t-1);
- // printf("\n::%s",list_add_node(head,q,t-1)->data);
- // printf("\nt::::%s",t);
- // printf("\nq::::%s",q);
- if(*t)strcpy(q,t);
- else *q=0;
- // printf("\nbuf is ::%s",tbuf);
- }while(q);
- }
- // show_sub_list(head);
- return head;
- }
- struct divlist *div_list(struct list_head *head)
- {
- struct divlist *div=newp(div);
- LIST_HEAD_INIT(div,positive);
- LIST_HEAD_INIT(div,negative);
- while(!list_empty_careful(head)){
- struct node *tnode=list_entry_node(head->next);
- list_del_init(&tnode->list);
- if(*(char*)tnode->data=='-')
- list_add_tail(&tnode->list,&div->negative);
- else list_add_tail(&tnode->list,&div->positive);
- }
- return div;
- }
- //char *buf="+sCm2sCm1gm3sCm1go2Gm1sCc2";
- struct node *list_entry_node(struct list_head *list)
- {
- return list_entry(list,struct node*,list);
- }
- char *strchr_reverse(char *p,char ch)
- {
- char *q=p+strlen(p)-1;
- while(q>=p)
- if(*q==ch)return q;
- else q--;
- return 0;
- }
- void show_affix_table(struct list_head *head)
- {
- struct table_affix *affxtable;
- fprintf(fop,"\n");
- int flag=0;
- list_for_each_entry(affxtable,head,list)
- {
- if(&affxtable->list!=head->next)fprintf(fop," + ");
- struct node *tnode;
- tnode=list_entry_node(affxtable->head.next);
- if(!list_is_singular(&affxtable->head)||*(char*)(tnode->data)=='-'){flag=1;fprintf(fop,"(");}
-
- list_for_each_entry(tnode,&affxtable->head,list)
- {
- char *p=(char*)tnode->data;
- if(&tnode->list==affxtable->head.next)
- fprintf(fop,"%s",p+(*p=='+'?1:0));
- else fprintf(fop,"%s",(char*)tnode->data);
- }
- if(flag){fprintf(fop,")");flag=0;}
- fprintf(fop,"%s",affxtable->lval);
- }
- fprintf(fop,"=0\n");
- }
- void show_global_affix_table(struct list_head *head)
- {
- struct node *tnode;
- fprintf(fop,"\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
- fprintf(fop,"\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
- list_for_each_entry(tnode,head,list)
- show_affix_table((struct list_head*)tnode->data);
- fprintf(fop,"\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
- fprintf(fop,"\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
- }
- void __list_unite(struct list_head *global,struct list_head *head)
- {
- struct node *tnode;
- struct list_head *list;
- struct list_head *next;
- for(list=head->next;list!=head;)
- {
- tnode=list_entry_node(list);
- next=list->next;
- list_del(list);
- list_add_tail(list,global);
- list=next;
- }
- }
- void _list_unite(struct list_head *global,struct list_head *head)
- {
- struct node *tnode;
- list_for_each_entry(tnode,head,list)
- __list_unite(global,(struct list_head*)tnode->data);
- }
- struct list_head *list_unite(struct list_head *thead)
- {
- struct list_head *global=newp(global);
- init_list_head(global);
- struct node *tnode;
- list_for_each_entry(tnode,thead,list)
- _list_unite(global,(struct list_head*)tnode->data);
- return global;
- }
- void list_kill_unite(struct list_head *origin,struct list_head *new)
- {
- init_list_head(origin);
- struct list_head *list,*next;
- for(list=new->next;list!=new;)
- {
- next=list->next;
- list_del_init(list);
- list_add_tail(list,origin);
- list=next;
- }
- }
- void list_put_unite(struct list_head *heada,struct list_head *headb)
- {
- struct list_head *list,*next;
- for(list=headb->next;list!=headb;)
- {
- next=list->next;
- list_del_init(list);
- list_add_tail(list,heada);
- list=next;
- }
- }
- struct node *node_copy(struct node *tnode)
- {
- struct node *pnode=node_new(0);
- char *buf=tnode->data;
- int len=strlen(buf)+1;
- char *tbuf=new(len);
- strcpy(tbuf,buf);
- pnode->data=tbuf;
- return pnode;
- }
- struct list_head *node_list_copy(struct list_head *head)
- {
- struct list_head *thead=newp(thead);
- init_list_head(thead);
- struct node *tnode;
- list_for_each_entry(tnode,head,list){
- struct node *pnode=node_copy(tnode);
- list_add_tail(&pnode->list,thead);
- }
- return thead;
- }
- struct table_affix *affix_table_copy(struct table_affix *affixtable)
- {
- struct table_affix *taffix=newp(taffix);
- LIST_HEAD_INIT(taffix,list);
- LIST_HEAD_INIT(taffix,head);
- char *lval=affixtable->lval;
- int len=strlen(lval)+1;
- char *buf=new(len);
- strcpy(buf,lval);
- taffix->lval=buf;
- struct list_head *thead=node_list_copy(&affixtable->head);
- list_kill_unite(&taffix->head,thead);
- return taffix;
- }
- struct list_head *affix_chain_copy(struct list_head *head)
- {
- struct list_head *thead=newp(thead);
- init_list_head(thead);
- struct table_affix *affixtable;
- list_for_each_entry(affixtable,head,list){
- struct table_affix *taffix=affix_table_copy(affixtable);
- list_add_tail(&taffix->list,thead);
- }
- return thead;
- }
- //////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////
- struct node *list_search_lval(struct list_head *head,char *buf)
- {
- struct node *tnode;
- list_for_each_entry(tnode,head,list)
- if(!strcmp(tnode->data,buf))return tnode;
- return 0;
- }
- struct list_head* setup_lval_list()
- {
- struct node *tnode;
- init_list_head(&g_lvalhead);
- list_for_each_entry(tnode,&g_affixhead,list)
- {
- struct list_head *head=tnode->data;
- struct table_affix *affixtable;
- list_for_each_entry(affixtable,head,list)
- {
- char *buf=affixtable->lval;
- int len=strlen(buf);
- if(strcmp(buf,"VIN")&&strcmp(buf,"VOUT")&&!list_search_lval(&g_lvalhead,buf))
- list_add_node(&g_lvalhead,buf,buf+len-1);
- }
- }
- return &g_lvalhead;
- }
- void show_last_result(struct list_head *head)
- {
- struct node *tnode=list_entry_node(head->next);
- struct list_head *affixhead=tnode->data;
- struct table_affix *affix_vi=table_search_lval(affixhead,"VIN");
- struct table_affix *affix_vo=table_search_lval(affixhead,"VOUT");
- myassert(affix_vi&&affix_vo);
- fprintf(fop,"\n\nThe last result is:\n\n");
- fprintf(fop,"Vo/Vi =====\n");
- struct list_head *vi_list=set_list_negative(&affix_vi->head);
- struct list_head *vo_list=&affix_vo->head;
- show_sub_list(vi_list);
- fprintf(fop,"\n________________________________________________________________________________");
- show_sub_list(vo_list);
- fprintf(fop,"\n\nResult end************\n\n");
- }
- int is_same_list(struct list_head *heada,struct list_head *headb)
- {
- struct node *unode=list_entry_node(heada->next);
- if(ismp(*(char*)unode->data))list_del_init(&unode->list);
- unode=list_entry_node(headb->next);
- if(ismp(*(char*)unode->data))list_del_init(&unode->list);
-
- while(!list_empty_careful(heada)){
- struct node *nodea=list_entry_node(heada->next);
- struct node *nodeb;
- int flag=0;
- list_for_each_entry(nodeb,headb,list){
- // printf("\n%s ::::::::::::: %s",nodea->data,nodeb->data);
- if(!strcmp(nodeb->data,nodea->data)){
- // printf("\nOK!!!!!!!!!!!!!!!!!!!!!\n");
- list_del(&nodea->list);
- list_del(&nodeb->list);
- flag=1;
- break;
- }
- }
- if(!flag)return 0;
- }
- // printf("\nlist_empty_careful(headb):::%d",list_empty_careful(headb));
- return list_empty_careful(headb);
- }
- struct list_head *adjust_res_list(struct list_head *head)
- {
- struct list_head *thead=newp(thead);
- // FILE *ft=fop;
- init_list_head(thead);
- while(!list_empty_careful(head)){
- // printf("\nshow head:\n");show_sub_list(head);
- int t=1,flag=0;
- struct node *tnode=list_entry_node(head->next);
- list_del_init(&tnode->list);
- struct node *nodea;
- if(*(char*)tnode->data=='-')t=-1;
- list_for_each_entry(nodea,head,list){
- if((*(char*)nodea->data=='-'&&t==-1)||(*(char*)nodea->data=='+'&&t==+1))continue;
- // printf("\nSuccess!!!! %s:::::::::::%s\n",tnode->data,nodea->data);
-
-
- // fop=stdout;
- struct list_head *heada=analysis_buf(tnode->data);
- // printf("\nshow heada:\n");show_sub_list(heada);
- struct list_head *headb=analysis_buf(nodea->data);
- // printf("\nshow headb:\n");show_sub_list(headb);
- if(is_same_list(heada,headb)){
- // printf("\nOK!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
- // printf("\n::%s :::: %s",tnode->data,nodea->data);
- list_del_init(&nodea->list);
- flag=1;
- break;
- }
- }
- if(!flag)list_add_tail(&tnode->list,thead);
- }
- // fop=ft;
- return thead;
- }
- struct list_head* test_show_last_result(struct list_head *head)
- {
- struct node *tnode=list_entry_node(head->next);
- struct list_head *affixhead=tnode->data;
- struct table_affix *affix_vi=table_search_lval(affixhead,"VIN");
- struct table_affix *affix_vo=table_search_lval(affixhead,"VOUT");
- myassert(affix_vi&&affix_vo);
- fprintf(fop,"\n\nThe last result is:\n\n");
- fprintf(fop,"Vo/Vi =====\n");
- struct list_head *vi_list=set_list_negative(&affix_vi->head);
- struct list_head *vo_list=&affix_vo->head;
- // show_sub_list(vi_list);
- show_sub_list(adjust_res_list(vi_list));
- fprintf(fop,"\n________________________________________________________________________________");
- struct list_head *new_vo_list=adjust_res_list(vo_list);
- show_sub_list(new_vo_list);
- // show_sub_list(vo_list);
-
- fprintf(fop,"\n\nResult end************\n\n");
- return new_vo_list;
- }
- char *list_unite2buf(struct list_head *head)
- {
- struct node *tnode;
- int len=0;
- list_for_each_entry(tnode,head,list)
- len+=strlen(tnode->data);
- char *buf=new(len+1);
- list_for_each_entry(tnode,head,list)
- strcat(buf,tnode->data);
- return buf;
- }
- void _adjust_by_s(struct list_head *head,struct node *tnode)
- {
- // printf("\ntnode data::%s",tnode->data);
- struct list_head *thead=analysis_buf(tnode->data);
-
- // printf("\n\nstart!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
- // show_sub_list(thead);
- // printf("\n\nstop!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
- int n=0;
- struct node *nodea;
- list_for_each_entry(nodea,thead,list){
- char *p=nodea->data;
- // printf("\nOK::::%s",nodea->data);
- if(!strncmp(p,"sC",2)){
- n++;
- strcpy(p,p+1);
- }
- }
- // printf("\nthe level is : %d",n);
- char *tbuf=list_unite2buf(thead);
- // printf("\nafter unite ::: %s",tbuf);
- int len=strlen(tbuf);
- struct slist *tslist;
- list_for_each_entry(tslist,head,list)
- if(tslist->s==n){
- list_add_node(&tslist->head,tbuf,tbuf+len-1);
- return;
- }
- tslist=newp(tslist);
- tslist->s=n;
- LIST_HEAD_INIT(tslist,list);
- LIST_HEAD_INIT(tslist,head);
- list_add_tail(&tslist->list,head);
- list_add_node(&tslist->head,tbuf,tbuf+len-1);
- }
- struct slist *list_entry_snode(struct list_head *list)
- {
- return list_entry(list,struct slist*,list);
- }
- struct list_head *list_by_level(struct list_head *head)
- {
- // printf("\n\nbefore!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n\n");
- // show_s_list(head);
- struct list_head *thead=newp(thead);
- init_list_head(thead);
- int maxlevel=0;
- struct slist *snode;
- list_for_each_entry(snode,head,list)
- if(maxlevel<snode->s)
- maxlevel=snode->s;
- // printf("\nmaxlevel==%d\n",maxlevel);
- struct slist** ppslist=new((maxlevel+1)*sizeof(struct slist*));
- while(!list_empty_careful(head)){
- snode=list_entry_snode(head->next);
- // printf("\nnode level:%d",snode->s);
- list_del_init(&snode->list);
- ppslist[snode->s]=snode;
- }
- int i;
- for(i=0;i<=maxlevel;i++)
- if(ppslist[i]){
- // printf("\nOK!!!!!!!\n");
- // _show_s_list(ppslist[i]);
- list_add(&ppslist[i]->list,thead);
- }
- // printf("\n\nafter:::::::::::::::::::::\n\n");
- // show_s_list(thead);
- return thead;
- }
- struct list_head *adjust_by_s(struct list_head *head)
- {
- struct list_head *thead=newp(thead);
- init_list_head(thead);
- // fop=stdout;
- // printf("\n\nadjust_by_s:::");
- // show_sub_list(head);
- while(!list_empty_careful(head)){
- struct node *tnode=list_entry_node(head->next);
- list_del_init(&tnode->list);
- _adjust_by_s(thead,tnode);
- }
- return list_by_level(thead);
- }
- void show_s_list(struct list_head *head)
- {
- struct slist *snode;
- fprintf(fop,"\n\n");
- list_for_each_entry(snode,head,list){
- fprintf(fop,"\n\ns%d:::",snode->s);
- show_sub_list(&snode->head);
- }
- }
阅读(1864) | 评论(0) | 转发(0) |