5 Replies - 686 Views - Last Post: 08 March 2010 - 12:55 PM Rate Topic: -----

#1 hailfall  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 54
  • Joined: 05-February 10

Ternary Operator

Posted 08 March 2010 - 12:03 PM

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<process.h>
#include<dos.h>

int adate[] = {31,28,31,30,31,30,31,31,30,31,30,31}, year, month, i, m, f, h, fh, count=1, count2=1;
char *amonth[] = {"January","Febuary","March","April","May","June","July","August","September","October","November","December"}, *aday[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"}, cont;

main()
{
	int jd = 0;
	int day = 0;
	clrscr();
	printf("\nProgrammed By Legaspi!");
	printf("\n\nProgram that prints the entire year:\n");
	Loop3:
	printf("\nEnter the value of the year (eg 2005) or 0 to Quit Program: ");
	scanf("%d",&year);
	clrscr();
	if (year==0)
		exit (0);
	for (i=0;i<12;i++){
		printf("\n%d = %s",count2,amonth[i]);
		count2++;
		}
	printf("\n13 = Return to main menu");
	count2=1;
	printf("\n\nEnter the month : 1 to 12 : ");
	scanf("%d",&month);
	clrscr();
	if (month==13)
		goto Loop3;
	if(month > 13 || month <= 0 || year < 0){
		printf("\n\aInvaild Entry!");
		delay(200000);
		main();
		}


	adate[1] = (year%4==0 && year%100!=0 || year%4==0 && year%100==0) ? 29:28;
	for (i=0;i<month-1;i++)
		jd = jd+adate[i];
	jd = jd+1;
	f= (year-1)/4;
	h= (year-1)/100;
	fh= (year-1)/400;
	day= (year+jd+f-h+fh)%7;
	if (day==0)
		day=7;
	Loop2:
	clrscr();
	printf("\nYear = %d\n",year);
	printf("Month = %s\n",amonth[month-1]);
	printf("\nSun\tMon\tTue\tWed\tThu\tFri\tSat\n");
	count = day;
	for (i=1;i<day;i++){
		if(day==1){
			break;
			}
		printf("\t");
		}
	for (i=1;i<=adate[month-1];i++){
		printf("%d\t",i);
		if(count==7){
			printf("\n");
			count=0;
			}
		count++;
		}
	printf("\n\n\nPress any key to view next or hit ESCAPE to Quit: ");
	cont = getch();
	if ((cont) == 27)
		exit(0);
	clrscr();
	for (i=1;i<=adate[month-1];i++){
		day++;
		if (day>7)
			day=1;
		}
	month++;
	if (month == 13)
		goto Loop1;
		goto Loop2;
	Loop1:
	printf("\n");
	printf("\nHit ENTER to Continue or ESCAPE To Quit");
	cont=getch();
	if( (cont) == 13){
		main();
		}
	else if( (cont) == 27){
		exit(0);
		}
	else{
		goto Loop1;
		}
	getch();
	return 0;
}




This post has been edited by JackOfAllTrades: 08 March 2010 - 12:21 PM
Reason for edit:: Added code tags. PLEASE!!! [code]...PUT YOUR CODE IN HERE...[/code]


Is This A Good Question/Topic? 0
  • +

Replies To: Ternary Operator

#2 hailfall  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 54
  • Joined: 05-February 10

Re: Ternary Operator

Posted 08 March 2010 - 12:11 PM

what is the meaning of "?" in this sir

adate[1] = (year%4==0 && year%100!=0 || year%4==0 && year%100==0) ? 29:28;
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6064
  • View blog
  • Posts: 23,520
  • Joined: 23-August 08

Re: Ternary Operator

Posted 08 March 2010 - 12:22 PM

Google "ternary operator" ... it's an if/else structure in a single line of code.
Was This Post Helpful? 0
  • +
  • -

#4 hailfall  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 54
  • Joined: 05-February 10

Re: Ternary Operator

Posted 08 March 2010 - 12:33 PM

what is the use of that sir?

ohh and sir I almost forgot.. what is the use of the header file dos.h and process.h in the whole program?
Was This Post Helpful? 0
  • +
  • -

#5 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Ternary Operator

Posted 08 March 2010 - 12:50 PM

Basic syntax:
condition ? value_if_cond_true : value_if_cond_false;


Example:
int Func(int a, int B)/>
{
   return (a == b ? 3 : 2);
}


The above example returns 3 if a equals to b, and 2 otherwise.

About dos.h and process.h, they are compatible with an ancient compiler, usually Turbo C. Read here: http://www.dreaminco...showtopic=53615 and here: http://en.wikipedia.org/wiki/Process.h
Was This Post Helpful? 0
  • +
  • -

#6 hailfall  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 54
  • Joined: 05-February 10

Re: Ternary Operator

Posted 08 March 2010 - 12:55 PM

thank a lot sir :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1