Jump to content

python script to c++ program

- - - - -

  • Please log in to reply
4 replies to this topic

#1
thommyy

thommyy

    Newbie

  • Members
  • Pip
  • 4 posts
I have this script that is in python, and i want to make c++ program from that.
Currency converter:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import urllib2
import datetime

def fetching_Exchange_Rate():
d = datetime.date.today()
date = d.strftime("%d%m%y")
url = "http://www.hnb.hr/tecajn/f" + date + ".dat"
link = urllib2.urlopen(url)
s = link.read()
link.close()

return s


def rate(code,type):
cont = fetching_Exchange_Rate()
list = cont.split("\r\n")



rate = 0.0
for i in list:
if i.find(kod) >= 0:
if tip == "s":
rate = float(i[31:39].replace(",","."))
if type == "k":
rate = float(i[16:24].replace(",","."))
if type == "p":
rate = float(i[46:].replace(",","."))
if i[6:9] == "100":
rate = rate / 100
break

return rate


more = "y"
while more.lower() == "y":
k = raw_input("Enter the currency (EUR € , USD $, GBP £, ...): ")
k = k.upper()

print "\n"

t = raw_input("type of transaction (s=Buying, p=Middle, k=Selling: ")
j = float(raw_input("amount of monetary units: "))

print "total: %s Kn" % str(j * rate(k, t))


more = raw_input("more? (d/n): ")




what this script do is connect to web page of Croatian National Bank, download exchange rate and convert needed currency, one line of that formatted data:
http://www.hnb.hr/tecajn/f041011.dat

036AUD001 5,389627 5,405845 5,422063

i figured out how to get date in that format in c++, but i don't know how to write that urllib2, urlopen command in c++. thanks for any advice, sorry if something make no sense, i just translated it in google translate in 5 minutes.

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
What library are you planning to use? C++ does not, by default, have support for any form of socket programming. You could use the boost library, however.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
thommyy

thommyy

    Newbie

  • Members
  • Pip
  • 4 posts
well, I don't really know which libraries exist for that purpose, i find some libraries on google but i cound't understand how to fit it into rest program

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Qt, wxWidgets, gtkmm, and boost all provide networking libraries. Some of them provide easier tools for getting data from a website than others. It's really going to depend on what you want to use.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
thommyy

thommyy

    Newbie

  • Members
  • Pip
  • 4 posts
okay, i find one solution. for date i write function:


void dat(string &d){
time_t rawtime;
struct tm * timeinfo;
char datum[80];
time ( &rawtime );
timeinfo=localtime(&rawtime);
strftime(datum,80,"%d%m%y.",timeinfo);
cout << "\ndatum: " << datum;
d=datum;
}

int main()
{
string f;
dat(f);
cout << "http://www.hnb.hr/tecajn/f" << f << ".dat";

and for exchange rates, i will download that list as txt file on disk with libcurl, and read data from file...




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users