برنامه نویسیسی پلاس پلاس

آموزش برنامه نویسی (حل تمرین ۳)

آموزش برنامه نویسی - مجله اینترنتی آی تی پورت

امیدوارم آموزش قبلی برای شما مفید واقع شده باشه در این سری از حل تمرین برنامه نویسی ++C  با توجه به اینکه  پایان ترم دانشگاها می باشد و بیشتر اساتید به دانشجویان خود پروژه پایان ترم داده اند ما در این سری بیشتر مثال های را ارئه داده ایم که احتمال داده شدن آنها به عنوان پروژه زیاد است . در سری قبلی آموزش ما  به برنامه های دفترچه تلفن ، برنامه برج هانوی ، مرتب سازی سریع و برنامه ای که پس از اجرای آن موشکی در صفحه نمایش ایجاد شده و به بالا حرکت کند پرداختیم. باز هم یادآوری می کنم اگر شما تمرین خاص دارید و به جواب آن نیاز دارید کافیست صورت مسئله مورد نظر خود را از طریق قسمت دیدگاه این مطلب به ما بدهید تا در سری بعدی آموزش جواب آن را به شما ارائه دهیم.(در صورتی که سریع به جواب خود نیاز دارید جواب آن را به ایمیل شما ارسال می کنیم. البته این احتمال وجود دارد که با توجه به امتحانات جواب سوال تان را با تاخییر دریافت کنید.)  

پروژه بزرگ انبارداری  که شامل امکانات زیر می باشد

بخش مدیر که شامل موارد زیر می باشد

۱٫نمایش کالا: نام کالا,قیمت کالا,تعداد کالا,کد کالا

۲٫حذف کالا: نام کاربر,کد کالا

۳٫نمایش کاربر: تلفن ,ایمیل ,یوزروپسورد

۴٫حذف کاربران: براساسنام کاربر

۵٫حذف مدیر: براساس نام کاربر

۶٫نمایش مدیر:. نام, تلفن,ایمیل, یوزروپسورد

۷٫اضافه کردن مدیر

۸٫گزارشات که شامل : نام کالا,تعداد کالا,کد کالا,تاریخ ورود و خروج کالا

بخش کاربر که شامل موارد زیر می باشد

۱٫نمایش کالا: نام کالا,قمیت کالا,تعداد کالا,کد کالا

۲٫حذف کالا: .براساس کد کالا

۳٫اضافه کردن کالا: نام کالا,قیمت کالا,تعداد کالا,کد کالا

۵٫خروج کالا: براساس کد کالا

۳٫ثبت کاربر: نام کاربرتلفن,ایمیل ,یوزروپسورد

اما برنامه :

#include<stdio.h>

#include<conio.h>

#include<string.h>

#include<fstream.h>

#include<iostream.h>

#include<stdlib.h>

#include<dos.h>



 char ddate[20];

struct kala

{

  char  name[20];

  long  pol;

  int   tedad;

  char  seller[20];

  char  codKala[20];

};



struct vkala

{

  char  name[20];

  int   tedad;

  char  seller[20];

  char  codKala[20];

  int   v;

  char  date[10];

};



class classKala

{



   kala    allKala[200];

   int   countKala;



   public :

   classKala(){

    countKala=0;

    fillArrayKala();



   }



void sabtkala(char  karbar[20]);



void nemayeshKala(char  karbar[20]);

void nemayeshKala();

void fillArrayKala();



void hazfKala();

void hazfKala(char karbar[20]);



void vorodkorojkala(char idkala[20],char karbar[20],int in);

void classKala::sabtVkala(char user[20],int ink,kala  stin,int num);

void classKala::gozaresh();

};

//ink 1 for vared 0 for khoroj



void classKala::vorodkorojkala(char idkala[20],char karbar[20],int ink)

{

 kala  st;



 clrscr();

 ifstream in("kala",ios::in |ios::binary);

 ofstream out("temp",ios::out | ios::binary);

 while(in.read((char *) &st, sizeof(struct kala)))

 {



 if(strcmp(idkala, st.codKala)!=0)

 out.write((char *) &st,sizeof(struct kala));

 else

 {

 cout<<"enter num of kala("<<st.tedad<<")\n";

 int id;

 cin>>id;

 if(ink==1)

 {

 st.tedad=st.tedad+id;

 sabtVkala(karbar,ink,st,id);

 }



 else

 {

 if(st.tedad-id<0)

 cout<<"\n kala beh andazeh kafi mojod nist";

 else

 {

 st.tedad=st.tedad-id;

  sabtVkala(karbar,ink,st,id);

 }

 }

 out.write((char *) &st,sizeof(struct kala));

 }



 }

 in.close();

 out.close();

 remove("kala");

 rename("temp","kala");



fillArrayKala();

 return;



}



void classKala::sabtkala(char user[20])

{



  clrscr();



 kala  st;



 int row=1;



 gotoxy(5,row);

 cout<<"name:";

 gotoxy(25,row);

 cin>>st.name;



 row++;

 gotoxy(5,row);

 cout<<"geymat:";

 gotoxy(25,row);

 cin>>st.pol;



 row++;

 gotoxy(5,row);

 cout<<"tedad:";

 gotoxy(25,row);

 cin>>st.tedad;



 strcpy(st.seller,user);



 row++;

 gotoxy(5,row);

 cout<<"code kala:";

 gotoxy(25,row);

 cin>>st.codKala;



 row++;

 char c;

 gotoxy(5,row);

 cout<<"aya mayel beh zakhireh hastid ? (y/n)";

 c=getch();



 if(c=='y')

 {

 ofstream out("kala",ios::app |ios::binary);

 out.write((char *) &st,sizeof(struct kala));

 out.close();

    fillArrayKala();

 }



}

void classKala::sabtVkala(char user[20],int ink,kala  stin,int num)

{



  clrscr();



 vkala  st;



 strcpy(st.seller,user);

 strcpy(st.codKala,stin.codKala);

 strcpy(st.name,stin.name);

 st.tedad=num;

 st.v=ink;

 strcpy(st.date,ddate);



 ofstream out("vkala",ios::app |ios::binary);

 out.write((char *) &st,sizeof(struct vkala));

 out.close();



}



void classKala::fillArrayKala()

{

    clrscr();



    ifstream in("kala",ios::in |ios::binary);



    countKala=0;



 while(in.read((char *) &allKala[countKala], sizeof(struct kala)))

 {

     countKala++;



 }

   in.close();



}



void classKala::nemayeshKala()

{

 int row=4,i=0;



 clrscr();

 cout<<"name        geymat      tedad     karbar      code "<<endl;

 cout<<"÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷"<<endl;

 while(i < countKala)

 {



 gotoxy(1,row);

 cout<<allKala[i].name;

 gotoxy(15,row);

 cout<<allKala[i].pol;

 gotoxy(25,row);

 cout<<allKala[i].tedad;

 gotoxy(35,row);

 cout<<allKala[i].seller;

 gotoxy(50,row);

 cout<<allKala[i].codKala;



 row++;

 i++;

 }



 getch();



}

void classKala::gozaresh()

{

 int row=4,i=0;



 clrscr();



    ifstream in("vkala",ios::in |ios::binary);



 clrscr();

 cout<<"name        tedad     tavasot      codekala      vorod     tarikh "<<endl;

 cout<<"÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷"<<endl;

 vkala vk;



 while(in.read((char *) &vk, sizeof(struct vkala)))

 {



 gotoxy(1,row);

 cout<<vk.name;

 gotoxy(15,row);

 cout<<vk.tedad;

 gotoxy(25,row);

 cout<<vk.seller;

 gotoxy(35,row);

 cout<<vk.codKala;

 gotoxy(50,row);

 cout<<vk.v;

 gotoxy(60,row);

 cout<<vk.date;



 row++;

 i++;

 }

   in.close();

 getch();



}



void classKala::nemayeshKala(char user[20])

{

 int row=4,i=0;



 clrscr();

 cout<<"name        geymat      tedad     karbar      code "<<endl;

 cout<<"÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷"<<endl;

 while(i < countKala)

 {



 if(strcmp(allKala[i].seller,user)==1)

 {

 gotoxy(1,row);

 cout<<allKala[i].name;

 gotoxy(15,row);

 cout<<allKala[i].pol;

 gotoxy(25,row);

 cout<<allKala[i].tedad;

 gotoxy(35,row);

 cout<<allKala[i].seller;

 gotoxy(50,row);

 cout<<allKala[i].codKala;



 row++;

 }

 i++;

 }



 getch();



}



void classKala::hazfKala(char user[20])

{



 char  id[20];



 kala  st;



 clrscr();

 ifstream in("kala",ios::in |ios::binary);

 ofstream out("temp",ios::out | ios::binary);



 gotoxy(20,1);



 cout<<" code kala (delet)\n" ;



 gotoxy(20,2);



 cin>>id;



 while(in.read((char *) &st, sizeof(struct kala)))

 {



 if(strcmp(user,st.seller)!=0 &&strcmp(id, st.codKala)!=0)

 out.write((char *) &st,sizeof(struct kala));



 }

 in.close();

 out.close();

 remove("kala");

 rename("temp","kala");



fillArrayKala();



}



void classKala::hazfKala()

{

 char  id[20];

 char  user[20];



 kala  st;



 clrscr();

 ifstream in("kala",ios::in |ios::binary);

 ofstream out("temp",ios::out | ios::binary);



 gotoxy(20,1);



 cout<<" name karbar \n" ;



 gotoxy(20,2);



 cin>>user;



 gotoxy(20,3);



 cout<<" code kala\n" ;



 gotoxy(20,4);



 cin>>id;



 while(in.read((char *) &st, sizeof(struct kala)))

 {



 if(strcmp(user, st.seller)!=0 &&strcmp(id, st.codKala)!=0)

 out.write((char *) &st,sizeof(struct kala));



 }

 in.close();

 out.close();

 remove("kala");

 rename("temp","kala");



 fillArrayKala();



}



struct ashkhas

{

  char  name[30];

  char  tel[15];

  char  emaile[30];

  char  user[20];

  char  pas[20];

  int   typeShakhs;

} ;



class classAshkhas

{



classKala      kala;



ashkhas    allAshkhas[300];

int     countAshkhas;



public:

	// ۱ for foroshandegan  2 for admin

	classAshkhas();

void addAshkhas(int type);

void showAshkhas(int type);

void fillArrayAshkhas();

void delAshkhas(int type);

int  logAshkhas(char  user[20],char pas[20],int type);



void menu();



};

 classAshkhas::classAshkhas()

{

countAshkhas=0;

fillArrayAshkhas();



}



void classAshkhas::menu()

{

  int  s =0;



  char   user[20],pas[20];



 while(s<4)

 {

  clrscr();

  gotoxy(20,1);

  cout<<"1...........modir";

  gotoxy(20,2);

  cout<<"2...........karbar";

  gotoxy(20,3);

  cout<<"3...........sabt karbar ";

  gotoxy(20,4);

  cout<<"4...........exit";



  gotoxy(20,5);

  cin>>s;

  if(s==1)

  {

   clrscr();

   cout<<"enter user\n";

   cin>>user;

   cout<<"enter pas\n";

   cin>>pas;



   if(logAshkhas(user,pas,3)==1 || (strcmp(user,"123")==0 && strcmp(pas,"123")==0))

   {



    int s1=0;

    while(s1<9)

    {



  clrscr();

  gotoxy(20,1);

  cout<<"1...........nemayesh kala";

  gotoxy(20,2);

  cout<<"2...........hazf kala";

  gotoxy(20,3);

  cout<<"3...........nemayesh karbar";

  gotoxy(20,4);

  cout<<"4...........hazf karbar ";

  gotoxy(20,5);

  cout<<"5...........hazf modir ";

  gotoxy(20,6);

  cout<<"6...........nemayesh modir ";

  gotoxy(20,7);

  cout<<"7...........ezafeh kardan modir";

  gotoxy(20,8);



  cout<<"8...........gozaresh";

  gotoxy(20,9);



  cout<<"9...........return ";

  gotoxy(20,10);



  cin>>s1;



  if(s1==1)

  kala.nemayeshKala();

  else if(s1==2)

  kala.hazfKala();

  else if(s1==3)

  showAshkhas(1);

   else if(s1==4)

  delAshkhas(0);



  else if(s1==5)

  delAshkhas(3);

  else if(s1==6)

  showAshkhas(3);

  else if(s1==7)

  addAshkhas(3);

  else if(s1==8)

  kala.gozaresh();



  }

  }

  }

  else if(s==2)

  {

   clrscr();

   cout<<"enter user\n";

   cin>>user;

   cout<<"enter pas\n";

   cin>>pas;

   if(logAshkhas(user,pas,1)==1)



   {



    int s2=0;

    while(s2<6)

    {

  clrscr();

  gotoxy(20,1);

  cout<<"1...........nemayesh kala";

  gotoxy(20,2);

  cout<<"2...........hazf kala";

  gotoxy(20,3);

  cout<<"3...........ezafeh kala";

  gotoxy(20,4);

  cout<<"4...........vorod kala";

  gotoxy(20,5);

  cout<<"5...........khoroj kala";

  gotoxy(20,6);

  cout<<"6...........return";

  gotoxy(20,7);

  cin>>s2;

  if(s2==1)

   kala.nemayeshKala();

  else if(s2==2)

   kala.hazfKala(user);

  else if(s2==3)

   kala.sabtkala(user);

  else if (s2==4)

  {

   char idkala[20];

   cout<<"\n enter id kala\n";

   cin>>idkala;

   kala.vorodkorojkala(idkala,user, 1);

  }

  else if(s2==5)

  {

   char idkala1[20];

   cout<<"\n enter id kala\n";

   cin>>idkala1;

   kala.vorodkorojkala(idkala1,user, 0);

  }



	}



  }



  }



  else if(s==3)

  {

     addAshkhas(1);

  }



 }



}



void classAshkhas::fillArrayAshkhas()

{



    ifstream in("ashkhas",ios::in |ios::binary);



    countAshkhas=0;



 while(in.read((char *) &allAshkhas[countAshkhas], sizeof(struct ashkhas)))

 {

     countAshkhas++;



 }

   in.close();

}



void classAshkhas::addAshkhas(int typeShakhs)

{



clrscr();



 ashkhas  st;



 int row=1;



 gotoxy(5,row);

 cout<<"name:";

 gotoxy(25,row);

 cin>>st.name;



 row++;

 gotoxy(5,row);

 cout<<"telefon:";

 gotoxy(25,row);

 cin>>st.tel;



 row++;

 gotoxy(5,row);

 cout<<"email:";

 gotoxy(25,row);

 cin>>st.emaile;



 row++;

 gotoxy(5,row);

 cout<<"user:";

 gotoxy(25,row);

 cin>>st.user;



 row++;

 gotoxy(5,row);

 cout<<"pas:";

 gotoxy(25,row);

 cin>>st.pas;



  st.typeShakhs=typeShakhs;



 row++;

 char c;

 gotoxy(5,row);

 cout<<"aya mayel beh zakhireh hastid ? (y/n)";

 c=getch();



 if(c=='y')

 {

 ofstream out("ashkhas",ios::app |ios::binary);

 out.write((char *) &st,sizeof(struct ashkhas));

 out.close();

 }

 fillArrayAshkhas();



}



int classAshkhas::logAshkhas(char  user[20],char pas[20],int type)

{

    ashkhas st;

	int i =0;



 while(i<countAshkhas)

 {



 if(strcmp(user, allAshkhas[i].user)==0 &&strcmp(pas, allAshkhas[i].pas)==0 && allAshkhas[i].typeShakhs==type)

 {

 return 1;

 }

 i++;



 }



 return 0;



}



void classAshkhas::showAshkhas(int typeAshkhas)

{



 int row=4,i=0;



  char  name[30];

  char  tel[15];

  char  emaile[30];

  char  user[20];

  char  pas[20];

  int   typeShakhs;



  clrscr();



 cout<<"\nname                   telfon           emaile             user          pas ";

 cout<<"\n÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷";

 while(i < countAshkhas)

 {



  if(allAshkhas[i].typeShakhs==typeAshkhas)

  {

 gotoxy(1,row);

 cout<<allAshkhas[i].name;

 gotoxy(20,row);

 cout<<allAshkhas[i].tel;

 gotoxy(25,row);

 cout<<allAshkhas[i].emaile;

 gotoxy(35,row);

 cout<<allAshkhas[i].user;

 gotoxy(50,row);

 cout<<allAshkhas[i].pas;



 row++;

  }



 i++;

 }



 getch();

}



void classAshkhas::delAshkhas(int type)

{

 char  user[20];

 clrscr();

 ashkhas  st;

 clrscr();

 ifstream in("ashkhas",ios::in |ios::binary);

 ofstream out("temp",ios::out | ios::binary);

 gotoxy(20,5);

 cout<<" vare konid name karbari ra" ;

 gotoxy(20,6);

 cin>>user;



 while(in.read((char *) &st, sizeof(struct ashkhas)))

 {



 if(strcmp(user, st.user)!=0  && st.typeShakhs != type )

 out.write((char *) &st,sizeof(struct ashkhas));



 }

 in.close();

 out.close();

 remove("ashkhas");

 rename("temp","ashkhas");

 fillArrayAshkhas();

}



 int  main(void)

{

    clrscr();

    cin>>ddate;

    classAshkhas cl ;

    cl.menu();



   return 0;

}

 

 

الگوریتم فشرده سازی و کد گذاری هافمن

#include <conio.h>

#include <stdio.h>

#include <iostream.h>

#include <string.h>

#include <stdlib.h>



struct treenode{

       int data;

       int num;

       treenode *next;

       treenode *right;

       treenode *left;

}*start=NULL,*end=NULL,*p;



struct code{

       char bcode[12];

       int ch;

}coding[256];



struct tree{

        int data;

        int num;

        }atree[512];



int o=0;



int search(int i);

void insert(int i);

void insertm(struct treenode *l);

void sort();

void create();

void binary(struct treenode *n,char byte[],int i);

int searchc(int i);

void write();

void read();

void tree(struct treenode *n,int i);

int numnode=0;

int numchar=0;

char filename[25];



void main()

{

 clrscr();

 FILE *fp1,*fp2;



 cout<<"\n Enter The Location And Name Of The Source File To Compress : ";

 cin>>filename;

 fp1=fopen(filename,"r+b");

 if (fp1==NULL)

    {

       cout<<"\n Error In Open File...";

       getch();

       exit(1);

    }

  int i=getc(fp1);

  while (i!=-1)

  {

   numchar++;

   if (search(i)==-1)

      insert(i);

   i=getc(fp1);

  }

  fclose(fp1);

  sort();

  create();

  char byte[20]={0};

  binary(start,byte,0);

  tree(start,1);

  write();

  cout<<"\n\nComplete Succssfuly Coding To Code.txt\n";

  read();

  cout<<"Complete Succssfuly Encoding To Decode.txt";

  getch();

}

//*************************search******************************

int search(int i)

{

 struct treenode *h;

 h=start;

 while(h)

 {

  if (h->data==i)

  {

   h->num=h->num+1;

   return 0;

  }

  h=h->next;

 }

 return -1;

}

//**************************insert******************************

void insert(int i)

{

 p=new treenode;

 p->data=i;

 p->num=1;

 p->right=p->left=p->next=NULL;

 if (start==NULL)

 {

     start=p;

     end=p;

 }

 else

 {

     end->next=p;

     end=p;

 } }

//******************************Sort*******************

void sort()

{

 struct treenode *h1,*h2,*n2,*right,*left;

 int d,n;

 n2=start;

 while(n2)

 { h1=start;

   h2=start->next;

  while(h2)

  {

    if (h1->num > h2->num)

    {

      d=h1->data;

      n=h1->num;

      right=h1->right;

      left=h1->left;

      h1->data=h2->data;

      h1->num=h2->num;

      h1->right=h2->right;

      h1->left=h2->left;

      h2->data=d;

      h2->num=n;

      h2->right=right;

      h2->left=left;

    }

     h1=h2;

     h2=h2->next;

    }

     n2=n2->next;

 } }

//****************Create*******************************

void create()

{

  struct treenode *h;

  while (start->next)

  {

   int i1,i2;

   p=new treenode;

   p->left=start;

   p->right=start->next;

   p->num=start->num+start->next->num;

   p->data=-1;

   p->next=NULL;

   h=start;

   start=start->next->next;

   h->next->next=NULL;

   h->next=NULL;

   insertm(p);

   sort();

  }}

//********************Insertm****************************

void insertm(struct treenode *l)

{

 if (start==NULL)

 {

     start=l;

     end=l;

 }

 else

 {

     end->next=l;

     end=l;

 } }

//********************Binary****************************

void binary(struct treenode *n,char byte[],int i)

{

 if (n->data==-1)

    {

      char b1[20]={0},b2[20]={0};

      strcpy(b1,byte);

      strcpy(b2,byte);

      b1[i]='1';

      binary(n->right,b1,i+1);

      b2[i]='0';

      binary(n->left,b2,i+1);

     }

   else

     {

       cout<<"Char :"<<(char)n->data<<" Byte : "<<byte<<"\n";

       strcpy(coding[o].bcode,byte);

       coding[o++].ch=n->data;

     }

}

//*********************Coding*******************************

void write()

{

  FILE *fp1,*fp2;

  fp1=fopen(filename,"r+b");

  fp2=fopen("Code.txt","w+b");

  //**************************************************************

  int i=1;

  while (i<=numnode)

  {

    fprintf(fp2,"%d",atree[i].data);

    i++;

  }

  //*****************************************************************

 i=getc(fp1);

 int j=0,place=1;

 unsigned char bytebuf=0;

 while (i!=-1)

  {

   j=searchc(i);

   int y=0;

   while (coding[j].bcode[y]) {

      if (coding[j].bcode[y]=='1')

          bytebuf |= place;



      if (place==128)

      {

     putc(bytebuf,fp2);

     place=1;

     bytebuf=0;

       }

       else

     place<<=1;

       y++;

      }

   i=getc(fp1);

  }

  if (place<=128 && place !=1) putc(bytebuf,fp2);

  fclose(fp1);

  fclose(fp2);

}

//********************searchcode***************************

int searchc(int i)

{

  int j=0;

  while (j<o)

      if (coding[j].ch==i)

           return j;

      else

           j++;

}

//**********************Encode*****************************

void read()

{

 FILE *fp1,*fp2;

 fp1=fopen("Decode.txt","w+b");

 fp2=fopen("Code.txt","r+b");

 //************************************************

 int i=1,a;

  while (i<=numnode)

  {

    fscanf(fp2,"%d",&a);

    i++;

  }

 //************************************************

 i=getc(fp2);

 int j=0,place=1;

 struct treenode *n;

 unsigned char bytebuf=0;

 while (i!=-1)

 {

   n=start;

   while (n->data==-1)

    {

      int x=(place & i)?1:0;

      if (x==1)

    n=n->right;

      else

    n=n->left;

      if (place==128)

     {i=getc(fp2);

      place=1; }

      else

      place<<=1;

     }

   if (j++<numchar)

     putc(n->data,fp1);

  }

  fclose(fp1);

  fclose(fp2);

}

//*********************************************************

void tree(struct treenode *n,int i)

{

 atree[i].data=n->data;

 atree[i].num=n->num;

 numnode=i;

 if (n->left!=NULL)

    tree(n->left,2*i);

 if (n->right!=NULL)

    tree(n->right,2*i+1);

}

 

 برنامه تقویم شمسی و میلادی

#include<iostream.h>

#include<stdio.h>

#include<conio.h>

#include<math.h>

#include<iomanip.h>



int MiladiIsLeap(int miladiYear)

{

if(((miladiYear % 100)!= 0 && (miladiYear % 4) == 0) || ((miladiYear % 100)== 0 && (miladiYear % 400) == 0))

  return 1;

else

  return 0;

}



int DoomsDay(int year)

{

  int a=0;

  if(year>=1800 && year<=1899) a=5;

  else if (year>=1900 && year<=1999) a=3;

  else if (year>=2000 && year<=2099) a=2;

  else if (year>=2100 && year<=2199) a=0;

  else return -1;

  int  y=year %100;

  int z= (y+floor(y/4)+a);

  z=z % 7;

  return (z);

}



int GetDayOfWeek(int y,int m,int d)

{

  int doom;

  doom=DoomsDay(y);

  int AllDoomsy[13] ={-1,3,7,7,4,2,6,4,1,5,3,7,5};

  int c=AllDoomsy[m];

  if((m==1 || m==2) && MiladiIsLeap(y)==1) c++;

  int r=d-c+doom;

  if(r<0) r=r+7;

  return (r%7);

}



typedef struct _SHAMSIDATE

{

    int iYear;

    int iMonth;

    int iDay;

}SDATE;



SDATE MiladiToShamsi(int iMiladiMonth,int iMiladiDay,int iMiladiYear)

{



    int  shamsiDay, shamsiMonth, shamsiYear;

    int  dayCount,farvardinDayDiff,deyDayDiff ;

    int  sumDayMiladiMonth[] = {0,31,59,90,120,151,181,212,243,273,304,334};

    int  sumDayMiladiMonthLeap[]= {0,31,60,91,121,152,182,213,244,274,305,335};

    SDATE  shamsidate;



 farvardinDayDiff=79;



if (MiladiIsLeap(iMiladiYear))

 {

    dayCount = sumDayMiladiMonthLeap[iMiladiMonth-1] + iMiladiDay;

 }

else

 {

    dayCount = sumDayMiladiMonth[iMiladiMonth-1] + iMiladiDay;

 }

if((MiladiIsLeap(iMiladiYear - 1)))

 {

    deyDayDiff = 11;

 }

else

 {

    deyDayDiff = 10;

 }

if (dayCount > farvardinDayDiff)

{

    dayCount = dayCount - farvardinDayDiff;

    if (dayCount <= 186)

     {

      switch (dayCount%31)

       {

    case 0:

     shamsiMonth = dayCount / 31;

     shamsiDay = 31;

    break;

    default:

     shamsiMonth = (dayCount / 31) + 1;

     shamsiDay = (dayCount%31);

    break;

      }

      shamsiYear = iMiladiYear - 621;

     }

   else

     {

    dayCount = dayCount - 186;

    switch (dayCount%30)

      {

       case 0:

    shamsiMonth = (dayCount / 30) + 6;

    shamsiDay = 30;

       break;

     default:

       shamsiMonth = (dayCount / 30) + 7;

       shamsiDay = (dayCount%30);

       break;

     }

      shamsiYear = iMiladiYear - 621;

    }

  }

else

  {

    dayCount = dayCount + deyDayDiff;



    switch (dayCount%30)

    {

    case 0 :

      shamsiMonth = (dayCount / 30) + 9;

      shamsiDay = 30;

     break;

    default:

      shamsiMonth = (dayCount / 30) + 10;

      shamsiDay = (dayCount%30);

     break;

    }

    shamsiYear = iMiladiYear - 622;



  }

    shamsidate.iYear = shamsiYear;

    shamsidate.iMonth = shamsiMonth;

    shamsidate.iDay = shamsiDay;



  return shamsidate ;

}



SDATE ShamsiToMiladi(int y,int m, int d)

{

 int sumshamsi[] = { 31, 62, 93, 124, 155, 186, 216, 246, 276, 306, 336, 365 };

        int miladidays[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

        int yy, mm, dd,daycount;

        daycount = d;

        if (m > 1) daycount =daycount+ sumshamsi[m - 2];

        yy = y + 621;

        daycount = daycount + 79;

        if (MiladiIsLeap(yy))

        {

        if (daycount > 366)

        {

            daycount -= 366;

            yy++;

        }



        }

        else if (daycount > 365)

        {

        daycount -= 365;

        yy++;

        }

        if (MiladiIsLeap(yy)) miladidays[1] = 29;

        mm=0;

        while (daycount > miladidays[mm])

        {

        daycount = daycount - miladidays[mm];

        mm++;

        }

        SDATE miladiDate;

        miladiDate.iYear= yy;

        miladiDate.iMonth=(mm + 1);

        miladiDate.iDay=daycount;

        return miladiDate;

}



void PrintCalander(int year,int month,int sm,int start,int end,int w)

{

  char* ShamsiMonths[]={"Farvardin","Ordibehesht","Khordad","Tir","Mordad","Shahrivar","Mehr","Aban","Azar","Dey","Bahman","Esfand"};

  char* MiladiMonths[]={"January","February","March","April","May","June","July","August","September","October","November","December"};

  char* ShamsiWeekDays[]={"Sh","1Sh","2Sh","3Sh","4Sh","5Sh","Jom"};

  char* MiladiWeekDays[]={"Su","Mo","Tu","We","Th","Fr","Sa"};

  cout<<endl;

  int i=0;

  if(sm==0)

  {

     cout<<ShamsiMonths[month-1]<<"  "<<year<<endl;

     for(i=0;i<7;i++) cout<<setw(10)<<ShamsiWeekDays[i];

     w++;

     w=w%7;

  }

  else

  {

     cout<<MiladiMonths[month-1]<<"  "<<year<<endl;;

     for(i=0;i<7;i++) cout<<setw(10)<<MiladiWeekDays[i];



  }

  cout<<endl;

  int k=w;

  for(i=0;i<k;i++)

  {

     cout<<setw(10)<<" ";

     if(w==7){ cout<<endl; w=0;}

  }

  for(i=start;i<=end;i++)

  {

    cout<<setw(10)<<i;

    w++;

    if(w==7){ cout<<endl; w=0;}

  }

  cout<<endl;

}



void ShowMenu()

{

   cout<<"Please Select:"<<endl<<"1.Persian Calander"<<endl<<"2.Miladi Calander"<<endl;

}



void main()

{

   clrscr();

   int a;

   int sm;//0 for shamsi,1 for miladi

   int MiladiMonths[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

   int ShamsiMonths[] = {31,31,31,31,31,31,30,30,30,30,30,29};

   SDATE date;

   int week;

   ShowMenu();

   char ch=getch();

   while(ch!='1' && ch!='2')

   {

      cout<<"Please Enter 1 or 2: "<<endl;

      ch=getch();

   }

   if(ch=='1') sm=0;//shamsi calander

   else sm=1; //miladi calander

   cout<<endl<<"Please Select:"<<endl<<"1.Weekly Calander"<<endl<<"2.Monthly Calander"<<endl;

   ch=getch();

   while(ch!='1' && ch!='2')

   {

      cout<<"Please Enter 1 or 2: "<<endl;

      ch=getch();

   }

   cout<<endl<<"Enter the year Like this yyyy: ";

   cin>>date.iYear;



   if(ch=='1')

   {

     cout<<"Enter the week Number: ";

     cin>>week;

     while(week>52)

     {

       cout<<"week must be less than 53"<<endl;

       cin>>week;

     }

     date.iDay=((week-1)*7)+1;

     date.iMonth=0;

     if (sm==0)

     {

       while(date.iDay>ShamsiMonths[date.iMonth])

       {

     date.iDay-=ShamsiMonths[date.iMonth];

     date.iMonth++;

       }

     }

     else

     {

       while(date.iDay>MiladiMonths[date.iMonth])

       {

     date.iDay-=MiladiMonths[date.iMonth];

     date.iMonth++;

       }

     }

     date.iMonth++;

     SDATE tmp;

     tmp.iYear=date.iYear;

     tmp.iMonth=date.iMonth;

     tmp.iDay=date.iDay;

     if(sm==0) tmp=ShamsiToMiladi(date.iYear,date.iMonth,date.iDay);



     int z=GetDayOfWeek(tmp.iYear,tmp.iMonth,tmp.iDay);

     int start,end;

     start=date.iDay;

     end=date.iDay+6;

     if(sm==0)

     {

    if(end>ShamsiMonths[date.iMonth])

    {

       end=ShamsiMonths[date.iMonth-1];

       cout<<"WARNINIG: Some of days Cannot be shown in this Month"<<endl;

    }

     }

     else

     {

    if(end>MiladiMonths[date.iMonth])

    {

       end=MiladiMonths[date.iMonth-1];

       cout<<"WARNINIG: Some of days Cannot be shown in this Month"<<endl;

    }

     }

     cout<<endl<<endl;

     PrintCalander(date.iYear,date.iMonth,sm,start,end,z);

   }

   else

   {

     cout<<"Enter the Month Number: ";

     cin>>date.iMonth;

     while(date.iMonth>12)

     {

       cout<<"Month must be less than 12"<<endl;

       cin>>date.iMonth;

     }

     date.iDay=1;

     SDATE tmp;

     tmp.iYear=date.iYear;

     tmp.iMonth=date.iMonth;

     tmp.iDay=date.iDay;

     if(sm==0) tmp=ShamsiToMiladi(date.iYear,date.iMonth,date.iDay);



     int z=GetDayOfWeek(tmp.iYear,tmp.iMonth,tmp.iDay);

     int start,end;

     start=1;

     if(sm==0)

     {

    if(date.iMonth<=6) end=31;

    else if(date.iMonth!=12) end=30;

    else if(date.iYear%4==3) end=30;

    else end=29;

     }

     else end=MiladiMonths[date.iMonth-1];

     cout<<endl<<endl;

     PrintCalander(date.iYear,date.iMonth,sm,start,end,z);

   }

   getch();

}

 

 برنامه جمع دو عدد خیلی بزرگ با vector

#include <iostream.h>

#include <conio.h>

#include <vector.h>



int main()

{

    vector <char> num1,num2,result;

    cout<<"Enter tow huge integer numbers to get the sum of them.\n";

     cout<<"Enter number one and press enter key.\n\n";

     char ch;

     while((ch=getch())!=13)

      {

          if(ch>='0'&& ch<='9')

            {

                cout.put(ch);

                ch-=48;

                num1.push_back(ch);

            }

      }

     cout<<"\n+"<<endl;

     while((ch=getch())!=13)

      {

          if(ch>='0'&& ch<='9')

            {

                cout.put(ch);

                ch-=48;

                num2.push_back(ch);

            }

      }

    cout<<"\n="<<endl;

    int min_size;

    min_size = num1.size() < num2.size() ?

               num1.size() : num2.size();

    bool carry=0;

    while(min_size>0)

     {

       if(num1.back()+num2.back()+carry<10)

        {

         result.push_back(num1.back()+num2.back()+carry);

         num1.pop_back();

         num2.pop_back();

         carry=0;

        }

       else

        {

    result.push_back((num1.back()+num2.back()+carry)%10);

         num1.pop_back();

         num2.pop_back();

         carry=1;

        }

      min_size--;

     }

   while(num1.size()!=0)

    {

      if(num1.back()+carry<10)

       {

           result.push_back(num1.back()+carry);

           num1.pop_back();

           carry=0;

        }

      else

        {

           result.push_back((num1.back()+carry)%10);

           num1.pop_back();

           carry=1;

        } 

    }

  while(num2.size()!=0)

    {

      if(num2.back()+carry<10)

        {

           result.push_back(num2.back()+carry);

           num2.pop_back();

           carry=0;

        }

      else

        {

           result.push_back((num2.back()+carry)%10);

           num2.pop_back();

           carry=1;

        }  

     }

   if(carry==1)

      result.push_back(1);

   while(result.size()>0)

    {

        cout.put(result.back()+48);

        result.pop_back();

    }

   cout<<endl;

  return 0;

}

 

 برنامه مربع جادویی

#define SIZE 160             /* Consider a memory for increasing the size. */



#include "stdio.h"

#include "stdlib.h"

#include "conio.h"



int m[SIZE][SIZE];



void odd_num(int n);

void even_num(int n);

void output(int n);

void _swap(int i1, int j1, int i2, int j2);



main()

{

    int i,j,n;

    char *s="           ";



    printf("\n***************************************************************************");

     printf("\n*                     <  < <   MAGIC  SQUARE   > >  >                     *");

     printf("\n*                                                         *");

     printf("\n***************************************************************************");



     while(1) {

        printf("\n\n\n:-) Input the number(3-%d): ",SIZE);

          gets(s);

          n=atoi(s);

                if(n < 3 || n > SIZE) break;

        if(n%2) odd_num(n);

          else even_num(n);

        output(n);

     }



     printf("\n\n* .. Quit .. .  [[ <   MAGIC  SQUARE   > ]]  . .. *\n\n\n");

}



void odd_num(int n)

{

    int i,j,num=1;

     int nn=n*3/2;



    for(i=0; i < n; i++)

          for(j=0; j < n; j++)

            m[(j-i+nn)%n][(i*2-j+n)%n]=num++;

}



void even_num(int n)

{

     int i,j,num=1;

    int nminus=n-1,nmiddle=n/2,nn=n*n+1;

    int osl=0;

     int switch_row[2];

    int last_switch_column;

    int first_block=(n-2)/4,second_block=nminus-first_block;

     int first_inside=n/4,second_inside=nminus-first_inside;



    for(j=0; j < n; j++)

          for(i=0; i < n; i++) {

            if(i >= first_inside && i <= second_inside && j >= first_inside && j <= second_inside)

                m[i][j]=num;

            else if((i > first_block && i < second_block) || (j > first_block && j < second_block))

                m[i][j]=nn-num;

                else m[i][j]=num;

            num++;

        }

    if(!(n%4)) return;



     switch_row[0]=random(nmiddle-1)+first_block+1;

    switch_row[1]=random(nmiddle-1);

    if(switch_row[1] >= first_block) switch_row[1]+=(nmiddle+1);

    last_switch_column=random(nmiddle-1);

    if(last_switch_column >= first_block) last_switch_column+=(nmiddle+1);



/* Simply, you can write as follows.. 

    switch_row[0]=nmiddle;

    switch_row[1]=0;

    last_switch_column=0;

*/



    for(i=0; i < nmiddle; i++) {

        if(i==first_block || i==second_block) {

            osl=1-osl;

            continue;

          }

        _swap(second_block, i, second_block, nminus-i);

        _swap(i, first_block, nminus-i, first_block);

        _swap(i, second_block, nminus-i, second_block);

        _swap(i, switch_row[osl], nminus-i, switch_row[osl]);

    }

     for(i=first_block+1; i < second_block; i++) {

        _swap(first_block, i, second_block, i);

        _swap(i, first_block, i, second_block);

    }

    _swap(first_block, nmiddle, second_block, nmiddle);

    _swap(last_switch_column, first_block, last_switch_column, second_block);

}



void output(int n)

{

    int i,j,ch,err=0;

    unsigned long sum,sl,sc,sd1=0,sd2=0;



    sum=(unsigned long)n*(n*n+1)/2;

    printf(";-) SUM = %lu.     * ..check sum... .  .    ",sum);

    for(j=0; j < n; j++) {

        sd1+=m[j][j];

        sd2+=m[j][n-j-1];

          sl=0;

        sc=0;

        for(i=0; i < n; i++) {

            sl+=m[i][j];

            sc+=m[j][i];

        }

        if(sl!=sum) {

            err++;

                printf("\n> Sum of the row-(%d) is %lu. It's incorrect..",j+1,sl);

        }

        if(sc!=sum) {

            err++;

            printf("\n> Sum of the column-(%d) is %lu. It's incorrect..",j+1,sc);

        }

    }

    if(sd1!=sum) {

          err++;

        printf("\n> Sum of the diagonal-(\\) is %lu. It's incorrect..",j+1,sd1);

    }

    if(sd2!=sum) {

        err++;

        printf("\n> Sum of the diagonal-(/) is %lu. It's incorrect..",j+1,sd2);

    }

    if(err) printf("\n\n* %d errors are happened.",err);

     else printf("- O.K. -");

    printf("\n\n:-? Do you want to display(Y/n)? ");

    ch=getch();

    if(ch=='n' || ch=='N') return;

    printf("\n---+---------------------------------------------------------------------------");

    for(j=0; j < n; j++) {

        printf("\n%2d)] ",j+1);

        for(i=0; i < n; i++)

                printf("%4d,",m[i][j]);

    }

    printf("\n---+---------------------------------------------------------------------------");

}



void _swap(int i1, int j1, int i2, int j2)

{

    int k;



     k=m[i1][j1];

    m[i1][j1]=m[i2][j2];

    m[i2][j2]=k;

}

 

برنامه هشت وزیر

#include <conio.h>

#include <iostream.h>

#include <iomanip.h>

#include <stdlib.h>



const int m=20;

int k[m][m];

int Count=0;

int v=0 , n=0 , i=0 , j=0 , state=0;



void remove(int i,int j)

{

      int p,q;

      k[i][j]=0;

      Count--;

      for(p=0;p<n;p++)

          if(p!=i)

              k[p][j]--;

      for(p=0;p<n;p++)

        if(p!=j)

                  k[i][p]--;

      p=i+1;

      q=j+1;

     while(p<n && q<n)

      {

                    k[p++][q++]--;

     }

      p=i-1;

      q=j-1;

     while(p>=0 && q>=0)

      {

               k[p--][q--]--;

      }

     p=i+1;

      q=j-1;

     while(p<n && q>=0)

      {

               k[p++][q--]--;

      }

     p=i-1;

      q=j+1;

     while(p>=0 && q<n)

     {

                    k[p--][q++]--;

     }

}



int apply(int i,int j)

{

      int p,q;

     k[i][j]=1;

     Count++;

      for(p=0;p<n;p++)

        if(p!=i)

              k[p][j]++;

      for(p=0;p<n;p++)

        if(p!=j)

              k[i][p]++;

      p=i+1;

     q=j+1;

     while(p<n && q<n)

      {

               k[p++][q++]++;

     }

      p=i-1;

     q=j-1;

     while(p>=0 && q>=0)

      {

               k[p--][q--]++;

     }

      p=i+1;

     q=j-1;

     while(p<n && q>=0)

     {

                    k[p++][q--]++;

     }

     p=i-1;

     q=j+1;

      while(p>=0 && q<n)

     {

               k[p--][q++]++;

      }

      return 0;

}



void draw()

{

      clrscr();

     for(int p=0;p<n;p++)

     {

             for(int q=0;q<n;q++)

                 {

                     if(k[p][q]!=1)

                                   cout<<setw(3)<<'.';

                     else

                                   cout<<setw(3)<<'X';

                 }

             cout<<endl<<endl;

     }

     cout<<endl<<endl<<"Total states founded for "<<n<<"*"<<n<<" boards and "<<v<<" Queens: "<<state<<endl;

}



void check()

{

     if(Count==v)

     {

                      state++;

                 draw();

                 cout<<endl<<endl<<"Press q to exit or any key to continue...";

                 int c=getch();

                      if(c=='q')exit(0);

     }

}



void move(int p,int q)

{

     apply(p,q);

     check();

      for(int i=p;i<n;i++)

     {

             for(int j=0;j<n;j++)

                 if(k[i][j]==0)

                    move(i,j);

     }

      remove(p,q);

}



int main()

{

    cout<<"**************Queens******************"<<endl<<endl;

     cout<<"Enter size of board :";

    cin>>n;

    cout<<"Enter number of queens:";

    cin>>v;

     clrscr;

    draw();

     for(i=0;i<n;i++)

        for(j=0;j<n;j++)

            move(i,j);



     clrscr;

    cout<<"Total states:"<<state<<endl;

     getch();

    return 0;

}

 

گفتگو ها
    سید جلال

    مرسی ، خیلی مفید بود

    با سلام.تشکر از سایت خوب و زیباتون.واقعا عالیه. به امید موفقیت شما.

    خیلی خوبه , عالی بود مث همیشه عـــــــــــــــــــــــالی

    فاطمه

    بسیار عالی و مفید بوده برای من