0

.dat ファイルを読み取って印刷できる Fortran プログラムを作成する必要があります。

(ファイル homework_6.dat には、書籍のレコードが含まれています: 名前 (最大 25 文字)、発行年 (4 桁の整数)、価格 (6 桁の実数)、ISBN (13 桁の整数)。ファイル (homework_6.dat) を読み取るプログラムを作成します。 dat) を作成し、次の形式で詳細を (画面上または別のファイルに) 印刷します。

                                     Publish   
          Name                        Year    ($)        ISBN
       -------------------------      ----   ------     -------------
       Principles of Combustion       2005    107.61     9780471046899
       An Introduction to Comb        2011    193.99     9780073380193 
       Guide to Fortran               2009     71.95     9781848825420
       Modern Fortran Explain         2011    100.00     9780199601417
       Introduction to Program        2012    200.00     9780857292322)

ここに私が書くもの

program dat
implicit none
character (len=25) :: Name
integer :: i, publish_year, ISBN
real :: price 
open(unit=7, file="homework_6.dat", status="old", action="readwrite")
do i=1, 10
read (unit=7,fmt="(a25,i4,f3.2,i13)") Name, publish_year, price, ISBN
write (unit=7,fmt="(a25,i4,f3.2,i13)") Name, publish_year, price, ISBN
end do
close(unit=7)
end program dat

しかし、Fortran は 8 行目にエラーがあると言っています。

どうすればいいのかわからない:(

ソーニャ (ITU)

- 編集 -

だから私はプログラムを書こうとしましたが、実行後にまだエラーがあります

program dat
    implicit none
    character (len=25) :: Name
    character (len=13) :: ISBN
    integer :: i, publish_year
    real :: price 
    open(unit=10, file="homework_6.dat", status="old", action="readwrite")
    open(unit=11, file="output_hw6.dat")
    !Comment: these below 3 lines are for skipping 3 heading your input
    read(10,*)
    read(10,*)
    read(10,*)
    do i=1, 10
    read (10,*) Name, publish_year, price, ISBN
    write (11,1) Name, publish_year, price, ISBN
    1 format(a25,2x,i4,2x,f3.2,2x,a13)
    end do
    close(unit=10)
    end program dat

14 行目にエラーがあります。 ERROR 52、フィールド DAT に無効な文字があります - ファイル homework.f95 の 14 行目 [+01b3]

4

2 に答える 2

1

リスト指示形式で読んでいますが、これは機能しません。書籍名にスペースが含まれているため、コンパイラーは、書籍の末尾と年がどこで始まるかを検出できません。

フォーマットを使用する必要があります。ヒント: ラベル付きのフォーマット ステートメントではなく、read ステートメントでフォーマット文字列を使用します。フォーマットは、あなたが持っている出力に似ています。

別のヒントとして、価格の出力形式が短すぎます。f6.2をお勧めします。

于 2013-01-07T15:52:02.100 に答える
0

8行目でunit=7を7に置き換えます。ただし、問題は、homework_6.datから読み取り、同じファイルに書き込んでいることです。新しいファイルを開いたり、画面に出力したりできます。データの前に3行の見出しがあると仮定して、表示したデータファイルから読み取るようにコードを変更しました。

    program dat
    implicit none
    character (len=25) :: Name
    integer :: i, publish_year, ISBN
    real :: price 
    open(unit=7, file="homework_6.dat", status="old", action="readwrite")
    open(unit=8, file="output_hw6.dat")
    !Comment: these below 3 lines are for skipping 3 heading your input                 
    read(7,*)
    read(7,*)
    read(7,*)
    do i=1, 10
    read (7,*) Name, publish_year, price, ISBN
    write (8,1) Name, publish_year, price, ISBN
    1 format(a25,2x,i4,2x,f3.2,2x,i13)
    end do
    close(unit=7)
    end program dat

あなたのハードウェアの問題は、初心者のFortran本を調べることで簡単に解決できます。

于 2013-01-04T21:08:37.253 に答える