Thread: Help Me Out!! Pls

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    70

    Unhappy Help Me Out!! Pls

    hi,Great programmer.

    i have a question that i dont understand and i don't know how to start to code it.i never had such a headache on this question.
    can someone out there who is good in c++ can help me with this out with some example and explanation?Thanks...
    as i need to finish this question....before monday!!
    thanks...guys ))

    Question:
    You have been assigned to develop an abstract data type called date.

    The member functions of the date class are:

    dow():This function returns the day of the week as a number.
    cdow():This function returns the day of the week as a string.
    day():This function returns the day of that date.

    how am i going to do this question?pls give me some example coding and explanation.Thanks.... URGENT ((

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    154
    What part don't you understand? Not being sarcastic, but is the problem you don't know how to create a class, or you can't figure out an algorithm for the functions, or something else?
    If you're not sure how to create a class, check out some tutorials a/o textbooks, then post your code if you have problems with that.
    There are probably more than a few ways to write an algorithm for the functions.

  3. #3
    Unregistered
    Guest
    look in time.h for information on struct tm, type time_t. The member variables for the struct may contain many of the items you are looking for. In BCB the TDateTime() class also has many of these features.

    IF you need to implement these functions on your own you can do something like this. Determine a stat date with a known day of the week. For example, we know that Oct 26, 2001 is a Friday, which would be day number 6 if Sunday is day number 1 or number 5 if Monday is day number 1, or day number 5 if Sunday is day number 0, etc. Now take a given date other than the reference date. Determine the number of days between the given date and reference date. Divide the number of days between the two by 7 using the modulus operator. The result can be correlated with day number and from that you can covert it to day name using an enum, a switch, an array, whatever.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    70

    Not ENough!!!

    hi,dear member in there
    i think the explanation that u given me is not enough as i really blur and not understand totally what u meant? can u someone pls give me some example coding or good explanation on that?
    pls........

  5. #5
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    We really need to know what special part of the program you have difficulties with. You need to post some code in the begining, so we can see what you know and how we can help.

    You have been assigned to develop an abstract data type called date.

    The member functions of the date class are:

    dow():This function returns the day of the week as a number.
    cdow():This function returns the day of the week as a string.
    day():This function returns the day of that date

    class date
    {

    };

    This creates the data type ( a class in this example ) named date.
    You can now have date as a type just like int or char.
    However, at the moment, you can't do anything with a date.
    We need some methods. And data.

    class date
    {
    private:
    int day;
    int month;
    int year;

    };

    Now your date can have values. Note that all values are private, so at the moment, you can't access any of these. When you create a date, you need to set them. Therefore, we define a constructor. If you don't know what a constructor is, consult the instance that gave you the assignment, this are the very basics:

    class date
    {
    private:
    int day;
    int month;
    int year;
    public:
    date( int newDay, int newMonth, int newYear );
    };

    You will have to add some code to the method of the constructor, so your variables inside date will get the values that you give in the constructor call.

    Now, you could do something like this:

    int main()
    {
    date d( 27, 10, 2001 );

    return 0;
    }

    You have a date, but can't do anything with it. So we add the functions that were required by your assignment:

    class date
    {
    private:
    int day;
    int month;
    int year;
    public:
    date( int newDay, int newMonth, int newYear );
    int dow();
    const char* cdow();
    int day();
    };

    Now you have all that is required, you will just have to fill in the code to make it work. You will probably need and text array of size 12 to make it work, and you need to save one date you know the weekday of, so you can calculate how many days passed and which day of the week we have now.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  6. #6
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    here is some code i think you will find helpful. You shouldn't use it as is but as a basis to learn from.....
    Code:
    date.h
    
    //Inclusion guard
    
    #ifndef DATE_H
    #define DATE_H
    
    class Date
    	
    	{
    		private:
    					int Day;
    					int Month;
    					int Year;
    
    					// private utility function for class
    					
    					int CheckDay(int)const; // checks day is legal for month and year
    
    		public:
    					Date(int day=1,int month=1,int year=2001) // defaults to 1/1/2001
    
    						{
    							SetYear(year);
    							SetMonth(month);
    							SetDay(day);					
    						}
    
    					~Date()	{} // do nothing destructor as nothing to clean up.
    
    					int GetDay()const	{ return Day;}
    					int GetMonth()const	{ return Month;}
    					int GetYear()const	{ return Year;}
    
    					void SetDay(int); // validates day
    					void SetMonth(int); // validates month
    					void SetYear(int); // validates year
    
    					bool IsLeapYear()const; // determines if Year is a leap year
    					int HowManyDays()const; // returns number of days in Month
    
    	}; //end of class declaration
    
    
    #endif
    Code:
    date.cpp
    
    //library includes
    
    #include "date.h"
    
    // Class function definitions
    
    bool Date::IsLeapYear()const
    
    	{
    		if ((Year % 400 == 0) || ((Year % 4 == 0) && (Year % 100 != 0)))
    
    			{
    				return true; // its a leap year
    			}
    	
    		else
    
    			{
    				return false; // its not a leap year
    			}
    
    	}
    
    
    int Date::HowManyDays()const
    
    	{
    		if ((Month==2) && IsLeapYear())
    			
    			{
    				return 29; // feb has 29 days in a leap year
    			}
    
    		static const int DaysInMonth[12]={31,28,31,30,31,30,31,31,30,31,30,31};
    		return DaysInMonth[Month-1];
    
    	}
    
    
    int Date::CheckDay(int TestDay)const
    	
    	{
    
    		if ((TestDay > 0) && (TestDay <= HowManyDays() ))
    
    			{
    				return TestDay; // day is valid for month
    			}
    
    		return 1; // if not then set to default value of 1
    
    	}
    
    void Date::SetDay(int TestDay)
    
    	{
    
    		Day=CheckDay(TestDay);
    
    	}
    
    
    void Date::SetMonth(int TestMonth)
    
    	{
    
    		if (TestMonth >= 1 && TestMonth <= 12)
    
    			{
    				Month=TestMonth;
    			}
    
    		else
    
    			{
    				Month=1; // default value
    			}
    	
    	}
    
    
    void Date::SetYear(int TestYear)
    
    	{
    
    		if (TestYear > 1582) // Gregorian calender accepted in late 1582
    
    			{
    				Year=TestYear;
    			}
    
    		else
    
    			{
    				Year=2001; // default value
    			}
    	
    	}
    Code:
    CalenderADT.h
    
    // inclusion guard
    #ifndef CALENDER_ADT_H
    #define CALENDER_ADT_H
    
    // includes
    
    #include <cmath>
    #include "date.h"
    
    class CalenderADT  // calender abstract data type
    
    	{
    		protected:
    					Date TheDate;
    					
    					int WhatDay(int)const;
    
    		public:
    					CalenderADT(Date TestDate) : TheDate(TestDate) {}
    					virtual ~CalenderADT() {}
    					
    					virtual void Draw()const = 0;
    
    	};
    
    #endif
    Code:
    CalenderADT.cpp
    
    #include "CalenderADT.h"
    
    //////////////////////////////////////////////////////////////////////////////////////////////
    // WhatDay function uses Zellers algorithm to return the day of the week for the given date.
    // returns 0 to 6 corresponding to sunday to saturday.
    // only valid for the gregorian calender
    //
    //////////////////////////////////////////////////////////////////////////////////////////////
    // details of algorithm are :-
    //
    // [x] = floor (x)
    //
    // f=k+[((13*m)-1)/5]+d+[d/4]+[c/4]-(2*c)
    //
    // where k=day of the month
    //       m=month number march=1 jan=11 feb=12 etc.
    //       d=last two digits of year.... if m=1 or m=2 d=d-1 (jan,feb treated as previous year)
    //       c=first two digits of year i.e. number of centuries.
    //
    // once we have f we divide by 7 and take remainder.If negative add 7. remainder = day of the week
    // 0=sunday 1=monday 6=saturday etc.
    //////////////////////////////////////////////////////////////////////////////////////////////
    
    
    int CalenderADT::WhatDay(int TestDay)const
    	
    	{
    
    		int c=TheDate.GetYear() / 100; // # of centuries
    		int d=TheDate.GetYear() % 100; // # of years through century
    		int m=(TheDate.GetMonth() + 10) % 12; // # of month march is 1,feb is 12
    		int k=TestDay; // day part
    		if ((TheDate.GetMonth() == 1) || (TheDate.GetMonth() == 2 )) // treat jan and feb as if they were in previous year
    			{
    				if (d == 0) // if d is 0 then to go back a year d becomes 99 and c become c-1
    				
    					{
    						d=99;
    						c -= 1;
    					}
    
    				else
    
    					{
    						d -= 1; // jan and feb are treated as previous year
    					}
    			}
    			
    		double g=(k + (floor(((13 * m) - 1) / 5)) + d + (floor(d / 4)) + (floor(c / 4)) - (2 * c));
    		int f=static_cast<int>(g) % 7; // cast result of algorithm to int to take modulus
    		if (f < 0) // if negative add 7
    
    			{
    				f += 7;
    			}
    		return f; // returns 0 to 6 corresponding to sunday to saturday
    
    	}
    Code:
    Calender.h
    
    #ifndef CALENDER_H
    #define CALENDER_H
    
    // includes
    
    #include "CalenderADT.h"
    
    class Calender:public CalenderADT  // console calender class
    
    	{
    		public:
    					Calender(Date TestDate) : CalenderADT(TestDate) {}
    					virtual ~Calender() {}
    
    					virtual void Draw()const; // draws the calender
    
    	};
    
    
    #endif
    Code:
    Calender.cpp
    
    // library includes
    
    #include <windows.h>
    #include <iostream>
    #include <iomanip>
    #include "Calender.h"
    #include "ScreenFuncs.h"
    
    using namespace std;
    
    // class function definitions
    
    void Calender::Draw()const
    
    	{
    	clrscr();
    	cout<<"Date entered was :- "<<TheDate.GetDay()<<"/"<<TheDate.GetMonth()<<"/"<<TheDate.GetYear()<<endl;
    	cout<<endl<<setw(8)<<"SUNDAY"<<setw(8)<<"MONDAY"<<setw(9)<<"TUESDAY"<<setw(11)<<"WEDNESDAY"<<setw(10)
    		<<"THURSDAY"<<setw(8)<<"FRIDAY"<<setw(10)<<"SATURDAY"<<endl;
    	int startday=WhatDay(1); // find out what day the first of month was
    	int endday=TheDate.HowManyDays();
    	for (int i=0;i<startday;i++)
    	{
    		if (i==0) gotoxy(4,4);
    		if (i==1) gotoxy(12,4);
    		if (i==2) gotoxy(21,4);
    		if (i==3) gotoxy(31,4);
    		if (i==4) gotoxy(42,4);
    		if (i==5) gotoxy(50,4);
    		
    		cout<<"-"<<flush;
    	} // end of for loop
    	
    	
    	int rows=4;
    	int count=1;
    	for(int j=startday;j<(startday+endday);j++)
    		{
    		if(j%7==0)
    		{ 
    			if(j==0) rows=2;
    			rows+=2;
    			gotoxy(4,rows);			
    		}
    		if(j%7==1) gotoxy(12,rows);
    		if(j%7==2) gotoxy(21,rows);
    		if(j%7==3) gotoxy(31,rows);
    		if(j%7==4) gotoxy(42,rows);
    		if(j%7==5) gotoxy(50,rows);
    		if(j%7==6) gotoxy(60,rows);
    		if(count==TheDate.GetDay()) // set text to bright red if count is the day entered
    		{
    			SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_RED);
    		}
    		else
    		{
    			SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_BLUE|FOREGROUND_GREEN);
    		}
    
    		cout<<count;
    		count ++;
    	} // end of for loop
    	cout<<endl;
    
    	}
    Code:
    ScreenFuncs.h
    
    // function prototypes
    #ifndef SCREENFUNCS_H
    #define SCREENFUNCS_H
    
    void clrscr(); // clears the screen in a windows console
    void gotoxy(int x,int y); // moves the cursor to x,y position in a windows console
    
    #endif
    Code:
    ScreenFuncs.cpp
    
    // includes
    
    #include <windows.h>
    #include "ScreenFuncs.h"
    
    // clearscreen function
    
    void clrscr()
    {
       COORD coordScreen = { 0, 0 };
       DWORD cCharsWritten;
       CONSOLE_SCREEN_BUFFER_INFO csbi;
       DWORD dwConSize;
       HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    
       GetConsoleScreenBufferInfo(hConsole, &csbi);
       dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
       FillConsoleOutputCharacter(hConsole, TEXT(' '), dwConSize, coordScreen, &cCharsWritten);
       GetConsoleScreenBufferInfo(hConsole, &csbi);
       FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten);
       SetConsoleCursorPosition(hConsole, coordScreen);
    }
    
    // gotoxy function
    
    void gotoxy(int x, int y) 
    { 
        COORD point; 
        point.X = x; point.Y = y; 
        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),point); 
    }
    Code:
    main.cpp
    
    #include <windows.h>
    #include "Calender.h"
    #include "date.h"
    #include <iostream>
    #include "ScreenFuncs.h"
    
    using namespace std;
    
    int main()
    
    {
    	while(1)
    		{
    			clrscr();
    			SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN);
    			cout<<"Enter date you want to see the calender for in this format dd/mm/yy :-";
    			char input_day[4];
    			char input_month[4];
    			char input_year[6];
    			cin.getline(input_day,3,'/'); // get the input as strings
    			cin.getline(input_month,3,'/');
    			cin.getline(input_year,5,'\n');
    			cout<<endl;
    			int d=atoi(input_day); // convert input to integer
    			int m=atoi(input_month);
    			int y=atoi(input_year);
    			Date TheDate(d,m,y);
    			CalenderADT* pCal= new Calender(TheDate);
    			pCal ->Draw();
    			delete pCal;
    			cout<<endl<<"Another (y/n)?";
    			char quit;
    			cin>>quit;
    			cin.ignore(80,'\n');
    			if(quit=='n' || quit=='N') break;
    		}
    			return 0;
    }
    Last edited by Stoned_Coder; 10-27-2001 at 05:11 AM.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic port scanner code .. pls help ???
    By intruder in forum C Programming
    Replies: 18
    Last Post: 03-13-2003, 08:47 AM
  2. i dont know what to do. pls. help!!!!
    By Unregistered in forum C++ Programming
    Replies: 14
    Last Post: 03-14-2002, 03:24 PM
  3. help me pls..... :(
    By mocha_frap024 in forum C++ Programming
    Replies: 2
    Last Post: 02-22-2002, 10:46 AM
  4. pls help me!!
    By hanseler in forum C++ Programming
    Replies: 1
    Last Post: 12-05-2001, 08:46 PM
  5. Pls Help Me In This Question!!!
    By Joanna in forum Windows Programming
    Replies: 1
    Last Post: 10-20-2001, 02:05 PM