Wednesday, November 12, 2008

C++ Date() Class

Our teacher gave us a quiz today which is to improve our previous program, the date class. The concept of this program is to input the number of day, the month, and the year. For example, your input for days = 30, month = 1, and year = 2008. The output will be 1/30/2008. So we need to improve our previous code into a much better program. We need to add functions that can check if the input of number of days are acceptable in the month. For example, the month of April has a maximum of 30 days, if I the input is 31, the function will able to know that input in number of days is wrong. We also add a function that can able to know if the input year is a leap year or not.

Here are some of my codes:
//this funtion will help you to know if the number of days and the month are acceptable
int checkDate()
{
int n;
//this code is for all the months w/ 31 days
if(month==1||month==3||month==4.........................................)
{ if(days<=31){ n=1;} else n=0; }
................................
...............................
...............................
................................
//you will get an idea for all the months w/ 30 days above
if(month==2)//for february
{
if(leapyear()==1)
{
if(days<=29)
n=1;
else n=0;
............................
............................
}
return n;
}
//this function will help you know if the input year is a leapyear
int leapyear()
{
int n;
if(year%4==0)
n=1;
else
n=0;

return n;
}

string getDate()
{
........................
.......................
.......................
char ambot[20]="1\1\1990";
if(checkDate()==1)//if the input days and month are acceptable, statements below will execute
{
...................................
.....................................
....................................
.....................................
so on..................

return "variable";
}
else //if the inputs are not acceptable, the 1\1\1990 will be the value for setDate()
return ambot;
}

Hope you get an idea from that. If you have a better idea than this, you can do what you want. It is a matter of style. ^^ If there are any question about the code or if I'm wrong, feel free to post comments.

Monday, November 10, 2008

12345 into 1 2 3 4 5 output

Create a program which will ask the user to input 5 digit number and display the numbers between spaces.

input:
12345

output:
1 2 3 4 5

Here's the tip:

12345 - 2345 = 10000/10000 = 1
2345 - 345 = 2000/1000 = 2
345 - 45 = 300/100 = 3
45 - 5 = 40/10 = 4
45 - 40 = 5

You will notice that you need to subtract the 12345 to 2345. 2345 is the digit except for the 1st digit number. How to get it?

12345%10000 = 2345

its like 12345/10000 = 1.2345

This % will get the remainder.

Hope you get an idea from that.

If you have any comment, feel free to post.

SUP!

Let me tell you that everyone can create a program!! THATS ALL :D!!