Skip to content

Instantly share code, notes, and snippets.

@friendleemachine
Created June 28, 2018 13:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save friendleemachine/96bb5de21e21a37cc8565b96936d9e26 to your computer and use it in GitHub Desktop.
Save friendleemachine/96bb5de21e21a37cc8565b96936d9e26 to your computer and use it in GitHub Desktop.
Lab 2
#include "/Users/fr_machine/lab2/rapidxml-1.13/rapidxml.hpp"
#include <fstream>
#include <iostream>
#include <string.h>
using namespace std;
using namespace rapidxml;
double exchange(xml_node<>* root_node, char currency_in[3], char currency_out[3], double amount) {
double value_in = 0.0, value_out = 0.0;
int nominal;
for(xml_node<>* tmp_node = root_node->first_node(); tmp_node; tmp_node = tmp_node->next_sibling()) {
if(!strcmp(currency_in, tmp_node->first_node("CharCode")->value()))
value_in = atoi(tmp_node->first_node("Value")->value());
if(!strcmp(currency_out, tmp_node->first_node("CharCode")->value())) {
value_out = atoi(tmp_node->first_node("Value")->value());
nominal = atoi(tmp_node->first_node("Nominal")->value());
}
}
if(value_in == 0 or value_out == 0) {
cout << "No such currency";
return -1.0;
}
value_out = (value_in * nominal * amount) / value_out;
return value_out;
}
int main(int argc, char* argv[]) {
xml_document<> doc;
xml_node<>* root_node;
ifstream input_xml("/Users/fr_machine/lab2/currency.xml");
string buffer((istreambuf_iterator<char>(input_xml)), istreambuf_iterator<char>());
buffer.push_back('\0');
doc.parse<0>(&buffer[0]);
root_node = doc.first_node();
char currency_in[4];
char currency_out[4];
auto sum_str = new char;
double sum = 0.0;
cout << strncpy(currency_in, argv[1]+7, 3) << endl;
currency_out[3] = '\0';
cout << strncpy(currency_out, argv[2]+5, 3) << endl;
currency_in[3] = '\0';
cout << strncpy(sum_str, argv[3]+6, sizeof(argv[1]) - 5) << endl;
sum = atoi(sum_str);
cout << exchange(root_node, currency_in, currency_out, sum);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment