Skip to content
Snippets Groups Projects
Commit 2bb49c3b authored by leo.muff's avatar leo.muff
Browse files

first commit

parents
No related branches found
No related tags found
No related merge requests found
/target
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "getrandom"
version = "0.2.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "be4136b2a15dd319360be1c07d9933517ccf0be8f16bf62a3bee4f0d618df427"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]]
name = "libc"
version = "0.2.147"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b4668fb0ea861c1df094127ac5f1da3409a82116a4ba74fca2e58ef927159bb3"
[[package]]
name = "ppv-lite86"
version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de"
[[package]]
name = "rand"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
dependencies = [
"libc",
"rand_chacha",
"rand_core",
]
[[package]]
name = "rand_chacha"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
dependencies = [
"ppv-lite86",
"rand_core",
]
[[package]]
name = "rand_core"
version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
dependencies = [
"getrandom",
]
[[package]]
name = "stack"
version = "0.1.0"
dependencies = [
"rand",
]
[[package]]
name = "wasi"
version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
[package]
name = "stack"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
rand = "0.8.5"
pub const STACK_SIZE: usize = 10;
pub mod stack;
\ No newline at end of file
use stack::stack::Stack;
fn main() {
let mut st:Stack<u32> = Stack::new();
match st.pop(){
Ok(_) => println!("valeur enlevée du sommet, sommet = {:?}", st.top),
Err(e) => println!("Error, cant pop value from stack : {}", e)
}
let values = [1,2,3,4,5,6,7,8,9,10,11];
for val in values {
match st.push(val){
Ok(_) => println!("Haut de la pile : {:?} - taille = {}", st.top, st.size),
Err(e) => println!("Error, cant push value on stack : {}", e)
}
}
println!("haut de la pile {:?}", st.top);
for _ in 0..3{
match st.pop(){
Ok(_) => println!("valeur enlevée du sommet, sommet = {:?}", st.top),
Err(e) => println!("Error, cant pop value from stack : {}", e)
}
}
let mut st:Stack<&str> = Stack::new();
match st.pop(){
Ok(_) => println!("valeur enlevée du sommet, sommet = {:?}", st.top),
Err(e) => println!("Error, cant pop value from stack : {}", e)
}
let values = ["un","deux","trois"];
for val in values {
match st.push(val){
Ok(_) => println!("Haut de la pile : {:?} - taille = {}", st.top, st.size),
Err(e) => println!("Error, cant push value on stack : {}", e)
}
}
println!("haut de la pile {:?}", st.top);
for _ in 0..3{
match st.pop(){
Ok(_) => println!("valeur enlevée du sommet, sommet = {:?}", st.top),
Err(e) => println!("Error, cant pop value from stack : {}", e)
}
}
}
use std::fmt;
use crate::STACK_SIZE;
#[derive(Debug, Clone)]
pub struct Stack<T>{
pub size: usize,
data: [Option<T>;STACK_SIZE],
pub top : Option<T>
}
#[derive(Debug, Clone)]
pub enum StackError{
Empty,
Full
}
impl fmt::Display for StackError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self{
Self::Empty => write!(f, "Removing value from empty stack"),
Self::Full => write!(f, "Pushing value from full stack")
}
}
}
impl<T> Stack<T>
where T: Copy, {
pub fn new() -> Stack<T> {
Stack{
data : core::array::from_fn(|_| None),
size : 0,
top : None
}
}
pub fn is_empty(&self) -> bool {
if &self.size == &0 {
true
}
else {
false
}
}
pub fn is_full(&self) -> bool {
if &self.size == &10 {
true
}
else {
false
}
}
pub fn push(&mut self, val:T) -> Result<(),StackError> {
if self.is_full() {
Err(StackError::Full)
}
else{
self.data[self.size] = Some(val);
self.size += 1;
self.top = Some(val);
Ok(())
}
}
pub fn pop(&mut self) -> Result<(),StackError> {
if self.is_empty() {
Err(StackError::Empty)
}
else{
if self.size == 1 {
self.top = None;
}
else{
self.top = self.data[self.size-2];
}
self.data[self.size-1] = None;
self.size -= 1;
Ok(())
}
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment