文字・配列・制御文

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
りな

文字・配列・制御文

#1

投稿記事 by りな » 17年前

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

int main(void) {

	char str_1[256],str_2[256];
	int i;

	printf("第一の文字列入力\n");
	gets(str_1);
	printf("第二の文字列入力\n");
	gets(str_2);

	/*文字列の長さを求める*/

	printf("第一の文字列の長さは%dです\n",strlen(str_1));
	printf("第二の文字列の長さは%dです\n",strlen(str_2));

	/*文字列の比較をする*/

	i=strcmp(str_1,str_2);
	if(!i){
		printf("文字列は等しい\n");
	}else if(i<0){
		printf("%sは%sより小さい\n",str_1,str_2);
	}else if(i>0){
		printf("%sは%sより大きい\n",str_1,str_2);
	}

	/*文字列連結*/

	if(strlen(str_1)+strlen(str_2)<256){
		strcat(str_1,str_2);
		printf("%s\n",str_1);
	}
	printf("終了\n");


	return 0;
}
質問させて頂きます。
主に、文字列比較のところの処理です。
1、strcmp()は文字列が等しいときは0をかえしますよね?
2、0は偽ですよね?
3、すると、if(!i)で0でないときだけループするわけですよね?
4、逆に文字列が等しくiが0のときはループしないとなると、「文字列は等しい」と出力されないのではないでしょうか?
・・・・いまいち理解ができません。ご教授お願いします。
<!--1

parapara

2分探索木--トラバーサルの非再帰版

#2

投稿記事 by parapara » 17年前

OS:XP-pro
開発環境:VC++.NET2002
トラバーサル(全て探索)の所が全く分かりません。
以下の//********の所です
何度もシミュレーションしてみたのですが・・・
#include<stdio.h>//2分探索木--トラバーサルの非再帰版
#include<string.h>
#include<stdlib.h>
#define MaxSize 256
struct tnode{
	struct tnode *left;
	char name[20];
	struct tnode *right;
}*temp1,*temp2,*head,*w[128];
struct tnode *gentree(struct tnode *a,struct tnode *temp2);
void search(char *key,struct tnode *a);
void disp(struct tnode *a);
void _free(struct tnode *a);
int main(void){
	temp1=(struct tnode *)malloc(sizeof(struct tnode));
	temp1->right=temp1->left=NULL;temp1->name[0]='\0';
	head=temp1;
	disp(head);
	while(1){
		temp2=(struct tnode *)malloc(sizeof(struct tnode));
		temp2->left=temp2->right=NULL;
		printf("追加したい名前>>");
		if(fscanf(stdin,"%s",temp2->name)==EOF)break;
		rewind(stdin);
		gentree(head,temp2);
		disp(head);
	}
	while(1){
		printf("探したい人の名前を入力して下さい>>");
		if(fscanf(stdin,"%s",temp2->name)==EOF)break;
		rewind(stdin);
		search(temp2->name,head);
		disp(head);
	}
	free(temp2);
	_free(head);
	return(0);
}
struct tnode *gentree(struct tnode *head,struct tnode *temp2){
	struct tnode *i=head;
	if(i==NULL)return(temp2);
	if(strcmp(i->name,temp2->name)>0)i->left=gentree(i->left,temp2);
	else if(strcmp(i->name,temp2->name)<0)i->right=gentree(i->right,temp2);
	else printf("名前:%sは既にいますので追加できません\n",temp2->name);
	return(i);
}
void search(char *key,struct tnode *head){
	struct tnode *i=head;
	if(i==NULL){
		printf("名前:%sはいません\n",key);
		return;
	}
	if(strcmp(i->name,key)>0)search(key,i->left);
	else if(strcmp(i->name,key)<0)search(key,i->right);
	else{
		printf("名前:%sは見つかりました\n",i->name);
		printf("%10p%10s%10p\n",i->left,i->name,i->right);
	}
}
void disp(struct tnode *head){
	int sp=0;
	struct tnode *temp1=head->right;
	while(!(sp==0&&temp1==NULL)){               //****************************
		while(temp1!=NULL){                 //****************************
			w[sp++]=temp1;              //****************************
			temp1=temp1->left;          //****************************
		}                                   //****************************
		sp--;                               //****************************
		printf("%s\n",w[sp]->name);         //****************************
		temp1=w[sp]->right;                 //****************************
	}                                           //****************************
}
void _free(struct tnode *a){
	struct tnode *temp1=a;
	if(temp1==NULL)return;
	disp(temp1->left);
	disp(temp1->right);
	free(temp1);
}

box

Re:2分探索木--トラバーサルの非再帰版

#3

投稿記事 by box » 17年前

> トラバーサル(全て探索)の所が全く分かりません。

二分木をどのようにたどるかは、二分木をどのように組み立てるかと
密接に関係します。

gentree関数の動きは把握されていますね?

parapara

Re:2分探索木--トラバーサルの非再帰版

#4

投稿記事 by parapara » 17年前

box様のヒントでひらめきました。
なんか解決できそうです。

parapara

Re:2分探索木--トラバーサルの非再帰版

#5

投稿記事 by parapara » 17年前

無事解決できました。box様毎度ながら、ありがとうございます。
本の解説が間違ってました。この本いくつ間違いがあるんだろう?もう5箇所くらい。

閉鎖

“C言語何でも質問掲示板” へ戻る