سورس کدهای آماده ++C
////////////////////////////////////////////////////////////////////////// Session 2\1 Even or Odd
#include <iostream>// دستورات سیستمی
#include <conio.h>//توقف اجرای تا زدن کلیدی توسط کاربر
using namespace std; // کتابخانه دستورات سیستمی
void main()
{
int num;
int rest;
cout<<" Enter your number: \n ";
cin>>num;
rest=num % 2;
if (rest!=0)
cout<<num<<" Number is Odd ! ";
else
cout<<num<<" Number is Even ! ";
//**********
getch();//توقف اجرای تا زدن کلیدی توسط کاربر
}
////////////////////////////////////////////////////////////////////////// Max
#include <iostream>// دستورات سیستمی
#include <conio.h>//توقف اجرای تا زدن کلیدی توسط کاربر
using namespace std; // کتابخانه دستورات سیستمی
void main()
{
int a,b,c,max;
cout<<" Enter your numbers\n ";
cin>>a>>b>>c;
max=a;
if (b>max)
max=b;
if (c>max)
max=c;
cout<<" The max is " <<max<<endl;
//**********
getch();//توقف اجرای تا زدن کلیدی توسط کاربر
}
////////////////////////////////////////////////////////////////////////// Max 3Number
#include <iostream>
#include <conio.h>
using namespace std; // کتابخانه دستورات سیستمی
void main()
{
int a,b,c,max,min;
cout<<" Enter your numbers \n ";
cin>>a>>b>>c;
max=a;
min=a;
if (b>a)
{
max=b; min=a;
}
else
{
max=a; min=b;
}
if (c>max)
max=c;
if (c<min)
min=c;
cout<<" The max is : " <<max<<endl;
cout<<" The min is : " <<min<<endl;
getch();
}
////////////////////////////////////////////////////////////////////////// Sort Numbers
#include <iostream>// دستورات سیستمی
#include <conio.h>//توقف اجرای تا زدن کلیدی توسط کاربر
using namespace std; // کتابخانه دستورات سیستمی
void main()
{
int a,k,b,c;
cout<<"enter 3 numbers : \n ";
cin>>a>>b>>c;
if ( b>a)
{
k=a; a=b; b=k;
}
if ( c>a)
{
k=a; a=c; c=k;
}
if ( c>b)
{
k=b; b=c; c=k;
}
cout<<"max number is : "<<a<<endl;
cout<<"mid number is : "<<b<<endl;
cout<<"min number is : "<<c<<endl;
//**********
getch();//توقف اجرای تا زدن کلیدی توسط کاربر
}
////////////////////////////////////////////////////////////////////////// Swich case
#include <iostream>
#include <conio.h>
using namespace std; // کتابخانه دستورات سیستمی
void main()
{
char a;
cout<<" Enter your selected character ! : \n ";
cin>>a;
switch (a)
{
case 'A':
case 'B':
case 'C':
case 'D':
cout<<" Big character ! ";
break;
case '1':
case '2':
case '3':
case '4':
cout<<" a figure ! ";
break;
case 'a':
case 'b':
case 'c':
case 'd':
cout<<" little character ! ";
break;
default:
cout<<" unknown character ! ";
break;
}
getch();
}
////////////////////////////////////////////////////////////////////////// Loop For
#include <iostream>// دستورات سیستمی
#include <conio.h>//توقف اجرای تا زدن کلیدی توسط کاربر
using namespace std; // کتابخانه دستورات سیستمی
void main()
{
cout<<"figures which are less than 100 : \n";
for(int i=0; i<100; i++)
{
cout<<i<<endl;
}
//**********
getch();//توقف اجرای تا زدن کلیدی توسط کاربر
}
////////////////////////////////////////////////////////////////////////// For Sum
#include <iostream>// دستورات سیستمی
#include <conio.h>//توقف اجرای تا زدن کلیدی توسط کاربر
using namespace std; // کتابخانه دستورات سیستمی
int s = 0;
void main()
{
cout<< "Sum Numbers are : ";
for(int i=0; i<100; i++)
{
s=s+i;
}
cout<<s;
//**********
getch();//توقف اجرای تا زدن کلیدی توسط کاربر
}
////////////////////////////////////////////////////////////////////////// For Numbers Even
#include <iostream>// دستورات سیستمی
#include <conio.h>//توقف اجرای تا زدن کلیدی توسط کاربر
using namespace std; // کتابخانه دستورات سیستمی
void main()
{
int s,k;
cout<<"even numbers between 100 and 1000 are : \n \n";
for(int i=100; i<1001; i++)
{
k=i%2;
if (k==0)
cout<<i<<" ";
}
//**********
getch();//توقف اجرای تا زدن کلیدی توسط کاربر
}
////////////////////////////////////////////////////////////////////////// Haselzarb
#include <iostream>// دستورات سیستمی
#include <conio.h>//توقف اجرای تا زدن کلیدی توسط کاربر
using namespace std; // کتابخانه دستورات سیستمی
void main()
{
long int p;
p=1;
for(int i=5; i<10; i++)
{
p=p*i;
}
cout<<" Multiply Numbers 5 ta 10 : " <<p;
//**********
getch();//توقف اجرای تا زدن کلیدی توسط کاربر
}
////////////////////////////////////////////////////////////////////////// Numbers with While
#include <iostream>// دستورات سیستمی
#include <conio.h>//توقف اجرای تا زدن کلیدی توسط کاربر
using namespace std; // کتابخانه دستورات سیستمی
void main()
{
long int a;
a=10;
while (a<99)
{
cout<<a<<" ";
a+=20;
}
//**********
getch();//توقف اجرای تا زدن کلیدی توسط کاربر
}
////////////////////////////////////////////////////////////////////////// Positive numbers
#include <iostream>// دستورات سیستمی
#include <conio.h>//توقف اجرای تا زدن کلیدی توسط کاربر
using namespace std; // کتابخانه دستورات سیستمی
void main()
{
int s,a;
cout<< "Enter your numbers : " <<endl;
s=0;
do
{
cin>>a;
s=a+s;
}
while (a>0);
cout<< "Sum Numbers is :" <<s;
//**********
getch();//توقف اجرای تا زدن کلیدی توسط کاربر
}
////////////////////////////////////////////////////////////////////////// Positive Max
#include <iostream>// دستورات سیستمی
#include <conio.h>//توقف اجرای تا زدن کلیدی توسط کاربر
using namespace std; // کتابخانه دستورات سیستمی
void main()
{
int s,max,min,a;
cout<<"enter your numbers : "<<endl;
cin>>a;
max=a; min=a;
while (a>0)
{
if (a>max)
max=a;
if (a<min)
min=a;
cin>>a;
}
cout<<" MAX is : "<< max;
cout<<"\t MIN is : "<< min;
//**********
getch();//توقف اجرای تا زدن کلیدی توسط کاربر
}
////////////////////////////////////////////////////////////////////////// session 3
////////////////////////////////////////////////////////////////////////// Number Aval
#include <iostream>
#include <conio.h>
using namespace std;
void main()
{
int i,p,mod,n,a;
cout<<" Enter a Number : "<<endl;
cin>>n;
i=1;
p=0;
while (i<=n)
{
mod=n%i;
if (mod==0)
p=p+1;
i++;
}
if (p==2)
cout<<" Number is AVAL ! ";
else
cout<<" Number is NOT AVAL ! ";
getch();
}
////////////////////////////////////////////////////////////////////////// add aval 1 to 100
#include <iostream>
#include <conio.h>
using namespace std;
int aval(int x)
{
int w=0;
for(int i=1;i<=x;i++)
{
if (x%i==0)
w++;
}
if(w==2)
return 1;
else
return 0;
}
void main()
{
int a;
for(int i=1;i<=100; i++)
{
if(aval(i)==1)
cout<<" adad e aval = " << i<<endl;
}
getch();
}
////////////////////////////////////////////////////////////////////////// Max 10 Number
#include <iostream>
#include <conio.h>
using namespace std;
void main()
{
int i,max,n,a;
cout<<" Enter 10 Numbers : "<<endl;
cin>>n;
max=n;
i=1;
while (i<10)
{
if (n>max)
max=n;
cin>>n;
i++;
}
cout<<max<<" is MAX number ";
getch();
}
////////////////////////////////////////////////////////////////////////// Maghsum alay
#include <iostream>
#include <conio.h>
using namespace std;
void main()
{
int i,p,mod,n,a;
cout<<"Enter a Number : "<<endl;
cin>>n;
i=1;
while (i<=n)
{
mod=n%i;
if (mod==0)
cout<<i<<" -- ";
i++;
}
cout<<endl;
getch();
}
////////////////////////////////////////////////////////////////////////// Sum and Tedad Maghsumalay
#include <iostream>
#include <conio.h>
using namespace std;
void main()
{
int i,mod,n,a;
cout<<" Enter a Number : "<<endl;
cin>>n;
i=1;
int s=0; // Sum
int p=0; // Numbers
cout<<" Divisors are : \n " ;
while (i<=n)
{
mod=n%i;
if (mod==0)
{
cout<<i<<" -- ";
p++;
s+=i;
}
i++;
}
cout<<endl<<"Sum Divisor :"<<s<<endl;
cout<<"Number Divisor : " <<p<<endl;
cout<<endl;
getch();
}
////////////////////////////////////////////////////////////////////////// Number TAM
#include <iostream>
#include <conio.h>
using namespace std;
void main()
{
int i,p,mod,n; // عدد تام عددی است که مجموع مقسوم علیه های کوچکتر از خودش برابر خودش باشد
cout<<" Enter a Number : "<<endl;
cin>>n;
i=1;
p=0;
while (i<n)
{
mod=n%i;
if (mod==0)
p=p+i;
i++;
}
if (p==n)
cout<<" Number is TAM ! ";
else
cout<<" Number is NOT TAM ! ";
getch();
}
////////////////////////////////////////////////////////////////////////// Factor
#include <iostream>
#include <conio.h>
using namespace std;
void main()
{
int i,n,a;
long int p;
cout<<" Structur is p=n! . Enter a Number : "<<endl;
cin>>n;
p=1;
i=1;
if(n==0)
cout<<" 0! is 1 ";
else
{
while (i<=n)
{
p=p*i;
i++;
}
cout<<n<<"! is "<<p;
}
getch();
}
////////////////////////////////////////////////////////////////////////// Fibonachi
#include <iostream>
#include <conio.h>
using namespace std;
void main()
{
int f1,f2,f,i,n;
i=1;
f1=1;
f2=1;
f=1;
cin>>n;
while ((i<n-1)&&(n>2))
{
f=f2+f1;
f1=f2;
f2=f;
i++;
}
cout<<"N="<<f;
getch();
}
////////////////////////////////////////////////////////////////////////// Fibonachi
#include <iostream>// دستورات سیستمی
#include <conio.h>//توقف اجرای تا زدن کلیدی توسط کاربر
using namespace std; // کتابخانه دستورات سیستمی
void main()
{
{ long bound;
cout << "Enter a positive integer: ";
cin >> bound;
cout << "Fibonacci numbers < " << bound << ":\n0, 1";
long f0=0, f1=1;
while (true)
{ long f2 = f0 + f1;
if (f2 > bound) break; // terminates the loop immediately
cout << ", " << f2;
f0 = f1;
f1 = f2;
}
}
//**********
getch();//توقف اجرای تا زدن کلیدی توسط کاربر
}
////////////////////////////////////////////////////////////////////////// Fibonachi - Halghe Namotanahi Ctrl+ C\sss
#include <iostream>// دستورات سیستمی
#include <conio.h>//توقف اجرای تا زدن کلیدی توسط کاربر
using namespace std; // کتابخانه دستورات سیستمی
void main()
{ long bound;
cout << "Enter a positive integer: ";
cin >> bound;
cout << "Fibonacci numbers < " << bound << ":\n0, 1";
long f0=0, f1=1;
while (true) // ERROR: INFINITE LOOP! Press <Ctrl>+c.)
{ long f2 = f0 + f1;
cout << ", " << f2;
f0 = f1;
f1 = f2;
}
//**********
getch();//توقف اجرای تا زدن کلیدی توسط کاربر
}
////////////////////////////////////////////////////////////////////////// Function
////////////////////////////////////////////////////////////////////////// SQR
#include <iostream>
#include <conio.h>
using namespace std;
int sqr(int x)
{
return x*x;
}
void main()
{
int a;
cout <<" Enter a Number :" << endl;
cin>>a;
cout << sqr(a) << endl;
getch();
}
////////////////////////////////////////////////////////////////////////// Function
////////////////////////////////////////////////////////////////////////// sqr 1 to 100 - 1
#include <iostream>
#include <conio.h>
using namespace std;
int sqr(int x)
{
return x*x;
}
void main()
{
cout << " Sqr numbers are : \n" ;
for (int i=1; i<=10; i++)
{
cout << sqr(i) << endl;
}
getch();
}
////////////////////////////////////////////////////////////////////////// sqr 1 to 100 - 2
#include <iostream>
#include <conio.h>
using namespace std;
int sqr(int x);
void main()
{
{
cout << "Sqr Number are : \n";
for (int i=1; i<=10; i++)
{
cout << sqr(i) << endl;
}
}
getch();
}
int sqr(int x)
{
return x*x;
};
////////////////////////////////////////////////////////////////////////// Add aval
#include <iostream>
#include <conio.h>
using namespace std;
int aval(int x)
{
int w=0;
for(int i=1;i<=x;i++)
{
if (x%i==0)
w++;
}
if(w==2)
return 1;
else
return 0;
}
void main()
{
int a;
for(int i=1;i<=100; i++)
{
if(aval(i)==1)
cout<<" Number is Aval = " << i<<endl;
}
getch();
}
////////////////////////////////////////////////////////////////////////// Call 2
#include <iostream>
#include <conio.h>
using namespace std;
void jj(int &a)
{
a*=10;
}
void main()
{
int x;
cout<<"Enter a number plz ! : \n ";
cin>>x;
jj(x);
cout<<"result is : " <<x;
getch();
}
////////////////////////////////////////////////////////////////////////// Call by
#include <iostream>
#include <conio.h>
using namespace std;
int f1( int a )
{
return a *= a;
}
void f2( int &b )
{
b*= b;
}
void main()
{
int x = 2, z = 4;
cout << "x = " << x << " before passByValue\n"
<< "Value returned by passByValue: "
<< f1( x ) << endl
<< "x = " << x << " after passByValue\n" << endl;
//********
cout << "z = " << z << " before passByReference" << endl;
f2( z );
cout << "z = " << z << " after passByReference" << endl;
getch();
}
////////////////////////////////////////////////////////////////////////// Max Number2
#include <iostream>
#include <conio.h>
using namespace std;
int max ( int x, int y, int z);
void main()
{
int a,b,c;
cout<<"Enter three numbers: " <<endl;
cin>>a>>b>>c;
cout<<"The max number is = " <<max(a,b,c)<<endl;
getch();
}
int max ( int x, int y, int z)
{
int max=x;
if(max<y) max=y;
if(max<z) max=z;
return max;
}
////////////////////////////////////////////////////////////////////////// Max Numbers
#include <iostream>
#include <conio.h>
using namespace std;
int max ( int x, int y, int z)
{
int max=x;
if(max<y) max=y;
if(max<z) max=z;
return max;
}
void main()
{
int a,b,c;
cout<<"Enter three numbers: " <<endl;
cin>>a>>b>>c;
cout<<"The max number is = " <<max(a,b,c)<<endl;
getch();
}
////////////////////////////////////////////////////////////////////////// Static int
#include <iostream>
#include <conio.h>
using namespace std;
void printme()
{
int i=1;
cout<<i++<<" ";
}
int main()
{
for(int k=0; k<10; k++)
printme();
getch();
}
////////////////////////////////////////////////////////////////////////// func sum bazgashti-
/ function example
#include <iostream>
#include <conio.h>
using namespace std;
int sum(int a, int b)
{
if(b==1)
return a;
else
return 1+sum(a,b-1);
}
void main ()
{
int a,b;
cout<<"a=";
cin>>a;
cout<<"b=";
cin>>b;
cout<<1+sum(a,b);
getch();
}
////////////////////////////////////////////////////////////////////////// func 9
// function example
#include <iostream>
#include <conio.h>
using namespace std;
void main ()
{
int a=3;
cout<<a++<<" "<<a++<<" "<<a++<<" "<<a++;
getch();
}
////////////////////////////////////////////////////////////////////////// odd and even
// function example
#include <iostream>
#include <conio.h>
using namespace std;
void odd (int a);
void even (int a);
void main ()
{
int i;
do {
cout << "Type a number (0 to exit): ";
cin >> i;
odd (i);
} while (i!=0);
getch();
}
void odd (int a)
{
if ((a%2)!=0) cout << "Number is odd.\n";
else even (a);
}
void even (int a)
{
if ((a%2)==0) cout << "Number is even.\n";
else odd (a);
}
////////////////////////////////////////////////////////////////////////// Muly and Div
// function example
#include <iostream>
#include <conio.h>
using namespace std;
int operate (int a, int b)
{
return (a*b);
}
float operate (float a, float b)
{
return (a/b);
}
void main ()
{
int x=5,y=2;
float n=5.0,m=2.0;
cout << operate (x,y);
cout << "\n";
cout << operate (n,m);
cout << "\n";
getch();
}
////////////////////////////////////////////////////////////////////////// value and refrence
// function example
#include <iostream>
#include <conio.h>
using namespace std;
void prevnext (int x, int& prev, int& next)
{
prev = x-5;
next = x+5;
}
void main ()
{
int x=100, y, z;
prevnext (x, y, z);
cout << "Previous=" << y << ", Next=" << z;
getch();
}
////////////////////////////////////////////////////////////////////////// value and refrence
// function example
#include <iostream>
#include <conio.h>
using namespace std;
// passing parameters by reference
void duplicate (int& a, int& b, int c)
{
a*=2;
b*=3;
c*=5;
}
void main ()
{
int x=2, y=3, z=7;
duplicate (x, y, z);
cout << "x=" << x << ", y=" << y << ", z=" << z;
getch();
}
////////////////////////////////////////////////////////////////////////// func 10
// function example
#include <iostream>
#include <conio.h>
using namespace std;
void vahid(int a , int b)
{
cout<<"a="<<a<<endl;
cout<<"b="<<b<<endl;
return ;
}
void main ()
{
int a=3;
vahid(a,a++);
getch();
}
////////////////////////////////////////////////////////////////////////// subtraction
// function example
#include <iostream>
#include <conio.h>
using namespace std;
int subtraction (int a, int b)
{
int r;
r=a-b;
return (r);
}
void main ()
{
int x=5, y=3, z;
z = subtraction (7,2); // تابع تفریق
cout << "The first result is " << z << '\n';
cout << "The second result is " << subtraction (7,2) << '\n';
cout << "The third result is " << subtraction (x,y) << '\n';
z= 4 + subtraction (x,y);
cout << "The fourth result is " << z << '\n';
getch();
}
////////////////////////////////////////////////////////////////////////// bazgashti fibo
// function example
#include <iostream>
#include <conio.h>
using namespace std;
int f ( int x)
{
if ((x==1)||(x==2))
return 1;
else
return f(x-1)+f(x-2);
}
void main ()
{
int a;
cout<<"enter Number of figure which u want : " ;
cin>>a;
cout<<"f("<<a<<")="<<f(a)<<endl;
getch();
}
////////////////////////////////////////////////////////////////////////// bazgashti sum with prev
// function example
#include <iostream>
#include <conio.h>
using namespace std;
int countdown(int y) {
if (y==1) {
return 1 && cout << y << endl;
}
else {
return countdown(y-1);
}
}
int main () {
cout << "Countdown from ten: " << endl;
cout << countdown(10) << endl;
getch();
}
////////////////////////////////////////////////////////////////////////// fact
// function example
#include <iostream>
#include <conio.h>
using namespace std;
long factorial (long a)
{
if (a > 1)
return (a * factorial (a-1));
else
return (1);
}
void main ()
{
long number;
cout << "Please type a number: ";
cin >> number;
cout << number << "! = " << factorial (number);
getch();
}
////////////////////////////////////////////////////////////////////////// bazgasht 22
#include <stdio.h>
#include<iostream>
#include<conio.h>
using namespace std;
double power(double x,int y)
{
double res=0;
if(y==0)
{
return 1;
}
else if(y>0)
{
res=x*power(x,--y);
return res;
}
else if(y<0)
{
y*=-1;
res=power(x,y);
return (1/res);
}
}
void main()
{
cout<<power(2,3);
getch();
}
////////////////////////////////////////////////////////////////////////// Session 5
////////////////////////////////////////////////////////////////////////// Recivation 2
// function example
#include <iostream>
#include <conio.h>
using namespace std;
void duolicate(int& a, int& b, int& c)
{
a *= 2;
b *= 3;
c *= 5;
}
void main()
{
int x = 2, y = 3, z = 7;
duoplicate(x, y, z);
cout << "x= " << x << ",y=" << y << ",z=" << z<<endl;
}
////////////////////////////////////////////////////////////////////////// func Fact
// function example
#include <iostream>
#include <conio.h>
#include "stdafx.h"
using namespace std;
long factorial (long a)
{
if (a > 1)
return (a * factorial (a-1));
else
return (1);
getch();
getch();
}
void main ()
{
long number;
cout << "Please type a number: ";
cin >> number;
cout << number << "! = " << factorial (number);
}
////////////////////////////////////////////////////////////////////////// bazgashti pow
#include <stdio.h>
#include<iostream>
#include<conio.h>
using namespace std;
double power(double x,int y)
{
double res=0;
if(y==0)
{
return 1;
}
else if(y>0)
{
res=x*power(x,--y);
return res;
}
else if(y<0)
{
y*=-1;
res=power(x,y);
return (1/res);
}
}
void main()
{
cout<<power(2,3);
getch();
}
////////////////////////////////////////////////////////////////////////// func sum bazgashti
// function example
#include <iostream>
#include <conio.h>
using namespace std;
int sum(int a, int b)
{
if(b==1)
return a;
else
return 1+sum(a,b-1);
}
void main ()
{
int a,b;
cout<<"a=";
cin>>a;
cout<<"b=";
cin>>b;
cout<<1+sum(a,b);
getch();
}
////////////////////////////////////////////////////////////////////////// func Fibo
// function example
#include <iostream>
#include <conio.h>
using namespace std;
int f ( int x)
{
if ((x==1)||(x==2))
return 1;
else
return f(x-1)+f(x-2);
}
void main ()
{
int a;
cout<<"enter Number of figure which u want : " ;
cin>>a;
cout<<"f("<<a<<")="<<f(a)<<endl;
getch();
}
////////////////////////////////////////////////////////////////////////// session 6
////////////////////////////////////////////////////////////////////////// Array 3
#include <iostream>
#include <conio.h>
using namespace std;
void main ()
{
int a[5]={15,4,3,2,1} ,b[4]={6,3,1,5};
cout<<"Union part is : " ;
for(int i=0; i<5; i++)
{
for(int k=0; k<4; k++)
{
if (a[i]==b[k])
cout<<a[i]<<" ";
}
getch();
}
}<iostream>
#include <conio.h>
using namespace std;
int fun(int a[])
{
int sum=0;
for(int i=0; i<5; i++)
{
sum+=a[i];
}
return sum;
}
void main ()
{
int a[5];
for(int i=0; i<5; i++)
{
cout<<"a["<<i<<"]=";
cin>>a[i];
}
cout<<"The sum is " <<fun(a);
getch();
}
////////////////////////////////////////////////////////////////////////// Array 1-4
#include <iostream>
#include <conio.h>
using namespace std;
void main ()
{
const int SIZE=5; // defines the size N for 5 elements
double a[SIZE]; // declares the array's elements as type double
cout << "Enter " << SIZE << " numbers:\t \n";
for (int i=0; i<SIZE; i++)
cin >> a[i];
cout << "In reverse order: ";
for (int i=SIZE-1; i>=0; i--)
cout << "\t" << a[i];
getch();
}
////////////////////////////////////////////////////////////////////////// Array 1-3
#include <iostream>
#include <conio.h>
using namespace std;
void main ()
{
enum Day { SUN, MON, TUE, WED, THU, FRI, SAT };
float high[SAT+1] = {28.6, 29.1, 29.9, 31.3, 30.4, 32.0, 30.7};
for (int day = SUN; day <= SAT; day++)
cout << "The high temperature for day " << day << " was " << high[day] << endl;
getch();
}
////////////////////////////////////////////////////////////////////////// Array 6
#include <iostream>
#include <conio.h>
using namespace std;
void main ()
{
int a[3][3]={{3,7,10},{3,34,8},{45,7,5}} ,b[3][3]={{4,23,13},{53,11,1}, {25,41,11}} ,c[3][3];
int s;
for (int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
s=0;
for(int k=0; k<3; k++)
s+=a[i][k]*b[k][j];
c[i][j]=s;
}
}
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
cout<<c[i][j]<<" " ;
cout<<endl;
}
getch();
}
////////////////////////////////////////////////////////////////////////// Array 1-2
#include <iostream>
#include <conio.h>
using namespace std;
void main ()
{
int a[3];
a[2] = 55;
a[0] = 11;
a[1] = 33;
cout << "a[0] = " << a[0] << endl;
cout << "a[1] = " << a[1] << endl;
cout << "a[2] = " << a[2] << endl;
getch();
}
////////////////////////////////////////////////////////////////////////// Array 5
#include <iostream>
#include <conio.h>
using namespace std;
void main ()
{
int a[3][2]={{-3,-7},{4,34},{45,7}} ,b[3][2]={{-4,8},{53,-17},{4,4}} ,c[3][2];
for(int i=0; i<3; i++)
{
for(int k=0; k<2; k++)
{
c[i][k]=a[i][k]+b[i][k];
cout<<c[i][k]<<" ";
}
cout<<endl;
}
getch();
}
////////////////////////////////////////////////////////////////////////// Array 4
#include <iostream>
#include <conio.h>
using namespace std;
void main ()
{
int a[5]={15,4,3,2,1} ,b[4]={6,3,1,5};
cout<<"Union part is : " ;
for(int i=0; i<5; i++)
{
for(int k=0; k<4; k++)
{
if (a[i]==b[k])
cout<<a[i]<<" ";
}
getch();
}
}
////////////////////////////////////////////////////////////////////////// Array 2
#include <iostream>
#include <conio.h>
using namespace std;
void main ()
{
int a[5],sum=0;
for(int i=0; i<5; i++)
{
cout<<"a["<<i<<"]=";
cin>>a[i];
sum=sum * a[i];
}
cout<<"The sum is " <<sum;
getch();
}
////////////////////////////////////////////////////////////////////////// Array 1
#include <iostream>
#include <conio.h>
using namespace std;
void main ()
{
int a[4];
cout<<"enter content of 4 elements : "<<endl ;
for(int i=0; i<4; i++)
{
cout<<"enter content of a["<<i<<"]"<<endl;
cin>>a[i];;
}
getch();
}
////////////////////////////////////////////////////////////////////////// sesstion 7
////////////////////////////////////////////////////////////////////////// Random String
#include <iostream>
#include <conio.h>
#include <string>
using namespace std;
void main()
{
char S1[]="computer";
string S2=S1;
cout<<S2<<endl;
strcpy(S1," science");
S2=S2+S1;
cout<<S2<<endl;
getch();
}
////////////////////////////////////////////////////////////////////////// strlen
#include <iostream>
#include <conio.h>
#include <string>
using namespace std;
void main ()
{
{
char str [] = " how long Univercity ";
int len ;
len = strlen(str) ;
cout<<str<<" is "<<len<<" characters long\n";
}
getch();
}
////////////////////////////////////////////////////////////////////////// Counter Large and Small
#include <iostream>
#include <conio.h>
#include <string>
using namespace std;
void main()
{
string str;
int x=0,y=0;
getline(cin,str);
for(int i=0;i<str.size();i++)
{
if(str[i]>='a'&&str[i]<='z')
x++;
else if(str[i]>='A'&&str[i]<='Z')
y++;
}
cout<<"large = "<<y<<"\nsmall = "<<x;
cin.get();
getch();
}
////////////////////////////////////////////////////////////////////////// String Function
#include <iostream>
#include <conio.h>
#include <string>
using namespace std;
void main ()
{
string str = "Hello";
cout << "str is : " << str << endl;
str += ",";
str += ' ';
cout << "str is : " << str << endl;
string s;
s = str + "World";
cout << "s is : " << s << endl;
char ch = '!';
s += ch;
cout << "s is : " << s << endl;;
getch();
}
////////////////////////////////////////////////////////////////////////// String Length
#include <iostream>
#include <conio.h>
using namespace std;
void main ()
{
char s[] = "ABCD";
for (int i = 0; i < 5; i++)
cout << "s[" << i << "] = '" << s[i] << "'\n";
getch();
}
////////////////////////////////////////////////////////////////////////// Azmoon
#include <iostream>
#include <conio.h>
using namespace std;
const int NUM_STUDENTS = 3;
const int NUM_QUIZZES = 5;
typedef int Score[NUM_STUDENTS][NUM_QUIZZES];
void read(Score);
void printQuizAverages(Score);
void printClassAverages(Score);
void main ()
{
Score score;
cout << "Enter " << NUM_QUIZZES
<< " quiz scores for each student:\n";
read(score);
cout << "The quiz averages are:\n";
printQuizAverages(score);
cout << "The class averages are:\n";
printClassAverages(score);
getch();
}
void read(Score score)
{ for (int s=0; s<NUM_STUDENTS; s++)
{ cout << "Student " << s << ": ";
for (int q=0; q<NUM_QUIZZES; q++)
cin >> score[s][q];
}
}
void printQuizAverages(Score score)
{ for (int s=0; s<NUM_STUDENTS; s++)
{ float sum = 0.0;
for (int q=0; q<NUM_QUIZZES; q++)
sum += score[s][q];
cout << "\tStudent " << s << ": " << sum/NUM_QUIZZES
<< endl;
}
}
void printClassAverages(Score score)
{ for (int q=0; q<NUM_QUIZZES; q++)
{ float sum = 0.0;
for (int s=0; s<NUM_STUDENTS; s++)
sum += score[s][q];
cout << "\tQuiz " << q << ": " << sum/NUM_STUDENTS
<< endl;
}
}
////////////////////////////////////////////////////////////////////////// Bainary Serch
#include <iostream>
#include <conio.h>
using namespace std;
int index(int, int[],int);
void main ()
{
int a[] = { 22, 33, 44, 55, 66, 77, 88 };
cout << "index(44,a,7) = " << index(44,a,7) << endl;
cout << "index(60,a,7) = " << index(60,a,7) << endl;
getch();
}
int index(int x, int a[], int n)
{ // PRECONDITION: a[0] <= a[1] <= ... <= a[n-1];
int lo=0, hi=n-1, i;
while (lo <= hi)
{
i = (lo + hi)/2;
if (a[i] == x) return i;
if (a[i] < x) lo = i+1;
else hi = i-1;
}
return n;
}
////////////////////////////////////////////////////////////////////////// Line Serch
#include <iostream>
#include <conio.h>
using namespace std;
//int index(int,int[],int);
int index(int x, int a[], int n)
{
for (int i=0; i<n; i++)
if (a[i] == x) return i;
return n;
}
void main ()
{
int a[] = { 22, 44, 76, 88, 44, 66, 55};
cout << "index(66,a,7) = " << index(66,a,7) << endl;
cout << "index(50,a,7) = " << index(50,a,7) << endl;
getch();
}
////////////////////////////////////////////////////////////////////////// مثال کلاس
#include <iostream>
#include<conio.h>
using namespace std;
class Ratio
{
public:
void assign(int, int);
void print();
void multiPly(int,int);
void divide(int, int);
private:
int num, den;
};
void main()
{
Ratio x;
Ratio y;
Ratio multiply;
Ratio divition;
x.assign(13, 7);
y.assign(19, 5);
cout << " x = ";
x.print();
cout << endl;
cout << " y = ";
y.print();
cout << endl;
multiply.multiPly(5,6);
divition.divide(30, 3);
cout << " multiply = ";
multiply.print();
cout << endl;
cout << " divition = ";
divition.print();
cout << endl;
_getch();
}
void Ratio::assign(int numerator, int denumirator)
{
num = numerator;
den = denumirator;
}
void Ratio::print()
{
cout << num << "/////" << den;
}
void Ratio::multiPly(int a,int b)
{
num = a * 5+b;
den = a * b;
}
void Ratio::divide(int a, int b)
{
num = a / b;
den = b / a;
}
////////////////////////////////////////////////////////////////////////// سازنده
#include <iostream>
#include<conio.h>
using namespace std;
class Ratio
{
private:
int num, den;
public:
Ratio()
{
num = 0; den = 1;
}
Ratio(int a)
{
num = a; den = 1;
}
Ratio(int a, int b)
{
num = a; den = b;
}
void print()
{
cout << num << "/" << den;
}
};
void main()
{
Ratio x, y(4), z(22, 7);
cout << " x = ";
x.print();
cout << "\ny = ";
y.print();
cout << "\nz = ";
z.print();
_getch();
}
sation 9
////////////////////////////////////////////////////////////////////////// class
#include <iostream>
#include <conio.h>
using namespace std;
class Ratio
{
public:
void assign(int, int);
void print();
private:
int num, den;
};
int main()
{
Ratio x;
Ratio y;
x.assign(13,7); // az nove class Ratio
y.assign(19,5); // az nove class Ratio
cout << "x = ";
x.print();
cout << endl;
cout << "y = ";
y.print();
cout << endl;
getch();
}
void Ratio::assign(int numerator, int denumirator)
{
num = numerator;
den = denumirator;
}
void Ratio::print()
{
cout << num << '/' << den;
}
////////////////////////////////////////////////////////////////////////// Extend Class
#include <iostream>
#include <conio.h>
using namespace std;
class Ratio
{
public:
void assign(int, int);
double convert();
void invert();
void print();
private:
int num, den;
};
int main()
{
Ratio x;
x.assign(22, 7);
cout << "x = ";
x.print();
cout << " = " << x.convert() << endl;
x.invert();
cout << "1/x = "; x.print();
cout << endl;
getch();
}
void Ratio::assign(int numerator, int denumirator)
{
num = numerator;
den = denumirator;
}
double Ratio::convert()
{
return double(num)/den;
}
void Ratio::invert()
{
int temp = num;
num = den;
den = temp;
}
void Ratio::print()
{
cout << num << '/' << den;
}
////////////////////////////////////////////////////////////////////////// Constructor Function
#include <iostream>
#include <conio.h>
using namespace std;
class Ratio
{
public:
Ratio(int n, int d)
{ num = n; den = d; }
void print()
{ cout << num << '/' << den; }
private:
int num, den;
};
int main()
{
Ratio x(13,7) , y(19,5);
cout << "x = ";
x.print();
cout << "and y = ";
y.print();
getch();
}
////////////////////////////////////////////////////////////////////////// Constructor Function
#include <iostream>
#include <conio.h>
using namespace std;
class Ratio
{
public:
Ratio()
{
num = 0; den = 1;
}
Ratio(int n)
{
num = n; den = 1;
}
Ratio(int n, int d)
{
num = n; den = d;
}
void print()
{
cout << num << '/' << den;
}
private:
int num, den;
};
int main()
{
Ratio x, y(4), z(22,7);
cout << "x = ";
x.print();
cout << "\ny = ";
y.print();
cout << "\nz = ";
z.print();
getch();
}
////////////////////////////////////////////////////////////////////////// destructor
#include <iostream>
#include <conio.h>
using namespace std;
class Ratio
{
public:
Ratio()
{
cout << "OBJECT IS BORN.\n";
}
~Ratio()
{
cout << "OBJECT DIES.\n";
}
private:
int num, den;
};
int main()
{
{
Ratio x; // beginning of scope for x
cout << "Now x is alive.\n";
} // end of scope for x
cout << "Now between blocks.\n";
{
Ratio y;
cout << "Now y is alive.\n";
}
getch();
}
////////////////////////////////////////////////////////////////////////// frind
#include <iostream>
#include <conio.h>
using namespace std;
class myclass
{
friend void print_a(myclass);
private:
unsigned a;
int b;
public:
myclass(int r)
{
a = r > 0 ? r : -r;
}
void print()
{
cout << a;
}
void print_a(myclass ob)
{
cout << ob.a;
}
};
void main()
{
myclass ob(10);
ob.print();
cout << "/t";
ob.print_a(ob);
_getch();
}
////////////////////////////////////////////////////////////////////////// چند ریختی
#include <iostream>
#include <conio.h>
using namespace std;
class base
{
public:
void vfunc(int x, int y)
{
y = x * y;
cout << "y= " << y << endl;
}
void vfunc(int x)
{
x = x + 2;
cout << "x = " << x << endl;
}
};
void main()
{
base p;
p.vfunc(10, 15);
p.vfunc(50);
_getch();
}
////////////////////////////////////////////////////////////////////////// وراثت
//وراثت
#include <iostream>
#include <conio.h>
using namespace std;
class base
{
public:
int x;
int y;
};
class ss : public base
{
public :
void fun1()
{
base b;
b.x;
x = 5;
cout << "x = " << x << endl;
}
};
class mm : public base
{
public :
void fun2()
{
base d;
d.y;
y = 7;
cout << "y = " << y << endl;
}
};
void main()
{
base d;
d.x;
_getch();
}
////////////////////////////////////////////////////////////////////////// وراثت
#include <iostream>
#include <conio.h>
using namespace std;
class base
{
public :
int x;
int y;
};
class first : base
{
public: void fun1()
{
x = 5;
y = 7;
cout << "x = " << x << endl;
cout << "y = " << y << endl;
}
};
class sum :base
{
public:
void fun2()
{
x = 5;
y = 7;
cout << "x + y = " << x + y << endl;
}
public:
void fun2(int h)
{
x = 5;
y = 7;
cout << "x + h = " << x + h << endl;
}
public:
void fun2(int h,int p)
{
x = 5;
y = 7;
cout << "x + h - p = " << x + h - p << endl;
}
};
void main()
{
first b;
sum s;
b.fun1();
s.fun2();
s.fun2(5);
s.fun2(5, 3);
_getch();
}