Tema 4

You might also like

You are on page 1of 1

#include <unordered_map>

#include <vector>
#include <iostream>

#define MAX_CURRENCIES 50
#define INF 1e9

class CurrencyConverter {
public:
CurrencyConverter() {
//initialize all distance to INF (infinity)
for (int i = 0; i < MAX_CURRENCIES; i++)
for (int k = 0; k < MAX_CURRENCIES; k++)
distance[i][k] = (i == k) ? 1 : INF;
}

void add_rate(std::string from_currency, std::string to_currency, double


rate) {
int i = get_index(from_currency), j = get_index(to_currency);
distance[i][j] = rate;
}

double get_rate(std::string from_currency, std::string to_currency) {


int i = get_index(from_currency), j = get_index(to_currency);

floydWarshall();
if (distance[i][j] == INF) {
return 0;
}
else {
return distance[i][j];
}
}

private:
std::unordered_map<std::string, int> currency_map;
double distance[MAX_CURRENCIES][MAX_CURRENCIES];
int current_index = 0;

int get_index(std::string currency) {


if (currency_map.find(currency) == currency_map.end()) {
currency_map[currency] = current_index++;
}
return currency_map[currency];
}

void floydWarshall() {
for (int k = 0; k < current_index; k++) {
for (int i = 0; i < current_index; i++) {
for (int j = 0; j < current_index; j++) {
distance[i][j] = std::min(distance[i][j], distance[i][k] *
distance[k][j]);
}
}
}
}
};

You might also like