Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chopin’s Nocturne.

Similar presentations


Presentation on theme: "Chopin’s Nocturne."— Presentation transcript:

1 Chopin’s Nocturne

2 宋.王安石《元日》 爆竹聲中一歲除, 春風送暖入屠蘇; 千門萬戶曈曈日, 總把新桃換舊符。
屠蘇:一種草名,據說屠蘇酒是漢末華陀創制,將中藥材大黃、白朮、桂枝、防風、花椒、烏頭、附子入酒浸泡而來,具有益氣溫陽、袪風散寒、避除疫痢之邪的功效。 曈曈:《廣韻》曈曨,日欲明也。曈曈即曈曨。「曈曈日」,指 由暗轉明 的朝陽。王安石〈元日〉詩用「曈曈日」表現日出時光輝燦爛,普照大地,象徵 新年無限光明美好的前景。

3 Platforms we shall use First 1/3: clang++ on FreeBSD
Middle 1/3: Visual C++ Express (text-only) Last 1/3: Visual Studio (MFC) – Windows Programming

4 Remote Login Python C++ PHP JavaScript TELNET (STU) SSH (PuTTY)
JuiceSSH (STU) SSH (PuTTY)

5 Problems on 3/1 雞兔同籠 What day is January 1st?
Volume and surface area of a sphere P.19 c= a+b P.41 Chicken & Rabbit P.46 What day is January 1st P.52 Volume and Surface

6 Chapter 2 Data, Variables, and Calculations

7 Programming Languages
Low-level language movl -4(%rbp), %ecx addl -8(%rbp), %ecx movl %ecx, -12(%rbp) High-level language c = a + b This needs to be translated into machine language that the computer can execute. Compilers convert programs written in a high-level language into the machine language of some computer. Python Programming, 2/e

8 Figure 1.2: Compiling a high-level Language
Source Code (Program) Machine Code Compiler Inputs Running Program Outputs Python Programming, 2/e

9 Programming Languages
Interpreters simulate a computer that understands a high-level language. The source program is not translated into machine language all at once. An interpreter analyzes and executes the source code, instruction by instruction. Python Programming, 2/e

10 Figure 1.3: Interpreting a high-level language
Source Code (Program) Computer Running an Interpreter Outputs Inputs Python Programming, 2/e

11 Programming Languages
Compiling vs. Interpreting Once program is compiled, it can be executed over and over without the source code or compiler. If it is interpreted, the source code and interpreter are needed each time the program runs. Compiled programs generally run faster since the translation of the source code happens only once. We run Python programs with an “interpreter”; we run C++ programs with an “compiler”. Python Programming, 2/e

12 Syntax of C Language Comments begin with //
// Ex2_00.cpp // Simple calculation #include <iostream> int main() { int a, b, c; a = 10; b = 20; c = a + b; std::cout << "The summation a + b = "; std::cout << c; std::cout << std::endl; return 0; } Comments begin with // A statement block is enclosed by braces. Variable declaration Each statement ends with a semicolon. End-of-Line Whitespace (P.35)

13 Hands-On Each student logins to STU, edit a C++ program, compile and run it. // Simple calculation #include <iostream> int main() { int a, b, c; a = 10; b = 20; c = a + b; std::cout << "The summation a + b = " << c; std::cout << std::endl; return 0; }

14 main( ) Every standard C++ program contains the function main().
A Program in C++ consists of one or more functions. A function is simply a self-contained block of code with a unique name. You can invoke a function by its name. The principal advantage of having a program broken up into functions is that you can write and test each piece separately. Re-use

15 The IPO Structure of a Program
int main( ) { input( ); process( ); output( ); return 0; }

16 Naming Variables Variable names can include the letters A-Z, a-z, the digits 0-9, and the underscore character (_). Variable names cannot begin with digits. Avoid naming a variable name to begin with an underscore (_this, _that), because it may conflict with standard system variables. Variable names are case-sensitive. Convention in C++ Classes names begin with a capital letter. Variable names begin with a lowercase letter.

17 Naming Variables (2) In FreeBSD C++, variable names can be arbitrarily long. number_of_students average_score However, not all compilers support such long names. It’s a good idea to limit names to a maximum of 31 characters. Illegal variable names: 8_Ball, 7UP Hash!, Mary-Ann

18 Declaring Variables int value; int i, j, k; int value = 10;
Q: Why do we need to declare variables? A: To reserve memory spaces. Declaring Variables int value; This declares a variable with the name value that can store integers. int i, j, k; A single declaration can specify the names of several variables. However, it is better to declare each variable in a single line. (Why? See. P.39) int value = 10; When you declare a variable, you can also assign an initial value to it.

19 Bits and Bytes Bit Byte The smallest unit of storage
Power on/off Magnetic north/south A bit can store just 0 or 1. Too small to be useful. Byte One byte = a group of 8bits e.g., One byte can store one character, e.g., ‘A’, ‘k’, or ‘?’ One byte can also store an integer, e.g., 0, 15, or 64

20 Kilobyte vs. Kibibyte Value Metric IEC 1000 kB kilobyte 1024 KiB
10002 MB megabyte 10242 MiB mebibyte 10003 GB gigabyte 10243 GiB gibibyte 10004 TB terabyte 10244 TiB tebibyte 10005 PB petabyte 10245 PiB pebibyte 10006 EB exabyte 10246 EiB exbibyte 10007 ZB zettabyte 10247 ZiB zebibyte 10008 YB yottabyte 10248 YiB yobibyte

21 Integer Types & Character Types
int // 4 bytes short // 2 bytes long // 4 bytes, same as int in Visual C However, in a 64-bit host (e.g. STU.ipv6.club.tw), sizeof(long) is 8 bytes. So this may lead to expected results if you run the same program on different platforms. long long // 8 bytes Character Data Types char letter = 'A'; // 1 byte Single quote, not double quote (") The ASCII code of the character is stored in that byte. Therefore, it is equivalent to char letter = 65;

22 Integer Type Modifier Examples: signed int; // equivalent to int
signed short; // equivalent to short Range: ~ 32767 unsigned short; Range: 0 ~ 65535 signed char; Range: -128 ~ 127 unsigned char; Range: 0 ~ 255

23 Boolean Type Examples: In old C language, there is no bool data type.
bool testResult; bool colorIsRed = true; In old C language, there is no bool data type. Variables of type int were used to represent logical values. Zero for false; non-zero for true. Symbols TRUE and FALSE are defined for this purpose. Note that TRUE and FALSE are not C++ keywords. Don’t confuse true with TRUE.

24 Floating-Point Type A floating-point constant contains a decimal point, or an exponent, or both. 112.5 1.125E2 Examples: double inch_to_cm = 2.54; 8 bytes 1 bit sign 11 bit exponent 52 bit mantissa float pi = f; // P.44 4 bytes 8 bit exponent 23 bit mantissa

25 The const Modifier const float inch_to_cm = 2.54f;
If you accidentally wrote an incorrect statement which altered the value of inch_to_cm, the compiler will fail and complain. Avoid using magic numbers like 2.54 in your program when the meaning is not obvious. Declare a constant for it. All the above data types can have const modifiers. Constant Expressions const float foot_to_cm = 12 * inch_to_cm;

26 The Using Decleration // Ex2_00.cpp // Simple calculation #include <iostream> int main() { int a, b, c; a = 10; b = 20; c = a + b; std::cout << "a + b = "; std::cout << c; std::cout << std::endl; return 0; } // Ex2_00a.cpp // Simple calculation #include <iostream> using std::cout; using std::endl; int main() { int a, b, c; a = 10; b = 20; c = a + b; cout << "a + b = "; cout << c; cout << endl; return 0; }

27 Basic Input/Output Operations
Input from the keyboard cin >> num1 >> num2; Output to the command Line cout << num1 << num2; cout << num1 << ' ' << num2;

28 Exercise: In which year were you born?
Input: In which year were you born? Output: Your age. Relationship: age = year The program may run as follows: In which year were you born? 1998 You are 20 years old.

29 Exercise: Chickens and Rabbits

30 #include <iomanip>
#include <iostream> #include <iomanip> using std::cout; using std::endl; using std::setw; int main() { int num1 = 10; int num2 = 200; cout << num1 << num2 << endl; cout << num1 << ' ' << num2 << endl; cout << setw(6) << num1 << setw(6) << num2 << endl; cout << setw(6) << num2 << setw(6) << num1 << endl; return 0; } 10200 10 200

31 Escape Sequences An escape sequence starts with a backslash character, \. cout << endl << "This is a book."; cout << endl << "\tThis is a book."; Some useful escape sequences: \a alert with a beep \n newline \b backspace \t tab \' single quote \" double quote \\ backslash

32 Assignment Statement variable = expression ; Repeated assignment
c = a + b; q = 27 / 4; // the quotient is an integer r = 27 % 4; // remainder Repeated assignment a = b = 2; Modifying a variable d = a + b / c; // d = a + (b / c) count = count + 5; count += 5; // shorthand notation count *= 5; // count = count * 5 a /= b + c; // a = a / (b + c)

33 Exercise: What Day is January 1st

34 Increment Operators Frequently used in C++
The following statements have exactly the same effect: count = count + 1; count +=1; // shorthand ++count; // unary operator Prefix form: increment before the value is used. int total, count = 1; total = ++count + 6; // count=2; total = 8 Postfix form: increment after the value is used. total = count++ + 6; // total = 7; count=2 total = 6 + count++;

35 We may have a quiz like this
What would be the output of the following code: #include <iostream> using std::cout; using std::endl; int main() { int a = 10; int b = 20; cout << a b << endl; return 0; }

36 Decrement Operators Unary operator to decrease the integer variable by 1. total = --count + 6; total = 6 + count--; Both increment and decrement operators are useful in loops, as we shall see in Chapter 3.

37 Comma Operator Specify several expressions in an assignment
int num1; int num2; int num3; int num4; num4 = (num1=10, num2=20, num3=30); Operator Precedence (see P.61~P.62) It is a good idea to insert parentheses to make sure.

38 Casting The conversion of a value from one type to another
Implicit type conversion int n; float a = 7.0; float b = 2.0; float c = a / b; n = c; The floating-point value will be rounded down to the nearest integer (3) The compiler will issue a warning. Explicit cast n = static_cast<int> ( c ); The compiler assumes you know what you are doing and will not issue a warning. Old-style cast (not recommended) n = (int) c ;

39 Exercise: Volume & Area
Input: radius Output: The volume and surface area of a sphere. Relationship: V = 4/3 π r3 A = 4 π r2 Hint: You may define a constant pi =

40 HW: Solving Linear Equations

41 HW (cont.) Sample input Sample output 2.0 1.0 7.0 -1.0 2.0 9.0
Sample output x = 1 , y = 5 x = 4.5 , y = 0.5

42 Bitwise Operators The bitwise operators are useful in programming hardware devices. & AND | OR ^ exclusive OR ~ NOT >> shift right << shift left You may pack a set of on-off flags into a single variable.

43 Examples of Bitwise Operators
Bitwise AND char letter = 0x41; char mask = 0x0F; letter = letter & mask; Bitwise Shift Operators char j = 2;// j <<= 1; // j >>= 2; // j = -104; // j >>= 2; // (=?)

44 Storage Duration and Scope
Automatic storage duration Static storage duration Dynamic storage duration (Chapter 4) Scope The part of your program over which the variable name is valid.

45 Automatic Variables Automatic variables have local scope (block scope). Every time the block of statements containing a declaration for an automatic variable is executed, the variable is created anew. If you specified an initial value for the automatic variable, it will be reinitialized each time it is created. When an automatic variable dies, its memory on the stack will be freed for used by other automatic variables. anew 重新

46 Ex2_07.cpp in P.73 From the viewpoint of the outer block, the inner block just behaves like a single statement. The inner block also declares a variable named count1, so the variable count1 declared in the outer block becomes hidden now. Other variables (count3) declared at the beginning of the outer scope are accessible from within the inner scope. After the brace ending the inner scope, count2 and the inner count1 cease to exist. Try to uncomment the line // cout << count2 << endl; to get an error.

47 Global Variables Variables declared outside of all blocks are called global variables and have global namespace scope. Global variables have static storage duration by default. It will exist from the start of execution of the program, until execution of the program ends. If you do not specify an initial value for a global variable, it will be initialized with 0 by default. On the contrary, automatic variables will not be initialized by default.

48 Figure 2-11 (P.76) An example that the lifetime and scope may be different (value1).

49 Namespaces global namespace scope
Namespace is a mechanism to prevent accidental naming clash. The libraries supporting the CLR and Windows Forms use namespaces extensively. The ANSI C++ standard library does, too. Every non-local variable or function must be qualified. // Ex2_09a.cpp #include <iostream> int value = 0; int main() { std::cout << "enter an integer: "; std::cin >> value; std::cout << "\nYou enterd " << value << std::endl; return 0; } global namespace scope Note the absence of using declarations for cout and endl

50 using Directive using namespace std;
This imports all the names from the std namespace so that you don’t need to qualifying the name with prefix std:: in your program. However, this negates the reason for using a namespace. Only introduce the names that you use with “using declaration”: using std::cout; using std::endl;

51 Declaring a Namespace // Ex2_09.cpp // Declaring a namespace
#include <iostream> namespace myStuff { int value = 0; } int main() int value = 925; std::cout << "enter an integer: "; std::cin >> myStuff::value; std::cout << "\nYou entered " << myStuff::value << std::endl; std::cout << "The local variable value = " << value << std::endl; return 0;

52 using Directive // Ex2_10.cpp // using a using directive
#include <iostream> namespace myStuff { int value = 0; } using namespace myStuff; int main() std::cout << "enter an integer: "; std::cin >> value; // myStuff::value std::cout << "\nYou entered " << value << std::endl; return 0;

53 using Declaration // Ex2_10a.cpp // using a using declaration
#include <iostream> namespace myStuff { int value = 0; } using myStuff::value; // only import variables that you need int main() std::cout << "enter an integer: "; std::cin >> value; std::cout << "\nYou entered " << value << std::endl; return 0;

54 Exercise P.87. Exercise 1, 2, 3, 4, 5. You don’t need to upload, but we shall have a quiz later. Also try to run the sample code introduced in this chapter, to get a feeling about the syntax of C++ language.


Download ppt "Chopin’s Nocturne."

Similar presentations


Ads by Google