Simple calculator C++
I’m not good at programming, but I like to create small things like this. Probably it can be much nicer and cleaner, but it works and C/C++ is also a learning curve for myself.
Create a file: ~/<some_folder/calc.cpp, copy bolow code, save and compile it with g++ (install gcc first)
# Compile
$ g++ calc.cpp -o calc
# Run
$ ./calc # To execute
#include <stdio.h>
#include <iostream>
float a,b;
char action;
int main () {
system ("clear");
std::cout<<"\033[1;34mSimple Calculator\033[0m\n"<<std::endl;
std::cout<<"num 1: "<<std::endl;
std::cin>>a;
system ("clear");
std::cout<<"\033[1;34mSimple Calculator\033[0m\n"<<std::endl;
std::cout<<"\033[1;32mChoose your operator\033[0m\n"<<std::endl;
std::cout<<"+ - * or /: "<<std::endl;
std::cin>> action;
system ("clear");
std::cout<<"\033[1;34mSimple Calculator\033[0m\n"<<std::endl;
std::cout<<"num 2: "<<std::endl;
std::cin>>b;
system ("clear");
switch (action) {
case '-':
std::cout<<"\033[1;34mSimple Calculator\033[0m\n"<<std::endl;
std::cout<<"\033[1;32mResult: \033[0m\n"<<std::endl;
std::cout<<a<<"-"<<b<<"="<<a-b<<"\n"<<std::endl;
break;
case '+':
std::cout<<"\033[1;34mSimple Calculator\033[0m\n"<<std::endl;
std::cout<<"\033[1;32mResult: \033[0m\n"<<std::endl;
std::cout<<a<<"+"<<b<<"="<<a+b<<"\n"<<std::endl;
break;
case '*':
std::cout<<"\033[1;34mSimple Calculator\033[0m\n"<<std::endl;
std::cout<<"\033[1;32mResult: \033[0m\n"<<std::endl;
std::cout<<a<<"*"<<b<<"="<<a*b<<"\n"<<std::endl;
break;
case '/':
std::cout<<"\033[1;34mSimple Calculator\033[0m\n"<<std::endl;
std::cout<<"\033[1;32mResult: \033[0m\n"<<std::endl;
std::cout<<a<<"/"<<b<<"="<<a/b<<"\n"<<std::endl;
break;
default:
std::cout<<"\033[1;31mNo valid operator given...\033[0m\n"<<std::endl;
}
}