主题:如何将转换为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;
}...................
#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;
}...................