http:^^www.cs.wisc.edu^~tick^cs302^week2.html
来自「This data set contains WWW-pages collect」· HTML 代码 · 共 507 行 · 第 1/2 页
HTML
507 行
</PRE> <LI><EM>Right:</EM> <LISTING> A = (B - C) / (D - E) </LISTING> <LI><EM>Wrong:</EM> <LISTING> A = B - C / D - E </LISTING> <LI><STRONG>EXCEPTION:</STRONG> Consecutive exponents are evaluated <EM>right-to-left</EM>. Example: <BLOCKQUOTE> y = x^(z^2) </BLOCKQUOTE> <BR>can be written as<BR> <LISTING> Y = X**Z**2 </LISTING> </UL> <H4>Arithmetic Functions</H4> <UL> <LI>FORTRAN has many built-in arithmetic functions (see Pg. 64 and Appendix A): <UL> <LI><TT>SQRT</TT> - square root <LI><TT>SIN</TT> - sine root <LI><TT>COS</TT> - cosine <LI><TT>TAN</TT> - tangent <LI><TT>EXP</TT> - e^x <LI><TT>ALOG</TT> - natural logarithm <LI><TT>ALOG10</TT> - logarithm base 10 <LI><TT>NINT</TT> - round a <TT>REAL</TT> number to nearest <TT>INTEGER</TT> </UL> <LI>Functions can be used in any arithmetic expression. <LISTING> Y = EXP(4.5) + TAN(X + 2*SQRT(X)) </LISTING> <LI>The <EM>arguments</EM> of a function can be either values, variables or even arithmetic expressions and are enclosed in parentheses (...). <LI>Some functions have more than one argument separated by commas. <LISTING> A = B + MOD(C, 4) </LISTING> </UL> <H4>Type Matching</H4> <UL> <LI>The <EM>type</EM> of an expression should match the <EM>type</EM> of the variable the result is going to be stored in. <LISTING> integer-variable = integer-expression real-variable = real-expression </LISTING> <LI>If the types <EM>mismatch</EM> then the expression is automatically converted to match the type of the variable, <EM>truncating</EM> any decimal places if necessary. <LISTING> REAL X INTEGER I X = 6.6 * 1.6 (10.56) X = 6.6 / 1.6 (4.1256) I = 6.6 / 1.6 (10) I = 6.6 / 1.6 (4) </LISTING> <LI>For all the operators, if <STRONG>both</STRONG> the operands are type <TT>INTEGER</TT> then the result is also type <TT>INTEGER</TT>. If either operand is type <TT>REAL</TT> then the result is type <TT>REAL</TT>. <LI><STRONG>WARNING:</STRONG> With division, if both the operands are type <TT>INTEGER</TT> then the result is automatically truncated to an <TT>INTEGER</TT> also! <LISTING> 2.0/3 = 0.66666 2/3 = 0 (!) </LISTING> <LI><STRONG>IMPORTANT:</STRONG>Always check the argument and return types for functions so you know whether or not your results will be truncated. </UL> <H4>REAL Numbers</H4> <UL> <LI><TT>REAL</TT> numbers can be written in either <EM>decimal</EM> or <EM>scientific</EM> notation. <UL> <LI>decimal: 12.3 <LI>scientific: 0.123E+2 (i.e. 0.1234*10^2) </UL> <LI>By default <TT>REAL</TT> numbers are printed in <EM>decimal</EM> notation. </UL> <H4>Rounding Errors</H4> <UL> <LI>Numbers are stored inside the computer in <EM>binary</EM> format (i.e. as powers of 2) <BLOCKQUOTE> 10 base 10 = 2^3 + 2^1 = 1010 base 2 </BLOCKQUOTE> <LI>Most fractions cannot be represented precisely in binary (e.g. 0.1) so instead the closest approximimation in base 2 is stored. Therefore, most <TT>REAL</TT> numbers are not stored precisely on <STRONG>any</STRONG> computer. <BLOCKQUOTE> 0.1 * 10.0 does not equal 1.0 (e.g. = 0.999998) </BLOCKQUOTE> <LI><STRONG>IMPORTANT:</STRONG> Always use <TT>INTEGER</TT> values and variables whenever possible because these are stored precisely. Only use <TT>REAL</TT> values if you absolutely have to. </UL> <H4>Storing in Binary</H4> <UL> <LI>Integers: Binary string <EM>abcde</EM> converts by<BLOCKQUOTE>a*2^4 + b*2^3 + c*2^2 + d*2^1 + e*2^0.<P>so 01000001 = 1*2^6 + 1*2^0 = 65</BLOCKQUOTE> <LI>Characters: Convert to ASCII code value<BLOCKQUOTE>(for example 'A' is stored as 01000001 i.e. 65)</BLOCKQUOTE> <LI>Reals: Stored in binary version of scientific notation.<p> <UL> <LI> <EM>ab.cd</EM> in binary is<BLOCKQUOTE>a*2^1 + b*2^0 + c*2^(-1) + d*2^(-2),</BLOCKQUOTE>just as <EM>ab.cd</EM> in decimal is<BLOCKQUOTE>a*10^1 + b*10^0 + c*10^(-1) + d*10^(-2)<p>So 1.0100 = 1*2^0 + 1*2^(-2) = 1 + 1/4 = 1.25</BLOCKQUOTE> <LI> Recall in decimal<BLOCKQUOTE>123.8 becomes 1.238 x 10^2 in sci. notation.<p></BLOCKQUOTE>Moving the decimal point multiplies of divides by two. In binary, since we have only two possibilities per digit (0 or 1), moving the decimal point multiplies and divides by 2.<BLOCKQUOTE>So 1000.1 becomes 1.0001 x 2^3<P>(or, to write this completely in binary, 1.0001 x 10^101).</BLOCKQUOTE> <LI>Also remember that sci. notation in decimal is of the form <EM>a.bcde x 10^f</EM> where a is between 1 and 9. In binary, since we only have 0's and 1's, a can only be a 1. So every time we convert to sci. notation in binary, we'll have a 1 there. Therefore we don't even need to store the 1 (we know it'll always be there)...this gives us more storage space.<BLOCKQUOTE>So 1.0100 is represented by only for digits...0100.</BLOCKQUOTE> <LI>So, suppose we're given 01000001 and told we're given 4 digits mantissa, 4 digits exponent. Think of it as 0100 0001<BLOCKQUOTE>So, in complete binary this is 1.0100 x 10^0001,<P>which converts to (1 + 1/4) x 2^1 = (1.25)*2 = 2.5</BLOCKQUOTE> </UL> </UL> <H4>CHARACTER Strings</H4> <UL> <LI>To assign a value to a <TT>CHARACTER</TT> variable it must be either another <TT>CHARACTER</TT> variable or a string enclosed in single apostrophes. <LISTING> CHARACTER *10 NAME1, NAME2 NAME1 = 'John Doe' NAME2 = NAME2 </LISTING> <LI>The apostrophes are not stored in the variable. To store an apostrophe inside and string type in <EM>two</EM> apostrophes. <LISTING> NAME1 = 'John''s dog' (John's dog) </LISTING> <LI>If the string is shorter than the variable then then variable is <EM>padded</EM> with blanks (denoted by a <TT>"#"</TT>). <LISTING> NAME1 = 'John Doe' (John Doe##) </LISTING> <LI>If the string is longer than the variable then the excess characters are ignored; i.e. the string is <EM>truncated</EM>. <LISTING> NAME1 = 'John Doesnt' (John Doesn) </LISTING> </UL> <HR> <CENTER> <H3>Input and Output</H3> </CENTER> <H4>Unformatted Output</H4> <UL> <LI>To display results on the screen use the <TT>PRINT</TT> statement. <LISTING> PRINT *, TOTCST </LISTING> <LI>To print multiple items on the same line separate them by commas. <LISTING> PRINT *, X, ' plus ', Y, ' equals ', X+Y </LISTING> <LI>You can print values, variables, arithmetic expressions or <TT>CHARACTER</TT> strings. <LI>The next <TT>PRINT</TT> statement prints on the next line, not on the end of the previous one. <LI>Always <EM>prompt</EM> the user before asking him/her to type something in. Otherwise when they run your program they will not know that the computer is waiting for them to enter a value. <LISTING> PRINT *, 'Please enter the cost of the item' READ *, UNTCST </LISTING> </UL> <H4>Unformatted Input</H4> <UL> <LI>To read in data entered at the keyboard by the user use the <TT>READ</TT> statement. <LISTING> READ *, UNTCST </LISTING> <LI>You always read in a <EM>variable</EM> you are specifying where the value is to be stored. You <STRONG>never</STRONG> read in expressions or literal values. <LI>The user must press the <STRONG>ENTER</STRONG> or <STRONG>RETURN</STRONG> key when he/she has finished entering in a line of data at the keyboard. <BLOCKQUOTE> 27<STRONG>ENTER</STRONG> </BLOCKQUOTE> <LI>To read in multiple values entered on the same line specify several variable separate by commas. <LISTING> READ *, DAY, MONTH, YEAR </LISTING> <LI>If the user enters multiple values on the same line he/she does <STRONG>not</STRONG> separate them by commas but uses spaces instead. <BLOCKQUOTE> 11 20 67<STRONG>ENTER</STRONG> </BLOCKQUOTE> <LI>The values the user types in must be entered in the same order and be the same type as the variables in the <TT>READ</TT> statement. </UL> <H4>Reading CHARACTER Strings</H4> <UL> <LI>When reading in <TT>CHARACTER</TT> strings from the keyboard the user must surround them by apostrophes. <BLOCKQUOTE> 'Gareth Bestor'<STRONG>ENTER</STRONG> </BLOCKQUOTE> <LI>This may be undesirable and it can be avoided by using <EM>formatted</EM> input which we will discuss later (see Program #0 for an example). </UL> <H4>Types of Errors</H4> <UL> <LI>Syntax Error: compile time problem. Computer has no idea how totranslate part of your program. Misspelling words (starting the first linewith PROBLAM instead of PROGRAM, for example), using keywords (such as REAL)as variable name, nonsense characters on lines are common examples.<p> <LI>Semantic Error: compile time problem. Lines can be translatedindividually, but it doesn't make sense with regard to the entire program. Forexample, typing IMPLICIT NONE and then trying to use a variable name notdelcared yet.<p> <LI>Run-Time Error: run-time problem. Everything translates fine, but whenrunning there are certain steps the computer itself cannot do. Saying Y = Z/0,for example, is a legal statement but the computer cannot divide by 0. It willcompile, but fail running when the program reached this point.<p> <LI>Line-position Error: can be any type of error, or none. Occurs whenline rules not obeyed. Can result in many things; example, if we had<BLOCKQUOTE>READ *,......<EM>ad nauseum</EM>,B,C</BLOCKQUOTE>and the B was on the 72nd column, rest of the line would be ignored, andyou'd still have a legal statement (not reading in C may cause a problem laterin the program, though). If the B was on the 71st column, however, then onlythe C would be ignored, and you'd have a syntax error (READ cannot end with acomma). <p> <LI>Logical Error: occurs at run-time (sort of). Nothing wrong with theprogram, but it's not doing what you wanted. The computer is doing just whatit's told, but you've written the program incorrectly. The hardest and mostannoying to correct (and why it's important to have the algorithm correct inyour head and on paper before you start typing it in) </UL></DL></BODY><HR><ADDRESS><H5>Copyright © 1996 Modified from <!WA2><!WA2><!WA2><A HREF="http://www.cs.wisc.edu/~bestor/bestor.html">Gareth S. Bestor</A> (<!WA3><!WA3><!WA3><A HREF="mailto:bestor@cs.wisc.edu">bestor@cs.wisc.edu</A>). Last modified September 16, 1996.</H5></ADDRESS><HR></HTML>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?