英語タイピング練習ソフトをプログラミングで

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

英語タイピング練習ソフトをプログラミングで

#1

投稿記事 by daigakusei » 8年前

用いているのはC言語です
問題
英語タイピング練習ソフトを作りたい.以下のような例文を入力した時の、入力にかかった時間を計測し,一分間に何文字入力できる早さであるかを出力するプログラムを作成せよ.入力ミスした際はもう一度行わせる.1分間に入力した文字数と時間を表示すること.
下記の英文は、char *words[5]のようなポインタ配列の初期値として与えるか、別途ファイルに書いておき、そこから読み込むようにする.

エラーは無いです
今は文章が一致しないため『A bad workman quarrels with his tools.』のみを定義しています

英文

A bad workman quarrels with his tools.
Every man is the architect of his own fortune.
People who live in glass houses should not throw stones.
A bird in the hand is worth two in the bush.
As you make your bed, so you must lie in it.

プログラム

/* -*-coding: utf-8-*- */
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
char n[5][50];
/*char *words[] = {"A bad workman quarrels with his tools.",
"Every man is the architect of his own fortune.",
"People who live in glass houses should not throw stones.",
"A bird in the hand is worth two in the bush.",
"As you make your bed, so you must lie in it."};*/
int i, len;
time_t start, end;
char words[50] = "A bad workman quarrels with his tools.";
srand(time(NULL));
start = time(NULL);
for(i = 0; i < 5; i++){
printf("words%d?", i);
fgets(n, 500, stdin);
len = strlen(n);
/*len += len;*/
printf("%s\n", words);
printf("%d\n", len - 2);
/*printf("%d\n", strcmp(n, words));*/
if(strcmp(n, words) == 0){
puts("nとwordsの文字列は一致しています。");
}
else{
puts("nとwordsの文字列は一致していません。");
}
}
end = time(NULL);
for(i = 0; i < 5; i++)
printf("%s\n", n);
printf("一分に%d文字打ちました:%.0f秒かかりました", len, difftime(end, start));
return 0;
}

実行結果

./a.out
words0?A bad workman quarrels with his tools.
A bad workman quarrels with his tools.
37
nとwordsの文字列は一致していません。
words1?Every man is the architect of his own fortune.
A bad workman quarrels with his tools.
45
nとwordsの文字列は一致していません。
words2?People who live in glass houses should not throw stones.
A bad workman quarrels with his tools.
55
nとwordsの文字列は一致していません。
words3?A bird in the hand is worth two in the bush.
A bad workman quarrels with his tools.
43
nとwordsの文字列は一致していません。
words4?As you make your bed, so you must lie in it.
A bad workman quarrels with his tools.
43
nとwordsの文字列は一致していません。
A bad workman quarrels with his tools.

Every man is the architect of his own fortune.

People who live in glass houses should not throw sA bird in the hand is worth two in the bush.

A bird in the hand is worth two in the bush.

As you make your bed, so you must lie in it.

一分に45文字打ちました:113秒かかりました
と表示されました。時間は測れているのですが、文字列が一致しません
またどうすれば文字列を間違えた時にもう一度入力し直す事が出来るのでしょうか・・・
どなたかお願い致します

daigakusei

Re: 英語タイピング練習ソフトをプログラミングで

#2

投稿記事 by daigakusei » 8年前

表示の仕方は
------------1---------
英文
ここ入力
合否の判定
--------------2---------
同上みたいな感じです

アバター
みけCAT
記事: 6734
登録日時: 14年前
住所: 千葉県
連絡を取る:

Re: 英語タイピング練習ソフトをプログラミングで

#3

投稿記事 by みけCAT » 8年前

ソースコードを提示する際は、BBCodeが有効な(無効にしない)状態でBBCodeのcodeタグで囲んでいただくと、見やすくてありがたいです。
daigakusei さんが書きました:時間は測れているのですが、文字列が一致しません
fgets関数で読み込んだ入力の最後に改行文字が入っているためですね。
以下のような「改行文字があったら消す」処理を追加すると、改善するかもしれません。

コード:

char* lf; /* 改行文字があったら消す処理に使う変数の宣言 */
fgets(n[i], 500, stdin); /* 入力 (元からある処理) */
if ((lf = strchr(n[i], '\n')) != NULL) *lf = '\0'; /* 改行文字があったら消す */
daigakusei さんが書きました: またどうすれば文字列を間違えた時にもう一度入力し直す事が出来るのでしょうか・・・
間違えた時はiをデクリメントすることで、for文でのインクリメントと相殺し、
「もう一度入力し直す」状態になるでしょう。
複雑な問題?マシンの性能を上げてOpenMPで殴ればいい!(死亡フラグ)

daigakusei

Re: 英語タイピング練習ソフトをプログラミングで

#4

投稿記事 by daigakusei » 8年前

何度も申し訳ありませんが
こんな感じです

/* -*-coding: utf-8-*- */
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
    char n[5][50];
   

コード:

 [color=blue]char[/color]*words[] = {"A bad workman quarrels with his tools.", 
                 [color=pink] "Every man is the architect of his own fortune.",
                  "People who live in glass houses should not throw stones.",
                  "A bird in the hand is worth two in the bush.",
                  "As you make your bed, so you must lie in it."};[/color]
    [color=green]int[/color] i, len;
    [color=green]time_t start[/color], end;
    /*char words[50] = "A bad workman quarrels with his tools.";*/
    srand(time([color=blue]NULL[/color]));
    start = time([color=blue]NULL[/color]);
    for(i = 0; i < 5; i++){
        printf([color=pink] "---------%d------------\n"[/color], i);
        printf([color=pink] "words%d?"[/color], i);
        fgets(n[i], [color=yelllow]500[/color], stdin);
        len = strlen(n[i]);
        /*len += len;*/
        printf("%s\n"[/color], words[i]);
        printf([color=pink] "%d\n"[/color], len - 2);
        /*printf("%d\n", strcmp(n[i], words));*/
        if(strcmp(n[i], words[i]) == 0){
            puts("nとwordsの文字列は一致しています。");
        }
        else{
            puts("nとwordsの文字列は一致していません。");
            }    
    }
    end = time(NULL);
    /*for(i = 0; i < 5; i++)
   printf([color=pink] "%s\n"[/color], n[i]);*/
   printf([color=pink] "一分に%d文字打ちました:%.0f秒かかりました\n"[/color], len, difftime(end, start));
    return 0;
}
 ./a.out
---------0------------
words0?A bad workman quarrels with his tools
A bad workman quarrels with his tools.
36
nとwordsの文字列は一致していません。
---------1------------
words1? Every man is the architect of his own fortune.
Every man is the architect of his own fortune.
46
nとwordsの文字列は一致していません。
---------2------------
words2?People who live in glass houses should not throw stones.
People who live in glass houses should not throw stones.
55
nとwordsの文字列は一致していません。
---------3------------
words3?A bird in the hand is worth two in the bush.
A bird in the hand is worth two in the bush.
43
nとwordsの文字列は一致していません。
---------4------------
words4?As you make your bed, so you must lie in it.
As you make your bed, so you must lie in it.
43
nとwordsの文字列は一致していません。
一分に45文字打ちました:75秒かかりました

表示方法はこのような感じです
問題点としては
・一致しない
・一分に打った文字列を正しく出力できない
・間違えた時にもう一度入力させ直すことができない(分からないのでプログラムしていない)です
色が変わっていますでしょうか?

アバター
みけCAT
記事: 6734
登録日時: 14年前
住所: 千葉県
連絡を取る:

Re: 英語タイピング練習ソフトをプログラミングで

#5

投稿記事 by みけCAT » 8年前

daigakusei さんが書きました:何度も申し訳ありませんが
こんな感じです
コード中にゴミと思われる文字列が大量にあり、コンパイルできるとは思えません。
本当のコードを貼ってください。
daigakusei さんが書きました:一分に打った文字列を正しく出力できない
「文字列」が文字数のことであると仮定すれば、
lenをループの前で0に初期化し、ループ中では入力の長さを代入ではなく加算し、
ループ後に合計の長さをかかった時間[分]で割った値を出力すれば、マシになるでしょう。
daigakusei さんが書きました:色が変わっていますでしょうか?
あなたは視覚障害者ですか?
もしくは、モノクロのディスプレイや色を処理できないブラウザを使用しているのですか?
そうでなければ、プレビュー機能や投稿後のスレッドを見て自分で確認すればいいでしょう。
複雑な問題?マシンの性能を上げてOpenMPで殴ればいい!(死亡フラグ)

閉鎖

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