Перейти из форума на сайт.

НовостиФайловые архивы
ПоискАктивные темыТоп лист
ПравилаКто в on-line?
Вход Забыли пароль? Первый раз на этом сайте? Регистрация
Компьютерный форум Ru.Board » Компьютеры » Прикладное программирование » Задачи по C/С++

Модерирует : ShIvADeSt

 Версия для печати • ПодписатьсяДобавить в закладки
На первую страницук этому сообщениюк последнему сообщению

Открыть новую тему     Написать ответ в эту тему

diablist



Advanced Member
Редактировать | Профиль | Сообщение | Цитировать | Сообщить модератору
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#include <iostream.h>
#include <string.h>
//#include <dos.h>
#include <windows.h>
#define N 7
 
struct Tree {
    struct  Tree *lPtr;
    char    Goods[20];
    char    Producer[20];
    float   Price;
    struct  Tree *rPtr;
};
    int Menu(int);
    int Kypcop(int);
    int Add();
    int Del_root();
    int Print(Tree *);
    int exit();
    int POISK();
    int Min_el();
    int Replace();
    int ramka();
    int n = 0, i=0;
    Tree Search(Tree *, char* );
    Tree *Search_creat(Tree *, char* );
    Tree *Dtree(Tree *Root, float);
    Tree *Root,*element,*Search_root;
    Tree *Addtree(Tree *, Tree *, float, char[20], char[20]);
    void FormatPrint(Tree *root);
//_______________________________________________________________________
int main()
{
    int p,key=0,kluch;
    clrscr();
    //_setcursortype(0);
    do {
        kluch=Menu(key);
        key=Kypcop(kluch);
        if (key == 10) {
            //ramka();
            gotoxy(30,2);
            cout<<" ДОБАВЛЕНИЕ ";
            key=Add();
        }
        if (key == 11) {
        //    ramka();
            gotoxy(30,2);
            cout<<" УДАЛЕНИЕ ";
            key=Del_root();
        }
        if (key == 12) {
            gotoxy(30,2);
            cout<<" ВЫВОД В ВИДЕ ДЕРЕВА ";
            key=Print(Root);
        }
        if (key == 13) {
        //    ramka();
            gotoxy(30,2);
            cout<<" ПОИСК ";
            key=POISK();
        }
        if (key == 14) {
        //    ramka();
            gotoxy(30,2);
            cout<<" МИНИМАЛЬНЫЙ ЭЛЕМЕНТ ";
            key=Min_el();
        }
        if (key == 15) {
        //    ramka();
            gotoxy(30,2);
            cout<<" ЗАМЕНА ";
            key=Replace();
        }
        if (key == 16)     key=exit();
        }
    while (key != -1);
    return 0;
}
 
//______________Dobavlenie_v_derevo______________________________________
int Add()
 
{
    char inGoods[20], inProducer[20];
    float inPrice;
    Tree* current, *new_r;
    cout<<"\n";
    gotoxy(3,3);cout<<"Ведите наименование товара:     ";
    gets(inGoods);
    gotoxy(3,5);cout<<"Введите производителя:  ";
    gets(inProducer);
    gotoxy(3,7);cout<<"Введите цену:     ";
    cin>>inPrice;
    if ((strlen(inGoods) !=0)  && (strlen(inProducer) !=0)) {
        Root = Addtree(Root, Root, inPrice, inGoods, inProducer);
        cout<<"\n";
        if (Root) {
            gotoxy(3,10);cout<<"ТОВАР         :"<<inGoods<<"\n"<<"\n";
            gotoxy(3,11);cout<<"ПРОИЗВОДИТЕЛЬ :"<<inProducer<<"\n"<<"\n";
            gotoxy(3,12);cout<<"ЦЕНА          :"<<inPrice<<"\n"<<"\n";
        }
        else cout<<"KOREN PUST"<<"\n";
    }
    else {
        gotoxy(10,10);
        cout<<"НЕ КОРЕКТНЫЙ ВВОД  - ПОПРОБУЙТЕ СНОВА!!!"<<"\n";
    }
    getch();
    clrscr();
    return(0);
}
 
//______________________________________________________________________
Tree *Addtree (Tree *root, // ykazatel' na koren' vsego dereva
            Tree *r,    // ykazatel' na koren' novogo poddereva
            float info, char tovar[20], char proizv[20]) // pole dannih
{
   if (!r){
        r = (Tree *) malloc(sizeof(Tree));
        if (!r){
            printf("Error in memory (1)\n");
            exit(0);
        }
        r->lPtr = NULL;
        r->rPtr = NULL;
        r->Price = info;
        strcpy(r->Goods,tovar);
        strcpy(r->Producer,proizv);
        if (!root)    return r;
        if (info<root->Price) root->lPtr = r;
        else root->rPtr = r;
        return root;
   }
 
  if (info < r->Price)
      Addtree(r, r->lPtr, info,tovar, proizv);
  else
      Addtree(r, r->rPtr, info, tovar, proizv);
  return root;
  }
 
//________________________________________________________________________
int Del_root()
{
   char Search_word[20];
   Tree *Search_root;
   cout<<"\n";
   cout<<"\n";
   gotoxy(3,3);cout<<"Ведите наименование товара для удаления  :";
   cin>>Search_word;
   *Search_root = Search(Root, Search_word);
   if ((Search_root != NULL) && (stricmp(Search_word,Search_root->Goods) == 0)) {
        gotoxy(10,5);cout<<"    Элемент найден:    "<<"\n";
        gotoxy(3,7);cout<<Search_root->Goods<<"\n";
        gotoxy(3,9);cout<<Search_root->Producer<<"\n";
        gotoxy(3,11);cout<<Search_root->Price<<"\n";
   }
   else {
        gotoxy(10,7);cout<<"Элемент не найден!!!"<<"\n";
        gotoxy(5,10); cout<<"             Press any key...";
        getch();
        return (1);
   }
   Root=Dtree(Root, Search_root->Price);
   gotoxy(7,13);cout<<"УДАЛЕНО..."<<"\n";
   getch();
   return(1);
}
//_________________________________________________________________________
Tree  *Dtree(Tree *root, float key){
     Tree *p, *p2;
     Tree *nil;
nil = NULL;
    if(!root) return root; //vershina ne naidena
    if(root->Price==key){  //ydaleniye kornya
    //pystoe derevo
       if(root->lPtr == root->rPtr){
            free(root);
            return nil;
       }else
         if(root->lPtr == NULL){  //esli odno iz podderev'ev pusto
                p = root->rPtr;
                free(root);
                return p;
         }else
                if(root->rPtr == NULL){
                    p = root->lPtr;
                    free(root);
                    return p;
                }
                else{
        // est' oba poddereva
                    p2 = root->rPtr;
                    p = root->rPtr;
                    while(p->lPtr != NULL)  p=p->lPtr;
                    p->lPtr = root->lPtr;
                    free(root);
                    return p2;
                }
     }
        if(root->Price < key) root->rPtr = Dtree(root->rPtr,key);
        else {
            if (root->Price > key) root->lPtr = Dtree(root->lPtr,key);
        };
        return root;
 
}
 
//___________________________________________________________________
int Print(Tree *Root1)
 
{
    int i;
    FormatPrint(Root1);
    getch();
    return(2);
}
//___________________________________________________________________
void FormatPrint(Tree *current)
{
int j;
j=0;
if (current != NULL)
   {
   i++;
   FormatPrint(current->rPtr);
   i--;
   for (j=0; j<=i; j++) cout<<"      ";
   Sleep(350);
   cout<<current->Price<<"\n";
   for (j=0; j<=i; j++) cout<<"      ";
   cout<<current->Goods<<"\n";
   for (j=0; j<=i; j++) cout<<"      ";
   cout<<current->Producer<<"\n";
   i++;
   FormatPrint(current->lPtr);
   i--;
   }
else
  {
  for (j=0; j<=i; j++) cout<<"      ";
   cout<<"\n"<<"\n"<<"\n";
  }
if (Root==NULL)
   {
   gotoxy(8,5);
   cout<<"ДЕРЕВО ПУСТО!";
   }
}
 
//________________________________________________________________________
 
int Min_el()
{
    Tree *current;
    current=Root;
    if (current != NULL) {
        while (current->lPtr != NULL) {
            current = current->lPtr;
        }
        gotoxy(3,3); cout<<" - Товар:          "<<current->Goods<<"\n";
        gotoxy(3,5); cout<<" - Производитель:  "<<current->Producer<<"\n";
        gotoxy(3,7); cout<<" - Цена:           "<<current->Price<<"\n";
        cout<<"\n\n";
        gotoxy(3,9); cout<<"             Press any key...";
    }
    else {
        gotoxy(8,8);
        cout<<"НЕ НАЙДЕНО!";
    }
  getch();
  return(4);
 
}
//_____REPLACE____________________________________________________________
int Replace()
{
   char search_word[20],Replace_good[20],search_creator[20];
   gotoxy(3,3);cout<<"Введете производителя для замены товара: ";
   cin>>search_creator;
   Search_root = Search_creat(Root, search_creator);
   if ((Search_root != NULL) && (stricmp(search_creator,Search_root->Producer) == 0)) {
        gotoxy(7,5);cout<<"     Элемент найден:    "<<"\n";
        gotoxy(3,6);cout<<Search_root->Goods<<"\n";
        gotoxy(3,8);cout<<Search_root->Producer<<"\n";
        gotoxy(3,10);cout<<Search_root->Price;
        gotoxy(3,13);cout<<"Заменить товар "<<Search_root->Goods<<" на товар :";
        cin>>Replace_good;
        for (int i=0;i<20;i++){
            Search_root->Goods[i] = NULL;
        }
        strcpy(Search_root->Goods,Replace_good);
        gotoxy(3,15);cout<<Search_root->Goods<<"\n";
        gotoxy(3,17);cout<<Search_root->Producer<<"\n";
        gotoxy(3,19);cout<<Search_root->Price<<"\n";
        gotoxy(15,23); cout<<"             Press any key...";
   }
   else {
        gotoxy(10,10);cout<<"ПРОИЗВОДИТЕЛЬ НЕ НАЙДЕН!!!"<<"\n";
        gotoxy(15,12); cout<<"             Press any key...";
   };
   getch();
   return(5);
}
//__________________________________________________________________________
Tree Search(Tree *Root_poisk, char Naimen[20])
{
   Tree *current;
   current=Root_poisk;
   if (current != NULL) {
       Search(current->lPtr, Naimen);
       Search(current->rPtr, Naimen);
       if (stricmp(Naimen, current->Goods) == 0) {
          element = current;
          return(*element);
       }
   }
   return(*element);
}
//POISC PO PROIZVODITELU
Tree * Search_creat(Tree *Root_poisk, char Creator[20])
{
    Tree *current;// *element;
    current=Root_poisk;
    if (current != NULL) {
        Search_creat(current->lPtr, Creator);
        Search_creat(current->rPtr, Creator);
        if (stricmp(Creator, current->Producer) == 0) {
            element = current;
        return(element);
        }
    }
    return(element);
}
/////////////////P_O_I_S_K_//////////////////
int POISK()
{
    char search_word[20];
    Tree *Search_root;
    cout<<"\n";
    gotoxy(3,3);cout<<"Введете наименование товара для поиска: ";
    cin>>search_word;
    *Search_root = Search(Root, search_word);
    if ((Search_root != NULL) && (stricmp(search_word,Search_root->Goods) == 0)) {
        gotoxy(10,5);cout<<"Элемент найден :"<<"\n";
        gotoxy(3,6);cout<<Search_root->Goods;
        gotoxy(3,7);cout<<Search_root->Producer;
        gotoxy(3,8);cout<<Search_root->Price<<"\n";
        gotoxy(10,12); cout<<"             Press any key...";
        getch();
    }
    else {
        gotoxy(10,5);cout<<"Элемент не найден!!!"<<"\n";
        gotoxy(10,7); cout<<"             Press any key...";
        getch();
    };
 
    return(3);
}
//________________VOHOD_____________________________________________
int exit()
   {
   return(-1);
   }
//__________________________________________________________________
int Kypcop(int k)
{
 char knopka;
    if (k!=(-1))
    while(1)
    if(kbhit())
             {
               knopka = getch();
               if (knopka == '\x0')
                {
                 knopka = getch();
                 if(knopka == 'H')
                     {
                     k = k - 1;
                     k = abs(k+N)%N;
                     return(k);
 
                     }
                 if(knopka=='P')
                     {
                     k++;
                     k = abs(k+N)%N;
                     return(k);
 
                     }
                }
               if (knopka == 13)
                  {
                  clrscr();
                  return(k+10);
                  }
             }
}
//_______________________________________________________________________
int Menu(int key)
{
    int j,i,kluch;
    int x_poz = 26, y_poz = 15;
    char fr = 219, ts = 187, tr = 188;
    const two = 2;
    char* punktu[N]={
                "1. Добавить товар",
                "2. Удалить товар ",
                "3. Распечатать...",
                "4. Поиск         ",
                "5. Мин элемент   ",
                "6. Замена        ",
                "7. Выход         "
               };
    clrscr();
    if (key != -1) {
        if (key < 10) {
            for (j=0; j<N; j++) {
                gotoxy(x_poz,y_poz);
                if (j == key)  {
                    cout<<fr<<fr<<">"<<strupr(punktu[j])<<"<"<<fr<<fr;
                    kluch = j;
                }
            else{
                cout<<strlwr(punktu[j]);
            }
            y_poz=y_poz+two;
            }
        }
    }
    gotoxy(60,34);cout<<"ст гр 11";
    gotoxy(60,35);cout<<"Пупкин Б.В.";
    gotoxy(60,36);cout<<"Вариант 99";
    return (kluch);
}

Всего записей: 898 | Зарегистр. 29-06-2002 | Отправлено: 21:17 18-12-2008
Открыть новую тему     Написать ответ в эту тему

На первую страницук этому сообщениюк последнему сообщению

Компьютерный форум Ru.Board » Компьютеры » Прикладное программирование » Задачи по C/С++


Реклама на форуме Ru.Board.

Powered by Ikonboard "v2.1.7b" © 2000 Ikonboard.com
Modified by Ru.B0ard
© Ru.B0ard 2000-2024

BitCoin: 1NGG1chHtUvrtEqjeerQCKDMUi6S6CG4iC

Рейтинг.ru