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

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

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

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

منتظر نظرات شما دوستان می باشیم

تابع برنامه مرتب سازی سریع (Quick Sort)

void quickSort(int x[], int left, int right)

{



        int i = left, j = right;



        int tmp;



        int pivot = x[(left + right) / 2];



        while (i <= j)

        {



                while (x[i] < pivot)



                        i++;



                while (x[j] > pivot)



                        j--;



                if (i <= j)

                {



                        tmp = x[i];



                        x[i] = x[j];



                        x[j] = tmp;



                        i++;



                        j--;



                }



        }



        if (left < j)



                quickSort(x, left, j);



        if (i < right)



                quickSort(x, i, right);



}

 

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

#include <iostream.h>

#include <stdio.h>

#include <conio.h>



void tower(int,char,char,char); /*prototype*/

int main()

{

    int ndisk;

    clrscr();

    cout<<"\n Enter number of disks <<<::: ";

    cin>>ndisk;

    tower(ndisk,'A','B','C');  /*Calling Function*/

    getch();

    return 0;



} /* End of program */



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



// src = Source | aux = Auxiliry | dest = Destination

void tower(int topN, char src,char aux,char dest)

{

    if(topN == 1)

    {

            cout<<"\n Disk 1 from "<<src<<" to "<<dest;

    }

    else

    {

        tower(topN-1,src,dest,aux); //src to aux

        cout<<"\n Disk "<<topN<<" from "<<src<<" to "<<dest;

        tower(topN-1,aux,src,dest); //aux to dest

    }

}



//۱)  if TopN==1, move the single disc from A to C and stop.

//۲)  Move the top n-1 discs from A to B, using C as Inter.

//۳)  Move the remaining disc from A to C.

//۴)  Move the n-1 discs from B to C, using A as destination(dest).

 

برنامه دفترچه تلفن

#include <iostream.h>

#include <stdlib.h>

#include <cstring.h>

#include <iomanip.h>

#include <conio.h>

#include <fstream.h>



struct Phone

{

    char name[15];

    char family[15];

    char tel[12];



    Phone *next;

};



Phone* Start_ptr = NULL;

Phone* current = NULL;



void Select ( int ); // which choice user selected;

void Add_new();

void Print_all();

void Menu1();

void Menu2();

void Edit_current();



void Delete_current ();

void Delete_all();

void Tel_or_name( int );

void Read_from_file();

void Write_to_file();



void Sort_name( int );

void Add_tail_from_file( Phone* );



Phone* Search( char* , int);



enum MENU{ ADD=1, SEARCH_NAME, SEARCH_NO, SORT_NAME, SORT_FAMIL, SHOW_ALL, SAVE, EXPORT_CSV, EXIT, UNINSTALL };



int main()

{

    Read_from_file();



    int choice;



    do

    {

        Menu1();    

        cin >> choice;

        clrscr();

        Select ( choice );



    }while ( choice != EXIT );



    Write_to_file();



    cout <<"Thank you" << endl;

    Delete_all();



    return 0;

}



/*===================Select=======================*/

void Select( int choose )

{

    switch ( choose )

    {

    case ADD :

        Add_new();      break;



    case SEARCH_NAME :

        Tel_or_name(1); break;



    case SEARCH_NO :

        Tel_or_name(2); break;



    case SORT_NAME :

        Sort_name(1);   break;



    case SORT_FAMIL :

        Sort_name(2);   break;



    case SHOW_ALL :

        Print_all();    break;



    case SAVE :

        Write_to_file();

        cout <<"We will save your data automatically when your work finished" << endl;

        getch();

        break;



    case EXIT :

        return;



    default :

        cout <<"Select again :" << endl;

    }

    return;

}

/*=================Add_new=====================*/

void Add_new()

{

    Phone* temp = new Phone;

    Phone* temp2 = Start_ptr;



    clrscr();



    cout << "Enter the name : ";

    cin >> temp->name;

    cout << "Family : ";

    cin >> temp->family;

    cout << "Phone number : ";

    cin >> temp->tel;



    temp->next = NULL;



    if ( Start_ptr == NULL )

    {

        Start_ptr = temp;        

    }



    else

    {

        while( temp2->next != NULL )

        {

            temp2 = temp2->next;

        }

        temp2->next = temp;

    }



    return;

}

/*==============Print_all=======================*/

void Print_all()

{

    Phone* temp = Start_ptr;

    char show[35];



    if ( Start_ptr == NULL )

    {

        cout <<"The list is empty!" << endl;

        getch();

        return;

    }

    else

    {

        cout<<"\nName                                Phone       "

            <<"\n----------------------------------- ------------" << endl;

        do

        {

            strcpy(show, "");

            strcat(show, temp->family);

            strcat(show, ", ");

            strcat(show, temp->name);

            show[0] = toupper(show[0]);

            cout << setiosflags( ios::left )

                << setw(36) << show << setw(12) <<temp->tel <<endl;



            temp = temp->next;

        }while(temp != NULL);

    }

    cout << endl;

    getch();

    return;

}



/*=================Search=======================*/

Phone* Search( char* temp_search , int choice )

{

    /* If list is empty */

    if ( Start_ptr == NULL )

    {

        cout <<"List is empty!" << endl;

        return NULL;

    }



    /* Search by name */

    if ( choice == 1 )

    {

        while( current != NULL && strcmp( current->name, temp_search ) != 0 )

        {

            current = current->next;

        }

    }



    /* Search by tel */

    if ( choice == 2 )

    {

        while( current != NULL && strcmp( current->tel, temp_search ) != 0 )

        {

            current = current->next;

        }



    }



    /* If record found */

    if ( current != NULL )

    {

        cout << "Record found" << endl

            << current->name << " " << current->family << " : " << current->tel << endl;



        return current;

    }



    /* If record !found */

    else

    {

        cout <<"Record NOT found" << endl;

        current = Start_ptr; //move back the current pointer to fisrt node

        return NULL;

    }

}



/*====================Delete_current====================*/

void Delete_current()

{

    Phone* temp = NULL;

    Phone* prev = NULL;



    /* If it`s the fisrt node */

    if ( current == Start_ptr ) 

    {

        temp = Start_ptr;

        Start_ptr = Start_ptr->next; //If we have only 1 node, start_ptr will point to NULL

        delete temp;

        temp = NULL;

    }



    /* If it`s in the middle of list or the last node */

    else    

    {

        prev = Start_ptr;

        while( prev->next != current )

        {

            prev = prev->next;

        }

        prev->next = current->next;// If it`s the last node prev will point to NULL

        delete current;

        current = Start_ptr;

    }            

}



/*=================Delete_all=================*/

void Delete_all()

{

    if ( Start_ptr == NULL )

    {

        return; // we have no memory allocated

    }



    Phone* temp = Start_ptr;

    while( Start_ptr != NULL )

    {

        temp = Start_ptr;

        Start_ptr = Start_ptr->next;

        delete temp;

    }

}



/*=================Tel_or_name================*/

void Tel_or_name(int choose)

{

    Phone* temp_del = NULL;

    char temp_search[15];

    int choice;



    current = Start_ptr;



    cout <<"Enter the "<<(choose == 1 ? "name" : "tel") <<" to search : ";

    cin >> temp_search;



    temp_del = Search( temp_search, choose );



    while ( temp_del != NULL )

    {

        Menu2();

        cin >> choice;



        switch( choice )

        {

        case 1: current = current->next; temp_del = Search( temp_search, choose ); break;

        case 2: Delete_current(); break;

        case 3: Edit_current();   break;

        case 4: return;

        }

    }

    getch();



}

/*==============Write_to_file=============================*/

void Write_to_file()

{

    Phone* temp = Start_ptr;

    ofstream outFile("Data.dat" , ios::out );



    if( !outFile )

    {

        cerr << "Some error ocured during writing to file." << endl;

        getch();

        return;

    }



    while( temp != NULL )

    {

        outFile << temp->name << " " << temp->family << " " << temp->tel;

        if( temp->next != NULL )

        {

            outFile << endl;

        }

        temp = temp->next;



    }



    outFile.close();



    cout <<"Data saved successfully." << endl;

}

/*===============Read_from_file=============================*/

void Read_from_file()

{

    ifstream inputFile("Data.dat" , ios::in );



    if ( !inputFile )

    {

        cout << "Data couldn`t be loaded." << endl;

        getch();

        return;

    }



    do

    {

        Phone* temp = new Phone;



        inputFile >> temp->name;

        inputFile >> temp->family;

        inputFile >> temp->tel;

        temp->next = NULL;



        Add_tail_from_file( temp );



        //Where should I place delete temp?????    

    }while( !inputFile.eof() );



    cout <<"Data loaded successfully" << endl;

}



/*==============Add_tail_from_file=================*/

void Add_tail_from_file( Phone* temp )

{



    if ( Start_ptr == NULL )

    {

        Start_ptr = temp;    

    }

    else

    {

        Phone* temp2 = Start_ptr;

        while ( temp2->next != NULL )

        {

            temp2 = temp2->next;

        }

        temp2->next = temp;

    }

}



/*================Sort_name=================================*/



void Sort_name(int choice)

{

    /* If list is empty */

    if ( Start_ptr == NULL )

    {

        cout <<"The list is empty!" << endl;

        getch();

        return;

    }



    /* Determine the size of list */

    int counter = 1;

    Phone* temp = Start_ptr;



    while( temp->next != NULL )

    {

        temp = temp->next;

        counter++;

    }



    /* an Array of pointers to struct Phone. I couldn`t do this part by dynamic memory

    allocation i.e Phone* sort = new Phone[counter] 

    or some thing like that. if you could help, please send me your suggestion .*/

    Phone* sort[1000];



    sort[0] = Start_ptr;

    for ( int cnt = 1; cnt < counter; cnt++ )

    {

        sort[cnt] = sort[cnt-1]->next;

    }

    sort[counter] = NULL;



    /* bubble sort */

    /* This part could be better too.for example if user enter capital 'F' it comes

       before 'a'. I had to change all letter to lower case but has no time. */



    for ( int i = 0; i < counter; i++ )

    {

        for ( int j = 0; j < counter - i - 1; j++)

        {

            /* Sort by name */

            if ( choice == 1 )

            {

                if ( strcmp(sort[j]->name, sort[j+1]->name) > 0 )

                {

                    Phone* temp2 = sort[j];

                    sort[j] = sort[j+1];

                    sort[j+1] = temp2;

                }

            }



            /* Sort by family */

            else

            {

                if ( strcmp(sort[j]->family, sort[j+1]->family) > 0 )

                {

                    Phone* temp2 = sort[j];

                    sort[j] = sort[j+1];

                    sort[j+1] = temp2;

                }

            }

        }

    }



    /* Showing sorted list */    

    char show[35];

    int index= 0;



    cout<<"\nName                                Phone       "

        <<"\n----------------------------------- ------------" << endl;

    do

    {

        strcpy(show, "");

        strcat(show, sort[index]->family);

        strcat(show, ", ");

        strcat(show, sort[index]->name);

        show[0] = toupper(show[0]);

        cout << setiosflags( ios::left )

            << setw(36) << show << setw(12) <<sort[index]->tel <<endl;



    }while(sort[++index] != NULL);



    cout << endl;

    getch();

    return;

}

/*====================Menu1=======================*/

void Menu1()

{

    clrscr();

        cout << "1.  Add new phone"

            <<"\n2.  Search for name"

            <<"\n3.  Search for number"

            <<"\n4.  Sort by name"

            <<"\n5.  Sort by family"

            <<"\n6.  Show all list"

            <<"\n7.  Save data"

            <<"\n9.  Exit"

            <<"\n\nYour choice : ";

}



/*====================Menu2=======================*/

void Menu2()

{

    cout << "\n1.  Find next"

        <<"\n2.  Delete current person"

        <<"\n3.  Edit current person"

        <<"\n4.  continue" << endl

        <<"\nYour choice : ";

}



void Edit_current()

{

    strcpy(current->name,"");

    strcpy(current->family,"");



    cout << "Enter the name : ";

    cin >> current->name;

    cout << "Family : ";

    cin >> current->family;

    cout << "Phone number : ";

    cin >> current->tel;



    clrscr();

}

 

برنامه ایجاد و حرکت موشک در صفحه نمایش

#include<iostream.h>

#include<conio.h>

#include<dos.h>

main(){

char a[5][5];

int s;

a[1][1]=' ';

a[1][2]=' ';

a[1][3]='*';

a[1][4]=' ';

a[1][5]=' ';

a[2][1]=' ';

a[2][2]='*';

a[2][3]='*';

a[2][4]='*';

a[2][5]=' ';

a[3][1]='*';

a[3][2]='*';

a[3][3]='*';

a[3][4]='*';

a[3][5]='*';

a[4][1]='*';

a[4][2]='*';

a[4][3]='*';

a[4][4]='*';

a[4][5]='*';

a[5][1]='*';

a[5][2]='*';

a[5][3]='*';

a[5][4]='*';

a[5][5]='*';



 for(int k=1;k<15;k++)

 {

 clrscr();

for(s=1;s<15-k;s++)

{

cout<<"\n";

}

for(int i=1;i<5;i++)

	{

   	for(int j=1;j<=5;j++)

      	{

         	cout<<a[i][j];

         }

   cout<<"\n";

   }

sleep(1);

   }

getche();

return(0);

}

 

گفتگو ها
    عباس

    سلام

    بابت برنامه دفترچه تلفن ممنون