Aha

Jjl
Last edited on
ico.hpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef ICO_HPP
#define ICO_HPP

#include <string>

class ico {
public:
    ico();
    ico(const std::string);
    std::string getId()const;
    bool setId(const std::string&);
private:
    std::string id;
    bool kontrola(const std::string&) const;
};

#endif // ICO_HPP 


ico.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include "ico.hpp"
#include <iostream>

ico::ico() { id = "99999999"; }

ico::ico(const std::string id_arg)
{
    if(kontrola(id)) { id = id_arg; }
    else             { id = "99999999"; }
}

bool ico::setId(const std::string& id_arg)
{
    if(kontrola(id)) {
        id = id_arg;
        return true;
    }
    return false;
}

std::string ico::getId() const { return id; }

bool ico::kontrola(const std::string& id_arg) const
{
    if(id_arg.length() != 8) { return false; }

    for(unsigned i = 0; i < id_arg.length(); i++) {
        if(id_arg.at(i) < '0' || '9' < id_arg.at(i)) { return false; }
    }

    int vaha = 8;
    int soucet = 0;
    for (int i = 0; i < 7; i++) {
        soucet += (id_arg.at(i) - '0') * vaha;
        vaha--;
    }
    int zbytek = soucet % 11;
    
    int kc = (11 - zbytek) % 10;
    
    if(kc == id_arg.at(7) - '0') { return true; }
    return false;
}


main.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "ico.hpp"
#include <iostream>

int main()
{
    ico prvni;
    std::cout << "Objekt vytvoreny konstruktorem bez parametru:\n"
                 "Ico firmy jedna " << prvni.getId() << '\n';
    do {
        std::cout << "Zadej ico: ";
        std::string zadan;
        std::cin >> zadan;
        if(prvni.setId(zadan)) {
            std::cout << "Ico je: " << prvni.getId() << '\n';
        } else {
            std::cout << "Zadal jsi chybne Ico, zadej znovu.\n";
        }
    } while(true); // Is there a way for the user to exit the program?
    return 0;
}


?
Topic archived. No new replies allowed.