Resolução de Exercícios EDA: 1-Data Types. Arithmetic Expressions. Input and Output
Resolução de Exercícios EDA: 1-Data Types. Arithmetic Expressions. Input and Output
EDA
1- DATA TYPES. ARITHMETIC EXPRESSIONS. INPUT AND
OUTPUT
1.1 LER UMA LETRA E RETORNAR O CÓDIGO ASCII
#include <iomanip>
#include <iostream>
using namespace std;
int main()
{
char c;
cout << "Enter a character: ";
cin >> c;
cout << "ASCII value of " << c << " is " << int(c);
return 0;
}
#include <iomanip>
#include <iostream>
using namespace std;
int main()
{
double A, B, C;
double average, A_average, B_average, C_average;
average = (A + B + C) / 3;
A_average = A - average;
B_average = B - average;
C_average = C - average;
#include <iomanip>
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
const double PI = 3.1415;
double ro, r, M;
M = (4 * ro*PI*pow(r, 3))/3;
#include <iomanip>
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double a, b, c, d, e, f, x, y;
A) SOMAR 2 TEMPOS
#include <iomanip>
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int d1, d2, h1, h2, m1, m2, s1, s2, d, h, m, s;
cout << "Time1 (hours minutes seconds) ? "; cin >> h1 >> m1
>> s1;
cout << "Time2 (hours minutes seconds) ? "; cin >> h2 >> m2
>> s2;
cout << "Time1 + Time2 = " << d << " day, " << h << "
hours, " << m << " minutes and " << s << " seconds";
return 0;
}
#include <iomanip>
#include <iostream>
int main()
{
int d1, d2, h1, h2, m1, m2, s1, s2, d, h, m, s;
char c1, c2, c3, c4;
cout << "Time1 (hours minutes seconds) ? "; cin >> h1
>>c1>> m1 >>c2>> s1;
cout << "Time2 (hours minutes seconds) ? "; cin >> h2
>>c3>> m2 >>c4>> s2;
#include <iomanip>
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
float s, area, x1, y1, x2, y2, x3, y3, a, b, c, p;
s = (a + b + c) / 3;
area=sqrt(fabs((s*(s - a)*(s - b)*(s - c))));
#include <iomanip>
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double a, b, c, d, e, f, x, y, n1, n2, d1, d2;
n1 = (c*e - b * f);
n2 = (a*f - c * d);
d = (a*e - b * d);
if (d == 0)
{
if (n1 == 0 || n2 == 0)
cout << "Inconsistent system";
else
cout<<"Impossible system"
}
else
{
x = n1 / d;
y = n2 / d;
cout << "x= "; cin >> x;
cout << "y= "; cin >> y;
}
return 0;
}
#include <iomanip>
#include <iostream>
using namespace std;
int main()
{
int n1, n2, n3, min, max, midle;
cout << "Please insert 3 numbers: "; cin >> n1 >> n2 >> n3;
cout << max << "<" << midle << "<" << min;
return 0;
}
B. DECIDIR SE OS 3 NÚMEROS SÃO OS COMPRIMENTOS DE UM
TRIÂNGULO
Adicionar ao anterior:
return 0;
}
2.3 SOMA DE DOIS NÚMEROS INTEIROS E SE OCORRE UM OVERFLOW OU
UNDERFLOW
int main()
{
int n1, n2, n3, sum;
cout << "Please insert 2 numbers: "; cin >> n1 >> n2;
sum = n1 + n2;
if (sum > INT_MAX)
cout << "Sum OVERFLOW";
else if (sum < INT_MIN)
cout << "Sum UNDERFLOW";
else
cout << sum;
}
#include <iomanip>
#include <iostream>
using namespace std;
int main()
{
double weight, cost, extra;
int main()
{
const unsigned int NUMBER_PRECISION = 3;
double A, B, C, delta, r1, r2, p;
cout << "Solution of Ax^2 + Bx + C = 0" << '\n' << "Insert
the coefficients (A B C): ";
cin >> A >> B >> C;
#include <iomanip>
#include <iostream>
using namespace std;
int main()
{
int n, i;
double q, j, extra;
#include <iomanip>
#include <iostream>
int main()
{
int n, counter, sum;
int divmax = sqrt(n);
cout << "Number= ";
bool isprime = true;
for (int i = 2; i < divmax; i++)
{
if (n%i == 0)
{
isprime = false;
i++;
}
}
if (isprime)
cout << "Is prime!";
else
cout << "Isn't prime!";
return 0;
}
A. TABELA DE 0 A 90
#include <iomanip>
#include <iostream>
int main()
{
int grau;
double seno, cosseno, tg;
const double PI = 3.1414929;
cout << setw(10) << "ang" << setw(10) << "sen" << setw(10)
<< "cos" << setw(10) << "tan" << endl;
for (grau = 0; grau < 90; grau = grau + 15)
{
seno = sin(grau*PI / 180);
cosseno = cos(grau*PI / 180);
tg = tan(grau*PI / 180);
cout << setw(10) << grau << setw(10) << fixed <<
setprecision(7) << seno << setw(10) << setprecision(7) <<
cosseno << setw(10) << setprecision(7) << tg << endl;
}
cout << setw(10) << 90 << setw(10) << 1.000000 << setw(10)
<< 0.000000 << setw(10) << "infinito" << endl;
return 0;
}
B. O INTERVALO E O INCREMENTO SÃO DETERMINADO PELO UTILIZADOR
#include <iomanip>
#include <iostream>
int main()
{
int grau_min, grau_max;
double seno, cosseno, tg, inc, grau;
const double PI = 3.1414929;
cout << "grau min = "; cin >> grau_min;
cout << "grau max = "; cin >> grau_max;
cout << "incremento= "; cin >> inc;
cout << setw(10) << "ang" << setw(10) << "sen" << setw(10)
<< "cos" << setw(10) << "tan" << endl;
for (grau = grau_min; grau < grau_max; grau = grau + inc)
{
seno = sin(grau*PI / 180);
cosseno = cos(grau*PI / 180);
tg = tan(grau*PI / 180);
cout << setw(10) << grau << setw(10) << fixed <<
setprecision(7) << seno << setw(10) << setprecision(7) <<
cosseno << setw(10) << setprecision(7) << tg << endl;
}
cout << setw(10) << 90 << setw(10) << 1.000000 << setw(10)
<< 0.000000 << setw(10) << "infinito" << endl;
return 0;
}
2.9 CAPICUAS
A) PARA NÚMEROS DE 3 ALGARISMOS
#include <iomanip>
#include <iostream>
int main()
{
int n, n1, n2;
int main()
{
int num, isCap, rev, resto;
cout << "Positive number" << endl; cin >> num;
rev = 0;
isCap = num;
do
{
resto = isCap % 10;
rev = (rev + resto) * 10;
isCap = isCap / 10;
} while (isCap != 0);
if (num == (rev / 10))
cout << "It's a palindrome!" << endl;
else
cout << "It's not a palindrome!" << endl;
return (0);
}
2.10 FATORES PRIMOS
#include <iomanip>
#include <iostream>
int main() {
int num, div, x, y;
cout << "introduza o numero ="; cin >> num;
div = 2;
for (;;)
{
if (num%div == 0) //divisão exata
{
if (num / div == 1) //resultado 1 acaba com a
conta
{
cout<< num<<'|'<< div<<'\n';
num = num / div;
break;
}
cout << num << '|' << div<<'\n';
num = int(num / div); //torna o número no
próximo
}
else //divisão não exata
{
for (y = 0, x = 1; x <= div; ++x) // teste do
fator primo
if (div%x == 0)
y += x; //y=y+x
if (div + 1 == y)
{
div = y - 1; // divisor passa para o
próximo número primo
}
div++; //inc
}
}
return 0;
}
2.11 SOMA DOS PRIMEIROS N FATORES
A) CONSTANTE
#include <iomanip>
#include <iostream>
using namespace std;
int main()
{
float sum=4;
int n, den;
B) CONSTANTE NEPER
#include <iomanip>
#include <iostream>
int main()
{
int n, i, a, fat;
double e;
cout << "Please insert the number of terms: "; cin >> n;
e = 1;
for (i = 2; i <= n; i++)
{
fat = 1;
for (a = i - 1; a != 1; a--)
{
fat = fat * a;
}
e = e + (1.0 / fat);
}
cout << "e= " << e;
}
C) NEPER^X
#include <iomanip>
#include <iostream>
using namespace std;
int main()
{
int n, i, a, fat, num, index;
double e;
int x;
cout << "Please insert the number of terms: "; cin >> n;
cout << "x value="; cin >> x;
e = 1;
for (i = 2; i <= n; i++)
{
index = -(i - 1);
num = pow(x, index);
fat = 1;
for (a = i - 1; a != 1; a--)
fat = fat * a;
e = e + (num / fat);
}
cout << "e= " << e;
}
#include <iomanip>
#include <iostream>
int main()
{
int comp, n, s, s_n, l_n;
double sum, mean, std;
s_n = 0; l_n = 0;
sum = 0;
mean = 0.0;
std = 0.0;
s = 0;
cout << "Lenght of sequence= "; cin >> comp;
if (n < s_n)
s_n = n;
else if (n > l_n)
l_n = n;
}
cout << "sum= " << sum<<endl;
cout << "mean= " << mean << endl;
cout << "std= " << sum << endl;
cout << "smallest number= " << s_n << endl;
cout << "largest number= " << l_n << endl;
}
#include <iomanip>
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
float rq, rqn, rqn2, dif, delta;
int n, nMaxIter, counter;
#include <iomanip>
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
//Tabuada
//teste das tabuadas da multiplicação entre 2 e 9
int randomBetween(int n1, int n2)
{
return rand() % (n2 - n1 + 1) + 2;
//este +1 serve para indicar a partir de onde é que se
inicia o intervalo
}
int main()
{
srand(time(NULL));
int a, b, result, ans;
time_t t1, t2, time_elapsed;
a = randomBetween(2, 9);
b = randomBetween(2, 9);
result = a * b;
t1 = time(NULL);
cout << a << '*' << b << '='; cin >> ans;
t2 = time(NULL);
time_elapsed = t2 - t1;
if (ans != result)
{
cout << "Very Bad!";
}
else
{
if (static_cast<double>(time_elapsed) <= 5)
{
cout << "Good!";
}
else if (time_elapsed > 5 || time_elapsed <= 10)
{
cout << "Satisfactory";
}
else
cout << "Insufficient";
}
}
#include <iomanip>
#include <iostream>
int main()
{
const unsigned int NUMBER_PRECISION = 3;
double n1, n2, result;
char operation, rpt, anotherOperation;
do
{
cout << " n1 operator n2= ";
cin >> n1 >> operation >> n2;
bool validOperation = true;
switch (operation)
{
case '+':
result = n1 + n2;
break;
case '-':
result = n1 - n2;
break;
case '*':
result = n1 * n2;
break;
case '/':
result = n1 / n2;
break;
default:
validOperation = false;
}
if (validOperation)
{
cout << fixed << setprecision(NUMBER_PRECISION);
cout << n1 << ' ' << operation << ' ' << n2 << "
= " << result
<< endl;
}
else
cerr << "Invalid operation !\n";
cout << "Another operation (Y/N) ? ";
cin >> anotherOperation;
anotherOperation = toupper(anotherOperation);
} while (anotherOperation == 'Y');
return 0;
}
3. FUNCTIONS
3.1 CALCULAR A ÁREA DO TRIANGULO (1.6), MAS COM FUNÇÕES
#include <iomanip>
#include <iostream>
double area(double x1, double y1, double x2, double y2, double
x3, double y3)
{
double a, b, c, s, area;
a = distance(x1, y1, x2, y2);
b = distance(x2, y2, x3, y3);
c = distance(x1, y1, x3, y3);
s = (a + b + c) / 3;
return sqrt(fabs((s*(s - a)*(s - b)*(s - c))));
}
int main()
{
double x1, y1, x2, y2, x3, y3;
cout << "Point 1 coordinates (x1, y1)= "; cin >> x1 >> y1;
cout << "Point 2 coordinates (x2, y2)= "; cin >> x2 >> y2;
cout << "Point 3 coordinates (x3, y3)= "; cin >> x3 >> y3;
double A = area(x1, y1, x2, y2, x3, y3);
cout << A;
}