佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 1023|回复: 24

很急!!! 求各位大大帮忙!!!

[复制链接]
发表于 8-9-2008 10:52 PM | 显示全部楼层 |阅读模式
我刚开始学C, LECTURER 就给难题了。 我完全不会写啦, 所以想要各位帮个忙。 就算只是一点SOURCE CODES 给我做参考也可以。
麻烦帮个忙!!! 以下是题目!!

Create an editable dictionary program with the followingfeatures:

§
Adding new words.
§
Deleting existing words andthe associated information.
§
Searching words. If thegiven word matches multiple entries, display the choices. For example, userenters “good”, and your dictionary contains “good”, “goodbye”, “goodness”,“goods”, and “goodwill”, these choices should be displayed to the user.Searching should be case insensitive.
§
Allow user to change displayoption, when listing the definition of a word, at run time. Examples of displayoption are:
o
Show all: all definitionsassociated with a word will be displayed.
o
Show noun only: show onlythe noun part of the definition.
o
Show adjective only: showonly the adjective part of the definition.
o
Show verb only: show onlythe verb part of the definition.
Indicate to the user if aword does not have the information for the given display option.
§
The initial dictionaryshould contain at least 100 entries. The chosen words must be able todemonstrate the use of display option.

All words and definitions are to be stored in a file(say, “dictionary.txt”) in your preferred format. On exit of your program, anychanges made must be stored in the file.


A sample output of your program may look like whatis shown below. (Bold text indicates user inputs.)


* Good
    * Goodbye
    * Goodness
    * Goods
    * Goodwill

   
    Enter your choice: 4
   
    Goods
    noun
    1.he dispatched the goods merchandise, wares, …
    2.the dead woman's …
   
    Search: ?
   
    Edit mode:
   

    * Add         word
    * Delete         word
    * Set         display option
    * Exit         display mode

   
    Enter your choice:
   


[ 本帖最后由 callmebrandon 于 9-9-2008 12:33 AM 编辑 ]
回复

使用道具 举报


ADVERTISEMENT

发表于 9-9-2008 11:13 AM | 显示全部楼层
这真的是初学的课题?

连我这个做了10几年的 programmer 看了都得皱一下眉头呢。。。

[ 本帖最后由 geekman 于 9-9-2008 11:14 AM 编辑 ]
回复

使用道具 举报

 楼主| 发表于 9-9-2008 12:40 PM | 显示全部楼层
原帖由 geekman 于 9-9-2008 11:13 AM 发表
这真的是初学的课题?

连我这个做了10几年的 programmer 看了都得皱一下眉头呢。。。

真的,我才学了两个月而已。 各位请帮帮我,真的很急了。!!!
明天LECTURER就要看了。
回复

使用道具 举报

发表于 9-9-2008 01:39 PM | 显示全部楼层
不是不想帮你,而是你的时间太紧迫了,如果有个3~5天的话我可以帮你写个绝对可以拿A+的program,但是一天真的不足够(我坦诚我的能力真的不足。。。),大家都也有自己的工作的嘛。。。

只能给你一些提示,你可以使用strcmp() (string compare)function 来进行字词的搜寻,它有许多变种,你可以参考你的IDE的documentation。

两个月就的做这样的project,你读的是速成班吗?还是现在大马的编程水准真的上升到这么高?我真的是老了。。。
回复

使用道具 举报

 楼主| 发表于 9-9-2008 03:42 PM | 显示全部楼层
原帖由 geekman 于 9-9-2008 01:39 PM 发表
不是不想帮你,而是你的时间太紧迫了,如果有个3~5天的话我可以帮你写个绝对可以拿A+的program,但是一天真的不足够(我坦诚我的能力真的不足。。。),大家都也有自己的工作的嘛。。。

只能给你一些提示,你可以 ...

我知道可以用strcmp() 可是写不出来。 真得帮不到吗??
回复

使用道具 举报

发表于 9-9-2008 03:55 PM | 显示全部楼层
哇靠,才2个月就要用array,function,fprintf,loop之类的东东做assignment = =!
2个月学到那么多东西,消化系统要很好。
这个1天很难做好的老实说。
我觉得你先写个基本的模型出来先,不然你的教授以为你懒惰不做 = =
回复

使用道具 举报

Follow Us
发表于 9-9-2008 04:20 PM | 显示全部楼层
我没办法帮你作出整个完整的program(真的很忙。。。忙到现在快4点了才得空吃午餐,现在正一边泡杯面,一边回信息),只能给你一些重点提示,其他的user input / output 方面真的得靠你自己了。

设定一个data structure:
  1. struct data_struct
  2. {
  3.     char keyword[50]; //你要搜索的词汇,50个字母应该够用了
  4.     int word_type; //代表这个词汇的种类,例如 1=noun, 2=adjective, 3=verb,以此类推
  5.     char definition[255]; //词汇的注释
  6.     //这个struct的大小应该是307bytes(assuming int = 16 bits)
  7. };

  8. data_struct myData;
复制代码
当你要写入资料时:
  1. FILE *file_ptr = fopen("dictionary.txt", "r+t");
  2. //move to end of file
  3. fseek(file_ptr, 0l, SEEK_END);
  4. fwrite(&myData, sizeof(struct_data), 1, file_ptr);
  5. fclose(file_ptr);
复制代码
注意:以上的代码并不管所写入的词汇是否有重复。要避免重复的话你得先读取所有的资料,比对你要写入的词汇,确定没有重复才写入。

当你要搜寻某个词汇时:
  1. FILE *file_ptr = fopen("dictionary.txt", "rt");//read only
  2. fseek(file_ptr, 0l, SEEK_SET);//move to start of file
  3. int result;
  4. do
  5. {
  6.     result = fread(&myData, sizeof(data_struct), 1, file_ptr);
  7.     //compare keywords
  8.     if(strnsmp(source_string, strlen(source_string), myData.keyword) == 0) //match
  9.     {
  10.         if(source_type == 0 /*display all type*/)
  11.         {
  12.              //just output this entry regardless of type
  13.         }
  14.         else
  15.         {
  16.              if(mtData.type == source_type) //you are looking for a particular type
  17.              {
  18.                    //output entry of matched type
  19.              }
  20.         }
  21.     }
  22. }while(result != 0); //while not reached end of file (eof)
复制代码
对比你的文件里面的每一笔资料(一笔资料就是对应一个data_struct 的大小),如果和source_string(你要搜寻的词汇,也就是user input)和当前所读取得一笔资料符合,就显示出来,不然就继续读取下一笔资料。

[ 本帖最后由 geekman 于 9-9-2008 04:26 PM 编辑 ]
回复

使用道具 举报

发表于 9-9-2008 04:28 PM | 显示全部楼层
惨!杯面被泡到足足有5mm那么大条。。。
回复

使用道具 举报


ADVERTISEMENT

 楼主| 发表于 9-9-2008 04:33 PM | 显示全部楼层
原帖由 geekman 于 9-9-2008 04:28 PM 发表
惨!杯面被泡到足足有5mm那么大条。。。

谢谢, 如果有问题可以问你吗??
回复

使用道具 举报

发表于 9-9-2008 04:44 PM | 显示全部楼层
可以可以可以可以可以(充字数)
回复

使用道具 举报

 楼主| 发表于 9-9-2008 04:56 PM | 显示全部楼层
原帖由 geekman 于 9-9-2008 04:44 PM 发表
可以可以可以可以可以(充字数)

好吧,现在要上课了。 晚上你还会在线上吗?? 可能那时才问你。谢谢
回复

使用道具 举报

发表于 9-9-2008 05:07 PM | 显示全部楼层
上课请专心
回复

使用道具 举报

发表于 9-9-2008 05:15 PM | 显示全部楼层

回复 11# callmebrandon 的帖子

应该会吧...如果没什么意外的话.
回复

使用道具 举报

 楼主| 发表于 9-9-2008 08:40 PM | 显示全部楼层

可以帮忙看有什么error吗?

可以帮忙看有什么error吗?

#include <stdio.h>
#include <stdlib.h>

#define STR_MAX 254

#define NOUN 1
#define ADJECTIVE 2
#define VERB 3

typedef struct wordnode * wordnodePtr

typedef struct wordnode
{
   char word[STR_MAX+1];
   char meaning[STR_MAX+1];
   wordnodePtr * next;
   int dataType;
} WordNode;

typedef struct dictionary
{
   wordnodePtr * head;
   int wordCount;
   int displayOption;
} Dictionary;

int main(void)
{
   char string[STR_MAX+1];
   int choice, exit;
   
   Dictionary dict;
   Init(dict);
   
   exit = 0;
   do{
      printf("Search: ");
      fgets(string, STR_MAX, stdin);
      
      if( strcmp(string, "?") == 0)
      {
         printf("1. Add word\n"
                "2. Delete word\n"
                "3. Set display option\n"
                "4. Exit display mode\n");
         fgets(string, STR_MAX, stdin);
         choice = atoi(string);
         
         switch(choice)
         {
            case 1:
               AddNewWord(dict);
               break;
            case 2:
               DeleteWord(dict);
               break;
            case 3:
               SetDisplayOption;
               break;
            case 4:
               exit = 1;
               break;
            default:
               break;            
         }
      }else{
         searchDictionary(dict, string);
      }
   } while( exit != 1);
}

Init(Dictionary * dict){
   //Initialise dict linklist
   dict->head = NULL
   dict->wordCount = 0;
}

void AddToDictionary(Dictionary * dict, char * word, char * meaning, int dataType)
{
   WordNode *newNode;
   //add to linklist
   newNode = ((WordNode *)malloc(sizeof(WordNode)));
   if( newNode == NULL )
   {
      printf("Failed to allocate new memory space.");
   }else{
      strcpy(newNode->word, word);
      strcpy(newNode->meaning, meaning);
      newNode = dataType;
   }
}

void SearchDictionary(Dictionary * dict, char * word)
{
   int i, choice;
   char string[STR_MAX+1];
   char ** possibleList;
   WordNode * current;
   //search
   i=0;
   current = dict->head;
   while( current != NULL )
   {
      if( strstr(word, current->word) == 0 )
      {
         if( dict->displayOption = current->dataType )
         {
            //print the possible list
            printf("%d. %s\n", word, i++);
            strcpy(possibleList, word);
         }
      }
      current = current->next;
   }
   //select from the list
   printf("\nEnter your choice: ");
   fgets(string, STR_MAX, stdin);
   choice = atoi(string);
   
   if(possibleList[choice] == NULL)   
   {   
      printf("No such index");
      return;
   }
   
   //display
   current = dict->head;
   while( current != NULL )
   {
      if( strcmp(possibleList[choice], current->word) == 0 )
      {
         printf("%s\n -----------------\n %s", word, meaning);
         //done
         return;
      }
      current = current->next;
   }
}

int LoadDictionary(Dictionary * dict)
{
   FILE * fp;
   char * p;
   char word[STR_MAX+1];
   char meaning[STR_MAX+1];
   char temp_str[STR_MAX+1];
   char * filename = "dictionary.txt";
   
   fp = fopen(filename, "r");   
   if( fp == NULL )
   {
      printf("Error");
      return EXIT_FAILURE;
   }

   while(!feof(fp) && fgets(temp_str, STR_MAX, fp))
   {
      char word[STR_MAX+1], meaning[STR_MAX+1];
      int dataType;
      p = strtok(temp_str, ":");
      strcpy(word, p);
      p = strtok(NULL, ":");
      dataType = p;
      p = strtok(NULL, ":");
      strcpy(meaning, p);
      addToDictionary(dict, word, meaning, dataType);
   }
}

void SaveToFile(Dictionary * dict)
{
   char * filename = "dictionary.txt";
}

void AddNewWord(Dictionary * dict)
{
//User input

//scanf word, meaning, word type


//Call AddToDictionary(dict, word, meaning)

}
void DeletewWord(Dictionary * dict)
{
   WordNode * current;
   printf("Enter word to delete: ");
   fgets(string, STR_MAX, stdin);
   
   current = dict->head;
   while( current != NULL )
   {
      if( strcmp(possibleList[choice], current->word) == 0 )
      {
         //found
         printf("%s has been deleted\n", word;
         free(current);
         return;
      }
      current = current->next;
   }

}

void SetDisplayOption(Dictionary * dict)
{
   printf("Set Display Option:\n"
          "1. Show All\n"
          "2. Show Noun\n"
          "3. Show Adjective\n"
          "4. Show Verb");
   
   fgets(string, STR_MAX, stdin);
   dict->displayOption = atoi(string);
}
回复

使用道具 举报

发表于 9-9-2008 10:46 PM | 显示全部楼层
function寫在int main(void) {}下面的話
上面要寫function prototype...

void DeletewWord(Dictionary * dict)
多了一個w

case 3:             SetDisplayOption;
少了()

typedef struct wordnode* wordnodePtr
少了;

還有一些scope的錯誤....
回复

使用道具 举报

 楼主| 发表于 9-9-2008 11:06 PM | 显示全部楼层
原帖由 cheng1986 于 9-9-2008 10:46 PM 发表
function寫在int main(void) {}下面的話
上面要寫function prototype...

void DeletewWord(Dictionary * dict)
多了一個w

case 3:             SetDisplayOption;
少了()

typedef struct wordnode* word ...

谢谢,我已经改了一部分。 还可以帮忙看一下下吗??
回复

使用道具 举报


ADVERTISEMENT

 楼主| 发表于 9-9-2008 11:09 PM | 显示全部楼层
原帖由 cheng1986 于 9-9-2008 10:46 PM 发表
function寫在int main(void) {}下面的話
上面要寫function prototype...

void DeletewWord(Dictionary * dict)
多了一個w

case 3:             SetDisplayOption;
少了()

typedef struct wordnode* word ...

谢谢,我已经改了一部分。 还可以帮忙看一下下吗??
回复

使用道具 举报

 楼主| 发表于 9-9-2008 11:20 PM | 显示全部楼层
原帖由 cheng1986 于 9-9-2008 10:46 PM 发表
function寫在int main(void) {}下面的話
上面要寫function prototype...

void DeletewWord(Dictionary * dict)
多了一個w

case 3:             SetDisplayOption;
少了()

typedef struct wordnode* word ...

还能帮忙多一些些吗??
回复

使用道具 举报

发表于 9-9-2008 11:39 PM | 显示全部楼层
要看syntax error還是logic error?
logic的話很暈下...
回复

使用道具 举报

发表于 10-9-2008 12:07 AM | 显示全部楼层

回复 1# callmebrandon 的帖子

MMU engineering 的?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 23-12-2025 05:00 PM , Processed in 0.910900 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表