Advertisement
Guest User

Untitled

a guest
Aug 16th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.75 KB | None | 0 0
  1. // general_project_informatic.cpp: определяет точку входа для консольного приложения.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <conio.h>
  6. #include <string.h>
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9.  
  10.  
  11. class rmatrix
  12. {
  13. private:
  14.  
  15.     struct list
  16.     {
  17.     int x,y,val;
  18.     list *next;
  19.     };
  20. list *hd;
  21.  
  22. public:
  23. int X,Y;
  24.     ~rmatrix()
  25.     {
  26.     while(hd->next!=NULL)
  27.         {
  28.         list *p=hd;
  29.         hd=hd->next;
  30.         delete p;
  31.         }
  32.  
  33.     }
  34.  
  35.     rmatrix()
  36.     {
  37.         list *hd=new list;
  38.         hd->next=NULL;
  39.         hd->val=0;
  40.         hd->x=0;
  41.         hd->y=0;
  42.     }
  43.  
  44.     rmatrix(char *str)                                                     
  45.     {                                                                      
  46.     int i=0,pot=0,sz=1;
  47.     list *p;
  48.     p=new list;
  49.     hd=p;
  50.    
  51.  
  52.     //char *celem=(char*)malloc(sz*sizeof(char*));
  53.     char* celem=NULL;
  54.     celem=(char*)realloc(celem,sz);
  55.  
  56.    
  57.     while(str[i]==' ')  i++;
  58.    
  59.    
  60.     do {
  61.        
  62.         while(str[i]!=' ' && str[i]!='\n' && str[i]!='\0')
  63.             {  
  64.             celem[sz-1]=str[i];
  65.             i++;
  66.             celem=(char*)realloc(celem,sz+1);
  67.             sz++;
  68.             }
  69.         celem[sz-1]='\0';
  70.        
  71.         pot++;
  72.         //celem-class
  73.         switch(pot)
  74.             {
  75.             case 1: p->x=TOI(celem); if(X<(p->x))   X=p->x; break;
  76.             case 2: p->y=TOI(celem); if(Y<(p->y))   Y=p->y; break;
  77.             case 3: p->val=TOI(celem);  break;
  78.             default: printf("\nError!!!!"); return ;
  79.             }
  80.        
  81.        
  82.         sz=1;
  83.         celem=(char*)realloc(celem,sz);
  84.         i++;
  85.        
  86.         if(pot==3)
  87.             {
  88.                 if(str[i]!=' ' && str[i]!='\n' && str[i]!='\0')
  89.                 {
  90.                     p->next=new list;
  91.                     p=p->next;
  92.                 }
  93.                 else p->next=NULL;
  94.             pot=0;
  95.             }
  96.     }
  97.     while(str[i]!=' ' && str[i]!='\n' && str[i]!='\0');
  98.    
  99.    
  100.  
  101. }
  102.     rmatrix(rmatrix &T)
  103. {
  104.     list *p1, *p2;
  105.     hd=new list;
  106.     p2=hd;
  107.     for(p1=T.hd; p1->next!=NULL; p1=p1->next)
  108.     {
  109.     p2->x=p1->x;
  110.     p2->y=p1->y;
  111.     p2->val=p1->val;
  112.     p2->next=new list;
  113.     p2=p2->next;
  114.     }
  115. p2->x=p1->x;
  116. p2->y=p1->y;
  117. p2->val=p1->val;
  118. p2->next=NULL;
  119. }
  120. int TOI(char *line)
  121.     {
  122.     int a=0,ten=1,i=0;
  123.         for(int j =strlen(line)-1; j>=0; j--)
  124.             {
  125.                 i++;
  126.                 if(i==1);
  127.                 else for(int i=0; i<strlen(line)-1; i++) ten*=10;
  128.                 a+=(line[j]-'0')*ten;
  129.             }
  130.         return a;
  131.     }
  132. int &get(int x, int y)
  133. {
  134.     static int foo=0;
  135.     list *p=hd;
  136.     do
  137.     {
  138.         if(p->x==x && p->y==y)
  139.         {
  140.             return p->val;
  141.             break;
  142.         }
  143.     p=p->next;
  144.     }
  145.     while(p!=NULL);
  146.  
  147.     return foo;
  148. }
  149.  
  150. void classprint()
  151. {
  152.     printf("\nPrinting of chose class...\n ");
  153.     list *p;
  154.     //printf("_______________________\n");
  155.     //printf("/d |%d| |%d|  val=|%d| //", hd->x, hd->y, hd->val);
  156.     if(hd->next!=NULL)  p=hd;
  157.     else return;
  158.     do
  159.     {
  160.     printf("\n|%d|\t|%d|\tval=|%d| ", p->x, p->y, p->val);
  161.     p=p->next;
  162.     }
  163.     while(p!=NULL);
  164. }
  165.  
  166.  
  167. void printy()
  168. {
  169.     int i,j;
  170.     list *p;
  171.    
  172.     for(i=1; i<=X; i++)
  173.     {
  174.         for(j=1; j<=Y; j++)
  175.         {
  176.             for(p=hd; p!=NULL; p=p->next)
  177.             {
  178.                 if(p->x==j && p->y==i)
  179.                 {  
  180.                     printf("\t|%d|",get(i,j));
  181.                     goto A1;
  182.                 }
  183.             }
  184.            
  185.             printf("\t0");
  186. A1:        
  187.         ;
  188.             }
  189.         printf("\n");
  190.     }  
  191.  
  192. //printf("\n \t|%d|",get(X,Y));
  193. }
  194.  
  195.  
  196.  
  197. void size()
  198. {
  199.     list *p;
  200.     for(p=hd; p->next!=NULL; p=p->next)
  201.         {
  202.             if(p->x>X)  X=p->x;
  203.             if(p->y>Y)  Y=p->y;
  204.         }
  205. }
  206. int f_set()
  207. {   //1 - элемент найден
  208.     //2 - элемент создан
  209.     list *p;
  210.     int k,l,v;
  211.     printf("\nPlease, enter (x,y, val) - ");
  212.     scanf("%d%d%d",&k,&l,&v);
  213.     //проверка существования заданного элемента
  214.    
  215.     for(p=hd; p->next!=NULL; p=p->next)
  216.         if(p->x==k || p->y==l)  // элемент найден
  217.         {
  218.             p->val=v;
  219.             return 1;
  220.         }
  221.        
  222.     for(p=hd; p->next!= NULL; p=p->next);   //перемотка на последний элемент
  223.  
  224.     // создание нового элемента
  225.     p->next=new list;
  226.     p=p->next;
  227.     p->x=k;
  228.     p->y=l;
  229.     p->val=v;
  230.     //переопределение размерности матрицы 
  231.     if(k>X) X=k;
  232.     if(l>Y) Y=l;
  233.  
  234.     /*(*this).*/printy();
  235.  
  236. }
  237. void f_get()
  238. {
  239.     int check;
  240.     printf("\nDo you wanna see any member?\t(1-9 ~ yes)");
  241.        
  242.         scanf("%d",&check);
  243.         if(check>=0 && check <=9)
  244.         {
  245.             printf("\nType (x,y) of element - ");
  246.             int i,j;
  247.             scanf("%d%d",&i,&j);
  248.             printf("\nCheck: \n\n\t|%d|",(*this).get(i,j));
  249.         }
  250. }
  251.  
  252. };
  253.  
  254.  
  255.  
  256. void main()
  257. {
  258.     char s[]={"5 2 55 1 1 11 1 2 12 1 3 13 2 1 21 2 2 22 2 3 23 3 1 31 3 2 32 3 3 33 "};
  259.     //char s[]={"  1 1 11 1 2 12 2 1 21 2 2 22 "};
  260.     //char s[]={"1 1 11 1 2 12 1 3 13 1 4 14 2 1 21 2 2 22 2 3 23 2 4 24 3 1 31 3 2 32 3 3 33 3 4 34 4 1 41 4 2 42 4 3 43 4 4 44 "};
  261.         /*char *s=new char[80];
  262.         printf("Please, enter string in format: (x,y,val)");
  263.         gets(s);*/
  264.    
  265.         rmatrix ex(s);
  266.         rmatrix ex2=ex;
  267.        
  268.         ex.printy();
  269.         ex.size();
  270.         printf("\n\nX= %d, Y=%d\n",ex.X,ex.Y);
  271.        
  272.        
  273.         printf("\n\n\n");
  274.         printf("\nPRESS 1- get value, 2 - set value");
  275.         char check;
  276.         scanf("%c",&check);
  277.         switch(check)
  278.         {
  279.             case '1': ex.f_get(); break;
  280.             case '2': ex.f_set(); break;
  281.             default: printf("\nDefault case ");
  282.         }
  283.         getch();
  284. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement