ログインしてさらにmixiを楽しもう

コメントを投稿して情報交換!
更新通知を受け取って、最新情報をゲット!

C言語コミュの数値の入力(場合分け)

  • mixiチェック
  • このエントリーをはてなブックマークに追加
いま課題でやっていたのですが、途中でつまってしまいました。
どなたかヒントなど教えていただけると助かります。

内容
まず、数値を入力する。

それが以下の条件に反さない時、値を返します。
1、数値をなにも入力しなかった時
2、6桁以上入力した時
3、ー(マイナス)サインがはじめの位置以外にある時(例:ーー2とかはだめ)
4、32767 > 数値 または、数値 < ー32768 の時

今まで書いたプログラムです

#include "stdio.h"
#include "string.h"

int str(char str1[]);

int main(void){
int len;
char line[10];

printf("Enter Velocity:");
gets(line);
len = strlen(line);

for( ; ;){

if(len == 0){
printf("\nToo Few Digits. Try Again.");
}

else if( len > 6){
printf("\nToo Many Digits. Try Again.");
}
else
printf("\nEnter Velocity:%s",line);

}
return 0;
}

という風に書きましたが、他の3、4の条件はどのように書いたらいいのかわかりません。
3については strpbrk()
4については sscsnf()
をつかうのがヒントのようですが、いろいろ書いてみましたうまく行きません。
もしご存知の方がいましたら教えていただけないでしょうか?

コメント(15)

> いろいろ書いてみましたうまく行きません。

いろいろ書いたことをとりあえず教えてください。
珠々さん、猫皇さんありがとうござます。

珠々さんのおかげで
3はおそらく上手くいったと思うのですが、
4はちゃんと出来てないような気がします。

それもそうなんですが、実行すると
途中でとまってしまうんです。

下に自分で書いたコードをのせるのでもし何か間違いがあったらご指摘していただけませんか?

実際に行いたいのは
数字を二つ入力して(もしそれが正しければ)
かけ算の値を返すというものです。


#include "stdio.h"
#include "string.h"

/*prototype*/
int int_in(char *prompt );

/*---------------------------------------------------------------------------
[Function main]
The purpose of this function is that
1.Ask the user for numbers and call the function int_in to see if they are
valid.
2.caluculate to see if this program is working (product and difference)
---------------------------------------------------------------------------*/

void main(void){
static int vel, time, v1, v2, dist, diff; //variables
vel = int_in("Enter Velocity :");
time = int_in("Enter Time:");
dist = vel * time;
printf("\fVt = %d",dist); //the result of the product

v1 = 15460;
v2 = 12109;
diff = v1 - v2; //difference
}

/*--------------------------------------------------------------------------
[Function int_in]
The purpose of this function is to determine if the numbers the user
entered are valid. If they are not valid, messages will appear on the
LCD.
---------------------------------------------------------------------------*/



int int_in(char *prompt){
int len; //length of number
int num; //binary number after the string
char line[20]; //array
printf("\f%s",prompt);
gets(line);
len = strlen(line);
sscanf(line,"%ld",&num); //convert a string to a number

for( ; ;){ //loop structure for if...else

if(gets(line) == NULL){
printf("\nToo Few Digits. Try Again.");
}

else if( len > 6){
printf("\nToo Many Digits. Try Again.");
}

else if(strchr(line +1, '-') != NULL){
printf("\nConversion Error. Try Again");

}

else if(32767<num||num<-32767){
printf("Out of Range. Try Again.");
}
else
printf("\nEnter Velocity:%s",line);


}
return 0;
}
ソースを見て見ました。
上から順に優先度の高そうな問題点です。

・入力値を返すなら、int_inの戻り値は、numを返すべきなのでは?いまだと、vel * timeは必ず0では?
・intが16Bitの環境だと、32767*32767は桁あふれする?
・numへの変換が一番初めに入力された値で行われており、1回目の値がNGであれば、それ以降正常な値が入力されたとしてもnumは変わらない。
・strchrの第二引数は文字ではなく文字列(""で囲むべき)
・sscanfは "%ld"ではなく%dで読むべき???実行環境が分からないですが、結果をLCDで表示とか組み込みですかねぇ?だと、intはほんとに32Bitで良いか不明。C99に対応できるなら、inttypesの定義を使うべき。
・char *prompt は、const char* prompt にしないとダメなのでは?ワーニングとか出てないですか?
・main関数の変数はなぜstatic?つけても意味がないのでは?(やっぱり組み込み???)
・#include "stdio.h" は<>で囲む形にするべき。

ちょっと興味本位ですが、環境、コンパイラは何でしょう?

こゆきさん
ご指摘ありがとうございます。

はい。まさにそうです。
いつもdist = 0になります。

それと普通に条件にもれない値を入力しても LCDにTry Againがでてきてしまいます。

こゆきさんが列挙された所をいま修正してまたここに書き込ませてください。

ちなみに環境は(これをもって環境と言うのか自信がありませんが
知っていることを全て書いておきます)

コンパイラ     ICC12ide
実行+デバッガ   NoICE
interface hardware ComPOD12
target computer HC12compact
microcomputer 68HC12
皆さんの意見を参考にまた変更してみました。
何回か試してみましたが、上手く行く時と行かない時(多分無限ループです(でもどこで無限になってるかわかりません))があります。

以下コードです。
まだ条件3、4が上手く出てこない気がします。

/* includes */
#include "stdio.h"
#include "string.h"

/*prototype*/
int int_in(char *prompt );

/*---------------------------------------------------------------------------
[Function main]
The purpose of this function is that
1.Ask the user for numbers and call the function int_in to see if they are
valid.
2.caluculate to see if this program is working (product and difference)
---------------------------------------------------------------------------*/

void main(void){
static int vel, time, v1, v2, dist, diff; //variables
vel = int_in("Enter Velocity :");
time = int_in("Enter Time:");
dist = vel * time;
printf("\fVt = %d",dist); //the result of the product

v1 = 15460; //0x3C64
v2 = 12109; //0x2F4D
diff = v1 - v2; //difference(0x0D17)
}

/*--------------------------------------------------------------------------
[Function int_in]
The purpose of this function is to determin if the numbers the user
entered are valid. If they are not valid, messages will appear on the
LCD.
---------------------------------------------------------------------------*/

int int_in(char *prompt){
int len; //length of number
int num; //binary number after the string
char line[100]; //array
printf("\f%s",prompt);
gets(line);
len = strlen(line);


for( ; ; ){ //loop structure if...else

if(gets(line) == NULL){
printf("\f\nToo Few Digits. Try Again.");
}

else if( len > 6){
printf("\f\nToo Many Digits. Try Again.\v%s",prompt);
}

else if(strchr(line +1, '-') != NULL){
printf("\f\nConversion Error. Try Again.\v%s",prompt);
}

else{
sscanf(line,"%d",&num); //convert a string to a number
if(32767<num||num<-32767){
printf("\fOut of Range. Try Again.\v%s",prompt);
}else break;

}
}
return (int)num;

}
16bit環境ですかねぇ?
だとするとdistはlongで定義しておくのがよさそうですね。
mainのstaticは組み込みならアリだと思います。
とりあえず、forループの外で、
gets(line);
len = strlen(line);
をするのはNGじゃないでしょうか?
for文内部のgets(line)とlen > 6は別のif文に切り分けるべきだと思います。
# 文字列長をチェックするのに、for文の内部ではlenに
# 変更がかかる場所が無いですよね?

あと、return のキャストは意味が無いので書くべきでは無いかと思います。

それがしであれば


do {
  入力処理
  if (!入力が成功した事をチェック)
    continue; // またはアプリケーションの強制終了
} while (入力値のチェック);

という形をとる。

ポイントは
1.入力の成否判定と入力値の値判定を分離させる事ができる。
2.do while() は必ず一回は処理がされる事が保障されている。

ログインすると、みんなのコメントがもっと見れるよ

mixiユーザー
ログインしてコメントしよう!

C言語 更新情報

C言語のメンバーはこんなコミュニティにも参加しています

星印の数は、共通して参加しているメンバーが多いほど増えます。