Question

#include <iostream> #include <string> #include <fstream> #include <sstream> using namespace std; struct transition{ // transition structure...

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>

using namespace std;

struct transition{ // transition structure
char start_state, to_state;
char symbol_read;
};

void read_DFA(struct transition *t, char *f, int &final_states, int &transitions){
int i, j, count = 0;
ifstream dfa_file;
string line;
stringstream ss;
dfa_file.open("dfa.txt");
getline(dfa_file, line); // reading final states
for(i = 0; i < line.length(); i++){
if(line[i] >= '0' && line[i] <= '9')
f[count++] = line[i];
}
final_states = count; // total number of final states
// reading transitions
j = 0;
while(getline(dfa_file, line)){
ss.clear();
ss << line;
ss >> t[j].start_state;
ss >> t[j].symbol_read;
ss >> t[j].to_state;
j++;
}
transitions = j; // total transitions
dfa_file.close();
}

void read_input(struct transition *t, char *f, int final_states, int transitions, string input){
char current_state = '0';
int i, j, len = input.length();
bool flag;
for(i = 0; i < len; i++){ // for all input symbols
flag = false;
for(j = 0; j < transitions; j++){ // for all transitions
if(t[j].start_state == current_state && input[i] == t[j].symbol_read){ // if current state and symbol matches with transition
cout<<current_state<<" on "<<input[i]<<" -> "<<t[j].to_state<<endl; // show transition
current_state = t[j].to_state; // update current state
flag = true; // move possible
break;
}
}
if(!flag){ // if no move available from current state with current input symbol
cout<<current_state<<" on "<<input[i]<<" -> "<<"No transition\n";
break;
}
}
flag = false;
// if current state is final state
for(i = 0; i < final_states; i++){
if(current_state == f[i]){
flag = true;
break;
}
}

if(flag)
cout<<"\nAccepted.\n\n";
else
cout<<"\nRejected.\n\n";
}

int main(){
string input;
struct transition *t = new struct transition[100];
char *f_states = new char[50];
int i, final_states, transitions;
read_DFA(t, f_states, final_states, transitions); // read DFA from file
// show final states and transitions
cout<<"Final states: ";
for(i = 0; i < final_states; i++)
cout<<f_states[i]<<" ";
cout<<endl;
cout<< "Transitions:\n";
for(int i = 0; i < transitions; i++)
cout<<t[i].start_state<<" "<<t[i].symbol_read<<" "<<t[i].to_state<<endl;
cout<<endl;
// accept string and check
cout<<"Enter a string (type \"quit\" to stop): ";
cin>>input;
while(input.compare("quit") != 0){
read_input(t, f_states, final_states, transitions, input);
cout<<"Enter a string (type \"quit\" to stop): ";

cin>>input;

}
return 0;
}

Someone posted this code for a DFA stimulator

and text file is

0
0 a 1
0 b 2
1 a 2
1 b 0
2 a 2
2 b 2

Can someone teach me how to run this code and load a text file input in GCC COMPILER IN CODE BLOCKS IDE, IT DOSENT RUN I WANT THIS DFA TO RUN. TEACH ME HOW WITH STEP BY STEP SCREENSHOT PLEASE

0 0
Add a comment Improve this question Transcribed image text
Answer #1

First of all to run the program successfully

Create a txt file name as dfa.txt in current directory where you keep your source file DFA.cpp

Once you create the file dfa.txt just copy paste the below text and save it.

0
0 a 1
0 b 2
1 a 2
1 b 0
2 a 2
2 b 2

Compile command : ctrl + shift + f9

НЫ dfa.cpp-Code:Blocks 16.01 File Edit View Search Project Build Debug Fortran wxSmith Tools Tools+ Plugins DoxyBlocks Settin

Run the program : ctrl + F10

Final Output window:

type string ab

you will get output as Accepted

type string ba

you will get output Rejected.

to exit the program type : quit

Add a comment
Know the answer?
Add Answer to:
#include <iostream> #include <string> #include <fstream> #include <sstream> using namespace std; struct transition{ // transition structure...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please add a detailed comment for this program. #include<iostream> #include<string> #include<fstream> #include<sstream> #include<cctype> using namespace std;...

    Please add a detailed comment for this program. #include<iostream> #include<string> #include<fstream> #include<sstream> #include<cctype> using namespace std; int is_palindrome(string word){ int len = word.size(); for(int i=0; i<len/2; i++){ if(toupper(word[i])!=toupper(word[len-i-1])) return 0; } return 1; } int have_vowels3(string word){ int cnt = 0; for(int i=0; i<word.size(); i++){ if(tolower(word[i])=='a' || tolower(word[i])=='e' || tolower(word[i])=='i' || tolower(word[i]) =='o' || tolower(word[i]) == 'u') cnt++; } if(cnt>=3) return 1; else return 0; } int have_consecutives(string word){ for(int i=0; i<word.size()-1; i++){ if(tolower(word[i])=='o' && tolower(word[i+1]=='o')) return 1; } return...

  • can you please split this program into .h and .cpp file #include <iostream> #include<string> #include<fstream> #define...

    can you please split this program into .h and .cpp file #include <iostream> #include<string> #include<fstream> #define SIZE 100 using namespace std; //declare struct struct word_block {    std::string word;    int count; }; int getIndex(word_block arr[], int n, string s); int main(int argc, char **argv) {    string filename="input.txt";    //declare array of struct word_block    word_block arr[SIZE];    int count = 0;    if (argc < 2)    {        cout << "Usage: " << argv[0] << "...

  • Write a psuedocode for this program. #include <iostream> using namespace std; string message; string mappedKey; void...

    Write a psuedocode for this program. #include <iostream> using namespace std; string message; string mappedKey; void messageAndKey(){ string msg; cout << "Enter message: "; getline(cin, msg); cin.ignore(); //message to uppercase for(int i = 0; i < msg.length(); i++){ msg[i] = toupper(msg[i]); } string key; cout << "Enter key: "; getline(cin, key); cin.ignore(); //key to uppercase for(int i = 0; i < key.length(); i++){ key[i] = toupper(key[i]); } //mapping key to message string keyMap = ""; for (int i = 0,j...

  • #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str);...

    #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str); int numConsonants(char *str); int main() {    char string[100];    char inputChoice, choice[2];    int vowelTotal, consonantTotal;    //Input a string    cout << "Enter a string: " << endl;    cin.getline(string, 100);       do    {        //Displays the Menu        cout << "   (A) Count the number of vowels in the string"<<endl;        cout << "   (B) Count...

  • #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str);...

    #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str); int numConsonants(char *str); int main() {    char string[100];    char inputChoice, choice[2];    int vowelTotal, consonantTotal;    //Input a string    cout << "Enter a string: " << endl;    cin.getline(string, 100);       do    {        //Displays the Menu        cout << "   (A) Count the number of vowels in the string"<<endl;        cout << "   (B) Count...

  • #include <iostream> #include <iomanip> #include <cstdlib> #include <fstream> #include <string> #include <vector> #include <sstream> using namespace...

    #include <iostream> #include <iomanip> #include <cstdlib> #include <fstream> #include <string> #include <vector> #include <sstream> using namespace std; void DisplayMenu() {    cout << "1. E games\n";    cout << "2. T games\n";    cout << "3. M games\n";    cout << "4. Total Games\n";    cout << "5. Exit\n"; } double total(double egames, double tgames, double mgames) {    int totalgames = egames + tgames + mgames;    cout << "There are " << totalgames << " games\n";    return...

  • #include <fstream> #include <iostream> #include <cstdlib> using namespace std; // Place charcnt prototype (declaration) here int...

    #include <fstream> #include <iostream> #include <cstdlib> using namespace std; // Place charcnt prototype (declaration) here int charcnt(string filename, char ch); int main() { string filename; char ch; int chant = 0; cout << "Enter the name of the input file: "; cin >> filename; cout << endl; cout << "Enter a character: "; cin.ignore(); // ignores newline left in stream after previous input statement cin.get(ch); cout << endl; chcnt = charcnt(filename, ch); cout << "# of " «< ch« "'S:...

  • please help me fix the error in here #include<iostream> #include <string> using namespace std; string getStudentName();...

    please help me fix the error in here #include<iostream> #include <string> using namespace std; string getStudentName(); double getNumberExams(); double getScoresAndCalculateTotal(double E); double calculateAverage(double n, double t); char determineLetterGrade(); void displayAverageGrade(); int main() { string StudentName; double NumberExam, Average, ScoresAndCalculateTotal; char LetterGrade; StudentName = getStudentName(); NumberExam = getNumberExams(); ScoresAndCalculateTotal= getScoresAndCalculateTotal(NumberExam); Average = calculateAverage(NumberExam, ScoresAndCalculateTotal); return 0; } string getStudentName() { string StudentName; cout << "\n\nEnter Student Name:"; getline(cin, StudentName); return StudentName; } double getNumberExams() { double NumberExam; cout << "\n\n Enter...

  • CODES: main.cpp #include <iostream> #include <string> #include "ShoppingCart.h" using namespace std; char PrintMenu() { char answer;...

    CODES: main.cpp #include <iostream> #include <string> #include "ShoppingCart.h" using namespace std; char PrintMenu() { char answer; cout << "MENU" << endl; cout << "a - Add item to cart" << endl; cout << "d - Remove item from cart" << endl; cout << "c - Change item quantity" << endl; cout << "i - Output items' descriptions" << endl; cout << "o - Output shopping cart" << endl; cout << "q - Quit" << endl << endl; while (true) {...

  • #include <iostream> #include <iomanip> #include <vector> #include <string> using namespace std; struct menuItemType { string menuItem;...

    #include <iostream> #include <iomanip> #include <vector> #include <string> using namespace std; struct menuItemType { string menuItem; double menuPrice; }; void getData(menuItemType menuList[]); void showMenu(menuItemType menuList[], int x); void printCheck(menuItemType menuList[], int menuOrder[], int x); int main() { const int menuItems = 8; menuItemType menuList[menuItems]; int menuOrder[menuItems] = {0}; int orderChoice = 0; bool ordering = true; int count = 0; getData(menuList); showMenu(menuList, menuItems); while(ordering) { cout << "Enter the number for the item you would\n" << "like to order, or...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT