前提・実現したいこと
このようなプログラムを書きたいのですが、removeがうまくいきません。
Cは苦手なので詳しく教えていただけると嬉しいです。
発生している問題・エラーメッセージ
error: expected expression before ‘=’ token
while(flg) = ( (c = getchar() ) = 'r');
^
error: lvalue required as left operand of assignment
while(flg) = ( (c = getchar() ) = 'r');
^
error: too few arguments to function ‘null_string’
c = null_string();
^~~~~~~~~~~
note: declared here
void null_string(char str[]){
^~~~~~~~~~~
該当のソースコード
include<stdio.h>
1include<string.h> //#は外しています。 2 char ha[1<<21],k[256]; 3void null_string(char str[]){ 4 str[0] = '\0'; 5} 6int main(int argc,char *argv[]){ 7 int flg; 8 int val,s,t; 9 char c; 10 11 scanf("%d", &val); 12 getchar(); 13 while (val--) { 14 s = 0; 15 t = 1; 16 flg = ( c = getchar() ) == 'a'; 17 while ( ( c = getchar() ) != ' ' ); 18 while(flg) = ( (c = getchar() ) = 'r'); 19 c = null_string(); 20 while ( ( c = k[getchar()] ) != 0 ) { 21 s += c * t; 22 t *= 4; 23 24 } 25 if (flg) ha[s] = 1; else puts( ha[s] ? "yes" : "no" ); 26 } 27 return 0; 28} 29
> removeがうまくいきません。
それ以外は出来た、ということなのでしょうか?
おそらくできていると思います。
なら、その動くプログラムを質問に追記してください。
いじくり回して文法エラーが出る=プログラム未満になってしまったものよりは、心機一転やり直した方がいいです。
そのプログラムでは、例えば
3
add 1ab
find 1ab
find 12a
という入力を与えると、
yes
no
という結果が得られるはずですね。
include<string.h> //#は外しています。
char ha[1<<21],k[256];
void null_string(char str[]){
str[0] = '\0';
}
int main(int argc,char *argv[]){
int flg;
int val,s,t;
char c;
scanf("%d", &val);
getchar();
while (val--) {
s = 0;
t = 1;
flg = ( c = getchar() ) == 'a';
while ( ( c = getchar() ) != ' ' );
while ( ( c = k[getchar()] ) != 0 ) {
s += c * t;
t *= 4;
}
if (flg) ha[s] = 1; else puts( ha[s] ? "yes" : "no" );
}
return 0;
}
上のプログラムになります。
そのプログラムでは、例えば
3
add 1ab
find 1ab
find 12a
という入力を与えると、
yes
no
という結果が得られていますか?
昨日やった時はうまくいったと思っていたのですが、
3
add 1ab
find 1ab
find 12a
と入力すると何も出力されずに終わってしまいます。
1
find aab
では
noと出力されます。プログラミング自体苦手なのでよくわかりません…
「出来た」という言葉の意味を合わせておかないといけないかしら。
「プログラムが出来た」というのは、コンパイラがエラーを出さなかったということではありません。もしそうなら、
int main(void){
return 0;
}
というのが万能で「出来た」ことになってしまいます...
「出来た」とはプログラムへの入力に対して期待する出力が確実に得られるようになったことをいいます。
コンパイルエラーではなくwarningがでました。
s.c:1:19: warning: extra tokens at end of #include directive
#include<string.h>
^
s.c: In function ‘main’:
s.c:9:1: warning: implicit declaration of function ‘scanf’ [-Wimplicit-function-declaration]
scanf("%d", &val);
^~~~~
s.c:9:1: warning: incompatible implicit declaration of built-in function ‘scanf’
s.c:9:1: note: include ‘<stdio.h>’ or provide a declaration of ‘scanf’
s.c:10:1: warning: implicit declaration of function ‘getchar’; did you mean ‘memchr’? [-Wimplicit-function-declaration]
getchar();
^~~~~~~
memchr
s.c:20:26: warning: implicit declaration of function ‘puts’ [-Wimplicit-function-declaration]
if (flg) ha[s] = 1; else puts( ha[s] ? "yes" : "no" );
^~~~
では、わかるところまで戻って、最初の「わからなくなったこと」を質問してみて下さい。
なぜaddが入力の文字列にはいると出力がされずに終わってしまうのでしょうか、またremoveをいれて実装するにはどのようなコードを書いたらよいのでしょうか。
> なぜaddが入力の文字列にはいると出力がされずに終わってしまう
現象の解釈が間違っています。
3
add zzz
www xxx
yyy zzz
を入力に与えてみて下さい。
> removeをいれて実装するにはどのようなコードを書いたら
これまでのコードも滅茶苦茶なので、全部書き換えです。
まずは、addコマンドで文字列を登録したら、それを表示する...という単純なプログラムを書いてみたらいかがですか。
