Created
June 28, 2018 13:09
-
-
Save friendleemachine/96bb5de21e21a37cc8565b96936d9e26 to your computer and use it in GitHub Desktop.
Lab 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
#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