Thread: HELP with starting out.

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    51

    Question HELP with starting out.

    Im really new at c, and i have an assignment that i have no idea of where to even start. I know the very basics of c, but i need a push in the right direction of where to even start with writing this program. If anyone can give me some example code that i can learn from, or can describe step by step in terms that a two year old would understand the steps that i would go through to write this proggy. Any help would be fantastic... I thank you in advance.

    Here is the whole task:

    My task is to write a C program calen that displays calendar details about specified dates. The program reads commands from standard input that specify what calendar information to display. Commands can be of several forms:

    If the command is of the form "d dd mm yyyy", where dd, mm, and yyyy are integers representing the day, month, and year, the program prints a line reporting the weekday for the given date:
    d 1 1 2000
    1 January 2000 is a Saturday
    If the year, month, or day are omitted, the program substitutes information for the current date. Thus (assuming the current date is 24 August 2002):
    d 31 12
    31 December 2002 is a Tuesday

    d 15
    15 August 2002 is a Thursday

    d
    24 August 2002 is a Saturday

    If the command is of the form "m mm yyyy", the program
    prints a calendar for the given month:

    m 12 1999
    December 1999
    S M Tu W Th F S
    1 2 3 4
    5 6 7 8 9 10 11
    12 13 14 15 16 17 18
    19 20 21 22 23 24 25
    26 27 28 29 30 31


    Note that the display consists of 6 "week" lines, even if (as in
    this example), the final week (or 2 weeks for February in some
    years) is entirely blank. If the year or month are omitted, the
    program uses information for the current date instead.

    If the command is of the form "y yyyy", the program
    prints a calendar for the entire given year:

    y 1998
    1998

    Jan Feb Mar
    S M Tu W Th F S S M Tu W Th F S S M Tu W Th F S
    1 2 3 1 2 3 4 5 6 7 1 2 3 4 5 6 7
    4 5 6 7 8 9 10 8 9 10 11 12 13 14 8 9 10 11 12 13 14
    11 12 13 14 15 16 17 15 16 17 18 19 20 21 15 16 17 18 19 20 21
    18 19 20 21 22 23 24 22 23 24 25 26 27 28 22 23 24 25 26 27 28
    25 26 27 28 29 30 31 29 30 31

    Apr May Jun
    S M Tu W Th F S S M Tu W Th F S S M Tu W Th F S

    The months are arranged in 4 rows (with the first row displaying
    calendars for Jan, Feb, and Mar), and the months in a row
    separated by 3 blank spaces. There should be no blank line
    between rows unless (as here) the last week of all months happens
    to be blank. Note the alignment of the day-of-the week letters,
    the month names, and the year. If the year is omitted, the
    current year is used instead.

    If the command begins with a digit, then "day" format is
    assumed. Thus 26 1 2000 is equivalent to d 26 1
    2000.

    If the command begins with any other character, the program
    prints an error message:

    f 24 8 2002
    Unknown command: f

  2. #2
    Registered User
    Join Date
    Aug 2002
    Posts
    51

    by the way

    By the way, i have done a search for calendar, but the results that i found didnt really help me. All of them are very different to what i want to do, so please dont just tell me to do a search!!!!

  3. #3
    Registered User
    Join Date
    Aug 2002
    Posts
    51

    How do i get it to say what day it will be on that date?

    Thank you so much for that code, it has helped me heaps. How do i get the program to say if i input d 23 11 1981, how do i get the program to output what day it was on that date. I have a file thats meant to be able to do it, but how do i use it within this program?

    There is also a date.h file but i wont include it here...

    Any more help on this would be great.

  4. #4
    Registered User
    Join Date
    Aug 2002
    Posts
    51

    please help me get this working

    I need this code to do the task below. Can anyone help me get it doing that? thanx in advance.

    TASK:

    write a C program calen that displays calendar details about specified dates. The program reads commands from standard input that specify what calendar information to display. Commands can be of several forms:

    If the command is of the form "d dd mm yyyy", where dd, mm, and yyyy are integers representing the day, month, and year, the program prints a line reporting the weekday for the given date:
    d 1 1 2000
    1 January 2000 is a Saturday
    If the year, month, or day are omitted, the program substitutes information for the current date. Thus (assuming the current date is 24 August 2002):
    d 31 12
    31 December 2002 is a Tuesday

    d 15
    15 August 2002 is a Thursday

    d
    24 August 2002 is a Saturday

    If the command is of the form "m mm yyyy", the program
    prints a calendar for the given month:

    m 12 1999
    December 1999
    S M Tu W Th F S
    1 2 3 4
    5 6 7 8 9 10 11
    12 13 14 15 16 17 18
    19 20 21 22 23 24 25
    26 27 28 29 30 31


    Note that the display consists of 6 "week" lines, even if (as in
    this example), the final week (or 2 weeks for February in some
    years) is entirely blank. If the year or month are omitted, the
    program uses information for the current date instead.

    If the command is of the form "y yyyy", the program
    prints a calendar for the entire given year:

    y 1998
    1998

    Jan Feb Mar
    S M Tu W Th F S S M Tu W Th F S S M Tu W Th F S
    1 2 3 1 2 3 4 5 6 7 1 2 3 4 5 6 7
    4 5 6 7 8 9 10 8 9 10 11 12 13 14 8 9 10 11 12 13 14
    11 12 13 14 15 16 17 15 16 17 18 19 20 21 15 16 17 18 19 20 21
    18 19 20 21 22 23 24 22 23 24 25 26 27 28 22 23 24 25 26 27 28
    25 26 27 28 29 30 31 29 30 31

    Apr May Jun
    S M Tu W Th F S S M Tu W Th F S S M Tu W Th F S

    The months are arranged in 4 rows (with the first row displaying
    calendars for Jan, Feb, and Mar), and the months in a row
    separated by 3 blank spaces. There should be no blank line
    between rows unless (as here) the last week of all months happens
    to be blank. Note the alignment of the day-of-the week letters,
    the month names, and the year. If the year is omitted, the
    current year is used instead.

    If the command begins with a digit, then "day" format is
    assumed. Thus 26 1 2000 is equivalent to d 26 1
    2000.

    If the command begins with any other character, the program
    prints an error message:

    f 24 8 2002
    Unknown command: f

    _______________________________________________

    CODE:

    #include <sys/time.h>
    #include <time.h>
    #include "date.h"
    #include <stdio.h>

    /* true for leap years */


    int leap_year (int year) {

    return year % 400 == 0 || year % 100 != 0 && year % 4 == 0;
    }

    /* the number of days in month of year */


    int days_in_month (int month, int year) {

    switch (month) {
    case 9: case 4: case 6: case 11:
    return 30;
    case 2:
    return leap_year(year) ? 29 : 28;
    default:
    return 31;
    }
    }

    /* the weekday (0 = Sunday) of a given date
    accurate for dates in the Gregorian calendar
    which began on 14 September 1752 */


    int week_day (int day, int month, int year) {

    int d = 14; /* The modern calendar began on 14 ... */
    int m = 9; /* ... September ... */
    int y = 1752; /* ... 1752 ... */
    int n = 4; /* ... which was a Thursday */

    while (year > y || month > m || day > d) {
    n++;
    day--;
    if (day < 1) { month--; day = days_in_month(month, year); }
    if (month < 1) { year--; month = 12; }
    }
    return n % 7;
    }

    struct timeval tv;


    int current_day () {

    gettimeofday(&tv, 0);
    return localtime(&tv.tv_sec)->tm_mday;
    }


    int current_month () {

    gettimeofday(&tv, 0);
    return localtime(&tv.tv_sec)->tm_mon + 1;
    }


    int current_year () {

    gettimeofday(&tv, 0);
    return localtime(&tv.tv_sec)->tm_year + 1900;
    }



    void do_day ( char *cmd ) {


    int day, month, year;
    int res;
    res = sscanf( cmd, "%*c %d %d %d", &day, &month, &year );
    if ( res == 3 ) {
    printf( "Calculate for day=%d, month=%d, year=%d\n", day, month, year );
    }
    else {
    printf( "Bad day command parameters\n" );
    }
    }


    void do_month ( char *cmd ) {


    int month, year;
    int res;
    res = sscanf( cmd, "%*c %d %d", &month, &year );
    if ( res == 2 ) {
    printf( "Calculate for month=%d, year=%d\n", month, year );
    }
    else {
    printf( "Bad month command parameters\n" );
    }}



    void do_year ( char *cmd ) {


    int year;
    int res;
    res = sscanf( cmd, "%*c %d", &year );
    if ( res == 3 ) {
    printf( "Calculate for year=%d\n", year );
    }
    else {
    printf( "Bad year command parameters\n" );
    }
    }



    int first_day_of_month ( int year, int month ) {


    struct tm t = { 0 };
    struct tm *tp;
    time_t now;
    t.tm_mday = 1;
    t.tm_mon = month-1;
    t.tm_year = year-1900;
    now = mktime(&t);
    tp = localtime(&now);
    return tp->tm_wday;
    }



    int main ( ) {


    char buff[BUFSIZ];
    while ( fgets( buff, BUFSIZ, stdin ) != NULL ) {
    switch ( buff[0] ) {
    case 'd':
    do_day ( buff );
    break;
    case 'm':
    do_month ( buff );
    break;
    case 'y': do_year ( buff );
    break;
    default:
    printf( "Unknown command %c\n", buff[0] );
    break;
    }
    }
    {
    int i;
    for ( i = 1 ; i <= 12 ; i++ ) {
    int day = first_day_of_month( 2002, i );
    printf( "First day of %d/%d is %d\n", 2002, i, day );
    }
    }
    return 0;
    }

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Please use code tags when posting code.

    Like this:
    [code]

    /* Your code here */

    [/code]
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Starting programs
    By Molokai in forum C Programming
    Replies: 1
    Last Post: 04-16-2009, 10:10 AM
  2. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  3. Starting out
    By VOX in forum Game Programming
    Replies: 5
    Last Post: 12-04-2004, 09:47 AM
  4. i am not able to figure ot the starting point of this
    By youngashish in forum C++ Programming
    Replies: 7
    Last Post: 10-07-2004, 02:41 AM
  5. question about reading inputs starting with 0
    By jibbles in forum C Programming
    Replies: 8
    Last Post: 08-09-2004, 03:27 AM