📄 spice.cpp.bak
字号:
// test.cpp : Defines the entry point for the console application.
//
#include "string.h"
#include "stdio.h"
#include "malloc.h"
#include "conio.h"
#define VOLTAGE 0
#define RESISTANT 1
#define CAPACITY 2
#define INDUCTANTCE 3
void getline(char *str);
void scanblock();
struct node *splitstr(char *str);
struct component *subop(char *str,struct node *nd);
struct component *inductance(char *up,struct node *nd);
struct component *resistant(char *up,struct node *nd);
struct component *voltage(char *up,struct node *nd);
struct component *capacity(char *up,struct node *nd);
int getnode(char *str,struct node *nd);
struct component *subproc(struct node *nd,int num);
struct component *process();
int find(struct node *nd,char *str);
int flag=0;
FILE *of,*sf;
struct sbname
{
char nm[20];
long bpos,epos;
long len;
};
int sblen=0;
struct sbname sbn[50];
int index=1;
struct component //记录元件的信息,包括左节点,右节点,类型,value
{
int lnode,rnode;
char type;
float val;
struct component *next;
};
struct node
{
char name[20];
int val;
struct node *next;
};
int main(int argc, char* argv[])
{
struct component *allcom;
char path[20];
if(argc<2)
{
printf("usage: cmd the path of the file!\n");
printf("such: spice d:\abc.txt\n");
return 0;
}
if((of=fopen(argv[1],"r+"))==NULL)
{
printf("fail to open the file!\n");
return -1;
}
if((sf=fopen("D:\\res.txt","w+"))==NULL)
{
printf("fail to create the file!\n");
return -1;
}
scanblock();
flag=0;
allcom=process();
while(allcom!=NULL)
{
fprintf(sf,"lnode=%d rnode=%d type=%d\n",allcom->lnode,allcom->rnode,allcom->type);
allcom=allcom->next;
}
return 0;
}
void scanblock()
{
char str[500];
char *up;
char midsb[20];
char midsbname[20];
long cur;
while(flag==0)
{
cur=ftell(of);
getline(str);
up=strupr(str);
memset(midsb,0,20);
memset(midsbname,0,20);
sscanf(up,"%s %s",midsb,midsbname);
if(strcmp(midsb,".SUBCKT")==0)
{
strcpy(sbn[sblen].nm,midsbname);
sbn[sblen].bpos=cur;
cur=0;
while(flag==0)
{
getline(str);
cur++;
up=strupr(str);
memset(midsb,0,20);
sscanf(up,"%s",midsb);
if(strcmp(midsb,".ENDS")==0)
{
sbn[sblen].len=cur;
sbn[sblen].epos=ftell(of);
break;
}
}
sblen++;
}
}
fseek(of,0,SEEK_SET);
}
struct node *splitstr(char *str)
{
int i,k=0,len;
char midstr[20];
struct node *mid,*head,*p;
head=(struct node *)malloc(sizeof(struct node));
mid=head;
len=strlen(str);
memset(midstr,0,20);
for(i=0;i<len;i++)
{
if(str[i]!=' ')
{
midstr[k]=str[i];
k++;
}
else
{
while(str[i]==' ') i++;
i-=1;
p=(struct node *)malloc(sizeof(struct node));
strcpy(p->name,midstr);
p->next=NULL;
mid->next=p;
mid=p;
memset(midstr,0,20);
k=0;
}
}
p=(struct node *)malloc(sizeof(struct node));
strcpy(p->name,midstr);
p->next=NULL;
mid->next=p;
mid=p;
head=head->next;
return head;
}
void getline(char *str)
{
char ch;
int k;
memset(str,0,500);
int i=0;
k=fscanf(of,"%c",&ch);
while(ch!='\n'&&k!=EOF)
{
str[i]=ch;
i++;
k=fscanf(of,"%c",&ch);
}
if(k==EOF)
flag=1;
}
struct component *subop(char *str,struct node *nd)
{
struct node *hcom,*mid,*p;
struct component *res,*llll;
struct node *subnd,*subp,*submid;
long prepos;
prepos=ftell(of);
submid=subnd=(struct node *)malloc(sizeof(struct node));
int i=0;
int kk;
char midstr[500];
hcom=p=splitstr(str);
while(p!=NULL)
{
mid=p;
fprintf(sf,"%s ",p->name); //delete
p=p->next;
}
while(strcmp(mid->name,sbn[i].nm)!=0) i++;
fprintf(sf,"\nthe subcircuit number:%s\n",sbn[i].nm); //delete
fseek(of,sbn[i].bpos,SEEK_SET);
getline(midstr);
p=splitstr(midstr);
p=p->next;p=p->next;
hcom=hcom->next;
while(p!=NULL)
{
kk=getnode(hcom->name,nd);
fprintf(sf,"map net:%d\n",kk); //delete
subp=(struct node *)malloc(sizeof(struct node));
strcpy(subp->name,strupr(p->name));
subp->val=kk;
subp->next=NULL;
submid->next=subp;
submid=subp;
p=p->next;
hcom=hcom->next;
}
subnd=subnd->next;
submid=subnd;
while(submid!=NULL)
{
fprintf(sf,"the node name:%s\n",submid->name);
fprintf(sf,"the node numbet:%d\n",submid->val);
submid=submid->next;
}
llll=res=subproc(subnd,sbn[i].len);
while(llll!=NULL)
{
printf("type:%d \n",llll->type);
llll=llll->next;
}
fseek(of,prepos,SEEK_SET);
return res;
}
struct component *process()
{
struct component *res,*p,*mid;
char str[500];
char mk[20];
char *up;
struct node *nd;
nd=(struct node *)malloc(sizeof(struct node));
nd->next=NULL;
strcpy(nd->name,"");
nd->val=-1;
mid=res=(struct component *)malloc(sizeof(struct component));
while(flag==0)
{
getline(str);
up=strupr(str);
if(strlen(str)>0)
switch(up[0])
{
case '*':
break;
case 'V':
p=voltage(up,nd);
mid->next=p;
mid=p;
break;
case 'C':
p=capacity(up,nd);
mid->next=p;
mid=p;
break;
case 'R':
p=resistant(up,nd);
mid->next=p;
mid=p;
break;
case 'L':
p=inductance(up,nd);
mid->next=p;
mid=p;
break;
case 'X':
p=subop(up,nd);
mid->next=p;
mid=p;
while(mid->next!=NULL)
mid=mid->next;
break;
default: break;
}
sscanf(up,"%s",mk);
if(strcmp(mk,".SUBCKT")==0)
{
getline(str);
up=strupr(str);
sscanf(up,"%s",mk);
while(strcmp(mk,".ENDS")!=0)
{
getline(str);
up=strupr(str);
sscanf(up,"%s",mk);
}
}
}
res=res->next;
return res;
}
struct component *subproc(struct node *nd,int num)
{
struct component *res,*subp,*submid;
char str[500];
char *up;
int i=1;
res=(struct component *)malloc(sizeof(struct component));
submid=res;
while(i<num)
{
getline(str);
fprintf(sf,"%s\n",str); //delete
up=strupr(str);
switch(up[0])
{
case '*':
break;
case 'V':
subp=voltage(up,nd);
submid->next=subp;
submid=subp;
break;
case 'C':
subp=capacity(up,nd);
submid->next=subp;
submid=subp;
break;
case 'R':
subp=resistant(up,nd);
submid->next=subp;
submid=subp;
break;
case 'L':
subp=inductance(up,nd);
submid->next=subp;
submid=subp;
break;
case 'X':
subp=subop(up,nd);
submid->next=subp;
submid=subp;
break;
default: break;
}
i++;
}
res=res->next;submid=res;
return res;
}
struct component *voltage(char *up,struct node *nd)
{
int len,i=0;
len=strlen(up);
char ss[4][20];
struct component *newcom;
float val;
sscanf(up,"%s %s %s %s",ss[0],ss[1],ss[2],ss[3]);
//sscanf(ss[3],"%f",&val);
newcom=(struct component *)malloc(sizeof(struct component));
newcom->lnode=getnode(ss[1],nd);
newcom->rnode=getnode(ss[2],nd);
newcom->type=VOLTAGE;
newcom->next=NULL;
return newcom;
}
struct component *capacity(char *up,struct node *nd)
{
int len,i=0;
len=strlen(up);
char ss[4][20];
struct component *newcom;
float val;
sscanf(up,"%s %s %s %s",ss[0],ss[1],ss[2],ss[3]);
//sscanf(ss[3],"%f",&val);
newcom=(struct component *)malloc(sizeof(struct component));
newcom->lnode=getnode(ss[1],nd);
newcom->rnode=getnode(ss[2],nd);
newcom->type=CAPACITY;
newcom->next=NULL;
return newcom;
}
struct component *resistant(char *up,struct node *nd)
{
int len,i=0;
len=strlen(up);
char ss[4][20];
struct component *newcom;
float val;
sscanf(up,"%s %s %s %s",ss[0],ss[1],ss[2],ss[3]);
//sscanf(ss[3],"%f",&val);
newcom=(struct component *)malloc(sizeof(struct component));
newcom->lnode=getnode(ss[1],nd);
newcom->rnode=getnode(ss[2],nd);
newcom->type=RESISTANT;
newcom->next=NULL;
return newcom;
}
struct component *inductance(char *up,struct node *nd)
{
int len,i=0;
len=strlen(up);
char ss[4][20];
struct component *newcom;
float val;
sscanf(up,"%s %s %s %s",ss[0],ss[1],ss[2],ss[3]);
//sscanf(ss[3],"%f",&val);
newcom=(struct component *)malloc(sizeof(struct component));
newcom->lnode=getnode(ss[1],nd);
newcom->rnode=getnode(ss[2],nd);
newcom->type=INDUCTANTCE;
newcom->next=NULL;
return newcom;
}
int getnode(char *str,struct node *nd)
{
struct node *p,*mid;
if(strcmp(str,"0")==0)
return 0;
if(nd==NULL)
{
mid=(struct node *)malloc(sizeof(struct node));
strcpy(mid->name,str);
mid->next=NULL;
mid->val=index++;
nd=mid;
return index-1;
}
int fres;
fres=find(nd,str);
if(fres==-1)
{
p=nd;
while(p->next!=NULL)
p=p->next;
mid=(struct node *)malloc(sizeof(struct node));
strcpy(mid->name,str);
mid->next=NULL;
mid->val=index++;
p->next=mid;
return index-1;
}
else
return fres;
}
int find(struct node *nd,char *str)
{
struct node *p;
p=nd;
while((strcmp(p->name,str)!=0)&&p!=NULL)
p=p->next;
if(p!=NULL)
return p->val;
else
return -1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -