main.cpp
 1  /* ************************************************************************** */
 2  /*                                                                            */
 3  /*                                                        :::      ::::::::   */
 4  /*   main.cpp                                           :+:      :+:    :+:   */
 5  /*                                                    +:+ +:+         +:+     */
 6  /*   By: gychoi <gychoi@student.42seoul.kr>         +#+  +:+       +#+        */
 7  /*                                                +#+#+#+#+#+   +#+           */
 8  /*   Created: 2023/08/02 17:45:27 by gychoi            #+#    #+#             */
 9  /*   Updated: 2023/08/04 01:45:43 by gychoi           ###   ########.fr       */
10  /*                                                                            */
11  /* ************************************************************************** */
12  
13  #include <cstdlib>
14  #include "Fixed.hpp"
15  
16  static void	_printFixedNumber(Fixed const& fixed)
17  {
18  	std::cout << "Argument is " << fixed << std::endl;
19  	std::cout << "Argument is " << fixed.toInt() << " as integer" << std::endl;
20  	std::cout << "Argument is " << fixed.toFloat() << " as float" << std::endl;
21  }
22  
23  static int	_testFixedNumber(char* arg)
24  {
25  	char*	end;
26  	int		intValue = std::strtol(arg, &end, 10);
27  	
28  	if (*end == '\0')
29  	{
30  		Fixed	intFixed(intValue);
31  		_printFixedNumber(intFixed);
32  	}
33  	else
34  	{
35  		float	floatValue = std::strtof(arg, &end);
36  		if (*end == '\0')
37  		{
38  			Fixed	floatFixed(floatValue);
39  			_printFixedNumber(floatFixed);
40  		}
41  		else
42  		{
43  			std::cout << "Invalid input: " << arg << std::endl;
44  			return (1);
45  		}
46  	}
47  	return (0);
48  }
49  
50  int	main(int argc, char* argv[])
51  {
52  	if (argc == 2)
53  		return _testFixedNumber(argv[1]);
54  	else if (argc > 2)
55  	{
56  		std::cout << "Usage: " << argv[0] << " <number>" << std::endl;
57  		return 1;
58  	}
59  	Fixed		a;
60  	Fixed const	b( 10 );
61  	Fixed const	c( 42.42f );
62  	Fixed const	d( b );
63  
64  	a = Fixed( 1234.4321f );
65  
66  	std::cout << "a is " << a << std::endl;
67  	std::cout << "b is " << b << std::endl;
68  	std::cout << "c is " << c << std::endl;
69  	std::cout << "d is " << d << std::endl;
70  
71  	std::cout << "a is " << a.toInt() << " as integer" << std::endl;
72  	std::cout << "b is " << b.toInt() << " as integer" << std::endl;
73  	std::cout << "c is " << c.toInt() << " as integer" << std::endl;
74  	std::cout << "d is " << d.toInt() << " as integer" << std::endl;
75  
76  	return 0;
77  }