回 帖 发 新 帖 刷新版面

主题:如何将转换为C++

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

#define MASK_NOUN          0x1
#define MASK_ADJECTIVE     0x2
#define MASK_VERB          0x4

struct dictionary_entry{
    char word[256];
    char definition_noun[1024];
    char definition_adjective[1024];
    char definition_verb[1024];
    struct dictionary_entry *prev;
    struct dictionary_entry *next;
};

typedef struct dictionary_entry entry;

entry *head = NULL;
int dictionary_size = 0;
int display_options = MASK_NOUN|MASK_ADJECTIVE|MASK_VERB;

void cleanup(){
    while(head){
         entry *next = head->next;
         free(head);
         head = next;
    }
    head = NULL;
}

int load_dictionary() {
    FILE *fp = fopen("dictionary.txt", "r");

    cleanup();
    dictionary_size = 0;

    if(!fp){
         // File does not exist
         printf("[INFO]: No existing dictionary found.\n");
    } else {
         entry *current = NULL;
         entry *new_ = NULL;

         // Load dictionary into memory
         printf("[INFO]: Loading dictionary...\n");

         while(1){
              char buffer[1024];

              new_ = (entry *)calloc(1, sizeof(entry));
              if(!new_){
                   printf("[ERROR]: Failed to allocate memory.\n");
                   fclose(fp);
                   return 1;
              }

              // Parse word
              if(!fgets(new_->word, 255, fp)){
                   free(new_);
                   break;
              }

              // Eliminate trailing '\n'
              new_->word[strlen(new_->word)-1] = '\0';

              // Parse definition until blank newline
              while(fgets(buffer, 1023, fp)){
                   if(buffer[0] == '\n')
                        break;
                   strcat(new_->definition_noun, buffer);
              }

              // Parse definition until blank newline
              while(fgets(buffer, 1023, fp)){
                   if(buffer[0] == '\n')
                        break;
                   strcat(new_->definition_adjective, buffer);
              }

              // Parse definition until blank newline
              while(fgets(buffer, 1023, fp)){
                   if(buffer[0] == '\n')
                        break;
                   strcat(new_->definition_verb, buffer);
              }

              if(current != NULL){
                   new_->prev = current;
                   current->next = new_;
              }

              current = new_;

              if(!head)
                   head = current;

              dictionary_size++;
         }

         printf("[INFO]: Number of entries loaded = %d.\n", dictionary_size);

         fclose(fp);
    }

    return 0;
}...................

回复列表 (共1个回复)

沙发

首先,你的提问似乎莫名其妙;其次,你是不是直接拷贝了某处的代码?有些低级的语法错误可以似乎看出你的C的基础非常的不够好。如果你真的有志于学好编程,请努力去掉浮躁的习惯和态度吧。你的这个文件读写的程序修改掉某些低级错误,形式上C/C++没什么区别,如果一定要区别开,请把malloc/free之类的语句改为new/delete语句,你也可以进一步以封装为类的形式体现,那样看起来就比较"C++"了。

我来回复

您尚未登录,请登录后再回复。点此登录或注册