-
حاسبة تقوم بالعمليات الحسابية الأربع
Code:
#include <iostream.h>
#include <conio.h>
main()
{
clrscr();
int choice ;
float a, b, result ;
cout << "Please enter a number : " ;
cout << "\n (1) add the numbers" ;
cout << "\n (2) subtract the numbers" ;
cout << "\n (3) multiply the numbers" ;
cout << "\n (4) devides the numbers\n " ;
cin >> choice ;
if( (choice>4) || (choice<1) )
{
cout << "\t\t *** Wrong input *** " ;
goto exit ;
}
cout << "Please enter the two numbers (a, and b) : " ;
cin >> a>> b ;
switch(choice)
{
case 1:
result = a+ b ;
cout << a<< " + "<< b<< " = "<< result ;
break ;
case 2:
result = a- b ;
cout << a<< " - "<< b<< " = "<< result ;
break ;
case 3:
result = a* b ;
cout << a<< " * "<< b<< " = "<< result ;
break ;
case 4:
result = a/ b ;
cout << a<< " / "<< b<< " = "<< result ;
break ;
} getch();
exit:
}
-
Reverse any integer
Code:
#include<iostream.h>
// Prints the reverse of any integer
main()
{
double m , d , n = 0 ;
cout << " Enter a positive integer : ";
cin >> m;
while ( m> 0)
{
d = m % 10;
m /= 10;
n = 10 * n + d;
}
cout << " The reverse is "<< n <<endl;
}
-
برنامج يستعمل الswitch
// A program to find the days of the week
Code:
#include<iostream.h>
main()
{
int x;
cout<<"Enter any number between 1-7\";
cin>>x;
switch(x)
{
case 1 : cout<<"Today is Saturday";
case 2 : cout<<"Today is Sunday";
case 3 : cout<<"Today is Monday";
case 4 : cout<<"Today is Tuesday";
case 5 : cout<<"Today is Wednesday";
case 6 : cout<<"Today is Thursday";
case 7 : cout<<"Today is Friday";
Default : cout<<"You have not entered a number between 1-7";
}
return 0;
}
-
قلب الmatrix
Code:
#include<iostream.h>
#include<conio.h>
main()
{
clrscr();
int a[3][3];
int b[3][3];
int i;
int j;
for (i=1;i<=3;i++)
for (j=1;j<=3;j++)
{
cout<<"a["<<i<<"]["<<j<<"]=";
cin>>a[j];
}
for (i=1;i<=3;i++)
for (j=1;j<=3;j++)
{
cout<<a[j]<<" ";
cout<<"\t";
}
for (i=1;i<=3;i++)
for (j=1;j<=3;j++)
{
b[j]=a[j];
cout<<b[j]<<" ";
}
return 0;
}
-
برنامج يحسب الفاتورة
Code:
#include <iostream.h>
main()
{
int oldvalue, newvalue , Unit , Cost;
cout<<"Enter the oldvalue:";
cin>>oldvalue;
cout<<"Enter the newvalue:";
cin>> newvalue;
Unit = newvalue - oldvalue;
if (Unit<=200)
Cost= (Unit*2);
else
if (Unit<=500)
Cost= (200*2)+(Unit-200)*5;
else
Cost=(200*2)+(300*5)+(Unit-500)*(7);
cout<<"The Cost is"<< Cost<<endl;
return 0;
}
-
Template Class
Code:
#include<iostream.h>
const int SIZE = 10;
template < class ListType> class List {
private :
ListType data[SIZE];
int back;
public:
List(){back=0;}
void add ( ListType ch);
ListType remove ();
};
template < class ListType > void List <ListType> :: add ( ListType ob)
{
if (back == SIZE)
cout<<"List is full";
else{
data[back] = ob ;
back++;
}
}
template < class ListType > ListType List <ListType> ::remove()
{
if (back == 0 ){
cout<<"List is empty";
return (ListType(0));
}
else{
back--;
return (data[back]);
}
}
void main(){
List<char> charL;
charL.add('A');
charL.add('B');
charL.add('C');
for(int i=0; i<3; i++)
cout << "remove :"<<charL.remove()<<endl;
List<double> doubleL;
doubleL.add(1.1);
doubleL.add(9.9);
doubleL.add(100.55);
for(i=0; i<3; i++)
cout << "remove :"<<doubleL.remove()<<endl;
}
-
Recursive Function
Code:
#include<stdio.h>
#include<conio.h>
void PrintReverse ();
void main()
{
printf("\nEnter a word :");
PrintReverse();
getch();
}
void PrintReverse ()
{
char n ;
scanf("%c",&n);
if( n != 10 )
PrintReverse();
printf("%c",n);
}
-
Sort an Array
Code:
#include<stdio.h>
#include<conio.h>
void SelectionSort(int arr[],int size);
void PrintArray(int arr[],int size);
void main()
{
int arr[10],size = 10;
for (int i=0;i<size;i++)
{
printf("\narr[%d] = ",i);
scanf("%d",&arr);
}
SelectionSort(arr,size);
PrintArray(arr,size);
getch();
}
void SelectionSort(int arr[],int size)
{
int n,j;
if (size > 0)
{
for(int i=size-2; i>=0;i--)
{
if (arr < arr[i+1])
{
n = arr;
j = i;
if (arr[size-1] > arr[j])
{
arr[j] = arr[size-1];
arr[size-1] = n;
}
}
}
SelectionSort(arr,size-1);
}
}
void PrintArray(int arr[],int size)
{
for(int i=0;i<size;i++)
printf("\narr[%d] = %d",i,arr);
}
-
Recursive Binary Search
Code:
#include<stdio.h>
int RecursiveBinarySearch(int *,int,int,int);
void sort(int *,int);
int main()
{
int array[10],key,size=10,Value;
for(int i=0;i<size;i++)
{
printf("\narray[%d] = ",i);
scanf("%d",&array);
}
sort(array,size);
printf("\nEnter Key Value :");
scanf("%d",&key);
printf("\nThe Key Value is %d",key);
Value = RecursiveBinarySearch(array,0,size,key);
if (Value == -1)
printf("\nThe key value is not found ");
else
printf("\nIt's found in the location %d in the array",Value);
return 0;
}
void sort(int *array,int size)
{
int temp;
for(int i=0;i<size-1;i++)
for(int j=i+1;j<size;j++)
if(*(array+i)>*(array+j))
{
temp = *(array+i);
*(array+i) = *(array+j);
*(array+j) = temp;
}
for(i=0;i<size;i++)
printf("\narray[%d] = %d",i,*(array+i));
}
int RecursiveBinarySearch(int *array,int low,int high,int key)
{
int mid;
mid = (high+low)/2;
if ( key == *(array+mid))
return mid;
else if ( mid == high || mid == low)
return -1;
else if(*(array+mid)>key)
return RecursiveBinarySearch(array,low,mid,key);
else
return RecursiveBinarySearch(array,mid,high,key);
}
-
Structures & Enumeration
Code:
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
struct Book{
char title[30],author[30],ISBN[30],publisher[30],month[30],price[30],quantity[30];
};
void AddBook(struct Book [],int *);
void displayTable(struct Book [],int *);
int Search(struct Book [],int *,char *);
void changePrice(struct Book [],int,char *);
void find_ToTal_New_Books(struct Book [],int *,char *[],int *);
enum Month {JAN=1,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC} ;
void main()
{
struct Book book[100];
char *MonthsName[]={"","January","February","March","April","May","June","July","August","September","October","November","December"};
int size = 100,key,count[13] = {0};
char enter,ISBN[30],price[30];
enum Month months;
do{
puts("1 - Add a Book");
puts("2 - Display Table");
puts("3 - Search for a Book");
puts("4 - Change a Price ");
puts("5 - Sold in each month");
puts("6 - Exit");
puts("Enter your Choice :");
scanf("%d%c",&key,&enter);
switch(key)
{
case 1:{
AddBook(book,&(--size));
break;
}
case 2:{
displayTable(book,&size);
break;
}
case 3:{
puts("Enter The ISBN of the Book:");
gets(ISBN);
if(Search(book,&size,ISBN)== -1)
printf("Not Found\n");
else
{
printf("ISBN : %s\n",ISBN);
printf("Book Title : %s\n",book[Search(book,&size,ISBN)].title );
}
break;
}
case 4:{
puts("Enter The ISBN of the Book:");
gets(ISBN);
if(Search(book,&size,ISBN)== -1)
printf("Not Found\n");
else
{
puts("Enter the new Price: ");
gets(price);
changePrice(book,Search(book,&size,ISBN),price);
}
break;
}
case 5:{
find_ToTal_New_Books(book,count,MonthsName,&size);
for( months=JAN;months<=DEC;months++)
printf("%-12s\t%d\n",*(MonthsName+months),count[months]);
break;
}
case 6:
{
break;
}
default:{
puts("Invalid Choice");
break;
}}
}while(key!=6);
}
void AddBook(struct Book *book,int *size)
{
puts("Enter the Title of the Book :");
gets(book[*size].title);
puts("Enter the Author of the Book :");
gets(book[*size].author);
puts("Enter the ISBN Number of the Book :");
gets(book[*size].ISBN );
puts("Enter the Publisher of the Book :");
gets(book[*size].publisher );
puts("Enter the Month of the Book first sold :");
gets(book[*size].month );
puts("Enter the Price of the Book :");
gets(book[*size].price );
puts("Enter the Quantity of the Book :");
gets(book[*size].quantity);
}
void displayTable(struct Book *book,int *size)
{
printf("%-15s%-15s%-10s%-10s%-10s%-10s%-10s\n","Title","Author","ISBN","Publisher","Month","Price","Quantity");
for(int i = 99;i>=*size;i--)
{
printf("%-15s%-15s%-10s%-10s%-10s%-10s%-10s\n",(book+i)->title,(book+i)->author,(book+i)->ISBN,(book+i)->publisher,(book+i)->month,(book+i)->price,(book+i)->quantity);
}
}
int Search(struct Book *book,int *size,char *ISBN)
{
for(int i = 99;i>=*size;i--)
if(strcmpi(book.ISBN,ISBN)==0)
return i;
return -1;
}
void changePrice(struct Book *book,int pos,char *price)
{
strcpy(book[pos].price,price);
}
void find_ToTal_New_Books(struct Book *book,int *count,char *MonthsName[],int *size)
{
for(int i = 99;i>=*size;i--)
{
if (isdigit(book.month[0]))
{
int m;
sscanf(book.month,"%d",&m);
strcpy(book.month,*(MonthsName+m));
++*(count+m);
}
else
{
for(int j=1;j<=12;j++)
if (strcmpi(MonthsName[j],book.month)==0)
++*(count+j);
}
}
}
-
passing virables by refrence
Code:
#include<iostream.h>
void year_calc(long total_days,int &year,int &month,int &day)
{
while(total_days>365)
{
total_days -= 365;
year++;
}
while(total_days>30)
{
total_days -= 30;
month++;
}
day = total_days;
}
void main()
{
int year=0,month=0,day=0;
long total_days;
cout<<"Enter Total Days :"<<endl;
cin>>total_days;
year_calc(total_days,year,month,day);
cout<<"years : "<<year<<endl;
cout<<"months: "<<month<<endl;
cout<<"days : "<<day<<endl;
}
-
Template Function
Code:
#include<iostream.h>
template < class T >
T Subtract ( T num1,T num2 )
{
if (num1>num2)
return num1-num2;
else
return num2-num1;
}
void main()
{
int i1,i2;
cout<<"Intagers :"<<endl;
cout<<"Enter 2 Integers :"<<endl;
cin>>i1>>i2;
cout<<"The Difference is : "<<Subtract(i1,i2)<<endl;
long l1,l2;
cout<<"Longs :"<<endl;
cout<<"Enter 2 Longs :"<<endl;
cin>>l1>>l2;
cout<<"The Difference is : "<<Subtract(l1,l2)<<endl;
float f1,f2;
cout<<"Floats :"<<endl;
cout<<"Enter 2 Floats :"<<endl;
cin>>f1>>f2;
cout<<"The Difference is : "<<Subtract(f1,f2)<<endl;
double d1,d2;
cout<<"Doubles :"<<endl;
cout<<"Enter 2 Doubles :"<<endl;
cin>>d1>>d2;
cout<<"The Difference is : "<<Subtract(d1,d2)<<endl;
}