I have to write this code can anyone help?

  1. Implement function from ymd and ymd, which convert between the year/month/day format and integer
    format, which stores days since 0/1/1, i.e., Year 0, Jan 1st. Note that dates before the reference date
    must be supported as well, represented as negative integer values.
  2. Implement traits Debug, Display and Add for Date to enable the code in function main to run. Dates
    in or before year 0 should be displayed using the BC notation of the Gregorian calendar. For Add, the
    expression some date + 30 should return the date 30 days after some date.
    Note: In the Gregorian calendar there is no year 0, and hence non-positive year values are offset by 1.
    That means that 0/1/1 (in the astronomical format used by from ymd and ymd) should be displayed as
    1/1/1 BC, while -1/12/31 should be displayed as 2/12/31 BC.
    You can (and should) implement additional functions as needed. Do not use any external crates. Once
    everything has been implemented, the code in function main should print the following:
    Date { days: 738885 }
    2/12/31 BC
    1/1/30 BC
    1/2/29 BC
    1/3/30 BC
    1/4/29 BC
    1/5/29 BC
    1/6/28 BC
    1/7/28 BC
    1/8/27 BC
    1/9/26 BC
    1/10/26 BC
    1/11/25 BC
    1/12/25 BC
    1/1/24
    1/2/23
    1/3/25
    1/4/24
    1/5/24
    1/6/23
    1/7/23

This looks like school homework. A piece of advice: If you want to have a career in software development, you better not cheat and learn to code.

7 Likes

Ok but please give the answer if you know

As someone who worked for several years as a computer science tutor, I just want to point out something important: people who got others to do their assignments for them (assuming they didn't get ejected from the subject with a zero score and a permanent mark on their academic record) generally almost always bombed the final exam. Might be different elsewhere, but it was also generally policy that you could do badly on assignments and still pass, but could not fail the final exam and pass the subject.

If you are not asking for someone to do your assignment for you, it would help to be more specific about what aspect of the task you are having difficulty with. It is hard to "give an answer" when there is no question.

7 Likes

If you don't know the answer then you didn't need to reply

Chill brother, they are probably trying to help you. Also, if you are beginner like me, you can try throwing the question into one of the Discord servers that support Rust, someone might be willing to go live with you and assist.

Cheers!

It's not that we don't know the answer, it's that there isn't really a question beyond a vague request for help, and noone is going to do your assignment for you. You need to ask specific questions about what you are struggling with, after attempting to complete the assignment for yourself.

If you want more general guidance, then I assume you have been supplied with some learning materials as part of where you got this assignment from, so looking through those may help, but you may also find the Rust book useful for learning. It looks like the first question shouldn't require much beyond the first 5 chapters. The second part asks you to implement some traits on a type. Traits are covered in section 10.2 of the book, and there is even a section on implementing them. The specific traits mentioned are all in the standard library, so they can be found in the standard library documentation.

8 Likes

This is a bit of a red flag that the brief may be in error: The Gregorian calendar didn't exist until 1582; there are multiple methods to extend it backwards prior to this point, and doing so at all is often wrong in context. If I were given this task in a professional context, my first step would be to ask the client a lot of things about where the data comes from and how it will be used, to make sure that I implement what is actually needed instead of just what I was asked for.

3 Likes

I tried bro, i have write this code :

use std::fmt;
use std::ops;

#[derive(Debug, Clone)]
struct Date {
    days: i32,
}

impl Date {
    fn from_ymd(year: i32, month: i32, day: i32) -> Date {
        let mut days = 0;
        for y in 0..year {
            days += if is_leap_year(y) { 366 } else { 365 };
        }
        for m in 1..month {
            days += days_in_month(year, m);
        }
        days += day;
        if year < 0 {
            days = 1;
        }
        Date { days }
    }
    
    
    fn ymd(&self) -> (i32, i32, i32) {
        let mut days = self.days;
        let mut year = 1;
        let bc = year <= 1;
        
        while days > if is_leap_year(year) { 366 } else { 365 } {
            days -= if is_leap_year(year) { 366 } else { 365 };
            year += 1;
        }
        
        let mut month = 1;
        while days > days_in_month(year, month) {
            days -= days_in_month(year, month);
            month += 1;
        }
        
        let actual_year = if bc { year - 1 } else { year };
        
        (actual_year, month, days)
    }

}

impl fmt::Display for Date {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let (year, month, day) = self.ymd();
        // println!("{}",year);
        if year <= 0 {
            write!(f, "{}/{:02}/{:02} BC", year, month, day)
        } else {
            write!(f, "{}/{:02}/{:02}", year, month, day)
        }
    }
}

impl ops::Add<i32> for Date {
    type Output = Date;

    fn add(self, days: i32) -> Date {
        Date { days: self.days + days }
    }
}

fn is_leap_year(year: i32) -> bool {
    (year % 4 == 0 && year % 100 != 0) || year % 400 == 0
}

fn days_in_month(year: i32, month: i32) -> i32 {
    match month {
        1 | 3 | 5 | 7 | 8 | 10 | 12 => 31,
        4 | 6 | 9 | 11 => 30,
        2 => {
            if is_leap_year(year) {
                29
            } else {
                28
            }
        }
        _ => panic!("Invalid month"),
    }
}

fn main() {
    println!("{:?}", Date::from_ymd(2022,12,30));

    let date = Date::from_ymd(-1, 12, 31);
    for i in 0..20 {
        let cloned_date = date.clone(); 
        println!("{}", cloned_date  + i * 30);
    }
}

but output is this:

Date { days: 738885 }
0/01/01 BC
0/01/31 BC
0/03/02 BC
0/04/01 BC
0/05/01 BC
0/05/31 BC
0/06/30 BC
0/07/30 BC
0/08/29 BC
0/09/28 BC
0/10/28 BC
0/11/27 BC
0/12/27 BC
1/01/26
1/02/25
1/03/27
1/04/26
1/05/26
1/06/25
1/07/25 

this is not expected output

1 Like

This loop won't run at all if the year is 0 or less, which it is in main; this is throwing off all of your tested dates.


Also, please refer to the pinned post on how to write code blocks on the forum so that they're readable. In short, you need to enclose the code in backticks

```
like this.
```

ok thankyou, can you provide the logic/hint

You might want to consider scenarios where year is positive and negative separately.

2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.