390 likes | 490 Views
C++ Programming Practice Time. Week 2. Practice : Create Project. Run Microsoft Visual Studio. Practice : Create Project. Practice : Create Project. Practice : Create Project. Practice : Create Project. Practice : Create Project. Practice : Create Project. Practice : Create Project.
E N D
C++ Programming Practice Time Week 2.
Practice : Create Project • Run Microsoft Visual Studio
Practice : Preprocessing • #include • #define • #define with parameter
Practice : Preprocessing #include <stdio.h> #define _PI 3.141592 #define SUM(_a, _b) (_a + _b) void main() { printf(“%d\n”, SUM(_PI, 5)); }
Practice : Preprocessing • #include • include pre-defined source • #define • define literal as other literal • #define with parameter • define literal like a function
Practice : Variable • int • char • float • naming rule
Practice : Variable #include <stdio.h> void main() { int Int_Value = 5; char Ch_Zero = ‘0’; char pCh_Test[4] = “val”; float _F_Value0 = 2.f; int &rInt_Test = (*(int*)pCh_Test); }
Practice : Variable • int • integer value : natural, zero, natural with negative • char • character value : ‘a’, ‘b’, ‘1’, ‘*’ … • float • number with floating point : 2.3f, 5.232f, …. • naming rule • alphabet(a~z, A~Z), underline(_), number(0~9) • not allowed start with number
Practice : Literal • integer • character • string • number with floating point
Practice : Literal #include <stdio.h> void main() { int Int_Value = 5; char Ch_Zero = ‘0’; char pCh_Test[4] = “val”; float _F_Value0 = 2.f; int &rInt_Test = (*(int*)pCh_Test); }
Practice : Expression • Math Expression • Comparison Expression • Condition Expression
Practice : Math Expression • Mathematical Operators • Associativity • Precedence • C++ Math Operator Rules
Practice : Mathematical Operators #include <stdio.h> #define _PI 3.141592 void main() { float F_Radius = 2.5f; float F_Area = _PI * F_Radius * F_Radius; float F_Circum = 2.f * _PI * F_Radius; printf(“Area : %f\n”, F_Area); printf(“Circumference : %d\n”, F_Circum); }
Practice : Math Expression • Mathematical Operators • + - * / % • Associativity • Precedence • C++ Math Operator Rules
Practice : Associativity #include <stdio.h> void main() { printf(“%f\n”, 2.f + (3.f + 1.f)); printf(“%f\n”, 2.f + 3.f + 1.f); printf(“%f\n”, (2.f + 3.f) + 1.f); }
Practice : Math Expression • Mathematical Operators • + - * / % • Associativity • (A + B) + C = A + (B + C) • (KOR : 결합법칙) • Precedence • C++ Math Operator Rules
Practice : Precedence #include <stdio.h> void main() { printf(“%f\n”, 2.f * (3.f + 1.f)); printf(“%f\n”, 2.f * 3.f + 1.f); printf(“%f\n”, (2.f * 3.f) + 1.f); }
Practice : Math Expression • Mathematical Operators • + - * / % • Associativity • (A + B) + C = A + (B + C) [KOR : 결합법칙] • Precedence • Order of caculation [KOR : 우선순위] • C++ Math Operator Rules • Check Prof’s Material
C++ Programming C++ Math Operator Rules Operator Associativity Precedence () left to right high * / % left to right middle + - left to right low • Now - what is the value of this?: 2 / 3 / 4 + 5 • How about this: (7*3/4-2)*5
Practice : Expression • Math Expression • Comparison Expression • Condition Expression
Practice : Comparison Expression • Relational and Equality Operators • Equal vs Assign • Precedence
Practice : Relational and Equality Operators #include <stdio.h> void main() { printf(“%d\n”, 1 < 2); printf(“%d\n”, 1 <= 2); printf(“%d\n”, 1 > 2); printf(“%d\n”, 1 >= 2); printf(“%d\n”, 1 == 2); printf(“%d\n”, 1 != 2); }
Practice : Comparison Expression • Relational and Equality Operators • < <= > >= == != • Equal vs Assign • Precedence
Practice : Equal vs Assign #include <stdio.h> void main() { int Int_Value = 2; printf(“%d\n”, (bool)(Int_Value == 3)); printf(“%d\n”, (bool)(Int_Value = 3)); }
Practice : Comparison Expression • Relational and Equality Operators • < <= > >= == != • Equal vs Assign • Equal => Comparison A & B is same • Assign 1) Assign value to Variable 2) Check Variable as bool (false : zero, true : others) • Precedence • Check Prof’s Material
C++ Programming Precedence Operators Precedence () highest (applied first) * / % + - < <= > >= == != = lowest (applied last)
Practice : Expression • Math Expression • Comparison Expression • Condition Expression
Practice : Condition Expression • Condition with Comparison • Complex Conditions(&& || !) • Precedence