aboutsummaryrefslogtreecommitdiff
path: root/cpp06/ex00/main.cpp
blob: ec30bb801c63f94990e723697b8b8991521d1573 (plain)
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   main.cpp                                           :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: charles <charles.cabergs@gmail.com>        +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2020/04/16 10:54:10 by charles           #+#    #+#             */
/*   Updated: 2020/11/11 09:21:08 by cacharle         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include <iostream>
#include <sstream>
#include <string>

int main(int argc, char **argv)
{
    if (argc != 2)
    {
        std::cerr << "Usage: " << argv[0] << " litteral" << std::endl;
        return 1;
    }


    std::string s(argv[1]);

    if (s.empty())
    {
        std::cerr << "Cannot be empty" << std::endl;
        return 1;
    }

    bool negative = false;
    if (s[0] == '-' || s[0] == '+')
    {
        if (s[0] == '-')
            negative = true;
        s.erase(0, 1);
    }

    char   c;
    int    i;
    float  f;
    double d;

    if (s == "nan" || s == "inf")
    {
        d = s == "nan" ? NaN : inf;
        if (negative)
            d = -d;
        f = static_cast<float>(d);
        i = static_cast<int>(d);
        c = static_cast<char>(d);
    }
    if (s == "nanf" || s == "inff")
    {
        f = s == "nanf" ? NaN : inff;
        if (negative)
            f = -f;
        d = static_cast<double>(f);
        i = static_cast<int>(f);
        c = static_cast<char>(f);
    }

    if (isdigit(s[0]))
    {
        std::stringstream ss(s);

        if (s.find(".") == -1)
        {
            ss >> i;
            d = static_cast<double>(i);
            f = static_cast<float>(i);
            c = static_cast<char>(i);
        }
        else if (s.find("f") != -1)
        {
            ss >> f;
            d = static_cast<double>(f);
            i = static_cast<int>(f);
            c = static_cast<char>(f);
        }
        else
        {
            ss >> d;
            f = static_cast<float>(d);
            i = static_cast<int>(d);
            c = static_cast<char>(d);
        }
    }
    else if (!negative && s.length == 1)
    {
        c = s[0];
        d = static_cast<double>(c);
        f = static_cast<float>(c);
        i = static_cast<int>(c);
    }
    else
    {
        std::cout << "Parsing error" << std::endl;
        return 1;
    }


    return 0;
}