Skip to content

Commit 947d324

Browse files
Added projects
1 parent 50b3ac4 commit 947d324

100 files changed

Lines changed: 145 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

projects/Calc_cli/Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

projects/Calc_cli/Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[package]
2+
name = "Calc_cli"
3+
version = "0.1.0"
4+
edition = "2024"
5+
6+
[dependencies]

projects/Calc_cli/src/main.rs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
use std::env; // gives all cli args
2+
3+
fn help(){
4+
println!("Usage: Calc_cli <number1> <operator> <number2> ");
5+
println!("Examples : Calc_cli 5 + 3");
6+
println!("supported operators: + - * / % ^");
7+
}
8+
fn main() {
9+
10+
let args: Vec<String> = env::args().collect();
11+
12+
if args.len() != 4 {
13+
help();
14+
return;
15+
}
16+
17+
let left_txt = &args[1];
18+
let operator = &args[2];
19+
let right_txt = &args[3];
20+
21+
// parse::<f64>() converts txt to floating pt number
22+
let left = match left_txt.parse::<f64>() {
23+
Ok(value) => value,
24+
Err(_) => {
25+
eprintln!("Error: '{}' is not a valid number.", left_txt);
26+
return;
27+
}
28+
};
29+
30+
let right = match right_txt.parse::<f64>(){
31+
Ok(value) => value,
32+
Err(_) =>{
33+
eprintln!("Error: '{}' is not a valid number.", right_txt);
34+
return;
35+
}
36+
};
37+
38+
let result = match operator.as_str() {
39+
"+" => Ok(left + right),
40+
"-" => Ok(left - right),
41+
"*" => Ok(left * right),
42+
"/" => {
43+
if right == 0.0 {
44+
Err("division by zero")
45+
} else {
46+
Ok(left / right)
47+
}
48+
}
49+
"%" => {
50+
if right == 0.0 {
51+
Err("modulo by zero")
52+
} else {
53+
Ok(left % right)
54+
}
55+
}
56+
"^" => Ok(left.powf(right)),
57+
_ => Err("unknown operator"),
58+
};
59+
60+
match result {
61+
Ok(value) => println!("Result: {}", value),
62+
Err("division by zero") => eprintln!("Error: division by zero."),
63+
Err("modulo by zero") => eprintln!("Error: modulo by zero"),
64+
Err("unknown operator") => {
65+
eprintln!("Error: unknown operator '{}'", operator);
66+
help();
67+
}
68+
69+
Err(other) => eprintln!("Error: {}", other),
70+
}
71+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"rustc_fingerprint":12530310647617139397,"outputs":{"17747080675513052775":{"success":true,"status":"","code":0,"stdout":"rustc 1.94.1 (e408947bf 2026-03-25)\nbinary: rustc\ncommit-hash: e408947bfd200af42db322daf0fadfe7e26d3bd1\ncommit-date: 2026-03-25\nhost: x86_64-unknown-linux-gnu\nrelease: 1.94.1\nLLVM version: 21.1.8\n","stderr":""},"7971740275564407648":{"success":true,"status":"","code":0,"stdout":"___\nlib___.rlib\nlib___.so\nlib___.so\nlib___.a\nlib___.so\n/home/hitesh/.rustup/toolchains/stable-x86_64-unknown-linux-gnu\noff\npacked\nunpacked\n___\ndebug_assertions\npanic=\"unwind\"\nproc_macro\ntarget_abi=\"\"\ntarget_arch=\"x86_64\"\ntarget_endian=\"little\"\ntarget_env=\"gnu\"\ntarget_family=\"unix\"\ntarget_feature=\"fxsr\"\ntarget_feature=\"sse\"\ntarget_feature=\"sse2\"\ntarget_has_atomic=\"16\"\ntarget_has_atomic=\"32\"\ntarget_has_atomic=\"64\"\ntarget_has_atomic=\"8\"\ntarget_has_atomic=\"ptr\"\ntarget_os=\"linux\"\ntarget_pointer_width=\"64\"\ntarget_vendor=\"unknown\"\nunix\n","stderr":""},"11857020428658561806":{"success":true,"status":"","code":0,"stdout":"___\nlib___.rlib\nlib___.so\nlib___.so\nlib___.a\nlib___.so\n/home/hitesh/.rustup/toolchains/stable-x86_64-unknown-linux-gnu\noff\npacked\nunpacked\n___\ndebug_assertions\npanic=\"unwind\"\nproc_macro\ntarget_abi=\"\"\ntarget_arch=\"x86_64\"\ntarget_endian=\"little\"\ntarget_env=\"gnu\"\ntarget_family=\"unix\"\ntarget_feature=\"fxsr\"\ntarget_feature=\"sse\"\ntarget_feature=\"sse2\"\ntarget_has_atomic=\"16\"\ntarget_has_atomic=\"32\"\ntarget_has_atomic=\"64\"\ntarget_has_atomic=\"8\"\ntarget_has_atomic=\"ptr\"\ntarget_os=\"linux\"\ntarget_pointer_width=\"64\"\ntarget_vendor=\"unknown\"\nunix\n","stderr":""}},"successes":{}}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Signature: 8a477f597d28d172789f06886806bc55
2+
# This file is a cache directory tag created by cargo.
3+
# For information about cache directory tags see https://bford.info/cachedir/

projects/Calc_cli/target/debug/.cargo-lock

Whitespace-only changes.
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This file has an mtime of when this was started.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
8878bce893697fc7
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"rustc":5391851738765093524,"features":"[]","declared_features":"[]","target":16068974997021304934,"profile":3316208278650011218,"path":4942398508502643691,"deps":[],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/Calc_cli-0947da7c3fc223f1/dep-test-bin-Calc_cli","checksum":false}}],"rustflags":[],"config":8247474407144887393,"compile_kind":0}

0 commit comments

Comments
 (0)