需要帮助才能将其转换为C语言 [英] Need help in converting this into C langauge
本文介绍了需要帮助才能将其转换为C语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
#include < iostream >
#include < string < span class =code-keyword>>
使用 namespace std;
class rec {
public :
< span class =code-keyword> 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 =<<长度;
char left = temp2 [ 0 ];
cout<< \ nleft =<<剩下;
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\\\
;
cout<< A =;
for (i = 0 ; beta [i]!= ' \ o'; i ++){
if (beta [i]!= ' |')
cout<<的β[I];
else
cout<< A'|;
}
cout<< \ n ----------- \ n;
cout<< A'=;
for (i = 0 ; alpha [i]!= ' \ o'; i ++){
while (alpha [i]!= ' |'){
cout<<阿尔法[I];我++;
}
cout<< A'|;
}
cout<< ^;
cout<< ENDL;
}
} r;
int main(){
cout<< 输入no.of productions:;
int count;
cin>>计数;
if (count> 1){
cout<< \ n输入生产1:\ n;
cin>> R,S;
cout<< \ n输入生产2:\ n;
cin>> R.A;
cout<< ------------------------- -------- \\\
跨度>;
cout<< \ nEnntered Production 1 is = \t<< R,S;
cout<< \ nEnntered Production 2 is = \t<< R.A;
int len1 = strlen(r.S);
int len2 = strlen(r.A);
cout<< \ 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 ---------------------- ----------- \\\
跨度>;
r.temp2 [r.q] = ' S'; r.q ++;
r.temp2 [r.q] = ' ='; r.q ++;
for ( int i = 2 ; i< len1; i ++){
if (rS [i] == r.left2){
r .flag3 = true ;
int h = i + 1 ;
while (rS [h]!= ' |'&& rS [h]!= ' \ o' ){
r.temp1 [ro] = rS [h];
h ++;
r.o ++;
}
for ( int p = 2 ; p< len2; p ++){
while (rA [p]!= ' |'&& rA [p]!= ' \ o'){
r.temp2 [rq] = rA [p];
r.q ++;
p ++;
}
for ( int g = 0 ; g<(strlen(r.temp1)); g ++){
r.temp2 [rq] = r.temp1 [g];
r.q ++;
}
r.temp2 [r.q] = ' |';
r.q ++;
}
i = h;
}
else if (rS [i]!= r.left2) {
r.temp2 [rq] = rS [i];
r.q ++;
}
}
r.temp2 [rq] = ' \ 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 {= \ nnter = production:\ n; = cin = >> r.temp2;
cout<< ------------------------- -------- \\\
跨度>;
cout<< \ nEnntered 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 节点* 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 =( struct node *)malloc( sizeof ( struct node));
root-> next = temp;
总++;
}
else if (ch == ' - ' || ch == ' >')
{
// 什么都不做
}
else if (ch == ' |')
{
temp-> arr [x] = ' \ 0';
x = 0 ;
current = temp;
root = temp;
temp =空;
empty =( struct node *)malloc( sizeof ( struct node));
current-> next = temp;
temp-> arr [x] = tmp;
x ++;
check = 1 ;
总++;
}
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 =( struct node *)malloc( sizeof ( struct node));
current-> next = temp;
总++;
}
else if (ch == ' - ' || ch == ' >')
{
// 什么都不做
}
else if (ch == ' |')
{
temp-> arr [x] = ' \ 0';
x = 0 ;
current = temp;
temp =空;
empty =( struct node *)malloc( sizeof ( struct node));
current-> next = temp;
temp-> arr [x] = tmp;
x ++;
总++;
}
else
{
temp-> arr [x] = ch;
if (x == 0 )
{
tmp = CH;
}
x ++;
}
}
}
fp = fopen( input.txt, r);
printf( 原文:\ 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) ;
// 左递归删除
当前= root;
for (i = 0 ; i< total; i ++){= if (current - = > arr [ 0 ] == current-> ; arr [ 1 ])
{
// 删除内容
tmp = current-> arr [ 0 ];
printf( 找到左递归:%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;
总++;
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;
总++;
}
if (current-> next-> next!= NULL)
{
current = current-> next;
}
}
nw = root;
printf( \ n新链接列表:\ n) ;
for (i = 0 ; i< total; i ++)puts(nw - = < span class =code-string> > arr);
if (nw-> next!= NULL)
{
nw = nw-> next;
}
printf( \ N跨度>);
getch();
return 0 ;
}
我正在尝试替代这个,但无法达到实际的解决方案
解决方案
';
temp-> arr [ 3 ] = ' \0' 跨度>;
nw-> next = temp;
nw = temp;
总++;
}
if (current-> next-> next!= NULL)
{
current = current-> next;
}
}
nw = root;
printf( \ n新链接列表:\ n) ;
for (i = 0 ; i< total; i ++)puts(nw - = < span class =code-string> > arr);
if (nw-> next!= NULL)
{
nw = nw-> next;
}
printf( \ N跨度>);
getch();
return 0 ;
}
我正在尝试替代这个,但无法达到实际的解决方案
1。声明类外的所有函数;
2.在c / cpp文件中将类中的所有变量重新声明为全局变量;
3.删除类声明;
就是这样。
#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="" <<="" 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;
}
What I have tried:
#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;
}
I was trying alternate to this but could not reach the actual solution
解决方案
'; 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; }
I was trying alternate to this but could not reach the actual solution
1. Declare all functions outside the class;
2. Re-declare all variable inside the class as global in your c/cpp-file;
3. Remove the class declaration;
That's it.
这篇关于需要帮助才能将其转换为C语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文