Você está na página 1de 21

ECOi2202 - Lógica de

Programação
Prof. Walter Aoiama Nagai
walternagai@unifei.edu.br
mas antes, abra o Replit...
Tipos primitivos
● char #include <iostream>
using namespace std;
● int
● float int main() {
char letra = 97;
● double cout << letra << endl;
● bool letra = 'b';
cout << letra << endl;

Modificadores return 0;
}
● short, long, unsigned,
signed
Tipos primitivos
● char #include <iostream>
using namespace std;
● int
● float int main() {
int x = 3, y = 5;
● double
● bool cout << x + y << endl;

return 0;
Modificadores }

● short, long, unsigned,


signed
Tipos primitivos
● char #include <iostream>
using namespace std;
● int
● float int main() {
float floatA = 3.14159;
● double float floatB = 12.3456;
● bool
cout << floatA << endl;
cout << floatB << endl;
Modificadores
return 0;
● short, long, unsigned, }
signed
Tipos primitivos
● char #include <iostream>
using namespace std;
● int
● float int main() {
double doubleA = 3.14159;
● double double doubleB = 12.3456;
● bool
cout << doubleA << endl;
cout << doubleB << endl;
Modificadores
return 0;
● short, long, unsigned, }
signed
Espaço de memória >> Tipos primitivos

IEEE Padrão de Ponto Flutuante 754


Tipos primitivos
● char #include <iostream>
using namespace std;
● int
● float int main() {
bool chove = false;
● double cout << chove << endl;
● bool chove = true;
cout << chove << endl;
chove = -1;
Modificadores cout << chove << endl;

● short, long, unsigned, return 0;


signed }
Conversão de tipos
● Estreitamento
○ float → int
● Alargamento
○ int → float
● Misto
○ Coerção → conversão implícita
○ Cast → conversão explícita
Conversão de tipos >> Estreitamento
#include <iostream>
using namespace std;

int main() {
int x;
float y;

y = 3.14159;
x = y;

cout << y << endl;


cout << x << endl;

return 0;
}
Conversão de tipos >> Alargamento
#include <iostream>
using namespace std;

int main() {
int x;
float y;

x = 3;
y = x + 0.14159;

cout << x << endl;


cout << y << endl;

return 0;
}
Conversão de tipos >> Cast
#include <iostream>
using namespace std;

typedef enum{sunday=1, monday, tuesday, wednesday, thursday, friday, saturday}


WeekDay;

int main() {
WeekDay weekday;

weekday = thursday;
cout << weekday << endl;
weekday = (WeekDay) 1;
cout << weekday << endl;
return 0;
}
Criando seu próprio tipo de dados
#include <iostream>
using namespace std;

typedef unsigned long ulong;


typedef enum{FALSO, VERDADEIRO} meubool;

int main() {
ulong inteiroLongo = 2415677;
meubool mVariavel = FALSO;

cout << mVariavel << endl;


cout << inteiroLongo << endl;

return 0;
}
Criando seu próprio tipo de dados
#include <iostream>
using namespace std;

typedef enum{sunday, monday, tuesday, wednesday, thursday, friday, saturday}


WeekDay;

int main() {
WeekDay weekday;

weekday = thursday;
cout << weekday << endl;
weekday = monday;
cout << weekday << endl;
return 0;
}
Tipo string
#include <iostream>
using namespace std;

int main() {
string nome, mensagemcurta;

cin >> nome;


cin >> mensagemcurta;

cout << nome << ", " << mensagemcurta << endl;

return 0;
}
Formatando a saída do cout...
Comando setw
#include <iostream>
#include <iomanip>
using namespace std;

int main() {
// formata o número com espaços em branco
// e alinhados a direita
cout << setw(10) << 123 << endl;
cout << setw(10) << 123456<< endl;
cout << setw(10) << 123456789 << endl;

return 0;
}
Comando setfill
#include <iostream>
#include <iomanip>
using namespace std;

int main() {
// troca os espaços em branco por um caracter

cout << setfill('x') << setw(10) << 123 << endl;


cout << setfill('x') << setw(10) << 123456<< endl;
cout << setfill('x') << setw(10) << 123456789 << endl;

return 0;
}
Comando setprecision
#include <iostream> // std::cout, std::fixed
#include <iomanip> // std::setprecision
#include <cmath>
using namespace std;

int main () {
double f = M_PI;
// precisão considerando o ponto decimal
cout << setprecision(5) << f << '\n';
cout << setprecision(9) << f << '\n';
// precisão desconsiderando o ponto decimal
cout << fixed;
cout << setprecision(5) << f << '\n';
cout << setprecision(9) << f << '\n';
return 0;
}
Comandos setfill, setw e setprecision
#include <iostream> // std::cout, std::fixed
#include <iomanip> // std::setprecision
#include <cmath>
using namespace std;

int main () {
double f = M_PI;

cout << setfill('0') << setw(12) << setprecision(5) << f << '\n';
cout << setfill('0') << setw(12) << setprecision(9) << f << '\n';
cout << fixed;
cout << setfill('0') << setw(12) << setprecision(5) << f << '\n';
cout << setfill('0') << setw(12) << setprecision(9) << f << '\n';

return 0;
}
Até a próxima aula!

Você também pode gostar