Chinaunix首页 | 论坛 | 博客
  • 博客访问: 192644
  • 博文数量: 27
  • 博客积分: 725
  • 博客等级: 上士
  • 技术积分: 347
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-04 09:01
文章分类

全部博文(27)

文章存档

2012年(15)

2011年(12)

分类: C/C++

2012-03-27 19:25:35

这个东东是两年前写的,当时是为了做一个模拟集成电路的辅助分析系统,分析电路呢就是要推导公式,什么电阻R,电容C,电感L,还有晶体管CMOS等等元器件的满天飞,印象特别深刻,为了推导一个仅有10来个晶体管的电路频率特性,要花上半天时间,写满几张草稿纸。当时就特别渴望如果电脑能帮助自己解决该有多好啊,因此就开始动手写了个所谓的电路分析系统,其实就是想偷懒而已啦,这个代数运算引擎呢,就是为了让电脑帮我推导一堆代数公式用的,额滴神啊,真不敢相信这是自己做的,虽然现在看来程序写的有点幼稚,不过这个程序一直都让自己觉得最有成就感,赞一个吧,哈哈。

  1. #include "c.h"
  2. #include "analysis.h"

  3. struct table_affix{
  4.     struct list_head list;
  5.     char *lval;
  6.     struct list_head head;
  7. };
  8. // -Cgs(Kua-Pdx+Cia-Dif)UlsFka(-Pds-Ajk+Ufd-Pof)Fua
  9. //提取括号内各项,并提取出来添加到链表中
  10. void analysis_exp_inside(struct list_head *head,char *pfrom,char *pto)
  11. {
  12.     char *p=pfrom;
  13.     if(ismp(*p))p++;
  14.     char *q=pfrom;
  15.     while(p<=pto){
  16.         if(ismp(*p)){
  17.             list_add_node(head,q,p-pto?p-1:p);
  18.             q=p;
  19.         }
  20.         p++;
  21.     }
  22.     list_add_node(head,q,p-1);
  23. }

  24. //返回从pstart开始的下一个单元块的地址
  25. char *get_block_exp(char *pstart)
  26. {
  27.     int flag=0;
  28.     char *p=pstart;
  29.     if(*p=='='||*p==0)return 0;
  30.     while(*p&&*
  31.         if(*p=='(')flag++;
  32.         else if(*p==')')flag--;
  33.         else if(ismp(*p)&&!flag&&p!=pstart)
  34.             return p-1;
  35.         p++;
  36.     }
  37.     return p-1;
  38. }

  39. // -Cgs(Kua-Pdx+Cia-Dif)UlsFka(-Pds-Ajk+Ufd-Pof)Fua
  40. //对一个带有括号的单元块进行链表化
  41. struct list_head *set_block_list(char *pstart,char *pto)
  42. {
  43.     struct list_head *head=newp(head);
  44.     init_list_head(head);
  45.     char *p=pstart,*q=p;
  46.     while(p<=pto){
  47.         if(*q=='(')q++;
  48.         if((*p=='('&&p>q)||*p==')'||p==pto){
  49.             struct node *tnode=node_new((int)sizeof(struct list_head));
  50.             init_list_head((struct list_head*)(tnode->data));
  51.             list_add_tail(&tnode->list,head);
  52.             analysis_exp_inside((struct list_head*)(tnode->data),q,p-pto||*p==')'?p-1:p);
  53.             q=p+1;
  54.         }
  55.         p++;
  56.     }
  57.     return head;
  58. }

  59. //对整个表达式建立链表
  60. struct list_head *set_exp_list(char *buf)
  61. {
  62.     char *p=buf,*q;
  63.     struct list_head *head=newp(head);
  64.     init_list_head(head);
  65.     while(1){
  66.         q=get_block_exp(p);
  67.         if(q){
  68.             struct list_head *thead=set_block_list(p,q);
  69.             struct node *tnode=node_new(0);
  70.             list_add_tail(&tnode->list,head);
  71.             tnode->data=thead;
  72.             p=q+1;
  73.         }
  74.         else break;
  75.     }
  76.     return head;
  77. }

  78. //对单项式进行括号展开
  79. void _extend_exp(struct list_head *head)
  80. {
  81.     while(!list_is_singular(head)){
  82.         struct node *nodea=list_entry_node(head->next);list_del(&nodea->list);
  83.         struct node *nodeb=list_entry_node(head->next);list_del(&nodeb->list);
  84.         struct node *nodec=node_new(sizeof(struct list_head));
  85.         init_list_head((struct list_head*)nodec->data);
  86.         list_add(&nodec->list,head);
  87.         struct node *noded,*nodee;
  88.         list_for_each_entry(noded,(struct list_head*)nodea->data,list)
  89.             list_for_each_entry(nodee,(struct list_head*)nodeb->data,list)
  90.                 unite_node_str((struct list_head*)nodec->data,noded->data,nodee->data);
  91.         destroy_node(nodea);
  92.         destroy_node(nodeb);
  93.     }
  94. }

  95. //对整个表达式进行括号展开
  96. struct list_head* extend_exp(char *buf)
  97. {
  98.     struct list_head *head=set_exp_list(buf);
  99.     struct node *tnode;
  100.     list_for_each_entry(tnode,head,list)
  101.         _extend_exp((struct list_head*)tnode->data);
  102.     return head;
  103. }

  104. void table_add_affix(struct list_head *affxhead,struct node *tnode)
  105. {
  106.     char *buf=(char*)tnode->data;
  107.     char *lval=strchr_reverse(buf,'V');
  108.     myassert(lval!=0);
  109.     int len=lval-buf+1;
  110.     char *rval=new(len);
  111.     strncpy(rval,buf,len-1);
  112.     table_insert_value(affxhead,lval,rval);
  113. }

  114. struct list_head *set_affix_table(struct list_head *head)
  115. {
  116.     struct list_head *affxhead=newp(affxhead);
  117.     init_list_head(affxhead);
  118.     struct node *nodea;
  119.     list_for_each_entry(nodea,head,list)
  120.         table_add_affix(affxhead,nodea);
  121.     return affxhead;
  122. }

  123. struct list_head *_exp_mul_extend(struct list_head *nodelista,struct list_head *nodelistb)
  124. {
  125.     struct list_head *head=newp(head);
  126.     init_list_head(head);
  127.     struct node *nodea,*nodeb;
  128.     list_for_each_entry(nodea,nodelista,list)
  129.         list_for_each_entry(nodeb,nodelistb,list)
  130.             unite_node_str(head,(char*)nodea->data,(char*)nodeb->data);
  131.     return head;
  132. }

  133. struct list_head *set_list_negative(struct list_head *nodelist)
  134. {
  135.     struct list_head head;
  136.     init_list_head(&head);
  137.     char *buf="-";
  138.     int len=strlen(buf);
  139.     analysis_exp_inside(&head,buf,buf+len-1);
  140.     return _exp_mul_extend(nodelist,&head);
  141. }

  142. struct list_head*exp_mul_extend(struct list_head *affixhead,struct list_head *nodehead)
  143. {
  144.     struct list_head *taffix=affix_chain_copy(affixhead);
  145.     struct table_affix *affixtable;
  146.     list_for_each_entry(affixtable,taffix,list)
  147.     {
  148.         struct list_head *head=_exp_mul_extend(&affixtable->head,nodehead);
  149.         list_kill_unite(&affixtable->head,head);
  150.     }
  151.     return taffix;
  152. }

  153. struct list_head* set_affix_negative(struct list_head *affixhead)
  154. {
  155.     struct list_head head;
  156.     init_list_head(&head);
  157.     char *buf="-";
  158.     int len=strlen(buf);
  159.     analysis_exp_inside(&head,buf,buf+len-1);
  160.     return exp_mul_extend(affixhead,&head);
  161. }

  162. struct list_head* affix_table_unite(struct list_head *affixheada,struct list_head *affixheadb)
  163. {
  164.     struct list_head *new_affixheadb=set_affix_negative(affixheadb);
  165.     struct list_head *new_affixheada=affix_chain_copy(affixheada);
  166.     struct table_affix *affixtable;
  167.     list_for_each_entry(affixtable,new_affixheadb,list){
  168.         char *lval=affixtable->lval;
  169.         struct table_affix *taffix=table_search_lval(new_affixheada,lval);
  170.         if(!taffix)taffix=table_new_lval(new_affixheada,lval);
  171.         list_put_unite(&taffix->head,&affixtable->head);
  172.     }
  173.     return new_affixheada;
  174. }

  175. struct list_head *affix_table_mul(struct list_head *affixheada,struct list_head *affixheadb,char *lval)
  176. {
  177.     struct table_affix *affixtablea=table_search_lval(affixheada,lval);
  178.     struct table_affix *affixtableb=table_search_lval(affixheadb,lval);
  179.     if(!affixtablea||!affixtableb)return 0;
  180.     struct list_head *new_affixheada=exp_mul_extend(affixheada,&affixtableb->head);
  181.     struct list_head *new_affixheadb=exp_mul_extend(affixheadb,&affixtablea->head);
  182.     affixtablea=table_search_lval(new_affixheada,lval);
  183.     affixtableb=table_search_lval(new_affixheadb,lval);
  184.     list_del(&affixtablea->list);
  185.     list_del(&affixtableb->list);
  186.     struct list_head *affixhead=newp(affixhead);
  187.     init_list_head(affixhead);
  188.     return affix_table_unite(new_affixheada,new_affixheadb);
  189. }

  190. struct list_expchain *setup_lval_analysis_affixchain(struct list_head *upchainhead,char *lval)
  191. {
  192.     struct list_expchain *expchain=newp(expchain);
  193.     LIST_HEAD_INIT(expchain,lval_on);
  194.     LIST_HEAD_INIT(expchain,lval_off);
  195.     while(!list_empty_careful(upchainhead)){
  196.         struct node *tnode=list_entry_node(upchainhead->next);
  197.         list_del_init(&tnode->list);
  198.         if(table_search_lval(tnode->data,lval))
  199.             list_add_tail(&tnode->list,&expchain->lval_on);
  200.         else list_add_tail(&tnode->list,&expchain->lval_off);
  201.     }
  202.     return expchain;
  203. }

  204. struct list_head *cal_expchain2_affixchain(struct list_expchain *expchain,char *lval)
  205. {
  206.     struct list_head *affixhead=newp(affixhead);
  207.     init_list_head(affixhead);
  208.     list_kill_unite(affixhead,&expchain->lval_off);
  209. //    fprintf(fop,"\n\nshow A:\n");
  210. //    show_global_affix_table(affixhead);
  211. //    fprintf(fop,"\n\nlval_off (%s):\n",lval);
  212. //    show_global_affix_table(affixhead);
  213. //    fprintf(fop,"\n\nlval_on (%s):\n",lval);
  214. //    show_global_affix_table(&expchain->lval_on);
  215.     
  216.     struct node *tnode;
  217.     struct list_head *lval_on=&expchain->lval_on;
  218.     
  219.     struct node *pnode=list_entry_node(lval_on->next);
  220. //    fprintf(fop,"\n\nshow_affix_table B:\n");
  221. //    show_affix_table((struct list_head*)pnode->data);
  222.     
  223.     list_del_init(&pnode->list);
  224.     
  225. //    fprintf(fop,"\n\nlval_on A(%s):\n",lval);
  226. //    show_global_affix_table(&expchain->lval_on);
  227.     
  228. //    fprintf(fop,"\n\nlast show_affix_table:\n");
  229.     
  230.     list_for_each_entry(tnode,lval_on,list){
  231.         struct list_head *affix_new=affix_table_mul(pnode->data,tnode->data,lval);
  232. //        show_affix_table(affix_new);
  233. //        fprintf(fop,"\n\n");
  234.         struct node *unode=node_new(0);
  235.         unode->data=affix_new;
  236.         list_add_tail(&unode->list,affixhead);
  237.     }
  238.     #if 0    
  239. #endif
  240. //    fprintf(fop,"\n\nshow B:\n");
  241. //    show_global_affix_table(affixhead);
  242.     return affixhead;
  243. }

  244. struct list_head *cal_affix_lval(struct list_head *upchainhead,char *lval)
  245. {
  246.     return cal_expchain2_affixchain(setup_lval_analysis_affixchain(upchainhead,lval),lval);
  247. }

  248. struct list_head *cal_affix(struct list_head *head)
  249. {
  250.     struct list_head *lvallist=setup_lval_list(head);
  251.     int i=0;
  252.     while(!list_empty_careful(lvallist)){
  253.         struct node *tnode=list_entry_node(lvallist->next);
  254.         list_del_init(&tnode->list);
  255.         struct list_head *thead=cal_affix_lval(head,tnode->data);
  256.         fprintf(fop,"\n\nCalculate start %d::%s",++i,tnode->data);
  257.         show_global_affix_table(thead);
  258.         head=thead;
  259.     }
  260.     return head;
  261. }


  262. void global_cal_circuit()
  263. {
  264.     read_netlist();
  265.     set_global_affix();
  266.     show_global_affix_table(&g_affixhead);
  267.     struct list_head *head=cal_affix(&g_affixhead);
  268.     
  269.     struct node *tnode=list_entry_node(head->next);
  270.     struct list_head *affixhead=tnode->data;
  271.     struct table_affix *affix_vi=table_search_lval(affixhead,"VIN");
  272.     struct table_affix *affix_vo=table_search_lval(affixhead,"VOUT");
  273.     myassert(affix_vi&&affix_vo);
  274.     
  275.     
  276.     struct list_head *vi_list=set_list_negative(&affix_vi->head);
  277.     struct list_head *vo_list=&affix_vo->head;
  278.     
  279.     
  280.     struct list_head *vi_adjust=adjust_res_list(vi_list);
  281.     struct list_head *vo_adjust=adjust_res_list(vo_list);
  282.     
  283.         
  284.     struct list_head *vi_by_s=adjust_by_s(vi_adjust);
  285.     struct list_head *vo_by_s=adjust_by_s(vo_adjust);

  286.     fprintf(fop,"\n\nVo/Vi =====\n");

  287.     show_s_list(vi_by_s);
  288.     fprintf(fop,"\n________________________________________________________________________________");
  289.     show_s_list(vo_by_s);
  290. }

  291. #include "c.h"
  292. #include "analysis.h"
  293. #include "table.h"
  294. struct list_head g_exphead;
  295. struct list_head g_affixhead;
  296. struct list_head g_lvalhead;
  297. FILE *fop;
  298. void read_netlist()
  299. {
  300.     init_list_head(&g_exphead);
  301.     FILE *fp=fopen(FILE_PATH,"r");
  302.     fop=fopen(OUT_PUT_PATH,"w");
  303.     
  304.     myassert(fp&&fop);
  305.     struct node *tnode;
  306.     char *buf;
  307.     while(!feof(fp)){
  308.         tnode=node_new(0);
  309.         buf=new(128);
  310.         fscanf(fp,"%s",buf);
  311.         fprintf(fop,"\n%s",buf);
  312.         tnode->data=buf;
  313.         list_add_tail(&tnode->list,&g_exphead);
  314.     }
  315.     fprintf(fop,"\n\n");
  316. }

  317. void set_global_affix()
  318. {
  319.     init_list_head(&g_affixhead);
  320.     struct node *tnode;
  321.     list_for_each_entry(tnode,&g_exphead,list)
  322.     {
  323.         struct list_head *head=list_unite(extend_exp((char*)tnode->data));
  324.         struct list_head *affxhead=set_affix_table(head);
  325.         struct node *nodea=node_new(0);
  326.         nodea->data=affxhead;
  327.         list_add_tail(&nodea->list,&g_affixhead);
  328.     }
  329. }

  330. struct node *node_new(int size)
  331. {
  332.     struct node *tnode=newp(tnode);
  333.     LIST_HEAD_INIT(tnode,list);
  334.     tnode->data=new(size);
  335.     return tnode;
  336. }

  337. struct node* list_add_node(struct list_head *head,char *pfrom,char *pto)
  338. {
  339.     int len=pto-pfrom+2;
  340.     struct node *tnode=node_new(len);
  341.     strncpy(tnode->data,pfrom,len-1);
  342.     list_add_tail(&tnode->list,head);
  343. //    printf("\nlist_add_node::%s",tnode->data);
  344.     return tnode;
  345. }

  346. struct node *_list_add_node(struct list_head *head,char *buf)
  347. {
  348.     list_add_node(head,buf,buf+strlen(buf)-1);
  349. }

  350. void show_sub_list(struct list_head *head)
  351. {
  352. //    fprintf(fop,"\n\nshow_sub_list:\n");
  353.     struct node *tnode;
  354.     if(!fop)fop=stdout;
  355.     fprintf(fop,"\n");
  356.     list_for_each_entry(tnode,head,list)
  357.         fprintf(fop,"%s\t",(char*)tnode->data);
  358. }

  359. void show_list(struct list_head *head)
  360. {
  361.     struct node *tnode;
  362. //    fprintf(fop,"\nA new round:\n");
  363.     list_for_each_entry(tnode,head,list)
  364.     {
  365.         show_sub_list((struct list_head*)tnode->data);
  366.         fprintf(fop,"\n");
  367.     }
  368. }

  369. void show_exp_list(struct list_head *head)
  370. {
  371.     struct node *tnode;
  372.     list_for_each_entry(tnode,head,list)
  373.         show_list((struct list_head*)tnode->data);
  374. }

  375. void show_string(char *pstart,char *pto)
  376. {
  377.     fprintf(fop,"\nstring:::");
  378.     while(pstart<=pto)
  379.         fprintf(fop,"%c",*pstart++);
  380.     fprintf(fop,"\n");
  381. }

  382. void unite_node_str(struct list_head *head,char *stra,char *strb)
  383. {
  384.     int lena=strlen(stra);
  385.     int lenb=strlen(strb);
  386.     int lenc=lena+lenb+1;
  387.     int affix=1;
  388.     if(*stra=='-')affix*=-1;
  389.     if(*strb=='-')affix*=-1;
  390.     
  391.     struct node *tnode=node_new(lenc);
  392.     char *buf=tnode->data;
  393.     if(affix==1)buf[0]='+';
  394.     else if(affix==-1)buf[0]='-';
  395.     strcpy(buf+1,stra+ismp(*stra));
  396.     strcat(buf+1,strb+ismp(*strb));
  397.     list_add_tail(&tnode->list,head);
  398. }

  399. void destroy_node(struct node *tnode)
  400. {
  401.     
  402. }
  403. //+sCm1go2Gm1sCc2
  404. char *next_div(char *buf)
  405. {
  406.     myassert(buf&&*buf);
  407.     char *str[]={"sC","gm","go","G"};
  408.     int size=sizeof(str)/sizeof(str[0]);
  409. //    printf("\nbuf is ::%s",buf);
  410.     int i,len=strlen(buf);
  411. //    printf("\nbuf size is ::%d",len);
  412.     char *pmin=buf+len;
  413. //    printf("\n\n\nmin is ::%s",pmin);
  414.     for(i=0;i<size;i++)
  415.     {
  416. //        printf("\n\n\nmin is ::%s, buf is ::%s, str[i] is ::%s",pmin,buf,str[i]);
  417.         char *t=strstr(buf,str[i]);
  418.         if(!t)continue;
  419. //        printf("\nt is ::%s",t);
  420.         pmin=min(pmin,t);
  421. //        printf("\nmin res is ::%s",pmin);
  422.     }
  423.     return pmin;
  424. }
  425. //char *buf="+sCm2sCm1gm3sCm1go2Gm1sCc2";
  426. struct list_head *analysis_buf(char *buf)
  427. {
  428.     int len=strlen(buf)+1;
  429. //    printf("buf len:%d",len);
  430.     char *tbuf=new(len);
  431.     
  432.     strcpy(tbuf,buf);
  433.     char *p=tbuf,*q;
  434.     struct list_head *head=newp(head);
  435.     init_list_head(head);
  436.     if(ismp(*p)){
  437.         list_add_node(head,p,p);
  438.         p++;
  439.     }
  440.     char *str[]={"sC","gm","go","G"};
  441.     int size=sizeof(str)/sizeof(str[0]);
  442.     int i;
  443.     for(i=0;i<size;i++){
  444. //        int j=0;
  445.         do{
  446. //            if(j++>6)break;
  447.             q=strstr(p,str[i]);
  448. //            if(q)printf("\n\n\nres-->q::%s",q);
  449. //            else printf("\nres-->q::null");
  450.             if(!q)break;
  451.             char *t=next_div(q+1);
  452. //            if(*t)printf("\nres-->t::%s",t);
  453. //            else printf("\nres-->t::null");
  454.             list_add_node(head,q,t-1);
  455. //            printf("\n::%s",list_add_node(head,q,t-1)->data);
  456. //            printf("\nt::::%s",t);
  457. //            printf("\nq::::%s",q);
  458.             if(*t)strcpy(q,t);
  459.             else *q=0;
  460. //            printf("\nbuf is ::%s",tbuf);
  461.         }while(q);
  462.     }
  463. //    show_sub_list(head);
  464.     return head;
  465. }

  466. struct divlist *div_list(struct list_head *head)
  467. {
  468.     struct divlist *div=newp(div);
  469.     LIST_HEAD_INIT(div,positive);
  470.     LIST_HEAD_INIT(div,negative);
  471.     while(!list_empty_careful(head)){
  472.         struct node *tnode=list_entry_node(head->next);
  473.         list_del_init(&tnode->list);
  474.         if(*(char*)tnode->data=='-')
  475.             list_add_tail(&tnode->list,&div->negative);
  476.         else list_add_tail(&tnode->list,&div->positive);
  477.     }
  478.     return div;
  479. }
  480. //char *buf="+sCm2sCm1gm3sCm1go2Gm1sCc2";
  481. struct node *list_entry_node(struct list_head *list)
  482. {
  483.     return list_entry(list,struct node*,list);
  484. }

  485. char *strchr_reverse(char *p,char ch)
  486. {
  487.     char *q=p+strlen(p)-1;
  488.     while(q>=p)
  489.         if(*q==ch)return q;
  490.         else q--;
  491.     return 0;
  492. }

  493. void show_affix_table(struct list_head *head)
  494. {
  495.     struct table_affix *affxtable;
  496.     fprintf(fop,"\n");
  497.     int flag=0;
  498.     list_for_each_entry(affxtable,head,list)
  499.     {
  500.         if(&affxtable->list!=head->next)fprintf(fop," + ");
  501.         struct node *tnode;
  502.         tnode=list_entry_node(affxtable->head.next);
  503.         if(!list_is_singular(&affxtable->head)||*(char*)(tnode->data)=='-'){flag=1;fprintf(fop,"(");}
  504.         
  505.         list_for_each_entry(tnode,&affxtable->head,list)
  506.         {
  507.             char *p=(char*)tnode->data;
  508.             if(&tnode->list==affxtable->head.next)
  509.                 fprintf(fop,"%s",p+(*p=='+'?1:0));
  510.             else fprintf(fop,"%s",(char*)tnode->data);
  511.         }
  512.         if(flag){fprintf(fop,")");flag=0;}
  513.         fprintf(fop,"%s",affxtable->lval);
  514.     }
  515.     fprintf(fop,"=0\n");
  516. }

  517. void show_global_affix_table(struct list_head *head)
  518. {
  519.     struct node *tnode;
  520.     fprintf(fop,"\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
  521.     fprintf(fop,"\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
  522.     list_for_each_entry(tnode,head,list)
  523.         show_affix_table((struct list_head*)tnode->data);
  524.     fprintf(fop,"\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
  525.     fprintf(fop,"\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
  526. }

  527. void __list_unite(struct list_head *global,struct list_head *head)
  528. {
  529.     struct node *tnode;
  530.     struct list_head *list;
  531.     struct list_head *next;
  532.     for(list=head->next;list!=head;)
  533.     {
  534.         tnode=list_entry_node(list);
  535.         next=list->next;
  536.         list_del(list);
  537.         list_add_tail(list,global);
  538.         list=next;
  539.     }
  540. }

  541. void _list_unite(struct list_head *global,struct list_head *head)
  542. {
  543.     struct node *tnode;
  544.     list_for_each_entry(tnode,head,list)
  545.         __list_unite(global,(struct list_head*)tnode->data);
  546. }

  547. struct list_head *list_unite(struct list_head *thead)
  548. {
  549.     struct list_head *global=newp(global);
  550.     init_list_head(global);
  551.     struct node *tnode;
  552.     list_for_each_entry(tnode,thead,list)
  553.         _list_unite(global,(struct list_head*)tnode->data);
  554.     return global;
  555. }

  556. void list_kill_unite(struct list_head *origin,struct list_head *new)
  557. {
  558.     init_list_head(origin);
  559.     struct list_head *list,*next;
  560.     for(list=new->next;list!=new;)
  561.     {
  562.         next=list->next;
  563.         list_del_init(list);
  564.         list_add_tail(list,origin);
  565.         list=next;
  566.     }
  567. }

  568. void list_put_unite(struct list_head *heada,struct list_head *headb)
  569. {
  570.     struct list_head *list,*next;
  571.     for(list=headb->next;list!=headb;)
  572.     {
  573.         next=list->next;
  574.         list_del_init(list);
  575.         list_add_tail(list,heada);
  576.         list=next;
  577.     }
  578. }

  579. struct node *node_copy(struct node *tnode)
  580. {
  581.     struct node *pnode=node_new(0);
  582.     char *buf=tnode->data;
  583.     int len=strlen(buf)+1;
  584.     char *tbuf=new(len);
  585.     strcpy(tbuf,buf);
  586.     pnode->data=tbuf;
  587.     return pnode;
  588. }

  589. struct list_head *node_list_copy(struct list_head *head)
  590. {
  591.     struct list_head *thead=newp(thead);
  592.     init_list_head(thead);
  593.     struct node *tnode;
  594.     list_for_each_entry(tnode,head,list){
  595.         struct node *pnode=node_copy(tnode);
  596.         list_add_tail(&pnode->list,thead);
  597.     }
  598.     return thead;
  599. }

  600. struct table_affix *affix_table_copy(struct table_affix *affixtable)
  601. {
  602.     struct table_affix *taffix=newp(taffix);
  603.     LIST_HEAD_INIT(taffix,list);
  604.     LIST_HEAD_INIT(taffix,head);
  605.     char *lval=affixtable->lval;
  606.     int len=strlen(lval)+1;
  607.     char *buf=new(len);
  608.     strcpy(buf,lval);
  609.     taffix->lval=buf;
  610.     struct list_head *thead=node_list_copy(&affixtable->head);
  611.     list_kill_unite(&taffix->head,thead);
  612.     return taffix;
  613. }

  614. struct list_head *affix_chain_copy(struct list_head *head)
  615. {
  616.     struct list_head *thead=newp(thead);
  617.     init_list_head(thead);
  618.     struct table_affix *affixtable;
  619.     list_for_each_entry(affixtable,head,list){
  620.         struct table_affix *taffix=affix_table_copy(affixtable);
  621.         list_add_tail(&taffix->list,thead);
  622.     }
  623.     return thead;
  624. }
  625. //////////////////////////////////////////////////////////////////////////
  626. //////////////////////////////////////////////////////////////////////////
  627. //////////////////////////////////////////////////////////////////////////
  628. //////////////////////////////////////////////////////////////////////////
  629. struct node *list_search_lval(struct list_head *head,char *buf)
  630. {
  631.     struct node *tnode;
  632.     list_for_each_entry(tnode,head,list)
  633.         if(!strcmp(tnode->data,buf))return tnode;
  634.     return 0;
  635. }

  636. struct list_head* setup_lval_list()
  637. {
  638.     struct node *tnode;
  639.     init_list_head(&g_lvalhead);
  640.     list_for_each_entry(tnode,&g_affixhead,list)
  641.     {
  642.         struct list_head *head=tnode->data;
  643.         struct table_affix *affixtable;
  644.         list_for_each_entry(affixtable,head,list)
  645.         {
  646.             char *buf=affixtable->lval;
  647.             int len=strlen(buf);
  648.             if(strcmp(buf,"VIN")&&strcmp(buf,"VOUT")&&!list_search_lval(&g_lvalhead,buf))
  649.                 list_add_node(&g_lvalhead,buf,buf+len-1);
  650.         }
  651.     }
  652.     return &g_lvalhead;
  653. }

  654. void show_last_result(struct list_head *head)
  655. {
  656.     struct node *tnode=list_entry_node(head->next);
  657.     struct list_head *affixhead=tnode->data;
  658.     struct table_affix *affix_vi=table_search_lval(affixhead,"VIN");
  659.     struct table_affix *affix_vo=table_search_lval(affixhead,"VOUT");
  660.     myassert(affix_vi&&affix_vo);
  661.     fprintf(fop,"\n\nThe last result is:\n\n");
  662.     fprintf(fop,"Vo/Vi =====\n");
  663.     struct list_head *vi_list=set_list_negative(&affix_vi->head);
  664.     struct list_head *vo_list=&affix_vo->head;
  665.     show_sub_list(vi_list);
  666.     fprintf(fop,"\n________________________________________________________________________________");
  667.     show_sub_list(vo_list);
  668.     fprintf(fop,"\n\nResult end************\n\n");
  669. }

  670. int is_same_list(struct list_head *heada,struct list_head *headb)
  671. {
  672.     struct node *unode=list_entry_node(heada->next);
  673.     if(ismp(*(char*)unode->data))list_del_init(&unode->list);
  674.     unode=list_entry_node(headb->next);
  675.     if(ismp(*(char*)unode->data))list_del_init(&unode->list);
  676.         
  677.     while(!list_empty_careful(heada)){
  678.         struct node *nodea=list_entry_node(heada->next);
  679.         struct node *nodeb;
  680.         int flag=0;
  681.         list_for_each_entry(nodeb,headb,list){
  682. //            printf("\n%s ::::::::::::: %s",nodea->data,nodeb->data);
  683.             if(!strcmp(nodeb->data,nodea->data)){
  684. //                printf("\nOK!!!!!!!!!!!!!!!!!!!!!\n");
  685.                 list_del(&nodea->list);
  686.                 list_del(&nodeb->list);
  687.                 flag=1;
  688.                 break;
  689.             }
  690.         }
  691.         if(!flag)return 0;
  692.     }
  693. //    printf("\nlist_empty_careful(headb):::%d",list_empty_careful(headb));
  694.     return list_empty_careful(headb);
  695. }

  696. struct list_head *adjust_res_list(struct list_head *head)
  697. {
  698.     struct list_head *thead=newp(thead);
  699. //    FILE *ft=fop;
  700.     init_list_head(thead);
  701.     while(!list_empty_careful(head)){
  702. //        printf("\nshow head:\n");show_sub_list(head);
  703.         int t=1,flag=0;
  704.         struct node *tnode=list_entry_node(head->next);
  705.         list_del_init(&tnode->list);
  706.         struct node *nodea;
  707.         if(*(char*)tnode->data=='-')t=-1;
  708.         list_for_each_entry(nodea,head,list){
  709.             if((*(char*)nodea->data=='-'&&t==-1)||(*(char*)nodea->data=='+'&&t==+1))continue;
  710. //            printf("\nSuccess!!!! %s:::::::::::%s\n",tnode->data,nodea->data);
  711.             
  712.             
  713. //            fop=stdout;
  714.             struct list_head *heada=analysis_buf(tnode->data);
  715. //            printf("\nshow heada:\n");show_sub_list(heada);
  716.             struct list_head *headb=analysis_buf(nodea->data);
  717. //            printf("\nshow headb:\n");show_sub_list(headb);
  718.             if(is_same_list(heada,headb)){
  719. //                printf("\nOK!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
  720. //                printf("\n::%s :::: %s",tnode->data,nodea->data);
  721.                 list_del_init(&nodea->list);
  722.                 flag=1;
  723.                 break;
  724.             }
  725.         }
  726.         if(!flag)list_add_tail(&tnode->list,thead);
  727.     }
  728. //    fop=ft;
  729.     return thead;
  730. }

  731. struct list_head* test_show_last_result(struct list_head *head)
  732. {
  733.     struct node *tnode=list_entry_node(head->next);
  734.     struct list_head *affixhead=tnode->data;
  735.     struct table_affix *affix_vi=table_search_lval(affixhead,"VIN");
  736.     struct table_affix *affix_vo=table_search_lval(affixhead,"VOUT");
  737.     myassert(affix_vi&&affix_vo);
  738.     fprintf(fop,"\n\nThe last result is:\n\n");
  739.     fprintf(fop,"Vo/Vi =====\n");
  740.     struct list_head *vi_list=set_list_negative(&affix_vi->head);
  741.     struct list_head *vo_list=&affix_vo->head;
  742. //    show_sub_list(vi_list);
  743.     show_sub_list(adjust_res_list(vi_list));
  744.     fprintf(fop,"\n________________________________________________________________________________");
  745.     struct list_head *new_vo_list=adjust_res_list(vo_list);
  746.     show_sub_list(new_vo_list);
  747. //    show_sub_list(vo_list);
  748.     
  749.     fprintf(fop,"\n\nResult end************\n\n");
  750.     return new_vo_list;
  751. }

  752. char *list_unite2buf(struct list_head *head)
  753. {
  754.     struct node *tnode;
  755.     int len=0;
  756.     list_for_each_entry(tnode,head,list)
  757.         len+=strlen(tnode->data);
  758.     char *buf=new(len+1);
  759.     list_for_each_entry(tnode,head,list)
  760.         strcat(buf,tnode->data);
  761.     return buf;
  762. }

  763. void _adjust_by_s(struct list_head *head,struct node *tnode)
  764. {
  765. //    printf("\ntnode data::%s",tnode->data);
  766.     struct list_head *thead=analysis_buf(tnode->data);
  767.     
  768. //    printf("\n\nstart!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
  769. //    show_sub_list(thead);
  770. //    printf("\n\nstop!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
  771.     int n=0;
  772.     struct node *nodea;
  773.     list_for_each_entry(nodea,thead,list){
  774.         char *p=nodea->data;
  775. //        printf("\nOK::::%s",nodea->data);
  776.         if(!strncmp(p,"sC",2)){
  777.             n++;
  778.             strcpy(p,p+1);
  779.         }
  780.     }
  781. //    printf("\nthe level is : %d",n);
  782.     char *tbuf=list_unite2buf(thead);
  783. //    printf("\nafter unite ::: %s",tbuf);
  784.     int len=strlen(tbuf);
  785.     struct slist *tslist;
  786.     list_for_each_entry(tslist,head,list)
  787.         if(tslist->s==n){
  788.             list_add_node(&tslist->head,tbuf,tbuf+len-1);
  789.             return;
  790.         }
  791.     tslist=newp(tslist);
  792.     tslist->s=n;
  793.     LIST_HEAD_INIT(tslist,list);
  794.     LIST_HEAD_INIT(tslist,head);
  795.     list_add_tail(&tslist->list,head);
  796.     list_add_node(&tslist->head,tbuf,tbuf+len-1);
  797. }

  798. struct slist *list_entry_snode(struct list_head *list)
  799. {
  800.     return list_entry(list,struct slist*,list);
  801. }

  802. struct list_head *list_by_level(struct list_head *head)
  803. {
  804. //    printf("\n\nbefore!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n\n");
  805. //    show_s_list(head);
  806.     struct list_head *thead=newp(thead);
  807.     init_list_head(thead);
  808.     int maxlevel=0;
  809.     struct slist *snode;
  810.     list_for_each_entry(snode,head,list)
  811.         if(maxlevel<snode->s)
  812.             maxlevel=snode->s;
  813. //    printf("\nmaxlevel==%d\n",maxlevel);
  814.     struct slist** ppslist=new((maxlevel+1)*sizeof(struct slist*));
  815.     while(!list_empty_careful(head)){
  816.         snode=list_entry_snode(head->next);
  817. //        printf("\nnode level:%d",snode->s);
  818.         list_del_init(&snode->list);
  819.         ppslist[snode->s]=snode;
  820.     }
  821.     int i;
  822.     for(i=0;i<=maxlevel;i++)
  823.         if(ppslist[i]){
  824. //            printf("\nOK!!!!!!!\n");
  825. //            _show_s_list(ppslist[i]);
  826.             list_add(&ppslist[i]->list,thead);
  827.         }
  828. //    printf("\n\nafter:::::::::::::::::::::\n\n");
  829. //    show_s_list(thead);
  830.     return thead;
  831. }

  832. struct list_head *adjust_by_s(struct list_head *head)
  833. {
  834.     struct list_head *thead=newp(thead);
  835.     init_list_head(thead);
  836. //    fop=stdout;
  837. //    printf("\n\nadjust_by_s:::");
  838. //    show_sub_list(head);
  839.     while(!list_empty_careful(head)){
  840.         struct node *tnode=list_entry_node(head->next);
  841.         list_del_init(&tnode->list);
  842.         _adjust_by_s(thead,tnode);
  843.     }
  844.     return list_by_level(thead);
  845. }

  846. void show_s_list(struct list_head *head)
  847. {
  848.     struct slist *snode;
  849.     fprintf(fop,"\n\n");
  850.     list_for_each_entry(snode,head,list){
  851.         fprintf(fop,"\n\ns%d:::",snode->s);
  852.         show_sub_list(&snode->head);
  853.     }
  854. }

阅读(1864) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~