X-CulT Community Forums: C help - X-CulT Community Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

C help since I suck at it

#1 User is offline   Hez

Icon
Posts:
154
Joined:
01-January 06
Gender:
Male
Country:
United States
Interest:
Asshole
Location:
HezLand

Posted 05 November 2008 - 01:45 PM

posted this over at retro, but no one seems to check the programming lounge as much as they used to...

Quote

Write a program to convert fully parenthesized infix notation to suffix notation. Fully parenthesized infix example:((a+b)*(c/d))$(the $ is an end marker)The suffix for the above:ab+cd/*In suffix notation, the operator followsthe operands.Your program:- all operands will be single letters.- a $ will indicate the end of the string.- no embedded blanks.Algorithm:read a character from the input.- if it is a $ you are done - see below.- if it is a open paren, ignore it.- if it is an operand, write it to the output.- if it is an operator (+-*/), push it on a stack.- if it is a close paren, pop the top of stack to outputuse getchar() for input and putchar(int) for output.the stack: an array with a corresponding index like:char stack[20];int stx = 0;to push: put the character into stack[stx] then incrementstx.to pop to output: decrement stx and putchar(stack[stx]);when done, check to see that the stack is empty (stx==0).if not, this is an error situation.what does all this mean? suffix notation can be directlyexecuted by a computer whereas infix cannot. convertinginput to suffix can be the first step to executing anexp​ression. some advanced calculators require you toenter your exp​ressions in suffix notation.
this is what Im working on

Quote

#include #include #include int main() {char I;char stack[20];int stx = 0;printf("enter your stuff\n");putchar(I);while((I=getchar())!='$');if(I=='('){continue; }if((I>='A' && I<= 'Z') || ((I>='a' && I<= 'z');put char (I);continue;if (I==')');put char (stack [--stx]);}
What Ive come up with so far

#2 User is offline   Overlord

  • Postcount whoring since 2003~
Icon
Posts:
3,711
Joined:
03-January 06
Gender:
Male
Country:
United Kingdom
Interest:
Programmer
Location:
Berkshire, UK

Posted 05 November 2008 - 02:17 PM

You expect a higher posting activity at CulT than Retro? =PEDIT: But yeah, what Sanik said

This post has been edited by Overlord: 05 November 2008 - 02:18 PM


#3 User is offline   Hez

Icon
Posts:
154
Joined:
01-January 06
Gender:
Male
Country:
United States
Interest:
Asshole
Location:
HezLand

Posted 05 November 2008 - 03:43 PM

View PostOverlord, on Nov 5 2008, 02:17 PM, said:

You expect a higher posting activity at CulT than Retro? =PEDIT: But yeah, what Sanik said
Of course =PI think I'll study this and see where this takes me. Is there also a way to actually excecute the postfix code in a program as well? I have a feeling that is going to be next.

#4 User is offline   Ultima

Icon
Posts:
404
Joined:
04-January 06
Gender:
Male
Country:
United Kingdom
Interest:
Musician
Location:
Bolton, England

Posted 06 November 2008 - 06:34 AM

Jesus, I've been working with C++ for about 3 years now and I don't think I'd ever be able to go back to C... I used to really love C as well :P

#5 User is offline   Hez

Icon
Posts:
154
Joined:
01-January 06
Gender:
Male
Country:
United States
Interest:
Asshole
Location:
HezLand

Posted 06 November 2008 - 02:24 PM

View PostUltima, on Nov 6 2008, 07:34 AM, said:

Jesus, I've been working with C++ for about 3 years now and I don't think I'd ever be able to go back to C... I used to really love C as well :P

Quote

Write a program to read in an exp​ression in suffix notation and execute it. Executing suffix: read a token from the input (a number or an operator or a terminating $) then: if the token is a number, push it on the stack if the token is an operator, pop the top two elements from the stack, do the operation and push the result onto the stack. if the token is a $, this means end of input. Print the result which is on the stack. If at the end there is more than one thing on the stack, this is an error condition. If during execution of an operator you do not have two things on the stack, this is also an error. Dividing by zero is an error. Simplifying assumptions: put a blank between each token and use a scanf() to read each token in as a string (char array). If the token is not a +, -, *, / or $, it must be a number. Convert from string to numeric integer with the atoi() function. The stack is an integer array. Use the strcmp() function to compare the tokens read with "+",. "-", "*", "/" and "$". Due: Fri Nov 10.
called it! Help would be cool with this too, but I haven't even tried to work on it yet though so I might be able to do it myself. Its due monday though so it must be easy?

#6 User is offline   The Government

  • la belle dame sans fucks
Icon
Posts:
1,062
Joined:
17-March 06
Gender:
Pie
Interest:
Been around forever

Posted 07 November 2008 - 12:49 AM

SANiK said:

Posted Imagewhat did I fucking tell you

#7 User is offline   Hez

Icon
Posts:
154
Joined:
01-January 06
Gender:
Male
Country:
United States
Interest:
Asshole
Location:
HezLand

Posted 07 November 2008 - 01:48 AM

View PostSANiK, on Nov 7 2008, 01:21 AM, said:

I just noticed that I had put char stack[20] and int stx twice in the original program. *fixed it*Most of this can be simplified - thing is - I don't know how much you're supposed to know.As in - would it be too suspicious for you to use Switch() statements, etc.
//Includes/Links#include <stdio.h>#include <stdlib.h>//Defines#define STACK_MAX_SIZE 20#define ERROR_NONE				0#define ERROR_POP_UNDERFLOW	   1#define ERROR_PUSH_OVERFLOW	   2#define ERROR_MATH_DIVIDE_BY_ZERO 3//Variablesint stack[STACK_MAX_SIZE];int stx=0;int errorLast=0;//Functionsvoid push(int mydick){  if(stx == STACK_MAX_SIZE)  {	errorLast=ERROR_PUSH_OVERFLOW;	return;  }  stack[stx++]=mydick;}int pop(){  if(stx == 0)  {	errorLast=ERROR_POP_UNDERFLOW;	return 0;  }  return stack[--stx];}void errorMsg(){  if(errorLast == ERROR_POP_UNDERFLOW)  {	printf("\n--ERROR: Stack Underflow!\n");  }  else if(errorLast == ERROR_PUSH_OVERFLOW)  {	printf("\n--ERROR: Stack Overflow!\n");  }  else if(errorLast == ERROR_MATH_DIVIDE_BY_ZERO)  {	printf("\n--ERROR: Divide By Zero Attempted!\n");  }}int main(){  char buffer[128];  printf("Exit exit exit: \n");  while(1)  {	char operator=0;		scanf("%s", buffer);		if(strcmp(buffer, "+") == 0)	{	  operator='+';	}	else if(strcmp(buffer, "-") == 0)	{	  operator='-';	}	else if(strcmp(buffer, "*") == 0)	{	  operator='*';	}	else if(strcmp(buffer, "/") == 0)	{	  operator='/';	}	else if(strcmp(buffer, "$") == 0)	{	  int result=pop();	  if(errorLast != ERROR_NONE)	  {		errorMsg();		break;	  }	  printf("Result: %d\n", result);	  break;	}	else //It's quite possibly a number	{ //On the other hand, never trust the user.	  //User is a faggot	  push(atoi(buffer));	  if(errorLast != ERROR_NONE)	  {		errorMsg();		break;	  }	}		//Notice that we found what the operators are	//And then we refind them again here...	//Thing is, strcmp is slow and compares the whole string	//We could take the below code and paste it to every above if statement	//But - that'd be memory wasteful	//So we basicly above find what operator it is (if any)	//And then we refind the operator but this time as a char	if(operator != 0)	{	  //If we push 1/0 onto the stack, as "1 0"	  //And then we pop, well, when we pop, 0 comes out	  //Then the 1	  //Meaning, the first pop gives us parameter 2, second one gives us parameter 1, etc.	  int valueB=pop();	  if(errorLast != ERROR_NONE)	  {		errorMsg();		break;	  }	  	  int valueA=pop();	  if(errorLast != ERROR_NONE)	  {		errorMsg();		break;	  }	  	  int valueC=valueA;	  	  if(operator == '+')		valueC+=valueB;	  else if(operator == '-')		valueC-=valueB;	  else if(operator == '*')		valueC*=valueB;	  else if(operator == '/')	  {		if(valueB == 0)		{		  errorLast=ERROR_MATH_DIVIDE_BY_ZERO;		  errorMsg();		  break;		}		valueC/=valueB;	  }	  	  push(valueC);	  if(errorLast != ERROR_NONE)	  {		errorMsg();		break;	  }	} //if(operator != 0)  } //while(1)    printf("\n");  system("pause");}
I compiled this and got some wierd results. I haven't had time to really study or mess with this though since I have a bio exam tomarrow. Im using codeblocks as of right now if that has anything to do with it?

#8 User is offline   Overlord

  • Postcount whoring since 2003~
Icon
Posts:
3,711
Joined:
03-January 06
Gender:
Male
Country:
United Kingdom
Interest:
Programmer
Location:
Berkshire, UK

Posted 07 November 2008 - 04:25 PM

//On the other hand, never trust the user.//User is a faggot
I see you code as professionally as I do:
if ($blankToFill == 0 || $blankToFill > 3) {//Should never happen unless some smart-arse is fiddling with .html files


#9 User is offline   Hez

Icon
Posts:
154
Joined:
01-January 06
Gender:
Male
Country:
United States
Interest:
Asshole
Location:
HezLand

Posted 10 November 2008 - 01:10 AM

View PostSANiK, on Nov 7 2008, 05:59 PM, said:

Hez, make sure to put a space after each token (not required for the $ token):"2 3 * $"The result should be 6. If not tell me the combos that fail
you're golden dude. I'm studying this shit like crazy tonight. Im hoping the next assignment doesn't involve anything like this anymore...

#10 User is offline   Ultima

Icon
Posts:
404
Joined:
04-January 06
Gender:
Male
Country:
United Kingdom
Interest:
Musician
Location:
Bolton, England

Posted 10 November 2008 - 09:10 PM

What course is it for, anyway? Just out of interest :P

#11 User is offline   Hez

Icon
Posts:
154
Joined:
01-January 06
Gender:
Male
Country:
United States
Interest:
Asshole
Location:
HezLand

Posted 20 November 2008 - 02:40 PM

View PostUltima, on Nov 10 2008, 09:10 PM, said:

What course is it for, anyway? Just out of interest :P
Intro to computers...sorry about the lateness of this response o.o

Quote

next assignment: modify the code to:- convert all the letters in token to lower case.- scan token for punctuation ( ispunct() ) and put a nul at the 1st punctuation character, if any, thus shortening, in some cases, the token.- if the length of the token ( strlen() ) is still > 0, add it to the dictionary.- insert tests for NULL pointers after maloc()'s to check for malloc() errors. terminate on error.route the output to sort with the -n (numeric) option.turn in your code and a list of the 10 most frequent words.Due: Fri Nov 21#include #include #include struct D { char * word; int count; struct D * next; };int main() {struct D *start=NULL, *p1;char token[256];while (scanf("%s",token) != EOF) { for (p1=start; p1 != NULL; p1=p1->next) { if (strcmp(token,p1->word)==0) break; } if (p1 != NULL) { p1->count++; continue; } p1 = (struct D *) malloc(sizeof(struct D)); p1->word = (char *) malloc (strlen(token)+1); strcpy(p1->word,token); p1->count=1; p1->next=start; start=p1; } for (p1=start; p1 != NULL; p1=p1->next) { printf("%d %s\n", p1->count, p1->word); }return EXIT_SUCCESS;}
Have no damn idea how to do this either, and its due tomarrow. I've been trying so hard but I keep getting errors :\

#12 User is offline   Hez

Icon
Posts:
154
Joined:
01-January 06
Gender:
Male
Country:
United States
Interest:
Asshole
Location:
HezLand

Posted 20 November 2008 - 11:40 PM

View PostSANiK, on Nov 20 2008, 11:18 PM, said:

HmmFirst of all, scanf() with "%s" rarely returns EOFSo I had to add an if(strcmp(token, "-n") == 0) break;As in, if one enters -n, the program exits the loop and prints out the list.If I had not done that, the program would have been stuck in an infinite loop.Second of all, I had no idea wtf it ment with "route the output to sort with the -n (numeric) option."I'm assuming he may have wanted, "if user enters -#, such as -8, the program prints out the data"Or he may have ment an actual sorting algorithm or something to sort the words by most uses.Needs to be more clear on thatI'm tired from work but here it is I guess:
//Includes#include <stdio.h>#include <stdlib.h>#include <string.h>//Structuresstruct D{  char * word;  int count;  struct D * next;};//Functionsint main(){  struct D *start=NULL, *p1;  char token[256];  while(scanf("%s", token) != EOF)  {	//Does the user want to finish?	if(strcmp(token, "-n") == 0) break;		//Format the token	//By converting all text to lowercase	//And punctuation to a terminator	//Note: After the loop is finished, 'i' should contain	//the length of the token, so no need to waste cycles calling strlen	int i=0;	for(; token[i] != '\0'; i++)	{	  if((token[i] >= 'A') && (token[i] <= 'Z'))		token[i]='a'+(token[i]-'A');	  else if(ispunct(token[i]))	  {		token[i]='\0';		break;	  }	}		//Get the size of the token	int sizeOfWord=i+1; //Including null terminator		//If the user enters the token "hey!!Hi!",	//It gets converted to "hey", length 4 (null terminator included in count)	//If user enters the token ",hmm",	//It gets converted to "", length 1 (null terminator included in count)	//But, that's an empty string, so we don't add it to the database	//Therefore we filter out all strings <=1	if (sizeOfWord <= 1) //Strlen could be used but it'd be a waste of cycles	{ //Fun Fact: Strlen doesn't include the null terminator in the count	  continue;	}		//Search for the word in the database	for (p1=start; p1 != NULL; p1=p1->next)	{	  if (strcmp(token,p1->word)==0)		break;	}	//If a word was found in the database	//This portion makes sure it's not added again	//By forcing the code-flow back to the start of the loop	if (p1 != NULL)	{	  p1->count++;	  continue;	}	//Execution reaches here only if a word was not found in the database	p1 = (struct D *) malloc(sizeof(struct D));	if(p1 == NULL)	{	  printf("Error: Malloc of Data Struct failed\n");	  break;	}		p1->word = (char *) malloc(sizeOfWord);	if(p1->word  == NULL)	{	  printf("Error: Malloc of Word Array failed\n");	  break;	}		//Copy the token to the allocated memory for the 'word'	strcpy(p1->word,token);		//Set the count to 1, telling us that this word has been used	//only once so far	p1->count=1;		//Add to the linked list	p1->next=start;	start=p1;  }  //Print out the results  //The number is the # of times the word was used  //The text is the word itself  for (p1=start; p1 != NULL; p1=p1->next)  {	printf("%d %s\n", p1->count, p1->word);  }    system("pause");  return EXIT_SUCCESS;}
You don't have to stress yourself over this. It helps a bunch though. I'm not taking any programming classes next term so you don't have to fret =P. Also have only one more assigment thats due three weeks from now...cant wait

#13 User is offline   Hez

Icon
Posts:
154
Joined:
01-January 06
Gender:
Male
Country:
United States
Interest:
Asshole
Location:
HezLand

Posted 03 December 2008 - 10:45 PM

I have one last assigment Im having trouble on"Make a time class to include a tick member function that increments the time stored in a time object by one second. The time object should always remain in a consistent state. Write a program that tests the tick member function in a loop that prints the time in standard format during each iteration of the loop to illustrate that the tick member function works correctly. Be sure to test the following cases: A) Incrementing into the next minute. B) Incrementing into the next hour. C) Incrementing into the next day. (i.e., 11:59:59 PM to 12:00:00 AM)."And this is what I have so far...that doesnt work :\

Quote

// prevent multiple inclusions of header file#ifndef TIME_H#define TIME_Hclass Time{ public: Time( int = 0, int = 0, int = 0); void setTime( int, int, int ); int getHour(); int &badSetHour( int ); // private int hour; int minute; int second;}; // ending class#endifTime::Time(int hr, int min, int sec ){ setTime( hr, min, sec );}void Time::setTime( int h, int m, int s){ hour = ( h >= 0 && h < 24 ) ? h : 0; //validate hour minute = ( m >= 0 && m < 60 ) ? m : 0; second = ( s >= 0 && s < 60 ) ? s : 0;}int Time::getHour(){ return hour;}
I've been trying on this one really hard...I suck at programming life.

#14 User is offline   Ultima

Icon
Posts:
404
Joined:
04-January 06
Gender:
Male
Country:
United Kingdom
Interest:
Musician
Location:
Bolton, England

Posted 05 December 2008 - 11:28 PM

I knocked something together but it's probably not the best way to do it.Time.h/Time.cpp
#ifndef TIME_H#define TIME_Hclass Time{	public:		//constructor		Time(int in_hour = 0, int in_minute = 0, int in_second = 0);				//tick function, increments by a second		void tick();				//set the time manually		void setTime(int in_hour, int in_minute, int in_second);		//get each component of time		bool isAM();		int getHours();		int getMinutes();		int getSeconds();		private:		bool am;		int hour;		int minutes;		int seconds;};#endifTime::Time(int in_hour, int in_minute, int in_second){	setTime(in_hour, in_minute, in_second);}void Time::tick(){	seconds++;		//increment seconds by one	if(seconds >= 60)	//if seconds hit 60	{		seconds = 0;	//reset seconds		minutes++;		//increment minutes by one				if(minutes >= 60)	//if minutes hit 60		{			minutes = 0;	//reset minutes			hour++;			if(hour < 12) { am = true; }			else			{				am = false;				if(hour >= 24)				{					hour = 0;				}			}		}	}}void Time::setTime(int in_hour, int in_minute, int in_second){	hour = ( in_hour >= 0 && in_hour < 24 ) ? in_hour : 0;	if(hour < 12) { am = true; } else am = false;	minutes = ( in_minute >= 0 && in_minute < 60 ) ? in_minute : 0;	seconds = ( in_second >= 0 && in_second < 60 ) ? in_second : 0;}bool Time::isAM(){	return am;}int Time::getHours(){	if(hour < 12)	//12 hour clock with AM/PM (hour is internally between 0 and 23, we want between 0 and 11 AM/PM)	{		return hour;	}	else return (hour - 12);}int Time::getMinutes(){	return minutes;}int Time::getSeconds(){	return seconds;}
main.cpp
#include <iostream>using namespace std;int main(){	Time time;	//create time object		for(int i = 0; i < 60 * 60 * 24; i++)	//do for every second in a day	{		time.tick();	//call tick function to increment time				if(i % 10 == 0)	//only print out every 10th iteration.. this can be removed if you want to print them all		{			cout << "Time is " << time.getHours() << ":" << time.getMinutes() << ":" << time.getSeconds();	//output hours/minutes/seconds			if(time.isAM) { cout << " AM." << endl; }	//output AM/PM			else { cout << " PM." << endl; }		}	}		system("pause");	return 0;}
EDIT: It's also untested.

#15 User is offline   Hez

Icon
Posts:
154
Joined:
01-January 06
Gender:
Male
Country:
United States
Interest:
Asshole
Location:
HezLand

Posted 11 December 2008 - 02:34 PM

View PostUltima, on Dec 5 2008, 11:28 PM, said:

I knocked something together but it's probably not the best way to do it.Time.h/Time.cpp
#ifndef TIME_H#define TIME_Hclass Time{	public:		//constructor		Time(int in_hour = 0, int in_minute = 0, int in_second = 0);				//tick function, increments by a second		void tick();				//set the time manually		void setTime(int in_hour, int in_minute, int in_second);		//get each component of time		bool isAM();		int getHours();		int getMinutes();		int getSeconds();		private:		bool am;		int hour;		int minutes;		int seconds;};#endifTime::Time(int in_hour, int in_minute, int in_second){	setTime(in_hour, in_minute, in_second);}void Time::tick(){	seconds++;		//increment seconds by one	if(seconds >= 60)	//if seconds hit 60	{		seconds = 0;	//reset seconds		minutes++;		//increment minutes by one				if(minutes >= 60)	//if minutes hit 60		{			minutes = 0;	//reset minutes			hour++;			if(hour < 12) { am = true; }			else			{				am = false;				if(hour >= 24)				{					hour = 0;				}			}		}	}}void Time::setTime(int in_hour, int in_minute, int in_second){	hour = ( in_hour >= 0 && in_hour < 24 ) ? in_hour : 0;	if(hour < 12) { am = true; } else am = false;	minutes = ( in_minute >= 0 && in_minute < 60 ) ? in_minute : 0;	seconds = ( in_second >= 0 && in_second < 60 ) ? in_second : 0;}bool Time::isAM(){	return am;}int Time::getHours(){	if(hour < 12)	//12 hour clock with AM/PM (hour is internally between 0 and 23, we want between 0 and 11 AM/PM)	{		return hour;	}	else return (hour - 12);}int Time::getMinutes(){	return minutes;}int Time::getSeconds(){	return seconds;}
main.cpp
#include <iostream>using namespace std;int main(){	Time time;	//create time object		for(int i = 0; i < 60 * 60 * 24; i++)	//do for every second in a day	{		time.tick();	//call tick function to increment time				if(i % 10 == 0)	//only print out every 10th iteration.. this can be removed if you want to print them all		{			cout << "Time is " << time.getHours() << ":" << time.getMinutes() << ":" << time.getSeconds();	//output hours/minutes/seconds			if(time.isAM) { cout << " AM." << endl; }	//output AM/PM			else { cout << " PM." << endl; }		}	}		system("pause");	return 0;}
EDIT: It's also untested.
I am going to try this tonight. I made one up myself but I can't get it to compile :|

#16 User is offline   Hez

Icon
Posts:
154
Joined:
01-January 06
Gender:
Male
Country:
United States
Interest:
Asshole
Location:
HezLand

Posted 12 December 2008 - 01:45 PM

View PostHez, on Dec 11 2008, 03:34 PM, said:

View PostUltima, on Dec 5 2008, 11:28 PM, said:

I knocked something together but it's probably not the best way to do it.Time.h/Time.cpp
#ifndef TIME_H#define TIME_Hclass Time{	public:		//constructor		Time(int in_hour = 0, int in_minute = 0, int in_second = 0);				//tick function, increments by a second		void tick();				//set the time manually		void setTime(int in_hour, int in_minute, int in_second);		//get each component of time		bool isAM();		int getHours();		int getMinutes();		int getSeconds();		private:		bool am;		int hour;		int minutes;		int seconds;};#endifTime::Time(int in_hour, int in_minute, int in_second){	setTime(in_hour, in_minute, in_second);}void Time::tick(){	seconds++;		//increment seconds by one	if(seconds >= 60)	//if seconds hit 60	{		seconds = 0;	//reset seconds		minutes++;		//increment minutes by one				if(minutes >= 60)	//if minutes hit 60		{			minutes = 0;	//reset minutes			hour++;			if(hour < 12) { am = true; }			else			{				am = false;				if(hour >= 24)				{					hour = 0;				}			}		}	}}void Time::setTime(int in_hour, int in_minute, int in_second){	hour = ( in_hour >= 0 && in_hour < 24 ) ? in_hour : 0;	if(hour < 12) { am = true; } else am = false;	minutes = ( in_minute >= 0 && in_minute < 60 ) ? in_minute : 0;	seconds = ( in_second >= 0 && in_second < 60 ) ? in_second : 0;}bool Time::isAM(){	return am;}int Time::getHours(){	if(hour < 12)	//12 hour clock with AM/PM (hour is internally between 0 and 23, we want between 0 and 11 AM/PM)	{		return hour;	}	else return (hour - 12);}int Time::getMinutes(){	return minutes;}int Time::getSeconds(){	return seconds;}
main.cpp
#include <iostream>using namespace std;int main(){	Time time;	//create time object		for(int i = 0; i < 60 * 60 * 24; i++)	//do for every second in a day	{		time.tick();	//call tick function to increment time				if(i % 10 == 0)	//only print out every 10th iteration.. this can be removed if you want to print them all		{			cout << "Time is " << time.getHours() << ":" << time.getMinutes() << ":" << time.getSeconds();	//output hours/minutes/seconds			if(time.isAM) { cout << " AM." << endl; }	//output AM/PM			else { cout << " PM." << endl; }		}	}		system("pause");	return 0;}
EDIT: It's also untested.
I am going to try this tonight. I made one up myself but I can't get it to compile :|
#ifndef Time1#define Time1class Time { public: Time( int = 0, int = 0, int = 0 ); void setTime( int, int, int ); void setHour( int ); void setMinute( int ); void setSecond( int ); int getHour( void ); int getMinute( void ); int getSecond( void ); void printStandard( void ); void tick( void ); private: int hour; int minute; int second; }; #endif#include using std::cout;#include using std::setfill;using std::setw;#include "TIME.H"#define max 60Time::Time( int hr, int min, int sec ) { setTime( hr, min, sec ); } void Time::setTime( int h, int m, int s ) { setHour( h ); setMinute( m );setSecond( s ); } void Time::setHour( int h ) { hour = ( h >= 0 && h < 24 ) ? h : 0; } void Time::setMinute( int m ) { minute = ( m >= 0 && m < max ) ? m : 0;} void Time::setSecond( int s ) {second = ( s >= 0 && s < max ) ? s : 0; } int Time::getHour( void ) { return hour; } int Time::getMinute( void ) { return minute; } int Time::getSecond( void ) { return second; } void Time::printStandard( void ) { cout << ( ( hour % 12 == 0 ) ? 12 : hour % 12 ) << ':' << ( minute < 10 ? "0" : "" ) << minute << ':' << ( second < 10 ? "0" : "" ) << second << ( hour < 12 ? " AM" : " PM" ); } void Time::tick( void ) { setSecond( getSecond() + 1 ); if ( getSecond() == 0 ) { setMinute( getMinute() + 1 ); if ( getMinute() == 0 ) setHour( getHour() + 1 ); }} const int MAX_TICKS = 3; main() { Time t; t.setTime( 23, 59, 59 ); for ( int ticks = 1; ticks < MAX_TICKS; ++ticks ) { t.printStandard(); cout << endl; t.tick();system("PAUSE"); // nifty?} return 0; }what I got

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users