No.15461![]() |
文字列の一致 投稿者---HTM(2004/07/12 21:34:03) |
||
以下のソースを、 >PushDown "Z" Command= PushDown Parameter= Z というようにしたいのですがどうしたらできますか? 教えてください。 後、実行結果の際には”は取れてます。 #include <stdio.h> /* stdin, printf, fgets */ #include <stdlib.h> /* strlen, strchr */ #include <string.h> /* isdigit, islower */ #include <ctype.h> /* exit, atoi */ #define MAX 256 /* 文字列の長さ */ int getToken(const char *, char *); /* トークン切り出し関数 */ int isOperator(char); /* 演算子判定関数 */ char* Command1[2] = { "PopUp" }; char* Command2[2] = { "Clear" }; char* Command3[2] = { "End" }; char* Command4[2] = { "PushDown" }; int main(void) { char line[MAX]; /* 入力データを格納する配列 */ char token[MAX]; /* 出力データを格納する配列 */ char *p; /* ポインタ */ int cT; /* 変数の状態 */ while (printf(">"), fgets(line, MAX, stdin)) { /* キーボードから入力 */ for (p = line; *p; p += strlen(token)) { cT = getToken(p, token); /* 状態をclassTokenへ渡す */ if (strcmp(Command1[0], token) == 0) { printf("Command= PopUp\n"); printf("Parameter= \n"); } if (strcmp(Command2[0], token) == 0) { printf("Command= Clear\n"); printf("Parameter= \n"); } if ( strcmp(Command4[0], token) == 0) { printf("Command= PushDown\n"); printf("Parameter= "); } if (strcmp(Command3[0], token) == 0) { printf("Command= End\n"); printf("Parameter= \n"); exit(0); } if( cT == 'N' || cT == 'O' ){ cT = getToken(p, token); /* 状態をclassTokenへ渡す */ printf(token); printf("\n"); } else if( cT == 'E'){ /* Eという状態なら */ printf("ERROR\n"); /* ERRORを表示 */ break; } } /* for */ } /* while */ return 0; /* 正常終了 */ } int getToken(const char *p, char *token) { int c = *p; /* トークンをcへ */ if( c == '.'){ /* cがピリオドなら */ *token++ = c; *token = 0; return 'T'; } /* if */ if ( c == '\n' ) { /* cが改行なら */ *token++ = c; *token = 0; return 'H'; /* 改行の状態 */ } /* if */ if ( isalpha(c)){ do { *token++ = c; c = *++p; /* pを進める */ } while ( isalpha(c) ); *token = 0; /* 初期化 */ return 'M'; /* 英文字の状態 */ } /* if */ if ( isOperator(c) ) { /* cが演算子なら */ *token++ = c; *token = 0; return 'O'; /* 演算子の状態 */ } /* if */ if ( c == ' ' ) { /* cがスペースなら */ do { *token++ = c; c = *++p; /* pを進める */ } while ( c == ' ' ); *token = 0; /* 初期化 */ return 'B'; /* スペースの状態 */ }/* if */ if ( c == '"' ) { do { *token++ = c; c = *++p; /* pを進める */ } while ( c == ' ' ); *token = 0; /* 初期化 */ return 'A'; /* スペースの状態 */ }/* if */ if ( isdigit(c) ) { /* cが数値なら */ do { *token++ = c; c = *++p; /* pを進める */ } while ( isdigit(c) ); *token = 0; /* 初期化 */ return 'N'; /* 数値の状態 */ } /* if */ /* それ以外のものは */ *token++ = c; *token = 0; /* 初期化 */ return 'E'; /* それ以外の状態 */ return 0; /* 正常終了 */ } int isOperator(char token) { return token == '+' || token == '-' || token == '*' || token == '/'; /* それぞれの演算子を返す*/ } |
No.15466![]() |
Re:文字列の一致 投稿者---ぽへぇ(2004/07/13 05:12:17) |
||
>>PushDown "Z" >Command= PushDown >Parameter= Z >というようにしたいのですがどうしたらできますか? >教えてください。 ・>if ( c == '"' ) { のときの動作って、 > } while ( c == ' ' ); で良いんですか? コメントも変ですよね。 ・もし文字(列)を正しく切り出せたのなら、あとは printf("Parameter= %c\n", ch); なり printf("Parameter= %s\n", str); という方法で表示できます。 他に、細かいツッコミ所はいくつかあるのですが、 以下はコメントとプログラムとどちらが正しいのでしょうか? >int isOperator(char token) >{ > return token == '+' || token == '-' || token == '*' || token == '/'; > /* それぞれの演算子を返す*/ >} |
No.15503![]() |
Re:文字列の一致 投稿者---HTM(2004/07/14 12:06:51) |
||
コメントミスの部分とかいろいろ直しました。 このプログラムだと、 >PushDown "Z" Command= PushDown Parameter= PushDown Z となってしまうんです。Zだけparameterのところに表示させたいのですが どうやったらできますか? #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #define MAX 256 /* 文字列の長さ */ int getToken(const char *, char *); /* トークン切り出し関数 */ int isOperator(char); /* 演算子判定関数 */ char* Command1[2] = { "PopUp" }; char* Command2[2] = { "Clear" }; char* Command3[2] = { "End" }; char* Command4[2] = { "PushDown" }; int main(void) { char line[MAX]; /* 入力データを格納する配列 */ char token[MAX]; /* 出力データを格納する配列 */ char *p; /* ポインタ */ int cT; /* 変数の状態 */ while (printf(">"), fgets(line, MAX, stdin)) { /* キーボードから入力 */ for (p = line; *p; p += strlen(token)) { cT = getToken(p, token); /* 状態をclassTokenへ渡す */ if (strcmp(Command1[0], token) == 0) { printf("Command= PopUp\n"); printf("Parameter= \n"); } if (strcmp(Command2[0], token) == 0) { printf("Command= Clear\n"); printf("Parameter= \n"); } if ( strcmp(Command4[0], token) == 0) { printf("Command= PushDown\n"); printf("Parameter= "); } if (strcmp(Command3[0], token) == 0) { printf("Command= End\n"); printf("Parameter= \n"); exit(0); } if( cT=='N' || cT=='O'|| cT=='M' ){ cT = getToken(p, token); /* 状態をclassTokenへ渡す */ printf(token); printf("\n"); } else if( cT == 'E'){ /* Eという状態なら */ printf("ERROR\n"); /* ERRORを表示 */ break; } } /* for */ } /* while */ return 0; /* 正常終了 */ } int getToken(const char *p, char *token) { int c = *p; /* トークンをcへ */ if( c == '.'){ /* cがピリオドなら */ *token++ = c; *token = 0; return 'T'; } if ( c == '\n' ) { /* cが改行なら */ *token++ = c; *token = 0; return 'H'; /* 改行の状態 */ } if ( isalpha(c)){ do { *token++ = c; c = *++p; /* pを進める */ } while ( isalpha(c) ); *token = 0; /* 初期化 */ return 'M'; /* 英文字の状態 */ } if ( isOperator(c) ) { /* cが演算子なら */ *token++ = c; *token = 0; return 'O'; /* 演算子の状態 */ } if ( c == ' ' ) { /* cがスペースなら */ do { *token++ = c; c = *++p; /* pを進める */ } while ( c == ' ' ); *token = 0; /* 初期化 */ return 'B'; /* スペースの状態 */ } if ( c == '"' ) { do { *token++ = c; c = *++p; /* pを進める */ } while ( c == '"' ); *token = 0; /* 初期化 */ return 'A'; } if ( isdigit(c) ) { /* cが数値なら */ do { *token++ = c; c = *++p; /* pを進める */ } while ( isdigit(c) ); *token = 0; /* 初期化 */ return 'N'; /* 数値の状態 */ } /* それ以外のものは */ *token++ = c; *token = 0; /* 初期化 */ return 'E'; /* それ以外の状態 */ return 0; /* 正常終了 */ } int isOperator(char token) { return token == '+' || token == '-' || token == '*' || token == '/'; } |
No.15505![]() |
Re:文字列の一致 投稿者---shu(2004/07/14 13:30:24) |
||
11−4.コマンドライン引数 |
No.15508![]() |
Re:文字列の一致 投稿者---HTM(2004/07/14 16:05:07) |
||
なんとかできました。 ぽへぇさん、あかまさん、shuさんありがとうございました。 |
No.15469![]() |
Re:文字列の一致 投稿者---あかま(2004/07/13 08:35:16) |
||
参考になるかもしれません。 #include <stdio.h> #include <string.h> int main(){ char *command[16]; char str[256]; int i; putc('>',stdout); fgets(str,256,stdin); //'\n'切捨て i = strlen(str); if(str[i-1] == '\n') str[i-1] = '\0'; //切り出し command[0] = strtok(str," \""); for(i = 1;i < 16;i++){ command[i] = strtok( NULL," \"" ); if ( command[i] == NULL ) break; } //表示 for(i = 0;command[i] != NULL && i < 16;i++){ printf("command[%d]=%s\n",i,command[i]); } return 0; } |