2017-06-17 6 views
-4

だから、この光プロジェクトは、ユーザーがテキストファイルを読んでいました私の過去のクラスのいずれかにあった(下記参照、「studentstuff.txt」それを呼び出すことができます)読むと(ラインごと)のtxtファイルを変更

*studentstuff.txt* 

1 
Bob Smith 
24 
3.5 
2 
Jill Williams 
23 
3.6 
3 
Tom Jones 
32 
2.4 
4 
Julie Jackson 
21 
3.1 
5 
Al Brown 
23 
3.35 
6 
Juan Garcia 
22 
3.4 
-7 
Melissa Davis 
20 
3.2 
8 
Jack Black 
44 
1.1 

と出力が印刷されます:1)生徒数2)平均年齢3)平均GPA。この割り当てでは、構造体を持っていた:

:いくつかの小さな数学と機能が吐き出した後

typedef struct{ 
    int id; 
    char name[255]; 
    int age; 
    float gpa; 
}student; 

は、プログラムによると、「studentstuff.txtは、」その後、読み、ソートされた構造体によるとされ、

  • 学生の '#':

  • 平均年齢:

  • 平均GPA:

問題は私の頭の中にありますが、私はそれをコードに入れているようです。誰も私に助けてくれますか?

+5

Cに入れることができないコードの周りに少なくとも[mcve]を指定します。 main()、scan&Co、printf。そして、特に[尋ねる]を見て、[ツアー]を取る。 – Yunnosch

+0

私はソートの必要性を見ません。しかし、並べ替えが楽しいだけのために使用される場合、結果は入力ファイルを上書きする必要がありますか?タイトルには、ソートのようなファイル操作全体とは一致しない「行単位」があります。そして、後でファイル全体を上書きしない限り、どちらかが「修正」されることはありません。 – Yunnosch

答えて

0

プログラミング上の問題と同様に、(入力と出力を決定した後の)最初のアクションは、問題を単純な離散ステップに分解することです。

のOPの問題のためのステップのようなセットはのようになり

:計算は、画分を生産する問題を回避するために、構造体は次のように定義することをお勧めので

open the input file 
if any errors: 
    output user message to stderr 
    exit program, indicating error occurred 
else 
    begin: loop: 
     input the info for one student 
     if any errors, except EOF: 
      output user message to stderr 
      cleanup by closing the input file 
      exit program, indicating an error occurred 
     else 
      update number of students 
      update total age 
      update total gpa 
     endif 
     goto top of loop 
    end loop: 
endif 

calculate the average age 
calculate the average gpa 

display number of students 
display average student age 
display average student gpa 

cleanup by closing the input file 
return to caller, indicating success 

struct studentStruct 
{ 
    float id; 
    char name[255]; 
    float age; 
    float gpa; 
}; 

typedef struct studentStruct student; 

struct定義とtypedef文の分離に注目してください。ここでは何の違いもありませんが、デバッガ(構造体のすべてのフィールドを適切に表示するには構造体タグ名が必要です)と、大規模なプロジェクトで混乱を避けるために作業するときに使用されます。

関連する問題