Here is my code:
#include <iostream>
#include <vector>
#include <fstream>
#include <stdio.h>
#include <string>
#include <cstring>
using namespace std;
class gate
{
private:
string op;
vector<string> ip;
string type;
public:
//to take in the type of gate
void takein_type(str ing a)
{type=a;}
//to take in input of gate
void ips(string c)
{ip.push_back(c );}
//to take in output of gate
void input_op(string a)
{op=a;}
//to read gate inputs
vector<string> read_ip()
{return ip;}
//to read value of output of gate
string read_op()
{return op;}
//read type of gate
string read_type()
{return type;}
//erase last input added as actually it is output of next gate
void rm()
{ip.erase(ip.be gin()+ip.size()-1);}
int fanout(gate g1)
{
int fout=0;
int p=g1.ip.size()-1;
while(p>=0)
{ if(g1.ip[p]==op)
{fout=fout+1;}
p=p-1;
}
return fout;
}
string ret_type()
{return type;}
int fanin(gate g1)
{
int fin=0;
for(int vv=0; vv<g1.ip.size() ; vv++)
{
if(g1.ip[vv]==op)
{fin++;}
}
return fin;
}
void output()
{
cout<<"output: "<<op<<endl ;
cout<<"gate type: "<<type<<en dl;
for(int i=0; i<ip.size(); i++)
{cout<<"inputs: "<<ip[i]<<endl;}
}
void flush()
{ip.clear();}
};
int main()
{
vector<gate> GATE;
vector<int> inp;
vector<int> out;
vector<string> inp_str;
vector<string> out_str;
char* pch;
string str2, str_temp, line;
int pins=0, ss=0, rr=0, c=0, bit=0;
gate g;
ifstream obj("b15_C.txt" );
if (obj.is_open())
{
while (! obj.eof() )
{
getline(obj,lin e);
char* temp; temp=&line[0];
if(*temp!='#')
{
pch = strtok(temp," ,()");
while (pch != NULL)
{
if(ss==1)
{inp_str.push_b ack(pch);ss=0;}
if(rr==1)
{out_str.push_b ack(pch);rr=0;}
if(pch[0]=='I'&&pch[1]=='N'&&pch[2]=='P'&&pch[3]=='U'&&pch[4]=='T')
{
inp.push_back(p ins);
pins++;
ss=1;
}
else if(pch[0]=='O'&&pch[1]=='U'&&pch[2]=='T'&&pch[3]=='P'&&pch[4]=='U'&&pch[5]=='T')
{
out.push_back(p ins);
pins++;
rr=1;
}
else if(pch[0]=='=')
{
if(c>0)
{
g.rm();
GATE.push_back( g);g.flush();
}
g.input_op(str_ temp);bit=0;
c++;
}
else if(str_temp=="= ")
{g.takein_type( pch);}
else if(bit==1)
{g.ips(pch);}
else if(str_temp=="n and" || str_temp=="and" || str_temp=="or" || str_temp=="nor" || str_temp=="not" || str_temp=="buf" || str_temp=="exor " || str_temp=="exno r")
{g.ips(pch);bit =1;}
str2=str_temp;
str_temp=pch;
pch = strtok (NULL, " ,()");
}
}
}
obj.close();
}
else {cout << "Unable to open file"; }
GATE.push_back( g);
//no of gates of each type
gate g1,g2;
int nand=0, exorg=0, norg=0, exnorg=0, andg=0, notg=0, org=0, buf=0, nand_fin=0, nand_fout=0, and_fin=0, and_fout=0, not_fin=0, not_fout=0, or_fout=0, or_fin=0, nor_fin=0, nor_fout=0, exor_fin=0, exor_fout=0, exnor_fin=0, exnor_fout=0;
int qq=0;
for(;qq<GATE.si ze();qq++)
{
if(GATE[qq].read_type()==" nand")
{nand=nand+1;}
if(GATE[qq].read_type()==" and")
{andg=andg+1;}
if(GATE[qq].read_type()==" or")
{org=org+1;}
if(GATE[qq].read_type()==" not")
{notg=notg+1;}
if(GATE[qq].read_type()==" buf")
{buf=buf+1;}
if(GATE[qq].read_type()==" nor")
{norg=norg+1;}
if(GATE[qq].read_type()==" exor")
{exorg=exorg+1; }
if(GATE[qq].read_type()==" exnor")
{exnorg=exnorg+ 1;}
}
//finding fanouts
int f=0;
vector<int> FanOut;
int u,v;
for(u=0; u<GATE.size(); u++)
{
for(v=0; v<GATE.size(); v++)
{
f=f+GATE[u].fanout(GATE[v]);
}
FanOut.push_bac k(f);
f=0;
}
//primary input fanouts
int aa,bb,cc,dd=0;
vector<string> sa;
for(aa=0; aa<inp_str.size (); aa++)
{
for(bb=0; bb<GATE.size(); bb++)
{
sa=GATE[bb].read_ip();
for(cc=0; cc<sa.size(); cc++)
{
if(inp_str[aa]==sa[cc])
{dd=dd+1;}
}
}
FanOut.push_bac k(dd);
dd=0;
}
//primary output fanouts
for(int ee=0; ee<out.size(); ee++)
{
FanOut.push_bac k(1);
}
gate tt;
int and_d=0, or_d=0, nand_d=0, nor_d=0, exor_d=0, exnor_d=0, buf_d=0, not_d=0;
int and_r=0, or_r=0, nand_r=0, nor_r=0, exor_r=0, exnor_r=0, buf_r=0, not_r=0;
int nand_D=0, nand_R=0;
int ii=0,jj=0;
for(ii=0; ii<GATE.size(); ii++)
{
for(jj=0; jj<GATE.size(); jj++)
{
//for nand
if(GATE[ii].ret_type()=="n and")
{
if(GATE[jj].ret_type()=="n and")
{
if(GATE[ii].fanout(GATE[jj])>0)
{nand_d=nand_d+ 1;}
if(GATE[ii].fanin(GATE[jj])>0)
{nand_r=nand_r+ 1;}
}
}
/*
//for and
if(tt.ret_type( )=="and")
{
if(ss.ret_type( )=="and")
{
and_d=and_d+ss. fanout(tt)>0?1: 0;
and_r=and_r+ss. fanin(tt)>0?1:0 ;
}
}
//for or
if(tt.ret_type( )=="or")
{
if(ss.ret_type( )=="or")
{
or_d=or_d+ss.fa nout(tt)>0?1:0;
or_r=or_r+ss.fa nin(tt)>0?1:0;
}
}
//for nor
if(tt.ret_type( )=="nor")
{
if(ss.ret_type( )=="nor")
{
nor_d=nor_d+ss. fanout(tt)>0?1: 0;
nor_r=nor_r+ss. fanin(tt)>0?1:0 ;
}
}
//for exor
if(tt.ret_type( )=="exor")
{
if(ss.ret_type( )=="exor")
{
exor_d=exor_d+s s.fanout(tt)>0? 1:0;
exor_r=exor_r+s s.fanin(tt)>0?1 :0;
}
}
//for exnor
if(tt.ret_type( )=="exnor")
{
if(ss.ret_type( )=="exnor")
{
exnor_d=exnor_d +ss.fanout(tt)> 0?1:0;
exnor_r=exnor_r +ss.fanin(tt)>0 ?1:0;
}
}
//for buf
if(tt.ret_type( )=="buf")
{
if(ss.ret_type( )=="buf")
{
buf_d=buf_d+ss. fanout(tt)>0?1: 0;
buf_r=buf_r+ss. fanin(tt)>0?1:0 ;
}
}
//for not
if(tt.ret_type( )=="not")
{
if(ss.ret_type( )=="not")
{
not_d=not_d+ss. fanout(tt)>0?1: 0;
not_r=not_r+ss. fanout(tt)>0?1: 0;
}
}
*/
}
if(nand_d>0){na nd_D=nand_D+1;}
if(nand_r>0){na nd_R=nand_R+1;}
nand_d=0; nand_r=0;
}
//max fanout
int b=0;
int z=0;
for(b=0; b<FanOut.size() ; b++)
{
if(z<FanOut[b])
{z=FanOut[b];}
}
cout<<z<<endl;
//F+1 lines
int nfs[z+1];
for(int kk=0; kk<=z; kk++)
{nfs[kk]=0;}
for(int hh=0; hh<=z; hh++)
{
for(int b=0; b<FanOut.size() ; b++)
{
if(hh==FanOut[b])
{nfs[hh]=nfs[hh]+1;}
}
}
for(int w=0; w<=z; w++)
{cout<<nfs[w]<<endl;}
cout<<inp.size( )<<" ";
for(int ww=0; ww<inp.size(); ww++)
{cout<<inp[ww]<<" ";}
cout<<endl;
cout<<out.size( )<<" ";
for(int x=0; x<out.size(); x++)
{cout<<out[x]<<" ";}
cout<<endl;
if(nand!=0)
{cout<<"nand "<<nand<<" "<<nand_D<< " "<<nand_R<< " "<<endl;}
if(andg!=0)
{cout<<"and "<<andg<<" "<<and_d<<" "<<and_r<<" "<<endl;}
if(notg!=0)
{cout<<"not "<<notg<<" "<<not_d<<" "<<not_r<<" "<<endl;}
if(org!=0)
{cout<<"or "<<org<<" "<<or_d<<" "<<or_r<<" "<<endl;}
if(norg!=0)
{cout<<"nor "<<norg<<" "<<nor_d<<" "<<nor_r<<" "<<endl;}
if(exorg!=0)
{cout<<"exor "<<exorg<<" "<<exor_d<< " "<<exor_r<< " "<<endl;}
if(exnorg!=0)
{cout<<"exnor "<<exnorg<< " "<<exnor_d< <" "<<exnor_r< <" "<<endl;}
if(buf!=0)
{cout<<"buf "<<buf<<" "<<buf_d<<" "<<buf_r<<" "<<endl;}
return 0;
}
when i run gdb i am getting the following message
Program received signal SIGSEGV, Segmentation fault.
0xb7ec6375 in std::basic_stri ng<char, std::char_trait s<char>, std::allocator< char> >::~basic_strin g () from /usr/lib/libstdc++.so.6
thisdoes not give me any line number to debug, even when i type in 'bt full' i get the same thing,
i am new to STL but know C++ (amatuerish)... ..
kindly help me understand whats the issue here,