i found this code in C++ but i can't convert it from C++ to C, help me please
i don't know C++.

#include <iostream>
#include<stack>
#include<stdio.h>
#include <string.h>

using namespace std;

#define MAX 10000
/*class node{
      int */
      
unsigned int graph[MAX][MAX];// = { { 0, 1 },// 0 1 0
                        //    { 1, 1 }};//2 0 0;
int nodes;
int fnodes;
int counter2;                
stack<int> myStack;
int start;
int final[MAX];
int V, E;
int visit[MAX];
char states[5];
bool empty = true;

int read()
{
  memset(states,0, sizeof(states));
  cin >> nodes;
  
  if(nodes == 0)
    return 0;
    
  cin >> fnodes;
  
  int counter = 0;//Start states
  counter2 = 0;
  int cont;//String Length
  for(int a=0; a<fnodes; a++)
  {
     cin >> states;
     cont = -1;
     for(int k=0; k<5; k++)
          if(states[k] != '\0')
             cont++;
          else
              break;
             
     //cout << "cont -> " << cont;
     //cout << *states;
    // cout << states[0] << endl;
    // cout << states[cont] << endl;
     int aux = 0;//Sum of characters for Nodes
     int aux2 = 0;//Sum of characters for Nodes
     int mult = 1;//Multiple
     
     if(states[cont]=='f')
     {  
          for(int k=cont-1; k>=0; k--)
          {
           //cout << " -> " << states[k]<< endl;
            aux+=((int)states[k]- 48)*mult;
            //cout << " -> " << aux << endl;
            mult*=10;
          }
            
         final[counter2++] = aux;
         //cout << final[counter2-1] << endl; 
     }
     else
     {
         if(counter++!=1)
         {
          for(int k=cont-1; k>=0; k--)
          {
           //cout << " -> " << states[k] << endl;
            aux+=((int)states[k]-48)*mult;
            mult*=10;
          }
         start = aux;
         }
         //cout << start << endl; 
     }
  }
  
  for(int a=0; a<nodes; a++)
  {
     for(int b=0; b<nodes; b++)
     {
      cin >> graph[a][b];
     }
  }
  if(counter>1)
     return -1;
  return 1;
}
void calc()
{
 for(int a=0;a<30;a++)
 {
     for(int b=0;b<30;b++)
     {
       if(a*b<100)
           graph[a][b]=0;
       else
          graph[a][b]=1;
     }     
 }
}
bool compareVertex(int v)
{
    for(int k=0; k<counter2; k++)
       if(final[k]==v)
       {
          //cout << " - >" << final[k] << " -> " << v << endl;
          return true;
       }
    return false;
     
}
void dfs(int vertex) 
{
  //cout << vertex << " -> ";
  if(compareVertex(vertex))
     empty=false;
     
  visit[vertex] = 1;
  for (int i=0;i<nodes;i++)
    if (!visit[i] && graph[vertex][i])
      dfs(i);
}
void bfs(int s) 
{
     int i, j, node;
     memset(visit, 0, sizeof(visit));
     myStack.push(s);
     
     while(!myStack.empty())
     {
          node = myStack.top();
          myStack.pop();
          if(visit[node]) continue;
          visit[node] = 1;
          
          for(i=0; i<V; i++)
               if(graph[node][i]) myStack.push(i);
     }
}
void print()
{
 for(int a=0;a<nodes;a++)
 {
     for(int b=0;b<nodes;b++)
     {
         cout << graph[a][b] << " ";
     }
      cout << endl;  
 }
}
int main() 
{
int j=0;
int locuas = 0;
//calc();
do
{
 locuas = read();//Check integer
  if(locuas == -1)
  {
     cout << "This is not an DFA" <<endl;
     continue;
  }
  if(locuas == 0)
     return 0;
//print();
  for (int i=0;i<nodes;i++) 
      visit[i] = 0;
  dfs(start);
  
  if(empty)
      cout << "The Automata Language is Empty" <<endl;
  else
      cout << "The Automata Language is not Empty" <<endl;
  
  empty = true;
      //bfs(0);
      //cout << endl;
}while(true);

 return 0;
}

The code is a mix of C and C++ as suggested by usage of iostream and stdio.h in the first place.

Secondly, I guess learning C++ would be easy as compared to converting such a code to pure C ;).

but i use turbo C compiler, and our project must be in C, can you convert it from C++ to C because i don't Know C++

Member Avatar for iamthwee

I think you should try to convert it to c yourself and post your problems when you encounter them.

I think you should try to convert it to c yourself and post your problems when you encounter them.

i try, but i have many errors, this is the code that i convert it

#include<stdio.h>
#include <stdlib.h>
#include<string.h>
#include<stddef.h>
#include <malloc.h>


#define MAX 256
#define true 1
#define false 0

typedef struct node  node
    struct node
{      int

long int graph[MAX][MAX];  /* = { { 0, 1 },/* 0 1 0 */
                /*{ 1, 1 }};2 0 0; */
int nodes;
int fnodes;
int counter2 = 0;
int counter = 0;//Start states
int myStack;
int start[MAX];
int final[MAX];
int V, E;
int visit[MAX];
char states[5];
int empty = true;

int read()
{
  int a,k,b;
  nodes = -1;
  memset(states,0, sizeof(states));
  scanf("%d" ,&nodes);
  if (nodes <0)
  {
            return 0;
  }
  /*if(nodes == 0) */
    /*return 0;  */

  scanf("%d ", &fnodes);

  counter = 0;/*Start States*/
  counter2 = 0;/*Final States*/
  int cont;/*String Length*/
  for(a=0; a<fnodes; a++)
  {
     scanf("%d ",states);
     cont = -1;
     for(k=0; k<5; k++)
      if(states[k] != '\0')
         cont++;
      else
          break;

     /*printf("cont " , cont);
       printf(" %d ", *states);
       printf("%d ", states[0]);
       printf(" %d ", states[cont]); */
     int aux = 0;/*Sum of characters for Nodes   */
     int aux2 = 0;/*Sum of characters for Nodes*/
     int mult = 1;/*Multiple */

     if(states[cont]=='f')
     {
      for(int k=cont-1; k>=0; k--)
      {
       /*printf(" -> %d " , states[k]); */
        aux+=((int)states[k]- 48)*mult;
        /*printf(" %d " ,aux);  */
        mult*=10;
      }

     final[counter2++] = aux;
     /*prinf(" %d ", final[counter2-1]);*/
     }
     else
     {
      for(int k=cont-1; k>=0; k--)
      {
        /*printf(" -> %d", states[k]);*/
        aux+=((int)states[k]-48)*mult;
        mult*=10;
      }
     start[counter++] = aux;
     }
     /*printf(" %d ",start[counter-1]);
     //printf(" %d " ,counter ); */
  }

  for(a=0; a<nodes; a++)
  {
     for(b=0; b<nodes; b++)
     {
      scanf(" %d %d " ,graph[a][b]);
     }
  }
  if(counter>1)
     return -1;
  return 1;
}


void calc()
{
  int a;
  int b;

 for(a=0;a<30;a++)
 {
     for(b=0;b<30;b++)
     {
       if(a*b<100)
           graph[a][b]=0;
       else
          graph[a][b]=1;
     }     
 }
}
int compareVertex(int v)
{

    int k;

    for(k=0; k<counter2; k++)
       if(final[k]==v)
       {
          /*printf(" %d , %d " ,final[k],v);*/
          return true;
       }
    return false;

}
void dfs(int vertex)
{
  int i;

  /*printf(" %d ",vertex);*/
  if(compareVertex(vertex))
     empty=false;

  visit[vertex] = 1;
  for (i=0;i<nodes;i++)
    if (!visit[i] && graph[vertex][i])
      dfs(i);
}
void bfs(int s) 
{
     int i, j, node;
     memset(visit, 0, sizeof(visit));
     myStack.push(s);

     while(!myStack.empty())
     {
          node = myStack.top();
          myStack.pop();
          if(visit[node]) continue;
          visit[node] = 1;

          for(i=0; i<V; i++)
               if(graph[node][i]) myStack.push(i);
     }
}
void print()
{
  int i,b; 

  for(a=0;a<nodes;a++)
   {
     for(b=0;b<nodes;b++)
     {
         printf(" %d  ",graph[a][b]);
     }

 }
}
int main() 
{
int j,h ;
int locuas = -1;

/*calc();*/
do
{
 locuas = read();//Check integer
  /*if(locuas == -1)
  {
     printf("This is not an DFA");
     continue;
  }*/
  if(locuas == 0)
     return 0;
/*print();*/
  for (i=0;i<nodes;i++)
      visit[i] = 0;
  for(h=0; h<counter;h++)
          dfs(start[h]);

  if(empty)
      printf("The Automata Language is Empty");
  else
      printf("The Automata Language is not Empty");

  empty = true;
      /*bfs(0);*/

}while(true);

 return 0;
}

please, help me

Member Avatar for iamthwee

I think you should start by getting your myStack bit working on its own as a separate program first.

can you show me how i can do that

Member Avatar for iamthwee

I would search google for a stack program written in c.

i need c program converts to automata. especially c conditional statements.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.