Member 12871493 Ответов: 1

Нужна помощь в преобразовании этого языка в язык Си


#include<iostream>
#include<string>
using namespace std;

class rec{
public:
 char S[10], A[10];
 char left1, left2;

 int i = 0;
 bool flag1 = false;
 bool flag2 = false;
 bool flag3 = false;

 char beta[50], alpha[50];
 char temp1[50], temp2[50];

 int j = 0, k = 0;
 int q = 0, o = 0;

 void direct(char temp2[]){
 i = 0;
 int length = strlen(temp2);
 cout << "\nlen= " << length;
 char left = temp2[0];
 cout << "\nleft= " << left;
 j = 0, k = 0;

 for (i = 2; i < length; i++){
 if (temp2[i] == left){
 int c = i + 1;
 while (temp2[c] != '|' && temp2[c] != '\o'){
 alpha[j] = temp2[c];
 c++;
 j++;

 }
 alpha[j] = '|';
 j++;
 i = c;
 alpha[j] = '\o';
 flag1 = true;
 }
 else if (temp2[i] != left){
 int z = i;
 while (temp2[z] != '|' && temp2[z] != '\o'){
 beta[k] = temp2[z];
 z++;
 k++;

 }
 beta[k] = '|';
 k++;
 i = z;
 beta[k] = '\o';
 flag2 = true;
 }
 }

 cout << "\n----------\n";
 if (flag1 == true){
 cout << "\nalpha= ";
 for (i = 0; alpha[i] != '\o'; i++)
 cout << alpha[i] << " ";
 }
 if (flag2 == true){
 cout << "\nbeta= ";
 for (i = 0; beta[i] != '\o'; i++)
 cout << beta[i] << " ";
 }

 cout << "\n----------form\n";
 cout << "A= ";
 for (i = 0; beta[i] != '\o'; i++){
 if (beta[i] != '|')
 cout << beta[i];
 else
 cout << " A'|";
 }

 cout << "\n-----------\n";
 cout << "A'= ";
 for (i = 0; alpha[i] != '\o'; i++){
 while (alpha[i] != '|'){
 cout << alpha[i]; i++;
 }
 cout << " A'|";
 }
 cout << "^";
 cout << endl;
 }
} r;

int main(){

 cout << "Enter the no.of productions: ";
 int count;
 cin >> count;
 if (count>1){

 cout << "\nEnter production 1:\n";
 cin >> r.S;
 cout << "\nEnter production 2:\n";
 cin >> r.A;
 cout << "---------------------------------\n";
 cout << "\nEntered Production 1 is=\t" << r.S;
 cout << "\nEntered Production 2 is=\t" << r.A;

 int len1 = strlen(r.S);
 int len2 = strlen(r.A);
 cout << "\n---------------------------------\n";
 cout << "\nlen1= " << len1 << "\tlen2= " << len2;

 r.S[len1] = '\o';
 r.A[len2] = '\o';
 r.left1 = r.S[0];
 r.left2 = r.A[0];
 cout << "\nleft1= " << r.left1 << "\nleft2= " << r.left2;
 cout << "\n---------------------------------\n";

 r.temp2[r.q] = 'S'; r.q++;
 r.temp2[r.q] = '='; r.q++;

 for (int i = 2; i< len1; i++){
 if (r.S[i] == r.left2){
 r.flag3 = true;
 int h = i + 1;

 while (r.S[h] != '|' && r.S[h] != '\o'){
 r.temp1[r.o] = r.S[h];
 h++;
 r.o++;
 }
 for (int p = 2; p < len2; p++){
 while (r.A[p] != '|' && r.A[p] != '\o'){
 r.temp2[r.q] = r.A[p];
 r.q++;

 p++;
 }
 for (int g = 0; g<(strlen(r.temp1)); g++){
 r.temp2[r.q] = r.temp1[g];
 r.q++;
 }
 r.temp2[r.q] = '|';
 r.q++;
 }
 i = h;
 }
 else if (r.S[i] != r.left2){
 r.temp2[r.q] = r.S[i];
 r.q++;
 }
 }
 r.temp2[r.q] = '\o';
 cout << endl;

 int length = strlen(r.temp2);
 for (int i = 0; i <length; i++)="" cout="" &lt;&lt;="" r.temp2[i];="" r.direct(r.temp2);="" }="" else{="" "\nenter="" production:\n";="" cin="">> r.temp2;
 cout << "---------------------------------\n";
 cout << "\nEntered Production is=\t" << r.temp2;
 r.direct(r.temp2);
 }
 return 0;
}


Что я уже пробовал:

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
struct node{
    char arr[10];
    struct node *next;
};

int main()
{
    struct node *root;
    root = (struct node *) malloc( sizeof(struct node) );
    struct node *temp;
    temp = (struct node *) malloc( sizeof(struct node) );
    struct node *current;
    current = (struct node *) malloc( sizeof(struct node) );
    struct node *empty;
    empty = (struct node *) malloc( sizeof(struct node) );

    FILE *fp;
    fp = fopen("test.txt","r");

    char ch;
    char tmp;
    int check = 0;
    int x=0;
    int total = 0;
    while( (ch = fgetc(fp)) != EOF)
    {
        if(check == 0)
        {
            if(ch == '\n')
            {
          		  temp->arr[x] = '\0';
                x=0;
                check = 1;
                root = temp;
                current = root;
                temp = empty;
                empty = (struct node *) malloc( sizeof(struct node) );
                root->next = temp;
                total++;
            }
            else if( ch == '-' || ch == '>' )
            {
                //do nothing
            }
            else if( ch == '|' )
            {
                temp->arr[x] = '\0';
                x=0;
                current = temp;
                root = temp;
                temp = empty;
                empty = (struct node *) malloc( sizeof(struct node) );
                current->next = temp;
                temp->arr[x] = tmp;
                x++;
                check = 1;
                total++;
            }
            else
            {
                temp->arr[x] = ch;
                if(x == 0)
                {
                    tmp = ch;
                }
                x++;
            }
        }

        else
        {
            if(ch == '\n')
            {
                temp->arr[x] = '\0';
                x=0;
                current = temp;
                temp = empty;
                empty = (struct node *) malloc( sizeof(struct node) );
                current->next = temp;
                total++;
            }
            else if( ch == '-' || ch == '>' )
            {
                //do nothing
            }
            else if( ch == '|' )
            {
                temp->arr[x] = '\0';
                x=0;
                current = temp;
                temp = empty;
                empty = (struct node *) malloc( sizeof(struct node) );
                current->next = temp;
                temp->arr[x] = tmp;
                x++;
                total++;
            }
            else
            {
                temp->arr[x] = ch;
                if(x == 0)
                {
                    tmp = ch;
                }
                x++;
            }
        }
    }

    fp = fopen("input.txt","r");
    printf("Original:\n");
    while( (ch = fgetc(fp)) != EOF)
    {
    	putchar(ch);
    }
    printf("\n");

    struct node *nw;
    nw = (struct node *) malloc( sizeof(struct node) );

    nw = root;

    printf("Linked List:\n");
    int i=0;
    for(i=0;i<total;i++) {="" puts(nw-="">arr);
            if( nw->next->next != NULL )
            	{
            		nw = nw->next;
            	}
    }
    printf("\n");

    //Left Recursion Remove

    current = root;
    for(i=0;i<total;i++) {="" if(current-="">arr[0] == current->arr[1])
            {
                //Remove stuff
                tmp = current->arr[0];
                printf("Left recursion found: %c\n", tmp);
                temp = (struct node *) malloc( sizeof(struct node) );
                temp->arr[0] = tmp;
                temp->arr[1] = '\'';

                int j=2;
                while ( current->arr[j] != '\0' )
                {
                    temp->arr[j] = current->arr[j];
                    j++;
                }
                temp->arr[j] = tmp;
                temp->arr[j+1] = '\'';
                temp->arr[j+2] = '\0';
                //puts(temp->arr);

                current->arr[1] = tmp;
                current->arr[2] = '\'';
                current->arr[3] = '\0';

                nw->next = temp;
                nw = temp;
                total++;

                temp = (struct node *) malloc( sizeof(struct node) );
                temp->arr[0] = tmp;
                temp->arr[1] = '\'';
                temp->arr[2] = '$';
                temp->arr[3] = '\0';

                nw->next = temp;
                nw = temp;
                total++;
            }

            if( current->next->next != NULL )
            {
                current = current->next;
            }
    }

    nw = root;

    printf("\nNew Linked List:\n");
    for(i=0;i<total;i++) puts(nw-="">arr);
            if( nw->next != NULL )
            	{
            		nw = nw->next;
            	}
            
    
	    
    printf("\n");


    getch();
    return 0;
}



Я пытался найти альтернативу этому, но не смог достичь реального решения

1 Ответов

Рейтинг:
1

Arthur V. Ratz

1. Объявить все функции вне класса;
2. Повторно объявите все переменные внутри класса глобальными в вашем c/cpp-файле;
3. Удалите объявление класса;

Вот и все.