Skip to content

Instantly share code, notes, and snippets.

@AliZafar120
Last active August 29, 2015 14:06
Show Gist options
  • Save AliZafar120/8638907f53e8f302a898 to your computer and use it in GitHub Desktop.
Save AliZafar120/8638907f53e8f302a898 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <sstream>
#include <algorithm>
using namespace std;
struct content
{
vector<char> store;
};
int main(void)
{
vector<char> fin,bgin,last,no,temps,finas;
char start,temp;
cout << "Enter the 1st Step" <<endl;
cin >>start;
temps.push_back(start);
cout << "Enter the final(accepted) steps\npress q to end entering" <<endl;
for(;;)
{
cin>>temp;
if(temp=='q') break;
fin.push_back(temp);
}
cout <<"Enter the transitions with spaces" <<endl;
string hudai;
getline(cin, hudai);
char sym[3]={'{',',','}'};
bool paisi= false;
for(;;)
{
char sym[3]={'{',',','}'};
bool paisi= false;
string givenstring;
getline(cin, givenstring);
for(int i=0;i<3;i++)
{
for(int j=0; j<givenstring.length(); j++)
{
if(sym[i]==givenstring[j]){
givenstring[j] = ' ';
paisi=true;
}
}
}
istringstream iss(givenstring);
if(!paisi)
{
iss>>temp;
if(temp!='q') bgin.push_back(temp);
else if(temp=='q') break;
iss>>temp;
no.push_back(temp);
iss>>temp;
last.push_back(temp);
}
else if(paisi)
{
iss>>temp;
bgin.push_back(temp);
iss>>temp;
no.push_back(temp);
iss>>temp;
last.push_back(temp);
while(iss>>temp)
{
bgin.push_back(bgin.back());
no.push_back(no.back());
last.push_back(temp);
}
}
// cout<<"bgin.size"<<bgin.size()<<endl
}
//total signs in automata
vector<char> tempsighsn;
for(int i=0;i<no.size();i++)
{
bool flag =true;
for(int j=0;j<tempsighsn.size();j++)
{
if(tempsighsn[j]==no[i]) flag=false;
}
if(flag) tempsighsn.push_back(no[i]);
}
cout <<"The signs are :";
for(int i=0;i<tempsighsn.size();i++)cout<<tempsighsn[i]<<" ";
cout<<endl;
int total=0;
content steps[no.size()+20];
steps[total].store.push_back(temps[0]);
total=1;
for (int b=0;b<total ;b++)//jotogula sign ase
{
for(int i=0;i< tempsighsn.size(); i++)
{
for(int g=0;g<steps[b].store.size();g++)//store er steps e joto element ta
{
// temp e dhukay
temps.push_back(steps[b].store[g]);
}
for(;;)
{
for(int k=0;k<temps.size();k++)
{
for(int j=0;j<bgin.size();j++)
{
if (temps[k]==bgin[j] && tempsighsn[i]==no[j])
{
bool flag=true;
for(int t=0;t<finas.size();t++) {if(finas[t]==last[j]) flag= false;}
if(flag) finas.push_back(last[j]);
}
}
}
sort(finas.begin(),finas.end());
if(finas.empty())
{
temps.clear();
finas.clear();
break;
}
bool flag= true;
for(int i=0;i<total;i++)
{
if(finas.size()== steps[i].store.size())
{
bool flagin=true;
for(int j=0;j<finas.size();j++)
{
if(finas[j] != steps[i].store[j]) flagin=false;
}
if(flagin==true) { flag=false;break;}
}
}
if(flag)
{
for(int i=0;i<finas.size();i++)
{
steps[total].store.push_back(finas[i]);
}
total++;
}
else if(!flag)
{
temps.clear();
finas.clear();
break;
}
temps.clear();
for(int m=0;m<finas.size();m++) temps.push_back(finas[m]);
finas.clear();
}
}
}
for(int i=0;i<total;i++)
{
cout<<"step : "<<i<<"has ";
for(int j=0;j<steps[i].store.size();j++)
{cout<<steps[i].store[j]<<" ";}
cout<<endl;
}
//Nfa to dfa :
temps.clear();
finas.clear();
for(int i=0;i<total;i++)
{
for(int j=0;j<tempsighsn.size();j++)
{
//showing input dfa
bool finl=false;
for(int k=0; k<steps[i].store.size();k++)
{
for(int f=0;f<fin.size();f++)
{
if (steps[i].store[k]==fin[f]) finl=true;
}
}
if(finl) cout<<"*{";
else cout<<"{";
for(int k=0; k<steps[i].store.size();k++)
{
if(k==steps[i].store.size()-1) cout<<steps[i].store[k]<<"}";
else cout<<steps[i].store[k]<<",";
}
for(int k=0; k<steps[i].store.size();k++)
temps.push_back(steps[i].store[k]);
sort(finas.begin(),finas.end());
//a space
cout <<"\t\t";
//showing sign
cout<<tempsighsn[j];
//a space
cout <<"\t\t";
//Showing output
for(int k=0;k<temps.size();k++)
{
for(int n=0;n<bgin.size();n++)
{
if (temps[k]==bgin[n] && tempsighsn[j]==no[n])
{
bool sim=false;
for(int s=0;s<finas.size();s++) if(finas[s]==last[n]) sim=true;
if(!sim) finas.push_back(last[n]);
}
}
}
sort(finas.begin(),finas.end());
finl=false;
for(int m=0;m<finas.size();m++)
{
for(int f=0;f<fin.size();f++)
{
if (finas[m]==fin[f]) finl=true;
}
}
if(finl) cout<<"*{";
else cout<<"{";
for(int m=0;m<finas.size();m++)
{
if(m==finas.size()-1)cout<<finas[m]<<"}";
else cout<<finas[m]<<",";
}
if(finas.empty()) cout<<"}";
cout<<endl;
temps.clear();
finas.clear();
}
}
cout<< " Now Enter a operation "<<endl;
string inp;
getline(cin,inp);
bool finalstep=false;
temps.clear();
temps.push_back(start);
for(int i=0;i< inp.size(); i++)
{
for(int k=0;k<temps.size();k++)
{
for(int j=0;j<bgin.size();j++)
{
if (temps[k]==bgin[j] && inp[i]==no[j])
{
bool flag=true;
for(int t=0;t<finas.size();t++) {if(finas[t]==last[j]) flag= false;}
if(flag) finas.push_back(last[j]);
}
}
}
cout<<"From ";
for(int m=0;m<temps.size();m++) cout<<temps[m]<<" ";
cout<<"input "<<inp[i]<<" we go to ";
for(int m=0;m<finas.size();m++) cout<<finas[m]<<" ";
cout<<endl;
temps.clear();
for(int m=0;m<finas.size();m++) temps.push_back(finas[m]);
if(i!=inp.length()-1) finas.clear();
}
for(int j=0;j<fin.size();j++)
{
for(int k=0;k<finas.size();k++)
{
if(finas[k]==fin[j])
{finalstep=true;
break;
}
}
}
if(finas.empty()) cout<<"Case Not Accepted"<<endl;
else if(finalstep==true) cout<<"Case Accepted"<<endl;
else cout<<"Case Not Accepted"<<endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment