1 Data Structure – C++ Programming Data Structure C++ Programming Dr Patrick Chan School of Computer Science and Engineering South China University of Technology 2 Data Structure – C++ Programming First Program in C++ Start Microsoft Visual Studio File > Win32 Application (Project) Input the project name File > C++ Source Input the file name Type the program Build > Run #include <iostream> using namespace std; int main() { cout<<"My name is XXX \n"; cout<< "My id is XXX \n"; return 0; } #include <iostream> using namespace std; int main() { cout<<"My name is XXX \n"; cout<< "My id is XXX \n"; return 0; } Use the library Main problem
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
1Data Structure – C++ Programming
Data Structure
C++ Programming
Dr Patrick Chan
School of Computer Science and Engineering
South China University of Technology
2Data Structure – C++ Programming
First Program in C++
� Start Microsoft Visual Studio
� File > Win32 Application (Project)
� Input the project name
� File > C++ Source
� Input the file name
� Type the program
� Build > Run
#include <iostream>
using namespace std;
int main()
{
cout<<"My name is XXX \n";
cout<< "My id is XXX \n";
return 0;
}
#include <iostream>
using namespace std;
int main()
{
cout<<"My name is XXX \n";
cout<< "My id is XXX \n";
return 0;
}
Use the library
Main problem
3Data Structure – C++ Programming
Declare a variable
� Declare a variable
� Assign a value
� Declare and assign a value at the same time
int age;int age;
age = 18;age = 18;
float age = 18;float age = 18;
4Data Structure – C++ Programming
Correct Variable Name
� Determine if the following identifiers are valid or not.
� _option □ valid □ invalid
� salesAmount □ valid □ invalid
� amount □ valid □ invalid
� $salesAmount □ valid □ invalid
� sales_amount □ valid □ invalid
� A3 □ valid □ invalid
� tax-rate □ valid □ invalid
� float □ valid □ invalid
� Case Sensitive!!
√
√
√
√
√
√
√
√
5Data Structure – C++ Programming
Input and Output
� Output
� Input
cin>>age;cin>>age;
cout<< "I am "<<"Peter.";cout<< "I am "<<"Peter.";
iostream
6Data Structure – C++ Programming
Operators
� int a = 2;
� int b = 5;
� cout << a + b;
� cout << a - b;
� cout << a * b;
� cout << a / b;
� cout << a % b;
� a += b;
� a -= b;
� a *= b;
� a /= b;
� a %= b;
7Data Structure – C++ Programming
Operators
� a++, ++a, a--, --a
� a=10;
b = ++a*5;
� a=10;
c = a++*5;
8Data Structure – C++ Programming
Exercise
� Write a program which accepts the number of
seconds and converts it into corresponding minutes and seconds.
Input the number of seconds: 1232 minutes 3 seconds
Hint: Use the operators / and %.Hint: Use the operators / and %.
9Data Structure – C++ Programming
Exercise - Answer
#include <iostream>
using namespce std;
int main()
{
return 0;
}
#include <iostream>
using namespce std;
int main()
{
return 0;
}
10Data Structure – C++ Programming
Condition
� int a = 1;
� int b = 2;
� a == 1
� a >= b
� b != a
� (a>b) || (a==1)
� (a<b) && (b==1)
� (a==1) || (a<b) && (b==1)
� (a==1) || ( (a<b) && (b==1) )
� !(a==1) || ( (a<b) && (b==1) )
True
False
True
True
False
False
True
False
11Data Structure – C++ Programming
if statement
� if (condition)
one statement
� if (condition)
one statement
else
one statement
� if (condition)
{
statements
}
else
{
statements
}
12Data Structure – C++ Programming
Nested if statements
� if (condition1)if (condition2)
one statement
� if (condition1)&&(condition2)one statement
� if (condition1)one statement
elseif (condition2)
one statement
13Data Structure – C++ Programming
Exercise
� If x=7 and y=9, what will be printed on the screen
after executing the following code?
if (x==1)
if (y==2)
cout<<x;
else
cout<<y;
cout<<(x+y);
if (x==1)
if (y==2)
cout<<x;
else
cout<<y;
cout<<(x+y);
if (x==1)
{
if (y==2)
cout<<x;
else
cout<<y;
}
cout<<(x+y);
if (x==1)
{
if (y==2)
cout<<x;
else
cout<<y;
}
cout<<(x+y);
14Data Structure – C++ Programming
Exercise
� If originally x=0, y=2, and z=1, what will be printed
on the screen by executing the following program fragment?
if (x <= 0 && y < z)
cout<<"x is not positive\n";
cout<<"y and z\n";
if (z == 1)
cout<<"z is 1\n";
if (x <= 0 && y < z)
cout<<"x is not positive\n";
cout<<"y and z\n";
if (z == 1)
cout<<"z is 1\n";
15Data Structure – C++ Programming
switch – case statement
� switch (variable) {
case value:
statements
break;
case value:
statements
break;
…
default:
statements
}
� switch (examGrade) {
case "A":
cout << "good";
break;
case "B":
cout << "ok";
break;
default:
cout << "bad";
}
16Data Structure – C++ Programming
Exercise
� For each different value of n as shown
on the right, what will be printed by the program fragment below?
switch (n) {
case 1: cout << "abc";
break;
case 2: cout << "def";
case 3: cout << "jkl";
break;
case 4: cout << "mno";
default: cout << "hello";
}
switch (n) {
case 1: cout << "abc";
break;
case 2: cout << "def";
case 3: cout << "jkl";
break;
case 4: cout << "mno";
default: cout << "hello";
}
� n = 1
� n = 2
� n = 3
� n = 4
� n = 0
17Data Structure – C++ Programming
Exercise
� Write a program that calculates the parking charge based on the following rates: