Академический Документы
Профессиональный Документы
Культура Документы
# include<iostream.h>
# include<conio.h>
# include<math.h>
class point
{
int x,y,z;
public:
point()
{
x=y=z=0;
}
point(int i,int j,int k)
{
x=i;
y=j;
z=k;
}
point(point &a)
{
x=a.x;
y=a.y;
z=a.z;
}
negate()
{
x=-x;
y=-y;
z=-z;
}
void print()
{
cout<<"("<<x<<","<<y<<","<<z<<")";
}
int norm()
{
return(sqrt(x*x+y*y+z*z));
}
};
void main()
{
clrscr();
point p(2,3,4),p1(p);
#include <iostream.h>
#include <string.h>
#include <conio.h>
class bestcoder
{
private:
char name[40];
char grade;
int age;
public:
";
return s;
}
struct hackit
{
char name[40];
char grade;
char age;
};
void main()
{
bestcoder bc;
cout<<bc;
void* ptr=&bc;
struct hackit* bettercoder=(hackit*)ptr;
bettercoder->grade='F';
bettercoder->age=56;
cout<<bc;
strcpy(bettercoder->name,"xxx");
bettercoder->age=14;
bettercoder->grade='A';
cout<<bc;
getch();}
3) Class for Library
# include<iostream.h>
# include<conio.h>
// Creating a basic template for book and magazine
class lib
{
private:
char title[20];
char pub[20];
unsigned int acc_no;
public:
//method for getting inputs
void get_details()
{
cout<<"Enter the book title"<<endl;
cin>>title;
cout<<"Enter the publisher name"<<endl;
cin>>pub;
cout<<"Enter the accession number"<<endl;
cin>>acc_no;
}
//method for showing output
void show_details()
{
cout<<"Title : "<<title<<endl;
cout<<"Publisher : "<<pub<<endl;
cout<<"Accession No. : "<<acc_no<<endl;
}
};
// Class Book derived from lib
class book : private lib
{
private:
char author[20];
public:
void get_details()
{
lib::get_details();
cout<<"Enter the author's name: "<<endl;
cin>>author;
}
void show_details()
{
lib::show_details();
cout<<"Autohr : "<<author<<endl;
}
};
//Class for Magazine derived from lib
class magz : private lib
{
private:
char editor[20];
public:
void get_details()
{
lib::get_details();
cout<<"Enter the editor's name: "<<endl;
cin>>editor;
}
void show_details()
{
lib::show_details();
cout<<"editor : "<<editor<<endl;
}
};
void main(void)
{
clrscr();
//creating objects
book b;
magz m;
b.get_details();
m.get_details();
b.show_details();
m.show_details();
getch();
}
4) Convert a number into words
void numword1(int);
void numword2(int);
int main()
{
long unsigned int number,temp;
int mult,i,digit,digits,last_two,hundred,thousand,lakh,crore;
digits=last_two=hundred=thousand=lakh=crore=0;
if(number>999999999)
{
cout<<"Number out of range!";
getch();
exit(0);
}
if(number==0)
{
cout<<"Zero";
getch();
exit(0);
}
temp=number;
if(digits>=8)
{
numword2(crore);
cout<<"crores ";
}
if(digits>=6)
{
if(lakh!=0)
{
numword2(lakh);
cout<<"lakh ";
}
}
if(digits>=4)
{
if(thousand!=0)
{
numword2(thousand);
cout<<"Thousand ";
}
}
if(digits>=3)
{
if(hundred!=0)
{
numword2(hundred);
cout<<"Hundred ";
}
}
numword2(last_two);
getch();
return 0;
}
void numword1(int num)
{
switch(num)
{
case 0: break;
return;
}
5) Forever Calender in C++
#include<iostream.h>
#include<conio.h>
void main()
{
restart:
clrscr();
cout<<"Enter year : ";
unsigned int y,m;
cin>>y;
int x;
x=step1(y);
int month[14][12]= {1,4,4,7,2,5,7,3,6,1,4,6,
2,5,5,1,3,6,1,4,7,2,5,7,
3,6,6,2,4,7,2,5,1,3,6,1,
4,7,7,3,5,1,3,6,2,4,7,2,
5,1,1,4,6,2,4,7,3,5,1,3,
6,2,2,5,7,3,5,1,4,6,2,4,
7,3,3,6,1,4,6,2,5,7,3,5,
1,4,5,1,3,6,1,4,7,2,5,7,
2,5,6,2,4,7,2,5,1,3,6,1,
3,6,7,3,5,1,3,6,2,4,7,2,
4,7,1,4,6,2,4,7,3,5,1,3,
5,1,2,5,7,3,5,1,4,6,2,4,
6,2,3,6,1,4,6,2,5,7,3,5,
7,3,4,7,2,5,7,3,6,1,4,6};
cout<<"Enter month (1 - 12) : ";
month_input:
cin>>m;
if(m<1 || m>12)
{
cout<<"Enter a valid month (1 - 12) : ";
goto month_input;
}
cout<<"
";
month_name(m);
cout<<' '<<y<<"
#include<iostream.h>
#include<conio.h>
class complex
{
float real;
float img;
public:
complex()
{
real = 0; img = 0;
}
complex(float a,float b)
{
real =a ; img = b;
}
complex operator -(complex );
void disp();
};
// Fn.for overloading of - operator for complex arithmetic
complex complex::operator-(complex a)
{
return complex(real-a.real,img-a.img);
}
// function for display of Real & Imaginary Parts
void complex::disp()
{
cout<<"
The real part is : "<<real;
cout<<"
The imaginary part is: "<<img;
}
void main(void)
{
complex c1(12.0,4.5),c2(8,6),c3;
cout<<"
The value of c1 is:
";
c1.disp();
cout<<"
The value of c2 is:
";
c2.disp();
cout<<"
The value of c3 is:
";
c3.disp();
c3=c1-c2;
cout<<"
After c3=c1-c2, c3 is :
";
c3.disp();
getch();
}
7) Program To Know the Day of Birth from Date of Birth
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
main()
{
clrscr();
int d,m,y,year,month,day,i,n;
printf("Enter how many times you want to run this program : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("
INVALID INPUT
");
getch();
exit(0);
}
year = y-1900;
year = year/4;
year = year+y-1900;
switch(m)
{
case 1:
case 10:
month = 1;
break;
case 2:
case 3:
case 11:
month = 4;
break;
case 7:
case 4:
month = 0;
break;
case 5:
month = 2;
break;
case 6:
month = 5;
break;
case 8:
month = 3;
break;
case 9:
case 12:
month = 6;
break;
}
year = year+month;
year = year+d;
day = year%7;
switch(day)
{
case 0:
printf("
Day is SATURDAY
");
break;
case 1:
printf("
Day is SUNDAY
");
break;
case 2:
printf("
Day is MONDAY
");
break;
case 3:
printf("
Day is TUESDAY
");
break;
case 4:
printf("
Day is WEDNESDAY
");
break;
case 5:
printf("
Day is THURSDAY
");
break;
case 6:
printf("
Day is FRIDAY
");
break;
}
}
getch();
return 0;
}
8) Simple Calculator in C++
#include"stdio.h"
#include"conio.h"
#include"dos.h"
#include"stdlib.h"
#include"string.h"
_setcursortype(0);
gotoxy(30,19);
textcolor(GREEN+BLINK);
cprintf("SIMPLE CALCULATOR");
gotoxy(50,21);
textcolor(BLUE+BLINK);
cprintf("Hello World");
getch();
// clrscr();
int x=30,y=10;
textcolor(WHITE);
gotoxy(x,y);
cprintf("7 8 9");
gotoxy(x,y+2);
cprintf("4 5 6");
gotoxy(x,y+4);
cprintf("1 2 3");
gotoxy(x,y+6);
cprintf("0");
textcolor(RED);
gotoxy(x+5,y+6);
cprintf(". =");
textcolor(GREEN);
x=x+15;
gotoxy(x,y);
cprintf("/");
gotoxy(x,y+2);
cprintf("*");
gotoxy(x,y+4);
cprintf("-");
gotoxy(x,y+6);
cprintf("+");
// Ú & ¿ //
cprintf("%c",218);
gotoxy(28+20,y);
cprintf("%c",191);
// Ú & ¿ //
//Horiz. Boundary
for(x=29;x<=28+19;x++)
{
gotoxy(x,y);
cprintf("%c",196);
gotoxy(x,y+12);
cprintf("%c",196);
}
//End of Horiz. Bound
// Ù & À //
cprintf("%c",217);
x=28;y=y+12;
gotoxy(x,y);
cprintf("%c",192);
//End of Ù & À //
//Vertic. Bound.
for(y=6;y<=16;y++)
{
gotoxy(x,y);
cprintf("%c",179);
gotoxy(x+20,y);
cprintf("%c",179);
}
//End of Vertic Bou.
y=6;
for(x=30;x<=30+16;x++)
{
gotoxy(x,y);
cprintf("%c",196);
gotoxy(x,y+2);
cprintf("%c",196);
}
gotoxy(30,y+1);
cprintf("%c %c",179,179);
gotoxy(30,y);
cprintf("%c",218);
gotoxy(30+16,y);
cprintf("%c",191);
gotoxy(30,y+2);
cprintf("%c",192);
gotoxy(30+16,y+2);
cprintf("%c",217);
//OutPut at X=30,Y=8//
char ch;
char operand1[15]="",operand2[15]="",BLANK[15]=" ";
char operator1,first='y';
long double num1=0,num2=0;
int i=0,ERROR=0; //Digits
int MAX=10;
DISPNUM(0);
do
{
ch=getch();
if(ch=='x1b')
{
for(int i=1000;i>=200;i=i-50)
{
sound(i);
delay(100);
}
nosound();
break;
}
//Numeric//
if((ch>='0')&&(ch<='9'))
{
if(i<MAX)
{
if(first=='y')
{
operand1[i]=ch;
DISPNUM(operand1);
i++;
}
else
{
operand2[i]=ch;
DISPNUM(operand2);
i++;
}
}
else //More than 8 digit
{
ERROR=1;
}
}
else if(ch=='.')
{
if(first=='y')
{
if(strchr(operand1,'.')==NULL)
{
operand1[i]=ch;
i++;
}
DISPNUM(operand1);
}
else
{
if(strchr(operand2,'.')==NULL)
{
operand2[i]=ch;
i++;
}
DISPNUM(operand2);
}
}
//Non Numeric
else if (ch=='*')
{
operator1='*';
first='n';
i=0;
}
else if (ch=='/')
{
operator1='/';
first='n';
i=0;
}
else if (ch=='+')
{
operator1='+';
first='n';
i=0;
}
else if (ch=='-')
{
operator1='-';
first='n';
i=0;
}
else if ((ch=='=')||(ch=='
'))
{
//Store in Floating
if(strcmpi(operand1,BLANK)!=0)
{
num1=_atold(operand1);
}
if(strcmpi(operand2,BLANK)!=0)
{
num2=_atold(operand2);
}
//Now Calculate
switch (operator1)
{
case '+':
num1=num1+num2;
break;
case '-':
num1=num1-num2;
break;
case '*':
num1=num1*num2;
break;
case '/':
num1=num1/num2;
break;
}
//ltoa(num1,operand1,10);
gcvt(num1,12,operand1);
DISPNUM(operand1);
i=0;
first='y';
strcpy(operand1,BLANK);
strcpy(operand2,BLANK);
}
else //Invalid Choice
{
ERROR=1;
}
//Beep On ERROR else ------ //
if (ERROR==0)
{
sound(920);
}
else
{
sound(100);
ERROR=0;
}
delay(250);
nosound();
gotoxy(1,1);
cprintf("%d",i);
}while(1);
// clrscr();
gotoxy(30,19);
textcolor(GREEN+BLINK);
cprintf("SIMPLE CALCULATOR");
gotoxy(50,21);
textcolor(BLUE+BLINK);
cprintf("Hello World");
// getch();
}
void DISPNUM(char *num)
{
textbackground(RED);
gotoxy(31,7);
cprintf(" ");
gotoxy(31,7);
cprintf("%s",num);}