hpp file :
#pragma once
#include <string>
using namespace std;
class LiczbaException: public exception{
private:
string s;
public:
LiczbaException(string ss) :
s(ss){
};
virtual ~ LiczbaException() throw () {
};
virtual const char * erorrr() const throw () {
return s.c_str();
}
};
class RzymArab {
private:
static string liczby[13];
static int arabliczby[13];
public:
static int rzym2arab (string rzym);
static string arab2rzym (int arab) throw(LiczbaException);
};
string RzymArab::liczby[13] = {"I","IV","V","IX","X","XL","L","XC","C","CD","D","CM","M"};
int RzymArab::arabliczby[13] = {1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000};
test cpp :
#include <iostream>
#include <cstdlib>
#include <cstring>
#include "RzymArab.hpp"
using namespace std;
int main(int argc, char * argv[]) {
bool sprawdz = true;
int t = 0;
string pom;
for (int l = 1; l < argc; l++) {
try {
t = atoi(argv[l]);
if (t == 0)
throw pom;
else
sprawdz = true;
} catch (string pom){
sprawdz = false;
}
if (sprawdz == false){
try{
int wynikk = RzymArab::rzym2arab(argv[l]);
if (argv[l] != RzymArab::arab2rzym(wynikk))
cout << "Nieistniejaca liczba" << endl;
else
cout << wynikk << "\n";
} catch (LiczbaException& w) {
cout << w.what() << endl;
}
} else {
try {
string wynik = RzymArab::arab2rzym(t);
cout << wynik << "\n";
} catch (LiczbaException& w){
cout << w.what() << endl;
}
}
}
return 0;
}
cpp file:
#include <iostream>
#include <cstdlib>
#include <cstring>
#include "RzymArab.hpp"
using namespace std;
class RzymArab
{
static int rzym2arab(string rzym) {
int wynik = 0;
for (int i = 0; i < rzym.length(); i++) {
int wart1 = 0;
int wart2 = 0;
for (int j = 0; j < 13; j++) {
if (rzym.at(i)==liczby[j].at(0)) {
wart1 = arabskie[j];
}
}
for (int k = 0; k < 13; k++) {
if (i<rzym.length()-1) {
if (rzym.at(i+1)==liczby[k].at(0))
wart2 = arabskie [k];
}
}
if (wart1 >= wart2) {
wynik+=wart1;
}else {
wynik+=(wart2 - wart1);
i = i + 1;
}
}
if (wynik > 3999)
cout << "Za duza liczba!";
if (wynik == 0)
cout << "Something get's wrong!";
return wynik;
}
static string arab2rzym(int arab) throw (LiczbaException) {
string wynik = "";
if (arab < 1 || arab > 3999)
throw LiczbaException("Zla podstawa");
for (int i = 0; i < 13; i++) {
while (arab >= arabskie[i]) {
wynik += liczby[i];
arab -= arabskie[i];
}
}
return wynik;
}
};
I got the errors while compiling: " RzymArab.cpp:10:7: error: redefinition of 'class RzymArab' class RzymArab
In file included form RzymArab.cpp:5:0: RzymArab.hpp:26:7: error: previous definition of 'class RzymArab' class RzymArab"
I don't know what is wrong, maybe something with pragma once but when i use old guard nothing change.