開発環境
- OS X Mavericks - Apple(OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- C (プログラミング言語)
- Clang (コンパイラ)
C実践プログラミング 第3版 (Steve Oualline (著)、 望月 康司 (監訳) (翻訳)、谷口 功 (翻訳)、オライリー・ジャパン)のⅢ部(高度なプログラミング概念)の18章(モジュールプログラミング)、18-14(プログラミング実習)、実習18-4.を解いてみる。
その他参考書籍
- プログラミング言語C 第2版 ANSI規格準拠 (B.W. カーニハン D.M. リッチー (著)、 石田 晴久 (翻訳)、共立出版)
- プログラミング言語Cアンサー・ブック 第2版 (クロビス・L.トンド、スコット・E.ギンペル(著)、矢吹 道郎(翻訳))
18-14(プログラミング実習)、実習18-4.
コード
words.h
struct node{
char *word;
int i;
int linenos[100];
struct node *left;
struct node *right;
};
extern struct node *root;
extern void scan(char *name);
extern void print_tree(struct node *top);
コード
words.c
#include "ia.h"
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
struct node{
char *word;
int i;
/* int linenos[100]; */
struct infinite_array *linenos;
struct node *left;
struct node *right;
};
static int current_lineno = 1;
struct node *root = NULL;
int is = 0;
void memory_error(void)
{
fprintf(stderr, "Error:Out of memory\n");
exit (8);
}
void add_list(struct node *node, int n)
{
ia_store(node->linenos, node->i, n);
node->i = node->i + 1;
}
void print_lineno(struct node *node)
{
/*
int j;
for(j = 0; j < node->i; j++){
printf("%d ", node->linenos[j]);
}
*/
int index;
for(index = 0; index < node->i; ++index){
int data;
data = ia_get(node->linenos, index);
printf("%d ", data);
}
printf("\n");
}
char *save_string(char *string)
{
char *new_string;
new_string = malloc((unsigned) (strlen(string) + 1));
if (new_string == NULL){
memory_error();
}
strcpy(new_string, string);
return (new_string);
}
void enter(struct node **node, char *word)
{
int result;
char *save_string();
void memory_error();
void add_list(struct node *node, int n);
if ((*node) == NULL){
(*node) = malloc(sizeof(struct node));
if ((*node) == NULL){
memory_error();
}
(*node)->linenos = malloc(sizeof(struct infinite_array));
if((*node)->linenos == NULL){
memory_error();
}
(*node)->i = 0;
(*node)->word = save_string(word);
add_list((*node), current_lineno);
(*node)->left = NULL;
(*node)->right = NULL;
return;
}
result = strcmp((*node)->word, word);
if (result == 0){
add_list((*node), current_lineno);
return;
}
if (result < 0){
enter(&(*node)->right, word);
} else {
enter(&(*node)->left, word);
}
}
void scan(char *name)
{
char word[100];
int index;
int ch;
FILE *in_file;
in_file = fopen(name, "r");
if (in_file == NULL){
fprintf(stderr, "Error:Unable to open %s\n", name);
exit(8);
}
while (1){
while(1){
ch = fgetc(in_file);
if (ch == '\n'){
current_lineno++;
}
if(isalpha(ch) || (ch == EOF)){
break;
}
}
if (ch == EOF){
break;
}
word[0] = ch;
for (index = 1; index < sizeof(word); ++index){
ch = fgetc(in_file);
if (!isalpha(ch)){
break;
}
word[index] = ch;
}
word[index] = '\0';
enter(&root, word);
}
fclose(in_file);
}
void print_tree(struct node *top)
{
if (top == NULL){
return;
}
print_tree(top->left);
printf("%-10s: ", top->word);
print_lineno(top);
print_tree(top->right);
}
コード
sample.c
#include "words.h"
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
if(argc != 2){
fprintf(stderr, "Error:Wrong numbr of parameters\n");
fprintf(stderr, " on the command line\n");
fprintf(stderr, "Usage is:\n");
fprintf(stderr, " words 'file'\n");
exit(8);
}
scan(argv[1]);
printf("単語 : 行\n");
print_tree(root);
return (0);
}
Makefile
CC=cc CFLAGS=-g OBJS=sample.o words.o ia.o all: sample sample: $(OBJS) $(CC) $(CFLAGS) -o sample $(OBJS) sample.o: words.h sample.c words.o: ia.h words.c ia.o: ia.c clean: rm -f sample sample.o words.o ia.o
入出力結果(Terminal)
$ make make: Nothing to be done for `all'. $ ./sample sample.c 単語 : 行 Error : 8 Usage : 10 Wrong : 8 argc : 5 7 argv : 5 14 char : 5 command : 9 exit : 12 file : 11 fprintf : 8 9 10 11 h : 1 2 3 if : 7 include : 1 2 3 int : 5 5 is : 10 line : 9 main : 5 n : 8 9 10 11 15 numbr : 8 of : 8 on : 9 parameters: 8 print : 16 printf : 15 return : 17 root : 16 scan : 14 stderr : 8 9 10 11 stdio : 2 stdlib : 3 the : 9 tree : 16 words : 1 11 $
0 コメント:
コメントを投稿