Thread: programme to finish - C

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    3

    Smile programme to finish - C

    I'm having real problems finishing this programme. It's for a simple calender. Any ideas?


    // cal.c - calendar for 1583 on
    #include <stdio.h>
    #include <string.h>

    FILE *myfile;

    typedef int calendar[20][21];

    int getYear(char []);
    int firstDay(int);
    void setCal(int, calendar);
    void writeHead(int);
    void writeCal(calendar);

    int main(void)
    {
    calendar cal = { {0} };
    char calName[] = " calendar.doc";
    int yr;

    yr = getYear(calName);
    setCal(yr, cal);
    myfile = fopen(calName, "w");
    writeHead(yr);
    writeCal(cal);
    fclose(myfile);
    printf("\nFinished\n");
    return 0;
    }

    int getYear(char calName[])
    {
    char dummy[80];
    int cnt, yr, y;

    do
    {
    printf("year(dddd): ");
    cnt = scanf("%d", &yr);
    gets(dummy);
    if (cnt == 1)
    if (yr > 1582)
    break;
    else
    printf("Year must be > 1582\n");
    else
    printf("Non-numeric data\n");
    } while (1);
    y = yr;
    calName[3] = y % 10 + '0';
    y /= 10;
    calName[2] = y % 10 + '0';
    y /= 10;
    calName[1] = y % 10 + '0';
    calName[0] = y / 10 + '0';
    return yr;
    }

    int firstDay(int yr) // using Zeller's congruence
    {
    int a, b, c, d, dd = 1, m, mm = 1;

    if (mm < 3)
    yr--;
    m = (mm + 9) % 12 + 1;
    a = (int)(2.6 * m - 0.2);
    b = yr % 100;
    c = yr / 100;
    d = (a + dd + b + b / 4 + c / 4 - 2 * c) % 7;
    if (d < 0)
    d += 7;
    return d;
    }

    void setCal(int yr, calendar cal) // prepare calendar for year yr
    {
    int mondays[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    int day, month, row, col, startCol, startRow;

    // you are to complete this
    }

    void writeHead(int yr)
    {
    int i, spaces, width, y1, y2, y3, y4;
    char *num[10][8] = {
    {" @@@ ", "@ @ ", "@ @ ", "@ @ ", "@ @ ", "@ @ ", " @@@ ", "======"},
    {" @ " , "@@ " , " @ " , " @ " , " @ " , " @ " , "@@@ " , "====" },
    {" @@@ ", "@ @ ", " @ ", " @ ", " @ ", " @ ", "@@@@@ ", "======"},
    {" @@@ ", "@ @ ", " @ ", " @@@ ", " @ ", "@ @ ", " @@@ ", "======"},
    {" @ ", " @@ ", " @ @ ", "@ @ ", "@@@@@ ", " @ ", " @ ", "======"},
    {"@@@@@ ", "@ ", "@ ", "@@@@ ", " @ ", "@ @ ", " @@@ ", "======"},
    {" @@@ ", "@ ", "@ ", "@@@@ ", "@ @ ", "@ @ ", " @@@ ", "======"},
    {"@@@@@ ", " @ ", " @ ", " @ ", " @ ", "@ ", "@ ", "======"},
    {" @@@ ", "@ @ ", "@ @ ", " @@@ ", "@ @ ", "@ @ ", " @@@ ", "======"},
    {" @@@ ", "@ @ ", "@ @ ", " @@@@ ", " @ ", " @ ", " @@@ ", "======"}};

    y4 = yr % 10;
    yr /= 10;
    y3 = yr % 10;
    yr /= 10;
    y2 = yr % 10;
    y1 = yr / 10;
    spaces = (int)(strlen(num[y1][0]) + strlen(num[y2][0])
    + strlen(num[y3][0]) + strlen(num[y4][0]));
    spaces = 3 + (70 - spaces) / 2;
    width = (int)strlen(num[y4][0]) - 1;
    for (i = 0; i < 8; i++)
    fprintf(myfile, "%*c%s%s%s%.*s\n", spaces, ' ', num[y1][i], num[y2][i],
    num[y3][i], width, num[y4][i]);
    }

    void writeCal(calendar cal)
    {
    char *head[] = {"JANUARY FEBRUARY MARCH",
    "APRIL MAY JUNE",
    "JULY AUGUST SEPTEMBER",
    "OCTOBER NOVEMBER DECEMBER"};
    char days[] = " S M T W Th F S S M T W Th F S S M T W Th F S";
    int i, row, col;

    // you are to complete this
    }

  2. #2
    Unregistered
    Guest
    First of all you might want to try using the forum code tags so we can actually read your code. Without them all indentation is lost making it near impossible to read your code.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Where did these comments come from (I mean who put them there):
    >// you are to complete this
    Was it your teacher?

    >gets(dummy);
    Loose gets(), it's bad. Do a board search to find out why.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Akilla
    Guest

    Exclamation Here's my version

    Here's my version.

    Don't copy it!!!!!

    Learn from it if there's anything
    you can. (Although this is not a very efficient program to learn from as there are so many if statements)

    It also write the calendar to a file if you want.
    PHP Code:
    #include <stdio.h>
    #include <iostream.h>
    #include <conio.h>

    int leapyear(int year)
    {
        if ( (
    year%4==0) && (year%400==0) )
        {
            return 
    1;
        }

        if ( (
    year%4==0) && (year%100!=0) )
        {
            return 
    1;
        }
        
        return 
    0;
    }


    void getweekday(int monthint dayint yearint *week)
    {
        
    int centurycodemonthcode[13], CD;

        
    monthcode[0]=1;
        
    monthcode[1]=1;
        
    monthcode[2]=4;
        
    monthcode[3]=4;
        
    monthcode[4]=0;
        
    monthcode[5]=2;
        
    monthcode[6]=5;
        
    monthcode[7]=0;
        
    monthcode[8]=3;
        
    monthcode[9]=6;
        
    monthcode[10]=1;
        
    monthcode[11]=4;
        
    monthcode[12]=6;

        
    C=year/100;
        
    D=year%100;


        if (
    C==17)
        {
         
    centurycode=4;
        }
        if (
    C==18)
        {
         
    centurycode=2;
        }
        if (
    C==19)
        {
         
    centurycode=0;
        }
        if (
    C==20)
        {
         
    centurycode=6;
        }

        *
    week = (centurycode + (D/D)%+monthcode[month] + day)%7;


    }

    void drawcalendar(int daysint firstday)
    {

        
    int idaythen;
        
    char space ' ';

        if (
    firstday == 0)
        {
            
    firstday 7;
        }

        
    daythen=firstday;
        
    printf("SUN MON TUE WED THU FRI SAT\n");

        for (
    i=1i<firstdayi++)
        {
            
    printf("%3c "space);
        }

        for (
    i=1i<=daysi++)
        {
            
    printf("%3d "i);
            
    daythen++;

            if (
    daythen>&& != days)
            {
                
    daythen=1;
                
    printf("\n");
                
            }

        }
        
        
    printf("\n\n\n");
    }

    void writecalendar(int daysint firstdayFILE *fp)
    {

        
    int idaythen;
        
    char space ' ';

        if (
    firstday == 0)
        {
            
    firstday 7;
        }

        
    daythen=firstday;
        
    fprintf(fp"SUN MON TUE WED THU FRI SAT\n");

        for (
    i=1i<firstdayi++)
        {
            
    fprintf(fp"%3c "space);
        }

        for (
    i=1i<=daysi++)
        {
            
    fprintf(fp"%3d "i);
            
    daythen++;

            if (
    daythen>&& != days)
            {
                
    daythen=1;
                
    fprintf(fp"\n");
                
            }

        }
        
        
    fprintf(fp"\n\n\n");
    }

    void getdays(int yeartypeint monthint *days)
    {
        if (
    yeartype == && month == 2)
        {
            *
    days 28;
        }
        
        if (
    yeartype == && month == 2)
        {
            *
    days 29;
        }
        
        if ( (
    month==4) || (month==6) || (month==9) || (month==11) )
        {
            *
    days 30;
        }

        if ( (
    month==1) || (month==3) || (month==5) || (month == 7) || (month==8) || (month==10) || (month==12) )
        {
            *
    days 31;
        }
    }

    void writefile(int monthint yearint daysint firstdayint yeartype)
    {
        
    FILE *fp;
        
    fp fopen("CALENDAR.TXT""w");
        for (
    month =1month <=12month++)
        {
            
    getweekday(month1year, &firstday);
            
    getdays(yeartypemonth, &days);

            if (
    month == 1)
            {
                
    fprintf(fp"January %d\n\n"year);
            }
            else if (
    month ==2)
            {
                
    fprintf(fp"February %d\n\n"year);
            }
            else if (
    month ==3)
            {
                
    fprintf(fp"March %d\n\n"year);
            }
            else if (
    month ==4)
            {
                
    fprintf(fp"April %d\n\n"year);
            }
            else if (
    month ==5)
            {
                
    fprintf(fp"May %d\n\n"year);
            }
            else if (
    month ==6)
            {
                
    fprintf(fp"June %d\n\n"year);
            }
            else if (
    month ==7)
            {
                
    fprintf(fp"July %d\n\n"year);
            }
            else if (
    month ==8)
            {
                
    fprintf(fp"August %d\n\n"year);
            }
            else if (
    month ==9)
            {
                
    fprintf(fp"September %d\n\n"year);
            }
            else if (
    month ==10)
            {
                
    fprintf(fp"October %d\n\n"year);
            }
            else if (
    month ==11)
            {
                
    fprintf(fp"November %d\n\n"year);
            }
            else if (
    month ==12)
            {
                
    fprintf(fp"December %d\n\n"year);
            }
            
    writecalendar(daysfirstdayfp);
        }
        
    fprintf(fp"Program written by Maharshi Akilla\n");
        
    fprintf(fp"http://mahurshi.tripod.com");
        
    fclose(fp);
    }

    main()
    {
        
    int monthyeardaysfirstdayyeartype;
        
    char answer;

        
    cout << "Calendar" << endl << "Generates the calendar for any year between 1700 and 2099" << endl << endl;
        do
        {
            
    cout << "Enter the year: ";
            
    cin >> year;
        } while (
    year<1700 || year>2099);

        
    clrscr();

        
    yeartype leapyear(year);

        for (
    month =1month <=12month++)
        {
            
    getweekday(month1year, &firstday);
            
    getdays(yeartypemonth, &days);

            if (
    month == 1)
            {
                
    cout << "January " << year << endl << endl;
            }
            else if (
    month ==2)
            {
                
    cout << "February " << year << endl << endl;
            }
            else if (
    month ==3)
            {
                
    cout << "March " << year << endl << endl;
            }
            else if (
    month ==4)
            {
                
    cout << "April " << year << endl << endl;
            }
            else if (
    month ==5)
            {
                
    cout << "May " << year << endl << endl;
            }
            else if (
    month ==6)
            {
                
    cout << "June " << year << endl << endl;
            }
            else if (
    month ==7)
            {
                
    cout << "July " << year << endl << endl;
            }
            else if (
    month ==8)
            {
                
    cout << "August " << year << endl << endl;
            }
            else if (
    month ==9)
            {
                
    cout << "September " << year << endl << endl;
            }
            else if (
    month ==10)
            {
                
    cout << "October " << year << endl << endl;
            }
            else if (
    month ==11)
            {
                
    cout << "November " << year << endl << endl;
            }
            else if (
    month ==12)
            {
                
    cout << "December " << year << endl << endl;
            }
            
    drawcalendar(daysfirstday);
        }

        do
        {
            
    cout << "Do you want to write to a file? (Y/N)" ;
            
    cin >> answer;
        } while (
    answer != 'y' && answer != 'Y' && answer != 'n' && answer != 'N');

        if (
    answer == 'y' || answer == 'Y')
        {
            
    writefile(monthyeardaysfirstdayyeartype);
            
    cout << endl << "A file 'CALENDAR.TXT' has been created which contains the calendar" << endl;
        }

        
    cout << endl << "Program written by Maharshi Akilla" << endl;
        
    cout << "http://mahurshi.tripod.com" << endl;
        while (!
    kbhit())
        {
        }


    GOOD LUCK!

    Feedback appreciated. (Tell me if the program sucks :-) )

    COOL PROGRAMS @ www.akilla.tk

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: Here's my version

    Originally posted by Akilla
    Here's my version.

    Don't copy it!!!!!

    Learn from it if there's anything
    you can. (Although this is not a very efficient program to learn from as there are so many if statements)

    It also write the calendar to a file if you want.
    <snip code>
    That's nice mix of C and C++ ! I think you should decide which one you are going to use, and stick to it.... try not to mix C and C++ in same source file.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Akilla
    Guest

    yeah, true :-)

    Yeah, you're right

    but the reason why I used cout was because
    I was too lazy to type printf syntax :-)

    The reason why I used printf was because I didn't
    know how to use this %3d type printing on the
    screen using cout :-)

    got it ?

    COOL PROGRAMS @ www.akilla.tk

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Yep, I got it alright!

    Now this is definately off-topic, but as you mentioned it, here's a way of changing the output modifiers for cout:
    >#include <iomanip>
    >cout <<setw(3) <<setfill('0') <<3;
    will output
    >003

    Now enough of the C++ If you want some more advice, just ask in the C++ forum.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    TK
    Guest
    program to finish - C
    I also think that this is a good idea because vendors are all using C and C++ in order to build their solutions, and than they turn around and tell us to use some prefabricated language that they set up for our own solutions. If C or C++ was reversible than their plans would be foiled and the truth would be revealed.

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Here's my version.
    Try to keep things standard so that everyone can run your code.

    >I'm having real problems finishing this programme.
    What have you tried and why doesn't it work? Writing the calendar is simple enough, just print out the values in the cal array in some aesthetically pleasing manner. setCal is a big part of this program and I wonder why you got so far without at least setting up enough of a placeholder to have something print out. The way I see it, you can simply loop through the cal array and place the values for each day until you reach the end of the month, then start over at 1. Though since I don't have your exact requirements for the program I can't be sure of what you want.

    >// you are to complete this
    I don't particularly like this if it's a message to us. If it's a message to you then something tells me someone else wrote the rest and gave you this as an assignment. Which means you may not have tried anything yet.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Closing a programme with cin.get
    By Dontgiveup in forum C++ Programming
    Replies: 2
    Last Post: 03-14-2009, 02:35 PM
  2. How to view the programme before it disappears
    By yousuf in forum C Programming
    Replies: 2
    Last Post: 03-22-2008, 08:12 AM
  3. Replies: 3
    Last Post: 05-13-2007, 08:55 AM
  4. Gui Programme does'nt Compiles from DOS
    By Shadowhunt in forum C++ Programming
    Replies: 1
    Last Post: 06-06-2003, 08:05 AM
  5. Simple C++ GUI programme does'nt run from dos
    By Shadowhunt in forum C++ Programming
    Replies: 4
    Last Post: 05-31-2003, 05:30 AM