第二课程序输入和输出

上传人:M****1 文档编号:497859117 上传时间:2022-09-10 格式:DOC 页数:7 大小:70KB
返回 下载 相关 举报
第二课程序输入和输出_第1页
第1页 / 共7页
第二课程序输入和输出_第2页
第2页 / 共7页
第二课程序输入和输出_第3页
第3页 / 共7页
第二课程序输入和输出_第4页
第4页 / 共7页
第二课程序输入和输出_第5页
第5页 / 共7页
点击查看更多>>
资源描述

《第二课程序输入和输出》由会员分享,可在线阅读,更多相关《第二课程序输入和输出(7页珍藏版)》请在金锄头文库上搜索。

1、PASCAL Ch2. Program Input and Output 程序輸入和輸出PASCALChapter 2: Program Input and Output第二課:程序輸入和輸出In chapter 1, we have learn some data types:我們在第一課已學會了幾種數據類型:Data Type數據類型Description描述Example例子1integer 整數zero, positive and negative number零、正數及負數-12340522real number 實數decimal and exponential value小數及指

2、數記數的數值0.0-23.33E23character 字符symbol, digit, letter and punctuation符號、數字、字母及標點x2#. (. :Space character)4string 字串a series of characters一連串的字符How are you?-12.123d2.1 ( writeln ) Statement 語句The format of the statement is as follow:writeln( Output List輸出表 );Output list can be constant, variable or exp

3、ressions輸出表可以是常量、變量或表式Output List輸出表Display format顯示格式Example例子Output result輸出結果I.integer整數same as in the statement如同在語句格式一般program PrintInt;begin writeln(0); writeln(123); writeln(-9999);end.0123-9999II.realnumber實數in the exponential form以指數型式表示program PrintReal;begin writeln(0.9); writeln(9.9); wr

4、iteln(-999.9);end.9.00000E-01.9.90000E+00-9.99900E+02Output List輸出表Display format顯示格式Example例子Output result輸出結果III.character字符same as in the statement, but it within a( single quotation mark ).program PrintChar;begin writeln(a); writeln(#, &); writeln(1, , A);end.a#&1AIV.string字串如同在語句格式一般,但必須放在兩個( 單

5、 引 號 )之間。program PrintString;begin writeln(Hello); writeln(I am fine!); writeln( Bye );end.HelloI am fine!.ByeV.value ofvariable變量的數值according to the( data type ) of variable根據變量的( 數 據 類 型 )program PrintVar;var A : integer; B : real; C : string;begin a := 2000; b := 3.1415; c := CNEC LWS; writeln(A)

6、; writeln(B); writeln(C)end.2000 3.1415000E+00CNEC LWSVI.expression表式result of the expression表式的結果program PrintE;var a, b, sum : integer;begin a := 3; b := 4; sum := a+b; writeln(a, +, b, =, sum); writeln(a-b); writeln(25-12); writeln(1.5*15.0)end. a+b=7-113 2.25000E+01VII.blank line空白行omit the pare

7、nthesis and output list省去括弧和輸出表program PrintBlankLine;begin writeln(Blank line); writeln; writeln(); writeln(The end.);end.Blank line (Line feed 換行)The end.Note:The symbol := means get the value of in PASCAL.注意:符號:= 的意思是取數值。2.2 ( write ) Statement 語句The format of the statement is as follow:write( Ou

8、tput List輸出表 );The write statement is similar to the statement writeln except the cursor does not go to the next line after displaying the output. (: line feed)write的語句與writeln很相似,唯一分別是當輸出顯示後,指標不會跳到另一行。( :換行)Example 例子Output result輸出結果Program UseWrite;begin writeln( 123 ); write( A ); write( a ); re

9、adln() end. 123Aa2.2 Formatting Output 輸出的格式制定Type 類型General Form 標準模式Display format 顯示格式I.Integer value整數數值writeln( x:f );x:integer整數f: field width欄寬If field width is larger than the number of digits of the integer, it will display as right-justified and fill with leading blanks.如果欄寬大過整數的數位,該整數會向右對

10、齊並在前填上空格。Example例子:Output result 顯示結果:program FormatInt1;begin writeln(1234:5);.1234 writeln(1234:6);.1234 writeln(-1234:6);.-1234 writeln(-1234:7);.-1234 readln()end.If field width is less than the number of digits of the integer, it will display as same as the integer value.如果欄寬小過整數的數位,該整數如常顯示。Exa

11、mple例子:Output result 顯示結果:program FormatInt2;begin writeln(1234:1);1234 writeln(1234:2);1234 writeln(-1234:3); -1234 writeln(-1234:4); -1234 readln()end.Type 類型General Form 標準模式Display format 顯示格式II.Real value實數數值There are 2 ways to format real number:實數有兩種輸格式:writeln( r:f ); orwriteln( r:f:d );r:re

12、al value實數f:field width欄寬d:decimal place小數點後位If only field width is specified, the real number will be displayed in ( exponential notation ). If field width is smaller than the number of digit, it will display ONE decimal places.如果只有制定欄寬,其實數會以( 指 數 記 數 法 )顯示。如果欄寬小過實數的數位,便會顯示一個小數位。Example例子:Output re

13、sult 顯示結果:program FormReal1;begin writeln(13.4:3);.1.3E+0001 writeln(13.4:10);.1.3E+0001 writeln(13.4:11);.1.34E+0001 writeln; writeln(-13.4:3);-1.3E+0001 writeln(-13.4:10);-1.3E+0001 writeln(-13.4:12); .-1.34E+0001 writeln; writeln(13.5:10);.1.4E+0001 writeln(-13.5:10);-1.4E+0001 readln()end.If both field width and decimal places are specified, the real number will be displaced in decimal format even if the field width is smaller than the digit of real number.如果欄寬及小數位後點也制定,就算欄寬不足,該實數之小數位也會顯示。Examp

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 建筑/环境 > 施工组织

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号