http:^^www.cs.wisc.edu^~tick^cs110^week2.html
来自「This data set contains WWW-pages collect」· HTML 代码 · 共 469 行 · 第 1/2 页
HTML
469 行
Date: Mon, 11 Nov 1996 17:19:17 GMTServer: NCSA/1.5Content-type: text/htmlLast-modified: Mon, 16 Sep 1996 18:51:54 GMTContent-length: 17163<HTML><HEAD><TITLE>CS 110 Section 2 Lecture Notes - Week 2</TITLE></HEAD><BODY><H2><!WA0><!WA0><!WA0><A HREF="http://www.cs.wisc.edu/~tick/cs110.html#text" ><!WA1><!WA1><!WA1><IMG SRC="http://www.cs.wisc.edu/~tick/icons/arrowleft.gif" WIDTH=15 HEIGHT=15></A> Lecture Notes - Week 2</H2><HR><DL> <DT>Topic: <DD>Program structure. Constants and variables. Arithmetic expressions. The assignment statement. Built-in functions. Unformatted input and output. Errors. <DT>Text: <DD>Chp. 2.1 - 2.8 <DT>Notes: <DD> <HR> <CENTER> <H3>Fortran Line Structure</H3> </CENTER> <UL> <LI>FORTRAN programs are composed of <EM>lines</EM>, with one <EM>statement</EM> per line. <LI>Each line has four sections: <OL> <LI>Column 1 is the <EM>comment</EM> field. <LI>Columns 2-5 is the <EM>line label</EM>. <LI>Column 6 is the <EM>continuation marker</EM>. <LI>Columns 7-72 is the actual Fortran <EM>statement</EM>. </OL><P> <LISTING> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ... 72 +-+-------+-+---------------------------+ C label + statement </LISTING> </UL> <H4>Comment Field</H4> <UL> <LI>Comments are indicated by having a "<TT>C</TT>" (or "<TT>*</TT>") in column 1. <LI>Comments take up the whole line. Everything on the line is ignored by the compiler. <LI>Comments explain what your program code is doing in plain English. <LISTING> C Compute the total cost from the C unit cost and quantity ordered TOTCST = UNTCST * QUANTY </LISTING> </UL> <H4>Continuation Marker</H4> <UL> <LI>If a statement is too long to fit in the 72 columns then it can be split over two or more lines. <LI>Put a "<TT>+</TT>" in column 6 to indicate that the line is a continuation of the previous line. <LISTING> C Display the total cost PRINT *, 'The total cost is', + TOTCST, 'dollars' </LISTING> </UL> <H4>FORTRAN Statements</H4> <UL> <LI>All your FORTRAN statements must start in at least column 7 (helpful hint: change your tab setting to 6 characters). Do <STRONG>not</STRONG> write any FORTRAN statements starting in columns 1 to 6. <LI><EM>Right:</EM> <LISTING> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ... 72 +---------+-+---------------------------+ TOTCST = UNTCST * QUANTY </LISTING> <LI><EM>Wrong:</EM> <LISTING> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ... 72 +---------+-+---------------------------+ TOTCST = UNTCST * QUANTY </LISTING> <LI>Anything past column 72 is <EM>ignored</EM> by the compiler. If your lines are too long then split them over two lines with a continuation marker. </UL> <HR> <CENTER> <H3>FORTRAN Program Structure</H3> </CENTER> <UL> <LI>All FORTRAN programs have the same overall structure: <BLOCKQUOTE> <EM>Program Header</EM><BR> <EM>Variable Declarations</EM><BR> <EM>Main Executable Section</EM><BR> <TT>STOP</TT><BR> <TT>END</TT><BR> </BLOCKQUOTE> </UL> <H4>Program Header</H4> <UL> <LI>Gives the name of the program. <LISTING> PROGRAM ENCIPH </LISTING> </UL> <H4>STOP</H4> <UL> <LI>Tells the program to stop running and return to MS-DOS. </UL> <H4>END</H4> <UL> <LI>Tells the compiler that this is the last statement in the program and it should stop compiling. </UL> <HR> <CENTER> <H3>FORTRAN Variables</H3> </CENTER> <H4>Variable Names</H4> <UL> <LI><EM>Variables</EM> store important values that your program will use or hold intermediate results. <LISTING> TOTCST = UNTCST * QUANTY </LISTING> <LI>Variable names must start with a letter, contain only uppercase letters or digits, and no longer than <EM>six</EM> characters. <LI><EM>Right:</EM> <LISTING> TOTCST, QUANTY, INDEX2 </LISTING> <LI><EM>Wrong:</EM> <LISTING> TotalCost, 2B_Or_Not_2B </LISTING> </UL> <H4>Variable Types</H4> <UL> <LI>You must explicitly specify the <EM>type</EM> of each variable; i.e. what sort of value it will store. <LI>The most common variable types are: <UL> <LI><TT>REAL</TT> - a real number with a decimal point and fractional part. <LI><TT>INTEGER</TT> - a positive or negative integer number (no decimal places). <LI><TT>CHARACTER</TT> - one or more characters (e.g. a word or name). </UL> </UL> <H4>Variable Declarations</H4> <UL> <LI>Specify the variable <EM>type</EM> followed by the variable <EM>name(s)</EM>. <LISTING> REAL TOTCST, UNTCST INTEGER QUANTY INTEGER INDEX2 </LISTING> <LI>For character variables you must also specify the maximum length of the character string (optional if just one character long). <LISTING> CHARACTER *9 NAME CHARACTER CHAR </LISTING> <LI><STRONG>WARNING:</STRONG> If you don't explicitly specify the type of each variable then it will be declared a default type according to the <EM>I-N Rule</EM> (see Pg. 35). <STRONG>You must always explicitly declare every variable</STRONG>. </UL> <H4>Constants</H4> <UL> <LI>If the value of a variable is known when you write the program and it never changes (e.g. Pi) then turn it into a <EM>constant</EM> with the <TT>PARAMETER</TT> statement. <LISTING> REAL PI PARAMETER (PI = 3.141593) </LISTING> <LI>A variable turned into a constant cannot be re-assigned a new value later on. <LI>The <TT>PARAMETER</TT> statement immediately follows the variable declaration. </UL> <HR> <CENTER> <H3>Assignment Statement</H3> </CENTER> <UL> <LI>To store a value to a variable you have declared use the <EM>assignment statement</EM>. <LISTING> variablename = expression </LISTING> <LI>The variable is always on the <EM>left-hand</EM> side of the assignment statement. <LI><EM>Right:</EM> <LISTING> TOTCST = UNTCST * 1.25 </LISTING> <LI><EM>Wrong:</EM> <LISTING> UNTCST * 1.25 = TOTCST </LISTING> <LI>The variable being assigned can also be part of the expression on the right-hand side. <LISTING> COUNT = COUNT + 1 </LISTING> <LI>First, the whole expression is evaluated to get the result, then the result is stored in the variable. <LI>The assignment statement does <STRONG>not</STRONG> mean <EM>variable equals expression</EM> but rather <EM>variable gets-assigned-the-value-of expression</EM>. This is a subtle but important difference. </UL> <HR> <CENTER> <H3>Arithmetic Expressions</H3> </CENTER> <H4>Aritmetic Operators</H4> <UL> <LI>FORTRAN supports all the standard mathematical operators: <UL> <LI><TT>*</TT> - multiplication <LI><TT>/</TT> - division <LI><TT>+</TT> - addition <LI><TT>-</TT> - subtraction <BR>and also<BR> <LI><TT>**</TT> - exponential (e.g. b^2 is written as <TT>b**2</TT>) </UL> <LI><EM>Example</EM>: translate the following quadratic equation into FORTRAN <BLOCKQUOTE> 2x^2 - 5x + 7 = y </BLOCKQUOTE>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?