Arama butonu
Bu konudaki kullanıcılar: 1 misafir
4
Cevap
573
Tıklama
0
Öne Çıkarma
C++ kodu C'ye çevirme
A
2 yıl
Er
Konu Sahibi

Merhaba arkadaşlar, elimde şöyle bir C++ kod var ancak C'ye dönüştürmem gerekiyor. Yardımcı olabilecek var mı ?





#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_CHAR 256

struct SuffixTreeNode {
struct SuffixTreeNode *children[MAX_CHAR];

//pointer to other node via suffix link
struct SuffixTreeNode *suffixLink;

/*(start, end) interval specifies the edge, by which the
node is connected to its parent node. Each edge will
connect two nodes, one parent and one child, and
(start, end) interval of a given edge will be stored
in the child node. Lets say there are two nods A and B
connected by an edge with indices (5, 8) then this
indices (5, 8) will be stored in node B. */

/*(start, end) interval, düğümün üst düğümüne bağlandığı kenarı belirtir. 
Her kenar, bir ebeveyn ve bir çocuk olmak üzere iki düğümü birbirine bağlar
ve belirli bir kenarın (başlangıç, bitiş) aralığı alt düğümde saklanir. 
Diyelim ki (5, 8) indeksli bir kenarla birbirine bağlanan iki düğüm A ve B var, 
o zaman bu indeksler (5, 8)  B düğümünde saklanacak. */
int start;
int *end;

/*yaprak düğümleri için, kökten yaprağa giden yolun sonek dizinini saklar*/
int suffixIndex;
};

typedef struct SuffixTreeNode Node;

char text[100]; //Grilen string
Node *root = NULL; //root node yani ilk nodu tutan pointer

/*SonYeniDugum will point to newly created internal node,
waiting for it's suffix link to be set, which might get
a new suffix link (other than root) in next extension of
same phase. SonYeniDugum will be set to NULL when last
newly created internal node (if there is any) got it's
suffix link reset to new internal node created in next
extension of same phase. */
Node *SonYeniDugum = NULL;
Node *aktifDugum = NULL;

/*aktifKenar is represented as input string character
index (not the character itself)*/
int aktifKenar = -1;
int aktifUzunluk = 0;

// kalanSonekSayisi tells how many suffixes yet to
// be added in tree
int kalanSonekSayisi = 0;
int sonYaprak = -1;
int *sonRoot = NULL;
int *bolunmeSonu = NULL;
int size = -1; //Verilen string dizinin uzunlugu


//YENİ NODE EKLEME FONKSİYONU---------------------------------------------------------------------------------------------------------------------//


Node *YeniDugum(int start, int *end)
{
Node *node =(Node*) malloc(sizeof(Node));
int i;
for (i = 0; i < MAX_CHAR; i++)
node->children = NULL;

/*For root node, suffixLink will be set to NULL
For internal nodes, suffixLink will be set to root
by default in current extension and may change in
next extension*/
/*Kök düğüm için, suffixLink NULL olarak ayarlanacak 
Dahili düğümler için, suffixLink 
varsayılan olarak root olarak ayarlanacak 
bir sonraki uzantıda ise değişebilir*/
node->suffixLink = root;
node->start = start;
node->end = end;

/*suffixIndex will be set to -1 by default and
actual suffix index will be set later for leaves
at the end of all phases*/
/*suffixIndex varsayılan olarak -1'e ayarlanacak 
ve tüm aşamaların sonundaki yapraklar için 
gerçek sonek dizini daha sonra ayarlanacak*/
node->suffixIndex = -1;
return node;
}

//YENİ NODE EKLEME FONKSİYONU SONU---------------------------------------------------------------------------------------------------------------------//



//KEANAR UZUNLUGU BULMA FONKSİYONU---------------------------------------------------------------------------------------------------------------------//


int kenarUzunlugu(Node *n) {
if(n == root)
return 0;
return *(n->end) - (n->start) + 1;
}
//KEANAR UZUNLUGU BULMA FONKSİYONU SONU---------------------------------------------------------------------------------------------------------------------//



//DUGUMLERİ GEZ FONKSİYONU---------------------------------------------------------------------------------------------------------------------//

int asagiyaDogruDugumleriGez(Node *guncelDugum)
{
/*activePoint change for walk down (APCFWD) using
Skip/Count Trick (Trick 1). If aktifUzunluk is greater
than current edge length, set next internal node as
aktifDugum and adjust aktifKenar and aktifUzunluk
accordingly to represent same activePoint*/

/* Atla/Say ile trick'i (Trick 1) kullanarak aşağı inmek (APCFWD) için aktifNoktayi degistir.
aktifUzunluk mevcut kenar uzunluğundan büyükse, bir sonraki dahili düğümü aktifDugum olarak ayarlayın
ve aktifKenar ve aktifUzunluk'u aynı aktif noktaya gore degistirin*/
if (aktifUzunluk >= kenarUzunlugu(guncelDugum))//aktifUzunluk büyük esit guncel Dugumun kenar uzunlugu ise
{
aktifKenar += kenarUzunlugu(guncelDugum);//aktif kenara guncel Dugumun kenar uzunlugunu ekle
aktifUzunluk -= kenarUzunlugu(guncelDugum);//aktif uzunluktan guncel Dugumun kenar uzunlugunu çıkar
aktifDugum = guncelDugum;//guncelDugumu yeni aktif dugum olarak belirle
return 1;
}
return 0;
}





void extendSuffixTree(int pos)
{
/*Uzantı Kuralı 1, 
bu, ağaçta şimdiye kadar yaratılan tüm yapraklardir */
sonYaprak = pos;

/*Increment kalanSonekSayisi indicating that a
new suffix added to the list of suffixes yet to be
added in tree*/
/*kalanSonekSayisi değerini bir artırarak 
ağaçta henüz eklenmemiş sonekler listesine 
yeni bir sonek eklendi*/
kalanSonekSayisi++;

/*set SonYeniDugum to NULL while starting a new phase,
indicating there is no internal node waiting for
it's suffix link reset in current phase*/

/*yeni bir aşamaya başlarken SonYeniDugum'u NULL olarak ayarla, 
bu, mevcut aşamada soneki bağlantı sıfırlamasını bekleyen 
bir dahili düğüm olmadığını gösterir*/
SonYeniDugum = NULL;

//Add all suffixes (yet to be added) one by one in tree
while(kalanSonekSayisi > 0) {

if (aktifUzunluk == 0) {
//APCFALZ
aktifKenar = (int)text[pos]-(int)' ';
}
// There is no outgoing edge starting with
// aktifKenar from aktifDugum
if (aktifDugum->children[aktifKenar] == NULL)
{
//Extension Rule 2 (A new leaf edge gets created)
aktifDugum->children[aktifKenar] =
YeniDugum(pos, &sonYaprak);

/*A new leaf edge is created in above line starting
from an existing node (the current aktifDugum), and
if there is any internal node waiting for it's suffix
link get reset, point the suffix link from that last
internal node to current aktifDugum. Then set SonYeniDugum
to NULL indicating no more node waiting for suffix link
reset.*/
if (SonYeniDugum != NULL)
{
SonYeniDugum->suffixLink = aktifDugum;
SonYeniDugum = NULL;
}
}
// There is an outgoing edge starting with aktifKenar
// from aktifDugum
else
{
// Get the next node at the end of edge starting
// with aktifKenar
Node *next = aktifDugum->children[aktifKenar];
if (asagiyaDogruDugumleriGez(next))//Do asagiyaDogruDugumleriGez
{
//Start from next node (the new aktifDugum)
continue;
}
/*Extension Rule 3 (current character being processed
is already on the edge)*/
if (text[next->start + aktifUzunluk] == text[pos])
{
//If a newly created node waiting for it's
//suffix link to be set, then set suffix link
//of that waiting node to current active node
if(SonYeniDugum != NULL && aktifDugum != root)
{
SonYeniDugum->suffixLink = aktifDugum;
SonYeniDugum = NULL;
}

//APCFER3
aktifUzunluk++;
/*STOP all further processing in this phase
and move on to next phase*/
break;
}

/*We will be here when activePoint is in middle of
the edge being traversed and current character
being processed is not on the edge (we fall off
the tree). In this case, we add a new internal node
and a new leaf edge going out of that new node. This
is Extension Rule 2, where a new leaf edge and a new
internal node get created*/
bolunmeSonu = (int*) malloc(sizeof(int));
*bolunmeSonu = next->start + aktifUzunluk - 1;

//New internal node
Node *split = YeniDugum(next->start, bolunmeSonu);
aktifDugum->children[aktifKenar] = split;

//New leaf coming out of new internal node
split->children[(int)text[pos]-(int)' '] =
YeniDugum(pos, &sonYaprak);
next->start += aktifUzunluk;
split->children[aktifKenar] = next;

/*We got a new internal node here. If there is any
internal node created in last extensions of same
phase which is still waiting for it's suffix link
reset, do it now.*/
if (SonYeniDugum != NULL)
{
/*suffixLink of SonYeniDugum points to current newly
created internal node*/
SonYeniDugum->suffixLink = split;
}

/*Make the current newly created internal node waiting
for it's suffix link reset (which is pointing to root
at present). If we come across any other internal node
(existing or newly created) in next extension of same
phase, when a new leaf edge gets added (i.e. when
Extension Rule 2 applies is any of the next extension
of same phase) at that point, suffixLink of this node
will point to that internal node.*/
SonYeniDugum = split;
}

/* One suffix got added in tree, decrement the count of
suffixes yet to be added.*/
kalanSonekSayisi--;
if (aktifDugum == root && aktifUzunluk > 0) //APCFER2C1
{
aktifUzunluk--;
aktifKenar = (int)text[pos -
kalanSonekSayisi + 1]-(int)' ';
}

//APCFER2C2
else if (aktifDugum != root)
{
aktifDugum = aktifDugum->suffixLink;
}
}
}

void print(int i, int j)
{
int k;
for (k=i; k<=j && text[k] != '#'; k++)
printf("%c", text[k]);
if(k<=j)
printf("#");
}

//Print the suffix tree as well along with setting suffix index
//So tree will be printed in DFS manner
//Each edge along with it's suffix index will be printed
void setSuffixIndexByDFS(Node *n, int labelHeight)
{
if (n == NULL) return;

if (n->start != -1) //A non-root node
{
//Print the label on edge from parent to current node
print(n->start, *(n->end));
}
int leaf = 1;
int i;
for (i = 0; i < MAX_CHAR; i++)
{
if (n->children != NULL)
{
if (leaf == 1 && n->start != -1)
printf(" [%d]\n", n->suffixIndex);

//Current node is not a leaf as it has outgoing
//edges from it.
leaf = 0;
setSuffixIndexByDFS(n->children, labelHeight +
kenarUzunlugu(n->children));
}
}
if (leaf == 1)
{
for(i= n->start; i<= *(n->end); i++)
{
if(text == '#') //Trim unwanted characters
{
n->end = (int*) malloc(sizeof(int));
*(n->end) = i;
}
}
n->suffixIndex = size - labelHeight;
printf("\tDugum Numarasi: (%d)\n", n->suffixIndex);
}
}

void freeSuffixTreeByPostOrder(Node *n)
{
if (n == NULL)
return;
int i;
for (i = 0; i < MAX_CHAR; i++)
{
if (n->children != NULL)
{
freeSuffixTreeByPostOrder(n->children);
}
}
if (n->suffixIndex == -1)
free(n->end);
free(n);
}

/*Build the suffix tree and print the edge labels along with
suffixIndex. suffixIndex for leaf edges will be >= 0 and
yaprak olmayan kenarlar için -1 olacaktır*/
void buildSuffixTree()
{
size = strlen(text);//Verilen metnin uzunlugunu size a ata
int i;
sonRoot = (int*) malloc(sizeof(int));//sonRoot icin hafızada yer ac
*sonRoot = - 1;//sonRoot'in adresinde -1 tut

/*Root, başlangıç ve bitiş indeksleri -1 olan özel bir düğümdür,
bir kenarın kökü yani bir ebeveyni olmadığı için*/
root = YeniDugum(-1, sonRoot);

aktifDugum = root; //İlk aktifDüğüm kök olacak
for (i=0; i<size; i++)
extendSuffixTree(i);//extendSuffixTree fonks çalıtır.
int labelHeight = 0;
setSuffixIndexByDFS(root, labelHeight);

//Free the dynamically allocated memory
freeSuffixTreeByPostOrder(root);
}

// driver program to test above functions
int main(int argc, char *argv[])
{
// strcpy(text, "xabxac#abcabxabcd$"); buildSuffixTree();
strcpy(text, "xabxa$"); 
buildSuffixTree();
return 0;
}



T
2 yıl
Yarbay

Yukardaki C++ değil, C kodu. Zaten C'ye cevrilmiş :)



< Bu ileti mini sürüm kullanılarak atıldı >
Bu mesaja 1 cevap geldi.
A
2 yıl
Er
Konu Sahibi

Bana da atan kişi bu şekilde sorunca dikkat etmemiştim ama gerçekten de öyleymiş. Teşekkürler ayrıca :)



M
geçen yıl
Er

peki c++ c ye dönüştürebiliyor muyuz ?


Bu mesaja 1 cevap geldi.

Bu mesajda bahsedilenler: @vonderplanitz
T
geçen yıl
Yarbay

Turing-complete (Turing uyumlu) her bir dil, başka bir Turing-complete dile dönüştürülebilir. C ile C++ Turing uyumlu.



< Bu ileti mini sürüm kullanılarak atıldı >


Bu mesajda bahsedilenler: @musti1905atly
DH Mobil uygulaması ile devam edin. Mobil tarayıcınız ile mümkün olanların yanı sıra, birçok yeni ve faydalı özelliğe erişin. Gizle ve güncelleme çıkana kadar tekrar gösterme.