sophuwu.site > termcalc
back to cpp i guess
sophuwu sophie@skisiel.com
Sun, 30 Jun 2024 03:21:47 +0200
commit

b05dc75f4cdc05d6ac8f54910bf0db6d71a0862c

parent

3757ef1ce3188ba16b560e38c4251259d5c04853

4 files changed, 78 insertions(+), 88 deletions(-)

jump to
M MakefileMakefile

@@ -1,12 +1,12 @@

NAME := termcalc -CCFLAGS := -O3 -SRC := src/*.c +CXXFLAGS := -O3 +SRC := src/*.cpp all: $(SRC) @./zig-cc/zigcomp zig: $(SRC) - $(CC) $(CCFLAGS) -o build/$(NAME).$(ZIGTARGET) $(SRC) + $(CXX) $(CXXFLAGS) -o build/$(NAME).$(ZIGTARGET) $(SRC) clean: @echo "clean target is for compatibility with autotools"
D src/calc.c

@@ -1,81 +0,0 @@

-// Creator: Sophuwu -// This program is a simple calculator that takes in two numbers and an operator and returns the result of the operation. - -#include "calc.h" - -int checkInput(int argc) { - if (argc != 4) print ("Usage: calc <num1> <op> <num2>\n\n\tOperators: + - x / %\n\tExample: calc 10 / 5\n"); - return argc != 4; -} - -int getFloat(char* str, flt_t* num) { - flt_t iPart = 0.0f; - flt_t fPart = 0.0f; - int_t fPartLen = 0ll; - long i = 0l; - int sign = 1; - - if (str[0] == '-') { - sign = -1; - i++; - } - - while (str[i] != '.') { - if (str[i] == '\0') { - *num = (flt_t)(sign * iPart); - return 0; - } - if (str[i] < '0' || str[i] > '9') return 1; - iPart = iPart * 10 + (int_t)(str[i]) - '0'; - i++; - } - i++; - while (str[i] != '\0') { - if (str[i] < '0' || str[i] > '9') return 1; - fPart = fPart * 10 + (flt_t)(str[i]) - '0'; - fPartLen++; - i++; - } - while (fPartLen > 0) { - fPart /= 10; - fPartLen--; - } - *num = (flt_t)(sign * (iPart + fPart)); - return 0; -} - -int getInput(char* argv[], flt_t* num1, flt_t* num2, char* op) { - if (getFloat(argv[1], num1) || getFloat(argv[3], num2)) { - print ("Error: Invalid number\n"); - return 1; - } - *op = argv[2][0]; - if (*op != '+' && *op != '-' && *op != 'x' && *op != '/' && *op != '%') { - print ("Error: Invalid operator\n"); - return 1; - } - return 0; -} - -flt_t calcs(const flt_t num1, const flt_t num2, char op) { - switch (op) { - case '+': return (flt_t)(num1 + num2); - case '-': return (flt_t)(num1 - num2); - case 'x': return (flt_t)(num1 * num2); - case '/': return (flt_t)(num1 / num2); - } - return 0.0f; -} - -int main(int argc, char* argv[]) { - if (checkInput(argc)) return 1; - flt_t num1 = 0.0f; - flt_t num2 = 0.0f; - char op = argv[2][0]; - - if (getInput(argv, &num1, &num2, &op)) return 1; - flt_t r = calcs(num1, num2, op); - printf("%.32Lf\n", r); - - return 0; -}
A src/calc.cpp

@@ -0,0 +1,73 @@

+#include <iostream> +#include "calc.h" + +long getFloat(char* str, flt_t& num) { + flt_t iPart = 0.0f; + flt_t fPart = 0.0f; + long fPartLen = 0; + long i = 0; + long sign = 1; + + if (str[0] == '-') { + sign = -1; + i++; + } + + while (str[i] != '.') { + if (str[i] == '\0') { + num = sign * iPart; + return 0; + } + if (str[i] < '0' || str[i] > '9') return 1; + iPart = iPart * 10 + flt_t(str[i]) - '0'; + i++; + } + i++; + while (str[i] != '\0') { + if (str[i] < '0' || str[i] > '9') return 1; + fPart = fPart * 10 + flt_t(str[i]) - '0'; + fPartLen++; + i++; + } + while (fPartLen > 0) { + fPart /= 10; + fPartLen--; + } + num = sign * (iPart + fPart); + return 0; +} + +int getInput(char* argv[], flt_t& num1, flt_t& num2, char* op) { + if (getFloat(argv[1], num1) || getFloat(argv[3], num2)) { + std::cout<<("Error: Invalid number\n") << std::endl; + return 1; + } + *op = argv[2][0]; + if (*op != '+' && *op != '-' && *op != 'x' && *op != '/' && *op != '%') { + std::cout<<("Error: Invalid operator\n") << std::endl; + return 1; + } + return 0; +} + +flt_t calcs(flt_t num1, flt_t num2, char op) { + switch (op) { + case '+': return (flt_t)(num1 + num2); + case '-': return (flt_t)(num1 - num2); + case 'x': return (flt_t)(num1 * num2); + case '/': return (flt_t)(num1 / num2); + default: return 0.0f; + } +} + +int main(int argc, char* argv[]) { + if (argc != 4) return 1; + + flt_t num1, num2; + num1 = num2 = 0.0f; + char op = argv[2][0]; + if (getInput(argv, num1, num2, &op)) return 1; + flt_t result = calcs(num1, num2, op); + std::cout << result << std::endl; + return 0; +}
M src/calc.hsrc/calc.h

@@ -1,11 +1,9 @@

#ifndef TERMCALC_CALC_H #define TERMCALC_CALC_H -#include <stdio.h> -#include <float.h> -#include <math.h> +#include <cfloat> +#include <cmath> typedef long double flt_t; typedef long long int_t; -#define print(s) printf("%s", s) #endif //TERMCALC_CALC_H