Add simple calculator (run using `calc` command)

This commit is contained in:
g1n 2021-07-27 14:58:04 +03:00
parent 8c9a80fde2
commit 5f39827937
2 changed files with 46 additions and 10 deletions

View File

@ -3,18 +3,16 @@ use alloc::string::String;
use alloc::vec::Vec;
use crate::alloc::string::ToString;
use crate::software::calc::calc;
pub mod calc;
pub fn command_func(input_command: String) {
let mut command = String::new();
let mut args = String::new();
let mut command_vec: Vec<&str> = input_command.split(" ").collect();
command = command_vec[0].to_string();
command_vec.remove(0);
for i in command_vec {
for j in i.chars() {
args.push(j);
}
args.push(' ');
}
let mut args_vec: Vec<&str> = input_command.split(" ").collect();
command = args_vec[0].to_string();
args_vec.remove(0);
match command.as_str() {
"hi" => {
println!("hello command!!!");
@ -23,6 +21,13 @@ pub fn command_func(input_command: String) {
println!("wow!!");
}
"echo" => {
let mut args = String::new();
for i in args_vec {
for j in i.chars() {
args.push(j);
}
args.push(' ');
}
println!("{} ", args);
}
"asciiart" => {
@ -33,6 +38,9 @@ pub fn command_func(input_command: String) {
println!(" | |__| | | \\ \\| |__| |____) |");
println!(" \\_____|_| \\_\\\\____/|_____/ ");
}
"calc" => {
calc(args_vec);
}
command => {
println!("{}: command not found", command);
}

28
src/software/calc.rs Normal file
View File

@ -0,0 +1,28 @@
use alloc::string::String;
use alloc::vec::Vec;
use crate::println;
pub fn calc(args: Vec<&str>) {
if args.len() < 3 || args.len() > 3 {
println!("Not enought or too much arguments");
return;
}
let arg1: i32 = args[0].parse().unwrap();
let arg2: i32 = args[2].parse().unwrap();
let operation = args[1];
if arg2 == 0 && operation == "/" {
println!("Divide by zero");
return;
} else if arg2 == 0 && operation == "%" {
println!("Modulo by zero");
return;
}
match args[1] {
"+" => {println!("{}", arg1+arg2)},
"-" => {println!("{}", arg1-arg2)},
"*" => {println!("{}", arg1*arg2)},
"/" => {println!("{}", arg1/arg2)},
"%" => {println!("{}", arg1%arg2)},
arg => {println!("Someting went wrong")},
}
}