質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Visual C++

Microsoft Visual C++はWindowsのCとC++の統合開発環境(IDE)であり、コンパイラやデバッガを含んでいます。

Q&A

解決済

1回答

482閲覧

文字数、単語数、行数の計数

退会済みユーザー

退会済みユーザー

総合スコア0

Visual C++

Microsoft Visual C++はWindowsのCとC++の統合開発環境(IDE)であり、コンパイラやデバッガを含んでいます。

0グッド

0クリップ

投稿2019/10/14 14:00

Visual Studio 2019でC++のコードを書いています.
入力テキスト中の文字数、単語数、行数を教える問題です.単語は、空白文字で区切られた非空白文字の列とします.
以下が入力テキストです.

NASA debuted a color picture from the Spirit rover
on Tuesday showing gray rocks peppering a Martian
lake bed awash in its natural hues of red pink
and orange Mission scientists said they were
bowled over by the spectacular quality of
the image taken with a dual camera system
called pancam that is mounted on a mast jutting
up from the rover I think my reaction has been
one of shock and awe said Jim Bell the team
member in charge of pancam Using special software
mission scientists can fly though the image zooming
in on rocks and other landscape features of interest
It is approximately the color that you would see
with your eyes if you were standing there Bell said
The resolution of course is pretty much what you
would see Pancam has 20 20 vision It is three to
four times better than any previous mission that has gone
to Mars in fact these pictures are the highest resolution
highest detailed pictures of Mars ever obtained They
are absolutely spectacular

以下のコードで実行すると,単語数:94と出力されます.
単語数:173と出力するには,どこを直したらいいでしょうか?
正しいコードを教えてください.お願いします.

C++

1#include <string> 2#include <fstream> 3#include <iostream> 4using namespace std; 5 6// 定数定義 7#define B 0 // 空白文字 8#define N 1 // 通常文字 9 10// プロトタイプ宣言 11void updateWord(void); // 単語数を更新する 12 13// 大域変数 14int word; // 単語数 15int type; // 直前入力文字の種別 16char ch; // 入力文字 17 18/******************************** 19メイン関数 20********************************/ 21int main() { 22 23 ifstream file; // 入力ファイル 24 25 int letter; // 文字数 26 int line; // 行数 27 28 // 1. 文字数、単語数、行数を0とする。 29 letter = 0; 30 word = 0; 31 line = 0; 32 33 // 2. 「直前入力文字の種別」を「空白文字」とする。 34 type = B; 35 36 // 3. ファイルを開く。 37 file.open("test.txt"); 38 39 // 4. ファイルから1文字読み込む。 40 ch = file.get(); 41 42 // 5. ファイルの未尾に到達するまで以下の処理を繰り返す。 43 while (!file.eof()) { 44 45 // 5-1. 文字数を1増やす。 46 letter++; 47 48 // 5-2. 単語数を更新する。 49 updateWord(); 50 51 // 5-3. 読み込んだ文字が改行文字ならば、行数を1増やす。 52 if (ch == '\n') { 53 line++; 54 } 55 56 // 5-4. ファイルから1文字読み込む。 57 ch = file.get(); 58 } 59 60 // 6. 入力ファイルを閉じる。 61 file.close(); 62 63 // 7. 文字数、単語数、行数を出力する。 64 cout << "文字数:" << letter << endl; 65 cout << "単語数:" << word << endl; 66 cout << "行数:" << line << endl; 67} 68 69/******************************************** 70単語数を更新する 71********************************************/ 72void updateWord(void) { 73 74 75 76 // 1. 読み込んだ文字が空白文字ならば、以下の処理を行う。 77 if (isspace(ch)) { 78 79 // 1-1. 「直前入力文字の種別」が「通常文字」ならば、単語数を1増やす。 80 if (type == N) { 81 word++; 82 83 84 // 1-2. 「直前入力文字の種別」を「空白文字」とする。 85 86 type = B; 87 } 88 89 // 2. そうでない場合、以下の処理を行う。 90 else { 91 92 // 2-1. 「直前入力文字の種別」を「通常文字」とする。 93 type = N; 94 } 95 } 96}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

takasima20

2019/10/14 14:43

関数 updateWord の中を見直した方がいいと思う。
退会済みユーザー

退会済みユーザー

2019/10/14 14:49

関数 updateWordをどう直したらいいのかがわからないので教えてください。よろしくお願いします。
episteme

2019/10/14 19:50

> // 2. そうでない場合、以下の処理を行う。 「そうでない」が「読みこんだ文字が空白ではない」なら、 if (isspace(ch)) { // 1-1. 「直前入力文字の種別」が「通常文字」ならば、単語数を1増やす。 if (type == N) { word++; // 1-2. 「直前入力文字の種別」を「空白文字」とする。 type = B; } } // **** 1. の閉じカッコはココ。 // 2. そうでない場合、以下の処理を行う。 else { // 2-1. 「直前入力文字の種別」を「通常文字」とする。 type = N; }
guest

回答1

0

ベストアンサー

とりあえず、変数 type の更新は関数 updateWord 内から main に移しましょう。
場所は、関数 updateWord を呼んだ次あたりに。
--- 追記 ---
たとえばmainのループをこんな感じにしたら

c++

1 while (!file.eof()) { 2 letter++; 3 updateWord(); 4 if (isspace(ch)) { 5 type = B; 6 else { 7 type = N; 8 } 9 if (ch == '\n') { 10 line++; 11 } 12 ch = file.get(); 13 }

関数内はこんな感じになりますね。

c++

1void updateWord(void) { 2 if (isspace(ch)) { 3 if (type == N) { 4 word++; 5 } 6 } 7}

投稿2019/10/14 14:57

編集2019/10/14 23:11
takasima20

総合スコア7458

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2019/10/14 15:12

どのようにコードを書き直したらよいのでしょうか?教えてください.
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問