Revision
Muhammad Magdi
Remember
- We must declare a variable and give it a name before using it.
- cin is used to get input from user into a variable.
- cout is used to print something on the screen.
- every variable in C++ has a Data Type.
- Data types in C++ are:
Data Type | Description |
---|---|
int | Small integer number |
double | Large real number |
long long | Large integer number |
string | A sequence of characters (word) |
char | A single character |
bool | Boolean number (0 or 1) |
Remember
Sometimes we need to execute statements depending on particular conditions, so we use if conditions:
if ( CONDITION#1 ) {
TRUE-STATEMENTS#1
} else if ( CONDITION#2 ) {
TRUE-STATEMENTS#2
} else if ( CONDITION#3 ) {
TRUE-STATEMENTS#3
} ... and so on
... else {
FALSE-STATEMENTS
}
Remember
Arithmetic (in Calculations) Operators in C++ are:
Operator | Meaning |
---|---|
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Integer Division |
% | Remainder after Division (Mod) |
Relational (in Conditions) Operators in C++ are:
Operator | Meaning |
---|---|
== | equal to |
!= |
not equal to |
< | less than |
> | greater than |
<= | less than or equal |
>= | greater that or equal |
Logical (in Conditions) Operators in C++ are:
Operator | Meaning |
---|---|
&& | AND |
|| | OR |
! | NOT |
Calculator Program
#include <iostream>
using namespace std;
int main(){
int a, b;
char op;
cin >> a >> op >> b;
if (op == '+') {
cout << a + b << endl;
} else if (op == '-') {
cout << a - b << endl;
} else if (op == '*') {
cout << a * b << endl;
} else if (op == '/') {
cout << a / b << endl;
}
return 0;
}
URI 1046 - Game Time
[Problem Summary]
- There is game that started at time start and ended at time end
- Write a program that takes start and end .
- Calculate and Print the Duration of the game in the form:
"O JOGO DUROU Duration HORA(S)" - Follow this message with an endl
URI 1046 - Game Time
[Solution]
#include <iostream>
using namespace std;
int main(){
int start, end;
cin >> start >> end;
if (start < end) {
cout << "O JOGO DUROU " << end - start << " HORA(S)" << endl;
} else {
cout << "O JOGO DUROU " << 24 - start + end << " HORA(S)" << endl;
}
return 0;
}
URI 1046 - Game Time
[Discussion]
There are two cases:
Case 1:
When the game starts and ends within the same day.
In this case start will be less than end.
So the Duration will be the difference end - start.
Case 2:
When the game starts in a day and ends in the following one.
In this case start will be greater than end, as it started late (with large value) in day1 and ended early (with small value) in day2.
So the Duration here is consisted of 2 parts:
Day1 Part = 24 - start
Day2 Part = end
Duration = 24 - start + end
Thanks
Content
- Remember
- Calculator Program
- URI 1046 - Game Time
Revision 1
By Muhammad Magdi
Revision 1
- 233