السلام عليكم
عندي مشكلة في هذا البرنامج ومحتاجة مساعدتكم الله يرضى عنكم
المطلوب هو عمل برنامج dictionary و المطلوب انه يكون مكون من 3 ملفات
main.cpp : التي يعمل بها البرنامج
dict.h: وهذا هو الHeader file الذي بيكون فيه المتغيرات وال prototype functions
dictionray.cpp: الموجود فيه كل الدوال الواجب عملها لتنفيذ البرنامج انا مش عافة كيف استخدم هذه الدوال بوجود ال structs بلييييييييييييييييز محتاجة مساعدتكم
هذا هو الكود ل
dictionary.cpp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dict.h"
BOOL dict::InsertWord(DICT dict, WORD word)
{
// adds word to dictionary , if word can't be added returns 0 else returns 1
WORD tmp = new WORD();
//while (0 != *dict )
//dict++;
//*dict = word;
//count [dict - tmp] = 1;
BOOL a ;
WORD *t ,q ;
if(word == NULL ){
word = new WORD(dict,word);
t->numWords = word;
t->Words =NULL
a = 0;
return a;
}//end if
else {
t = dict;
while (t->Words != NULL)
t = t->Words ;
q = new WORD;
q->numWords =word;
q->Words = NULL;
t->numWords = q;
a = 1 ;
return a;
}//end else
return a;
}//end InsertWord
void dict::DumpDictionary(DICT dict) {
int i , j , num ;
i = j = num;
for (i = 0 ; (dict[num]!=0 && num < MAX) ;i++)
{
num++;
}//end for loop
for(i = num ; i> 0 ;i--)
for( j = 0; j<num-1 ;j++)
if ( strcmp(dict[j] , dict[j+1]) > 0 ){
WORD t = dict[j];
dict[j] = dict[j+1];
dict[j+1] = t;
//to extange the frequency
int t1 = count[j];
count[j] = count[j+1];
count[j+1] = t1;
}//end if
printf("Dump My Dictionary \n ");
for (i = 0 ; (dict!=0 &&i<MAX);i++)
printf("[%d] %s\t\t\t %d\n ",i dict,count);
}
WORD dict::GetNextWord(void){
/*
will retrieve next word in input stream. Word is defined just as in assignment #1
returns WORD or 0 if no more words in input stream
*/
char ch ;
int i = 0 ;
STRING s ;
int noWords = 0 ;
WORD myWord;
while ((ch = getchar()) != EOF)
if (isalpha(ch)){
s[i++] = ch;
noWords = 1;
}//end if
//if there is no word we are at the end of the list then we have to get out of the loop
else if (noWords){
s ='\0';
break;
}//end else if
if(0 == i)
return 0 ;
myWord = (WORD)malloc(i*sizeof(WORD)); //Dynamicly allocate a space for myWord by using the malloc
strcpy(myWord,s);
return myWord;
}//end getNextWord
BOOL dict::FullDictionary(DICT dict) {
/*
if dictionary is full, return 1 else 0
*/
int i = MAX - 1;
while ((i>MAX)&&(dict != 0 )) //*(dict+i) same as dic
i++;
if( i == MAX)
return 1;
else
return 0 ;
}//end FullDictionary
ENTRY dict::*LocateWord(DICT dict, WORD word) {
/*
will determine if dictionary contains word. if found, returns pointer to entry else returns 0
*/
int i = 0 ;
while((i<MAX)&&(*(dict+i))!= 0){
if((!strcmp(word, *(dict+1))))
return i ;
i++;
}//end while loop
return 0;
}//end LocateWord
وهذا الكود ل dict.h
#define MAX 100
typedef int BOOL;
typedef char *WORD;
/*
structure describing a word entry in the dictionary
*/
typedef struct entry {
int count; /* frequency count for a particular word */
WORD w; /* the word itself */
struct entry *nextWord; /* pointer to next entry */
} ENTRY;
/*
structure describing the dictionary
*/
typedef struct dict {
int maxEntries; /* maximum number of entries allowed; this is an artificial limits */
/* link lists can be as big as you want. This limit ensures that */
/* this code tries to behave like the previous ones */
int numWords; /* number of words in the dictionary */
ENTRY *Words; /* pointer to the entries in the dictionary */
} DICT;
typedef char STRING[132];
/*
you will have to write code for these 5 functions.
*/
ENTRY *LocateWord(DICT, WORD);
BOOL FullDictionary(DICT);
BOOL InsertWord(DICT,WORD);
WORD GetNextWord(void);
void DumpDictionary(DICT);
/*
note that these are global variables so that they are already initialized to 0
*/
DICT dictionary={MAX,0,0}; /* your dictionary */
WORD word; /* remember : this is a pointer. No memory exists. You must allocate all memory */
[/size
]