質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C

C言語は、1972年にAT&Tベル研究所の、デニス・リッチーが主体となって作成したプログラミング言語です。 B言語の後継言語として開発されたことからC言語と命名。そのため、表記法などはB言語やALGOLに近いとされています。 Cの拡張版であるC++言語とともに、現在世界中でもっとも普及されているプログラミング言語です。

データ構造

データ構造とは、データの集まりをコンピュータの中で効果的に扱うために、一定の形式に系統立てて格納する形式を指します。(配列/連想配列/木構造など)

Q&A

解決済

2回答

1235閲覧

c言語 木構造 実行時エラー

tamintya

総合スコア34

C

C言語は、1972年にAT&Tベル研究所の、デニス・リッチーが主体となって作成したプログラミング言語です。 B言語の後継言語として開発されたことからC言語と命名。そのため、表記法などはB言語やALGOLに近いとされています。 Cの拡張版であるC++言語とともに、現在世界中でもっとも普及されているプログラミング言語です。

データ構造

データ構造とは、データの集まりをコンピュータの中で効果的に扱うために、一定の形式に系統立てて格納する形式を指します。(配列/連想配列/木構造など)

0グッド

0クリップ

投稿2021/06/26 08:11

編集2021/06/26 13:01

###木構造を作り、間順と後順でたどり画面表示したい

データを実行時にファイルから読み込み、木構造を作り間順と後順でたどりその結果を画面表示するプログラムを作ったのですが一文字しか表示されません。
原因と直し方を教えて欲しいです。

また変数 w に str の値を次々と取り除いた値を代入していきたいです。
さらに可能であればbin_tree関数内で使う変数は
c:文字変数 w:文字列 p:ポインタ変数 str:cで始まる文字列(記号列)
のみでお願いしたいです。
注文が多くすみません。よろしくおねがいします。

発生している問題・エラーメッセージ

% ./a.out < file_prefix infix notation ====> / postifx notation ==> /

###理想の結果

str = *   の場合 w = + 15.5 5.2 - 7.1 / 8.2 2 str = +   の場合 w = 15.5 5.2 - 7.1 / 8.2 2 str = 15.5 の場合 w = 5.2 - 7.1 / 8.2 2 ------------------------------------------------- % cat file_prefix * + 15.5 5.2 - 7.1 / 8.2 2 % ./a.out < file_prefix infix notation ====> 15.5 + 5.2 * 7.1 - 8.2 / 2 postifx notation ==> 15.5 5.2 + 7.1 8.2 2 / - *

該当のソースコード

c

1#include<stdio.h> 2#include<stdlib.h> 3#include<string.h> 4#include<ctype.h> 5#include<math.h> 6 7struct node{ 8 char oper[10]; 9 struct node *left; 10 struct node *right; 11}; 12typedef struct node node; 13 14node *bin_tree(){ 15 char c; 16 char w[100],str[100]; 17 node *p; 18 19 fgets( w , 20 , stdin); 20 while((c = getchar()) != EOF){ 21 if(c != ' '){ 22 ungetc(c,stdin); 23 scanf("%s" , str); 24 p = (node *)malloc(sizeof(node)); 25 strcpy(p->oper , str); 26 // w = wにstrをの値を取り除いた値を入れたい 27 if(isdigit(c) != 0){ 28 p->left = NULL; 29 p->right = NULL; 30 return p; 31 } 32 else{ 33 p->left = bin_tree(); 34 p->right = bin_tree(); 35 return p; 36 } 37 } 38 } 39 return 0; 40} 41 42void inorder(node *p){ 43 if(p == NULL){ 44 return ; 45 } 46 else{ 47 inorder(p->left); 48 printf("%s" , p->oper); 49 inorder(p->right); 50 } 51} 52 53void postorder(node *p){ 54 if(p == NULL){ 55 return ; 56 } 57 else{ 58 postorder(p->left); 59 postorder(p->right); 60 printf(p->oper); 61 } 62} 63 64int main(void){ 65 node *root = NULL; 66 root = bin_tree(); 67 printf("infix notation ====> "); 68 inorder(root); 69 printf("\n"); 70 71 printf("postifx notation ==> "); 72 postorder(root); 73 printf("\n"); 74 75 return 0; 76} 77

###追記
解決時のコード

#include<stdio.h> #include<stdlib.h> #include<string.h> #include<ctype.h> #include<math.h> struct node{ char oper[10]; struct node *left; struct node *right; }; typedef struct node node; node *bin_tree(){ char c; char str[100]; node *p; while((c = getchar()) != EOF){ if(c != ' '){ ungetc(c,stdin); scanf("%s" , str); p = (node *)malloc(sizeof(node)); strcpy(p->oper , str); if(isdigit(c) != 0){ p->left = NULL; p->right = NULL; return p; } else{ p->left = bin_tree(); p->right = bin_tree(); return p; } } } return 0; } void inorder(node *p){ if(p == NULL){ return ; } else{ inorder(p->left); printf("%s " , p->oper); inorder(p->right); } } void postorder(node *p){ if(p == NULL){ return ; } else{ postorder(p->left); postorder(p->right); printf("%s " , p->oper); } } int main(void){ node *root = NULL; root = bin_tree(); printf("infix notation ====> "); inorder(root); printf("\n"); printf("postifx notation ==> "); postorder(root); printf("\n"); return 0; }

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

また変数 w に str の値を次々と取り除いた値を代入していきたいです。

さらに可能であればbin_tree関数内で使う変数は
c:文字変数 w:文字列 p:ポインタ変数 str:cで始まる文字列(記号列)
のみでお願いしたいです。

c と w を別の型のグローバル変数にしたので、条件に合っていないのですが、

C

1#include <stdio.h> // scanf, printf, putchar 2#include <stdlib.h> // malloc 3#include <string.h> // strcpy 4#include <ctype.h> // isdigit 5 6struct node { 7 char oper[10]; 8 struct node *left; 9 struct node *right; 10}; 11typedef struct node node; 12 13char *w[1000]; 14int c; 15 16node *bin_tree() 17{ 18 char str[100]; 19 if (scanf("%99s", str) != 1) return NULL; // 再帰呼出しを使うので while は不要 20 node *p = malloc(sizeof(node)); 21 w[c++] = strcpy(p->oper, str); 22 if (isdigit(*str)) 23 p->left = p->right = NULL; 24 else { 25 p->left = bin_tree(); 26 p->right = bin_tree(); 27 } 28 return p; 29} 30 31void inorder(node *p) 32{ 33 if (p == NULL) return; 34 inorder(p->left); 35 printf("%s ", p->oper); 36 inorder(p->right); 37} 38 39void postorder(node *p) 40{ 41 if (p == NULL) return; 42 postorder(p->left); 43 postorder(p->right); 44 printf("%s ", p->oper); 45} 46 47int main(void) 48{ 49 node *root = bin_tree(); 50 for (int i = 0; i < c; i++) { 51 printf("str = %-4s の場合 w =", w[i]); 52 for (int j = i + 1; j < c; j++) printf(" %s", w[j]); 53 putchar('\n'); 54 } 55 printf("infix notation ====> "); 56 inorder(root); 57 printf("\n"); 58 59 printf("postifx notation ==> "); 60 postorder(root); 61 printf("\n"); 62 63 return 0; 64}

投稿2021/06/27 00:01

kazuma-s

総合スコア8224

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

tamintya

2021/06/28 08:53

ありがとうございます。
guest

0

ベストアンサー

fgets( w , 20 , stdin);

これ何のため?

投稿2021/06/26 08:52

episteme

総合スコア16614

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

tamintya

2021/06/26 12:00

回答ありがとうございます。 試行錯誤していた時のを消し忘れていました。
episteme

2021/06/26 12:30

それ消すと動きが変わりますよね? どうなりました?
tamintya

2021/06/26 12:52

ほしい結果が表示されました! なぜ値が変わったのでしょうか?
tamintya

2021/06/26 13:02

また質問文にあるようにwに値を代入していくことは可能なのでしょうか?
episteme

2021/06/26 15:26

> なぜ値が変わったのでしょうか? fgets( w , 20 , stdin); しちゃったら、その後に続く while((c = getchar()) != EOF){ このgetcharは読むものがなくなるんじゃ? > また質問文にあるようにwに値を代入していくことは可能なのでしょうか? できます。そのくらい考えなさい。
tamintya

2021/06/28 08:54

分かりました。 ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問