الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

[ تمت الإجابة ]مساعدة بطريقة وضع المدخلات بملف و استدعاها بالكود الاساسي بلغة الـ C

بدأه طالبة مجتهدة 2012 في 18 أكتوبر 2013 · 5 رد · 954 مشاهدة · في ارشيف قسم C/C++
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع



السلام عليكم

عندي كودين تعمل نفس الغرض 

ترتب الكلمات المدخلة ترتيب ابجدي زي الدكشنري

مطلوب مننا اننا ندور الاكواد جاهزة و نضيف له الرن تايم فانكشن و كمان اهم شي انه يكون المدخلات بملف لحال و نعمل له استدعاء من الكود الاساسي

لقينا الاكواد و اضفنا له الرن تايم فانكشن و الحمد لله اشتغل

بس مشكلتنا الان حاولنا كثير اننا نخلي المدخلات بملف لحال 

هذا الكود الاول شغال 


 

// C program to demonstrate Ternary Search Tree (TST) insert, travese 
// and search operations
#include <stdio.h>
#include <stdlib.h>
#define MAX 50
#include <time.h>
 FILE *infile;
	FILE *outfile;

 
// A node of ternary search tree

clock_t start = clock();
clock_t end;

struct Node
{
    char data;
 
    // True if this character is last character of one of the words
    unsigned isEndOfString: 1;
 
    struct Node *left, *eq, *right;
};
 
// A utility function to create a new ternary search tree node
struct Node* newNode(char data)
{
    struct Node* temp = (struct Node*) malloc(sizeof( struct Node ));
    temp->data = data;
    temp->isEndOfString = 0;
    temp->left = temp->eq = temp->right = NULL;
    return temp;
}
 
// Function to insert a new word in a Ternary Search Tree
void insert(struct Node** root, char *word)
{
    // Base Case: Tree is empty
    if (!(*root))
        *root = newNode(*word);
 
    // If current character of word is smaller than root's character,
    // then insert this word in left subtree of root
    if ((*word) < (*root)->data)
        insert(&( (*root)->left ), word);
 
    // If current character of word is greate than root's character,
    // then insert this word in right subtree of root
    else if ((*word) > (*root)->data)
        insert(&( (*root)->right ), word);
 
    // If current character of word is same as root's character,
    else
    {
        if (*(word+1))
            insert(&( (*root)->eq ), word+1);
 
        // the last character of the word
        else
            (*root)->isEndOfString = 1;
    }
}
 
// A recursive function to traverse Ternary Search Tree
void traverseTSTUtil(struct Node* root, char* buffer, int depth)
{
    if (root)
    {
        // First traverse the left subtree
        traverseTSTUtil(root->left, buffer, depth);
 
        // Store the character of this node
        buffer[depth] = root->data;
        if (root->isEndOfString)
        {
            buffer[depth+1] = '\0';
            printf( "%s\n", buffer);
        }
 
        // Traverse the subtree using equal pointer (middle subtree)
        traverseTSTUtil(root->eq, buffer, depth + 1);
 
        // Finally Traverse the right subtree
        traverseTSTUtil(root->right, buffer, depth);
    }
}
 
// The main function to traverse a Ternary Search Tree.
// It mainly uses traverseTSTUtil()
void traverseTST(struct Node* root)
{
    char buffer[MAX];
    traverseTSTUtil(root, buffer, 0);
}
 
// Function to search a given word in TST
int searchTST(struct Node *root, char *word)
{
    if (!root)
        return 0;
 
    if (*word < (root)->data)
        return searchTST(root->left, word);
 
    else if (*word > (root)->data)
        return searchTST(root->right, word);
 
    else
    {
        if (*(word+1) == '\0')
            return root->isEndOfString;
 
        return searchTST(root->eq, word+1);
    }
}
 
// Driver program to test above functions
int main()
{
    struct Node *root = NULL;
 
insert(&root, "cat");
insert(&root, "cats");
insert(&root, "up");
insert(&root, "bug");
insert(&root, "able");
insert(&root, "acid");
insert(&root, "angry");
insert(&root, "automatic");
insert(&root, "awake");
insert(&root, "bad");
insert(&root, "beautiful");
insert(&root, "bent");
insert(&root, "bitter");
insert(&root, "black");
insert(&root, "blue");
insert(&root, "boiling");
insert(&root, "bright");
insert(&root, "broken");
insert(&root, "brown");
insert(&root, "certain");
insert(&root, "cheap");
insert(&root, "chemical");
insert(&root, "chief");
insert(&root, "clean");
insert(&root, "clear");
insert(&root, "cold");
insert(&root, "common");
insert(&root, "complete");
insert(&root, "complex");
insert(&root, "conscious");
insert(&root, "cruel");
insert(&root, "cut");
insert(&root, "dark");
insert(&root, "dead");
insert(&root, "dear");
insert(&root, "deep");
insert(&root, "delicate");
insert(&root, "dependent");
insert(&root, "different");
insert(&root, "dirty");
insert(&root, "dry");
insert(&root, "early");
insert(&root, "elastic");
insert(&root, "electric");
insert(&root, "equal");
insert(&root, "false");
insert(&root, "fat");
insert(&root, "feeble");
insert(&root, "female");
insert(&root, "fertile");
insert(&root, "first");
insert(&root, "fixed");
insert(&root, "flat");
insert(&root, "foolish");
insert(&root, "free");
insert(&root, "frequent");
insert(&root, "full");
insert(&root, "future");
insert(&root, "general");
insert(&root, "good");
insert(&root, "gray");
insert(&root, "great");
insert(&root, "green");
insert(&root, "hanging");
insert(&root, "happy");
insert(&root, "hard");
insert(&root, "healthy");
insert(&root, "high");
insert(&root, "hollow");
insert(&root, "ill");
insert(&root, "important");
insert(&root, "kind");
insert(&root, "last");
insert(&root, "late");
insert(&root, "left");
insert(&root, "like");
insert(&root, "living");
insert(&root, "long");
insert(&root, "loose");
insert(&root, "loud");
insert(&root, "low");
insert(&root, "male");
insert(&root, "married");
insert(&root, "material");
insert(&root, "medical");
insert(&root, "military");
insert(&root, "mixed");
insert(&root, "narrow");
insert(&root, "natural");
insert(&root, "necessary");
insert(&root, "new");
insert(&root, "normal");
insert(&root, "old");
insert(&root, "open");
insert(&root, "opposite");
insert(&root, "parallel");
insert(&root, "past");
insert(&root, "physical");
insert(&root, "political");
insert(&root, "poor");
insert(&root, "possible");
insert(&root, "present");
insert(&root, "private");
insert(&root, "probable"); 
 
 
 
    printf("Following is traversal of ternary search tree\n");
    traverseTST(root);
 
 /*   printf("\nFollowing are search results for cats, bu and cat respectively\n");
    searchTST(root, "cats")? printf("Found\n"): printf("Not Found\n");
    searchTST(root, "bu")?   printf("Found\n"): printf("Not Found\n");
    searchTST(root, "cat")?  printf("Found\n"): printf("Not Found\n");
 */
    
    end = clock();
printf("\n%f\n", (double)(end - start));

return 0;
}

وهذا الكود

بعد محاولة فصل المدخلات بملف صيغتها و اسمها "Eb.txt"و استدعاها


 

// C program to demonstrate Ternary Search Tree (TST) insert, travese 
// and search operations
#include <stdio.h>
#include <stdlib.h>
#define MAX 50
#include <time.h>
 FILE *infile;
	FILE *outfile;

 
// A node of ternary search tree

clock_t start = clock();
clock_t end;

struct Node
{
    char data;
 
    // True if this character is last character of one of the words
    unsigned isEndOfString: 1;
 
    struct Node *left, *eq, *right;
};
 
// A utility function to create a new ternary search tree node
struct Node* newNode(char data)
{
    struct Node* temp = (struct Node*) malloc(sizeof( struct Node ));
    temp->data = data;
    temp->isEndOfString = 0;
    temp->left = temp->eq = temp->right = NULL;
    return temp;
}
 
// Function to insert a new word in a Ternary Search Tree
void insert(struct Node** root, char *word)
{
    // Base Case: Tree is empty
    if (!(*root))
        *root = newNode(*word);
 
    // If current character of word is smaller than root's character,
    // then insert this word in left subtree of root
    if ((*word) < (*root)->data)
        insert(&( (*root)->left ), word);
 
    // If current character of word is greate than root's character,
    // then insert this word in right subtree of root
    else if ((*word) > (*root)->data)
        insert(&( (*root)->right ), word);
 
    // If current character of word is same as root's character,
    else
    {
        if (*(word+1))
            insert(&( (*root)->eq ), word+1);
 
        // the last character of the word
        else
            (*root)->isEndOfString = 1;
    }
}
 
// A recursive function to traverse Ternary Search Tree
void traverseTSTUtil(struct Node* root, char* buffer, int depth)
{
    if (root)
    {
        // First traverse the left subtree
        traverseTSTUtil(root->left, buffer, depth);
 
        // Store the character of this node
        buffer[depth] = root->data;
        if (root->isEndOfString)
        {
            buffer[depth+1] = '\0';
            printf( "%s\n", buffer);
        }
 
        // Traverse the subtree using equal pointer (middle subtree)
        traverseTSTUtil(root->eq, buffer, depth + 1);
 
        // Finally Traverse the right subtree
        traverseTSTUtil(root->right, buffer, depth);
    }
}
 
// The main function to traverse a Ternary Search Tree.
// It mainly uses traverseTSTUtil()
void traverseTST(struct Node* root)
{
    char buffer[MAX];
    traverseTSTUtil(root, buffer, 0);
}
 
// Function to search a given word in TST
int searchTST(struct Node *root, char *word)
{
    if (!root)
        return 0;
 
    if (*word < (root)->data)
        return searchTST(root->left, word);
 
    else if (*word > (root)->data)
        return searchTST(root->right, word);
 
    else
    {
        if (*(word+1) == '\0')
            return root->isEndOfString;
 
        return searchTST(root->eq, word+1);
    }
}
 
// Driver program to test above functions
int main()
{
	
	{
		
	
	FILE *fp;
	char *fileName = "Eb.txt";
	char letter;
   struct Node *root = NULL;
	
	
	
	fp = fopen(fileName, "r");
	


/* 
insert(&root, "cat");
insert(&root, "cats");
insert(&root, "up");
insert(&root, "bug");
insert(&root, "able");
insert(&root, "acid");
insert(&root, "angry");
insert(&root, "automatic");
insert(&root, "awake");
insert(&root, "bad");
insert(&root, "beautiful");
insert(&root, "bent");
insert(&root, "bitter");
insert(&root, "black");
insert(&root, "blue");
insert(&root, "boiling");
insert(&root, "bright");
insert(&root, "broken");
insert(&root, "brown");
insert(&root, "certain");
insert(&root, "cheap");
insert(&root, "chemical");
insert(&root, "chief");
insert(&root, "clean");
insert(&root, "clear");
insert(&root, "cold");
insert(&root, "common");
insert(&root, "complete");
insert(&root, "complex");
insert(&root, "conscious");
insert(&root, "cruel");
insert(&root, "cut");
insert(&root, "dark");
insert(&root, "dead");
insert(&root, "dear");
insert(&root, "deep");
insert(&root, "delicate");
insert(&root, "dependent");
insert(&root, "different");
insert(&root, "dirty");
insert(&root, "dry");
insert(&root, "early");
insert(&root, "elastic");
insert(&root, "electric");
insert(&root, "equal");
insert(&root, "false");
insert(&root, "fat");
insert(&root, "feeble");
insert(&root, "female");
insert(&root, "fertile");
insert(&root, "first");
insert(&root, "fixed");
insert(&root, "flat");
insert(&root, "foolish");
insert(&root, "free");
insert(&root, "frequent");
insert(&root, "full");
insert(&root, "future");
insert(&root, "general");
insert(&root, "good");
insert(&root, "gray");
insert(&root, "great");
insert(&root, "green");
insert(&root, "hanging");
insert(&root, "happy");
insert(&root, "hard");
insert(&root, "healthy");
insert(&root, "high");
insert(&root, "hollow");
insert(&root, "ill");
insert(&root, "important");
insert(&root, "kind");
insert(&root, "last");
insert(&root, "late");
insert(&root, "left");
insert(&root, "like");
insert(&root, "living");
insert(&root, "long");
insert(&root, "loose");
insert(&root, "loud");
insert(&root, "low");
insert(&root, "male");
insert(&root, "married");
insert(&root, "material");
insert(&root, "medical");
insert(&root, "military");
insert(&root, "mixed");
insert(&root, "narrow");
insert(&root, "natural");
insert(&root, "necessary");
insert(&root, "new");
insert(&root, "normal");
insert(&root, "old");
insert(&root, "open");
insert(&root, "opposite");
insert(&root, "parallel");
insert(&root, "past");
insert(&root, "physical");
insert(&root, "political");
insert(&root, "poor");
insert(&root, "possible");
insert(&root, "present");
insert(&root, "private");
insert(&root, "probable"); 
 
 
  */
    printf("Following is traversal of ternary search tree\n");
     
    	do
	{
		fscanf(fp, "%c", &letter);
		printf("%c", letter);
	
		
	} 
	
	while(!feof(fp));
	
	printf("\n");
	fclose(fp);

 traverseTST(root);
	}
 /*   printf("\nFollowing are search results for cats, bu and cat respectively\n");
    searchTST(root, "cats")? printf("Found\n"): printf("Not Found\n");
    searchTST(root, "bu")?   printf("Found\n"): printf("Not Found\n");
    searchTST(root, "cat")?  printf("Found\n"): printf("Not Found\n");
 */
    
    end = clock();
    
    
    
printf("\n%f\n", (double)(end - start));

}

يظهر لنا الاوون بوت و يطلع لنا الرن تايم

بس الاون بوت ما يترسل للديكشنري فنكشن يعني ما يكون مرتب ترتيب ابجدي

و كمان يكرر اخر حرف باخر كلمة

2dFhY.jpg

حاولنا كثييييييييييير فيه نحنا ما درسنا شي زي كدا و بحثنا بالنت عن الطريقة و جربناها زي الكود اللي فوق و ما زبط


smil%20(1).gif

تم تعديل هذه المشاركة بواسطة طالبة مجتهدة 2012 في 18 أكتوبر 2013 في 16:25

−1
#2

السلام عليكم ..اختي الكريمة ..

بداية أرجو منك مستقبلا أن تحاولي الكلام بالعربي .. اووون بوت .. رن تايم .. فنكشن .. كلها لها ترجمة بسيطة .. (رغم أني لم أفهم اوون بوت هذه :p )
بالنسبة لمشكلة الكود ..

لم أقرأ كل الكود باعتباره يعمل .

المشكلة فقط في main  وهي بسيطة جداً ..

لقد قمتم فقط بقراءة وطباعة المحتويات دون القيام بعملية insert :]   هذه هي المشكلة فقط ..

وبالنسبة لإعادة طباعة الحرف الأخير فأظن أن هذا بسبب ما يلي  (لست متأكدا ):

في الكود التالي :

printf("Following is traversal of ternary search tree\n");
    do
    {
        fscanf(fp, "%c", &letter);
        printf("%c", letter);
    }
    while(!feof(fp));

عندما تصل القراءة إلى نهاية الملف فهذا يحدث في منتصف الحلقة وليس في آخرها ( أي قبل printf)  وهذا يعني أن البرنامج لن يخرج فوراً من الحلقة .. بل سيطبع آخر حرف قرأه ثم سيخرج

ولحل هذه المشكلة هناك حلّان بسيطان جداً ..

الأول : ضعوا سطرا فارغاً في نهاية الملف :) وبذلك سيطبع سطرا فارغا في النهاية ..

الثاني : عدّلو الكود بحيث تضعون جملة القراءة في آخره بدلا من أوله

 

وطبعاً لا تنسوا جملة الinsert

 

قمت بتعديل الكود  وأقترح تجريبه الآن (طبعاً يمكنكم التعديل بطريقة أفضل )

printf("Following is traversal of ternary search tree\n");
char word[1000];
    while(fscanf(fp,"%s",word)>1)
puts(word),insert(&root,word);

بالتوفيق :)

1
#3

كيف يمكننا كتابة جملة الـinsert 
المشكلة بالكود الان انها لا ترسل الكلمات اللي تقراها للدالة اللي ببداية الكود التي تقوم بترتيب الكلمات ابجدياً

ممكن توضيح اكثر لان حاولت اطبق الرد ما عرفت

#4

تم حل المشكلة بالكود الاول

و الان بنشتغل على الكود الثاني فيها اخطاء

#include <stdio.h>#include <stdlib.h>#include <string.h> /*to use strcpy*/#include <stddef.h> /*to use NULL*/ #define N 4#define SEED 1234567 struct words{    char word[N+1];    int used; /*If used is 1, word has been used and if used is 0, it has not.*/};typedef struct words WORDS; struct treenode{    char data[N+1];    struct treenode *left;    struct treenode *right;};typedef struct treenode NODE; WORDS words[1000]={0};NODE data[N+1]={0};FILE *W4;NODE *root; void getWords(void);void printWords(void);void randomWords(void);NODE *insert(NODE *root, char *word);void traverse(NODE *root);  int main(void){    getWords();    /*printWords();*/    randomWords();    root=NULL;    traverse(root);    system("PAUSE");    return 0;} void getWords(void){    int c=0, i=0, j=0; /*i increments words, j increments string*/    char string[N+1];     /*read in 1000 words from W4*/    W4=fopen("W4", "r");    while((c=getc(W4))!=EOF){        if(i!=1000){            if(c!='\n'&&c!=' ')            {                if(j==N){                    i++;                    string[j]='\0'; /*terminate string*/                    /*printf("%s ", string);*/                    strcpy(words.word, string);/*store string into an array*/                    j=0; /*reset string incrementer to 0*/                    string[j]=c; /*read in next character*/                    j++;                }                else{                    string[j]=c; /*read in a character*/                    j++;                }            }            else                j=0;        }    }    fclose(W4);    printf("\n%d words have been put in the array words[1000].\n", i);} void printWords(void){    int i;     for(i=0;i<1000;i++)        printf("%s%d ", words.word, words.used);} void randomWords(void){    int i, randnum;    /* initialize random generator */    srand(SEED);     for(i=0;i<100;i++){        randnum=rand()%1000;        if(words[randnum].used!=1)/* generate a random number */            /*printf("%d:%d ", i+1, randnum);*/            /*printf("%s ", words[randnum].word);*/            insert(root, words[randnum].word);            words[randnum].used=1;    }} NODE *insert(NODE *node, char *word){    if(node==NULL){        node=(NODE *) malloc(sizeof(NODE));        strcpy(node->data, word);        node->left=NULL;        node->right=NULL;    }    else{        if(strcmp(word, node->data)<0)            insert(node->left, word);        else if(strcmp(word, node->data)>0)            insert(node->right, word);    }    return node;} void traverse(NODE *root){    if(root!=NULL){        traverse(root->left); /*recur left*/        printf("%s ", root->data);        traverse(root->right); /*recur right*/    }}

تم تعديل هذه المشاركة بواسطة طالبة مجتهدة 2012 في 2 نوفمبر 2013 في 12:47

−1
#5

أختي الكريمة  هل هذا يعني أنك لا تعرفين كتابة الكود بنفسك ؟ مجرد استفسار .. أرى أنك تلصقين الأكواد معاً !

فالخطأ الذي تسألين عنه واضح جداً بشكل كبير .. والمنتدى هنا ليس لحل المشاكل .. ولكن للنقاش والمساعدة بعد المحاولة من قبل الأعضاء ..

root=NULL;traverse(root);

ثم في الدالة traverse

void traverse(NODE *root)
{
if(root!=NULL){
traverse(root->left); /*recur left*/
printf("%s ", root->data);
traverse(root->right); /*recur right*/
}
}

أي أنه لن يتم تنفيذ محتوى الدالة لأن الوسيط NULL !

أرجو أن ترجاعي الكود جيدا قبل السؤال

أين هو الجزء الذي يفترض به إدخال عناصر المصفوفة داخل الroot ؟

يجب أن يكون قبل استدعاء traverse هناك جزء يستعمل التابع insert فأين هو ؟

بالتوفيق

تم تعديل هذه المشاركة بواسطة مصطفى 36a2 في 1 نوفمبر 2013 في 16:35

1
#6

شكراً 

مواضيع مشابهة

عدد الزوار حالياً

المتواجدون خلال آخر دقيقتين · يتحدّث كل ٣٠ ثانية

—الإجمالي—أعضاء مسجّلون—زوار بدون تسجيل

جارٍ التحقق من المتواجدين…