Created
April 9, 2012 02:00
-
-
Save HudsonAfonso/2340805 to your computer and use it in GitHub Desktop.
Estruturas condicionais parte 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*! @file e22.cpp | |
@brief Exercício 22 - Estruturas condicionais | |
@author Hudson Afonso | |
@version 1.0.0-alpha - http://semver.org | |
@note Enviar para alexandrebrrib@gmail.com | |
@link https://gist.github.com/2340805#file_e22.cpp | |
Escrever um algoritmo que lê um conjunto de 4 valores i, a, b, c, onde i é um | |
valor inteiro e positivo e a, b, c, são quaisquer valores reais e os escreva. | |
A seguir: \n | |
(a) Se i=1 escrever os três valores a, b, c em ordem crescente. \n | |
(b) Se i=2 escrever os três valores a, b, c em ordem decrescente. \n | |
(c) Se i=3 escrever os três valores a, b, c de forma que o maior entre a, b, | |
c fique entre os dois. | |
*/ | |
#include <iostream> | |
using namespace std; | |
int i; //!< @brief Opção. | |
float num1; //!< @brief Número 1. | |
float num2; //!< @brief Número 2. | |
float num3; //!< @brief Número 3. | |
/*! @brief Exch troca os valores de duas variáveis. | |
@param &i Valor Inteiro a ser trocado. | |
@param &j Valor Inteiro a ser trocado. | |
@return void | |
*/ | |
void exch(float &i, float &j) | |
{ | |
int t = i; | |
i = j; | |
j = t; | |
} | |
int main() | |
{ | |
cout << "Entre com o i: "; | |
cin >> i; | |
cout << "Entre com o número 1: "; | |
cin >> num1; | |
cout << "Entre com o número 2: "; | |
cin >> num2; | |
cout << "Entre com o número 3: "; | |
cin >> num3; | |
if (num1 > num2) | |
{ | |
exch(num2, num1); | |
} | |
if (num1 > num3) | |
{ | |
exch(num3, num1); | |
} | |
if (num2 > num3) | |
{ | |
exch(num3, num2); | |
} | |
if (i == 1) | |
{ | |
cout << num1 << " " << num2 << " " << num3; | |
} | |
else | |
{ | |
if (i == 2) | |
{ | |
cout << num3 << " " << num2 << " " << num1; | |
} | |
else | |
{ | |
if (i == 3) | |
{ | |
cout << num1 << " " << num3 << " " << num2; | |
} | |
} | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*! @file e24.cpp | |
@brief Exercício 24 - Estruturas condicionais | |
@author Hudson Afonso | |
@version 1.0.0-alpha - http://semver.org | |
@note Enviar para alexandrebrrib@gmail.com | |
@link https://gist.github.com/2340805#file_e24.cpp | |
Desenvolver um algoritmo para calcular e imprimir o preço final de um carro. | |
O valor do preço inicial de fábrica é fornecido por um meio de entrada. O | |
carro pode ter as seguintes opções: \n | |
(a) Ar condicionado: R$ 1750,00 \n | |
(b) Pintura Metálica: R$ 800,00 \n | |
(c) Vidro Elétrico: R$ 1200,00 \n | |
(d) Direção Hidráulica: R$ 2000,00. | |
*/ | |
#include <iostream> | |
#include <math.h> | |
using namespace std; | |
float preco; //!< @brief Preço do carro. | |
string ar; //!< @brief Ar condicionado. | |
string pintura; //!< @brief Pintura Metálica. | |
string vidro; //!< @brief Vidro Elétrico. | |
string direcao; //!< @brief Direção Hidráulica. | |
int main() | |
{ | |
//Configura o cout para fixar em 2 casas decimais quando for float | |
cout.setf(ios::fixed); | |
cout.precision(2); | |
cout << "Entre com o preço do Carro: "; | |
cin >> preco; | |
cout << "Deseja Ar Condicionado? [Sim/Não] "; | |
cin >> ar; | |
cout << "Deseja Pintura Metálica? [Sim/Não] "; | |
cin.ignore(); | |
cin >> pintura; | |
cout << "Deseja Vidro Elétrico? [Sim/Não] "; | |
cin.ignore(); | |
cin >> vidro; | |
cout << "Deseja Direção Hidráulica? [Sim/Não] "; | |
cin.ignore(); | |
cin >> pintura; | |
if (ar.compare("Sim") == 0) | |
{ | |
preco += 1750; | |
} | |
if (pintura.compare("Sim") == 0) | |
{ | |
preco += 800; | |
} | |
if (vidro.compare("Sim") == 0) | |
{ | |
preco += 1200; | |
} | |
if (direcao.compare("Sim") == 0) | |
{ | |
preco += 2000; | |
} | |
cout << endl << "Valor total do Carro é R$ " << preco << endl; | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*! @file e26.cpp | |
@brief Exercício 26 - Estruturas condicionais | |
@author Hudson Afonso | |
@version 1.0.0-alpha - http://semver.org | |
@note Enviar para alexandrebrrib@gmail.com | |
@link https://gist.github.com/2340805#file_e26.cpp | |
Números palíndromos são aqueles que escritos da direira para esquerda ou da | |
esquerda para direita tem o mesmo valor. Ex. 929, 44, 97379. Fazer um | |
algoritmo que dado um número de 5 dígitos; calcule e escreva se este número é | |
palíndromo. | |
*/ | |
#include <iostream> | |
using namespace std; | |
int num; //!< @brief Número de 5 dígitos. | |
int num2; //!< @brief Número de 2 dígitos. | |
int num3; //!< @brief Número de 2 dígitos. | |
int main() | |
{ | |
cout << "Entre com o número de 5 dígitos: "; | |
cin >> num; | |
num2 = num / 1000; | |
num3 = (num % 10) * 10 + (num % 100) / 10; | |
if (num2 == num3) | |
{ | |
cout << "O número " << num << " é palíndromo." << endl; | |
} | |
else | |
{ | |
cout << "O número " << num << " não é palíndromo" << endl; | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*! @file e28.cpp | |
@brief Exercício 28 - Estruturas condicionais | |
@author Hudson Afonso | |
@version 1.0.0-alpha - http://semver.org | |
@note Enviar para alexandrebrrib@gmail.com | |
@link https://gist.github.com/2340805#file_e28.cpp | |
O número 3025 possui a seguinte característica: \n | |
30+25 =55 \n | |
55^2 = 3025 \n | |
Fazer um algoritmo que dado um número de 4 dígitos calcule e escreva se este | |
número possui ou não tal característica. | |
*/ | |
#include <iostream> | |
#include <math.h> | |
using namespace std; | |
int num; //!< @brief Número de 4 dígitos. | |
int num2; //!< @brief Número de 2 dígitos. | |
int num3; //!< @brief Número de 2 dígitos. | |
int soma; //!< @brief Soma das partes. | |
int raiz; //!< @brief Raiz quadrada do Número. | |
int main() | |
{ | |
cout << "Entre com o número de 4 dígitos: "; | |
cin >> num; | |
num2 = num / 100; | |
num3 = num % 100; | |
if (sqrt(num) == num2 + num3) | |
{ | |
cout << "O número " << num << " possui a característica" << endl; | |
} | |
else | |
{ | |
cout << "O número " << num << " não possui a característica" << endl; | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*! @file e30.cpp | |
@brief Exercício 30 - Estruturas condicionais | |
@author Hudson Afonso | |
@version 1.0.0-alpha - http://semver.org | |
@note Enviar para alexandrebrrib@gmail.com | |
@link https://gist.github.com/2340805#file_e30.cpp | |
Um vendedor necessita de um algoritmo que calcule o preço total devido por um | |
cliente. O algoritmo deve receber o código de um produto e a quantidade | |
comprada e calcular o preço total, usando a tabela abaixo: \n | |
Código do Produto Preço Unitário \n | |
1001 R$ 5,32 \n | |
1324 R$ 6,45 \n | |
6548 R$ 2,37 \n | |
0987 R$ 5,32 \n | |
7623 R$ 6,45 \n | |
*/ | |
#include <iostream> | |
using namespace std; | |
int id_produto; //!< @brief Código do Produto. | |
int qtd; //!< @brief Quantidade. | |
float total; //!< @brief Valor Total. | |
int main() | |
{ | |
cout << "Entre com o código do produto: "; | |
cin >> id_produto; | |
cout << "Entre com a quantidade: "; | |
cin >> qtd; | |
switch (id_produto) | |
{ | |
case 1001: | |
total = qtd * 5.32; | |
break; | |
case 1324: | |
total = qtd * 6.45; | |
break; | |
case 6548: | |
total = qtd * 2.37; | |
break; | |
case 987: | |
total = qtd * 5.32; | |
break; | |
case 7623: | |
total = qtd * 6.45; | |
break; | |
default: | |
total = -1; | |
break; | |
} | |
if (total != -1) | |
{ | |
cout << "Total a ser pago é R$ " << total; | |
} | |
else | |
{ | |
cout << "Produto não encontrado"; | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*! @file e32.cpp | |
@brief Exercício 32 - Estruturas condicionais | |
@author Hudson Afonso | |
@version 1.0.0-alpha - http://semver.org | |
@note Enviar para alexandrebrrib@gmail.com | |
@link https://gist.github.com/2340805#file_e32.cpp | |
Uma empresa concederá um aumento de salário aos seus funcionários, variável | |
de acordo com o cargo, conforme a tabela abaixo. Faça um algoritmo que leia | |
o salário e o cargo de um funcionário e calcule o novo salário. Se o cargo | |
do funcionário não estiver na tabela, ele deverá, então, receber 40% de | |
aumento. Mostre o salário antigo, o novo salário e a diferença. \n | |
Código Cargo Percentual \n | |
101 Gerente 10% \n | |
102 Engenheiro 20% \n | |
103 Técnico 30% \n | |
*/ | |
#include <iostream> | |
using namespace std; | |
float salario; //!< Salário Antigo. | |
float novo_salario; //!< Salário Novo. | |
string cargo; //!< Cargo. | |
int main() | |
{ | |
//Configura o cout para fixar em 2 casas decimais quando for float | |
cout.setf(ios::fixed); | |
cout.precision(2); | |
cout << "Entre com o salário: "; | |
cin >> salario; | |
cout << "Entre com o cargo: "; | |
cin >> cargo; | |
switch (cargo[0]) | |
{ | |
case 'G': //Gerente | |
novo_salario = salario * 1.1; | |
break; | |
case 'E': //Engenheiro | |
novo_salario = salario * 1.2; | |
break; | |
case 'T': //Técnico | |
novo_salario = salario * 1.3; | |
break; | |
default: | |
novo_salario = salario * 1.4; | |
break; | |
} | |
cout << "Salário antigo R$ " << salario << endl; | |
cout << "Salário novo R$ " << novo_salario << endl; | |
cout << "Diferença de R$ " << novo_salario - salario << endl; | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*! @file e34.cpp | |
@brief Exercício 34 - Estruturas condicionais | |
@author Hudson Afonso | |
@version 1.0.0-alpha - http://semver.org | |
@note Enviar para alexandrebrrib@gmail.com | |
@link https://gist.github.com/2340805#file_e34.cpp | |
Fazer um algoritmo que leia os dados de um usuário de telefonia de uma | |
empresa de telecomunicações: bairro e número completo do telefone e verifique | |
se o número do telefone (Exemplo:2121212) está correto, ou seja, se o | |
prefixo (3 primeiros dígitos) é correspondente ao bairro especificado. | |
Sabendo-se que os prefixos existem nos bairros conforme a tabela abaixo: \n | |
Bairro Prefixo \n | |
Oeste, Vila Nova, Coimbra 223,225,212,243 | |
Centro, Universitario, Sul 223,224,215,295 | |
Bueno, Campinas, Leste 215,212,291,268 | |
*/ | |
#include <iostream> | |
#include <string> | |
using namespace std; | |
int telefone; //!< @brief Telefone. | |
int prefixo; //!< @brief Prefixo de 3 dígitos. | |
string bairro; //!< @brief Bairro; | |
bool valido = false; //!< @brief Válido, se o Telefone e o Bairro estão corretos. | |
int main() | |
{ | |
cout << "Entre com o telefone: "; | |
cin >> telefone; | |
cout << "Entre com o bairro: "; | |
cin >> bairro; | |
prefixo = telefone / 10000; | |
cout << "Prefixo: " << prefixo << endl; | |
if (prefixo == 223 || prefixo == 225 || prefixo == 212 || prefixo == 243) | |
{ | |
if (bairro.compare("Oeste") == 0 || bairro.compare("Vila Nova") == 0 | |
|| bairro.compare("Coimbra") == 0) | |
{ | |
valido = true; | |
} | |
} | |
if (prefixo == 223 || prefixo == 224 || prefixo == 215 || prefixo == 295) | |
{ | |
if (bairro.compare("Centro") == 0 || bairro.compare("Sul") == 0 | |
|| bairro.compare("Universitario") == 0) | |
{ | |
valido = true; | |
} | |
} | |
if (prefixo == 215 || prefixo == 212 || prefixo == 291 || prefixo == 268) | |
{ | |
if (bairro.compare("Bueno") == 0 || bairro.compare("Campinas") == 0 | |
|| bairro.compare("Leste") == 0) | |
{ | |
valido = true; | |
} | |
} | |
if (valido) | |
{ | |
cout << "Telefone e Bairro estão corretos" << endl; | |
} | |
else | |
{ | |
cout << "Telefone e Bairro NÃO estão corretos" << endl; | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*! @file e36.cpp | |
@brief Exercício 36 - Estruturas condicionais | |
@author Hudson Afonso | |
@version 1.0.0-alpha - http://semver.org | |
@note Enviar para alexandrebrrib@gmail.com | |
@link https://gist.github.com/2340805#file_e36.cpp | |
Elabore um algoritmo que leia o valor de dois números inteiros e a operação | |
aritmética desejada; calcule, então, a resposta adequada. Utilize os símbolos | |
da tabela a seguir para ler qual a operação aritmética escolhida. \n | |
Símbolo Operção \n | |
+ Adição \n | |
- Subtração \n | |
* Multiplicação \n | |
/ Divisão | |
*/ | |
#include <iostream> | |
using namespace std; | |
int num1; //!< @brief Número 1. | |
int num2; //!< @brief Número 2. | |
char op; //!< @brief Operação. | |
float resp; //!< @brief Resposta. | |
int main() | |
{ | |
//Configura o cout para fixar em 2 casas decimais quando for float | |
cout.setf(ios::fixed); | |
cout.precision(2); | |
cout << "Entre com o primeiro número: "; | |
cin >> num1; | |
cout << "Entre com o segundo número: "; | |
cin >> num2; | |
cout << "Entre com a operação[ + - * / ]: "; | |
cin >> op; | |
switch (op) | |
{ | |
case '+': | |
resp = num1 + num2; | |
break; | |
case '-': | |
resp = num1 - num2; | |
break; | |
case '*': | |
resp = num1 * num2; | |
break; | |
case '/': | |
resp = float(num1) / float(num2); | |
break; | |
default: | |
break; | |
} | |
cout << num1 << " " << op << " " << num2 << " = " << resp; | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*! @file e38.cpp | |
@brief Exercício 38 - Estruturas condicionais | |
@author Hudson Afonso | |
@version 1.0.0-alpha - http://semver.org | |
@note Enviar para alexandrebrrib@gmail.com | |
@link https://gist.github.com/2340805#file_e38.cpp | |
A cidade de Perdiz das Cruzes possui um único posto telefônico. Por este | |
posto são feitas todas as ligações interurbanas da cidade. O valor a ser pago | |
e calculado seguindo as seguintes regras: \n | |
• Taxa de R$2,00 pela ligação, R$1,00 para os 3 primeiros minutos; \n | |
• Acima do três primeiros minutos as regras são de R$1,50 para cada intervalo | |
de 5 minutos e R$0,25 para cada minuto abaixo disto. \n | |
A telefonista ira fornecer o nome do usuário e o tempo da ligação em minutos, | |
o algoritmo devera calcular o valor a ser pago e escrever o nome e o valor da | |
conta. | |
*/ | |
#include <iostream> | |
using namespace std; | |
string nome; | |
int tempo; | |
float total = 2; | |
int main() | |
{ | |
//Configura o cout para fixar em 2 casas decimais quando for float | |
cout.setf(ios::fixed); | |
cout.precision(2); | |
cout << "Entre com o nome do usuário: "; | |
getline(cin, nome); | |
//cin.ignore(); | |
cout << "Entre com o tempo da ligação em minutos: "; | |
cin >> tempo; | |
if (tempo <= 3) | |
{ | |
total++; | |
} | |
else | |
{ | |
total += (tempo / 5) * 1.5 + (tempo % 5) * 0.25; | |
} | |
cout << "Valor da conta é R$ " << total; | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*! @file e40.cpp | |
@brief Exercício 40 - Estruturas condicionais | |
@author Hudson Afonso | |
@version 1.0.0-alpha - http://semver.org | |
@note Enviar para alexandrebrrib@gmail.com | |
@link https://gist.github.com/2340805#file_e40.cpp | |
Desenvolver um algoritmo para receber uma data e consistir esta data. | |
(Consistir: verificar se é uma data válida) | |
*/ | |
#include <iostream> | |
using namespace std; | |
int dd; //!< @brief Dia. | |
int mm; //!< @brief Mês. | |
int yyyy; //!< @brief Ano. | |
bool valid = true; //!< @brief Se a data é válida. | |
bool bissexto = false; //!< @brief Se o ano é Bissexto. | |
int main() | |
{ | |
cout << "Entre com o dia: "; | |
cin >> dd; | |
cout << "Entre com o número do mês: "; | |
cin >> mm; | |
cout << "Entre com o ano: "; | |
cin >> yyyy; | |
if ((!(yyyy % 4) && yyyy % 100) || !(yyyy % 400)) | |
{ | |
bissexto = true; | |
} | |
if (mm < 13) | |
{ | |
if ((mm == 4 || mm == 6 || mm == 9 || mm == 11) && dd > 30) | |
{ | |
valid = false; | |
} | |
if (mm == 2) | |
{ | |
if (bissexto) | |
{ | |
if (dd > 29) | |
{ | |
valid = false; | |
} | |
} | |
else | |
{ | |
if (dd > 28) | |
{ | |
valid = false; | |
} | |
} | |
} | |
} | |
else | |
{ | |
valid = false; | |
} | |
if (valid) | |
{ | |
cout << "Data válida"; | |
} | |
else | |
{ | |
cout << "Data inválida"; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment