Advertisement
Guest User

Teza

a guest
Dec 8th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 38.07 KB | None | 0 0
  1. //1. Simple lists as Stacks
  2. #include<iostream>
  3. using namespace std;
  4.  
  5. class element{
  6. private:
  7. int info;
  8. class element *next;
  9. public:
  10. element(){ next=NULL; }
  11. int set_value(int this_value){ info=this_value; }
  12. int reveal_value(){ return info; }
  13. int set_next(element *next_element){ next=next_element; }
  14. element *reveal_next(){ return next; }
  15. };
  16.  
  17. class some_list{
  18. private:
  19. element *start,*curent,*sfarsit;
  20. int lungime;
  21. public:
  22. some_list(){
  23. start=curent=sfarsit=NULL;
  24. lungime=0;
  25. }
  26. int reveal_lenght(){ return lungime; }
  27. int set_lenght(int new_lenght){ lungime=new_lenght; }
  28. int print_me(){
  29. element *walker=start;
  30. cout<<"START : ";
  31. if (reveal_lenght()>=1){
  32. while(walker->reveal_next()!=NULL){
  33. cout<<walker->reveal_value()<<" -> ";
  34. walker=walker->reveal_next();
  35. }
  36. cout<<walker->reveal_value(); cout<<" : STOP ";
  37. return 1;
  38. }
  39. cout<<" : STOP "<<endl;
  40. return 1;
  41. }
  42. int set_start(element *new_start){ start=new_start; }
  43. int set_curent(element *new_curent){ curent=new_curent; }
  44. int set_sfarsit(element *new_sfarsit){ sfarsit=new_sfarsit; }
  45. element *reveal_sfarsit(){ return sfarsit; }
  46. element *reveal_start(){ return start; }
  47. };
  48.  
  49. class some_stack:public some_list{
  50. public:
  51. some_stack(){
  52. set_start(NULL);
  53. set_curent(NULL);
  54. set_sfarsit(NULL);
  55. set_lenght(0);
  56. }
  57. int push(int value);
  58. int pop(int*old_value);
  59. int stack_empty();
  60. };
  61. some_stack L;
  62. int main(){
  63. int old_top;
  64. L.push(13);
  65. L.push(17);
  66. L.print_me();
  67. L.pop(&old_top);
  68. cout<<endl<<old_top<<endl;
  69. L.print_me();
  70. return 1;
  71. }
  72.  
  73. int some_stack::stack_empty(){
  74. if(reveal_lenght()>=1) return 0;
  75. return 1;
  76. }
  77. int some_stack::push(int value){
  78. element *new_one=new element;
  79. if(new_one==NULL) return 0;
  80. new_one->set_value(value);
  81. new_one->set_next(NULL);
  82. if(stack_empty()){
  83. set_start(new_one);
  84. set_curent(new_one);
  85. set_sfarsit(new_one);
  86. set_lenght(1);
  87. }
  88. else{
  89. reveal_sfarsit()->set_next(new_one);
  90. set_curent(new_one);
  91. set_sfarsit(new_one);
  92. set_lenght(reveal_lenght()+1);
  93. }
  94. return 1;
  95. }
  96. int some_stack::pop(int *old_value){
  97. if(stack_empty()) return 0;
  98. if (reveal_lenght()==1){
  99. *old_value=reveal_sfarsit()->reveal_value();
  100. delete reveal_sfarsit();
  101. set_lenght(0);
  102. set_start(NULL);
  103. set_curent(NULL);
  104. set_sfarsit(NULL);
  105. return 1;
  106. }
  107. else{
  108. element *walker=reveal_start();
  109. while(walker->reveal_next()!=reveal_sfarsit()) walker=walker->reveal_next();
  110. *old_value=reveal_sfarsit()->reveal_value();
  111. delete reveal_sfarsit();
  112. set_lenght(reveal_lenght()-1);
  113. set_sfarsit(walker);
  114. set_curent(walker);
  115. walker->set_next(NULL);
  116. return 1;
  117. }
  118. }
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125. //2. Simple lists as Queue
  126. #include<iostream>
  127. using namespace std;
  128.  
  129. class element{
  130. private:
  131. int info;
  132. class element *next;
  133. public:
  134. element(){ next=NULL; }
  135. int set_value(int this_value){ info=this_value; }
  136. int reveal_value(){ return info; }
  137. int set_next(element *next_element){ next=next_element; }
  138. element *reveal_next(){ return next; }
  139. };
  140.  
  141. class some_list{
  142. private:
  143. element *start,*curent,*sfarsit;
  144. int lungime;
  145. public:
  146. some_list(){
  147. start=curent=sfarsit=NULL;
  148. lungime=0;
  149. }
  150. int reveal_lenght(){ return lungime; }
  151. int set_lenght(int new_lenght){ lungime=new_lenght; }
  152. int print_me(){
  153. element *walker=start;
  154. cout<<"START : ";
  155. if (reveal_lenght()>=1){
  156. while(walker->reveal_next()!=NULL){
  157. cout<<walker->reveal_value()<<" -> ";
  158. walker=walker->reveal_next();
  159. }
  160. cout<<walker->reveal_value(); cout<<" : STOP ";
  161. return 1;
  162. }
  163. cout<<" : STOP "<<endl;
  164. return 1;
  165. }
  166. int set_start(element *new_start){ start=new_start; }
  167. int set_curent(element *new_curent){ curent=new_curent; }
  168. int set_sfarsit(element *new_sfarsit){ sfarsit=new_sfarsit; }
  169. element *reveal_sfarsit(){ return sfarsit; }
  170. element *reveal_start(){ return start; }
  171. };
  172. class some_queue:public some_list{
  173. public:
  174. some_queue(){
  175. set_start(NULL);
  176. set_curent(NULL);
  177. set_sfarsit(NULL);
  178. set_lenght(0);
  179. }
  180. int add(int value);
  181. int serve(int*old_value);
  182. int queue_empty();
  183. };
  184. some_queue L;
  185. int main(){
  186. int old_start;
  187. L.add(13);
  188. L.add(17);
  189. L.print_me();
  190. L.serve(&old_start);
  191. cout<<endl<<old_start<<endl;
  192. L.print_me();
  193. return 1;
  194. }
  195. int some_queue::queue_empty(){
  196. if(reveal_lenght()>=1) return 0;
  197. return 1;
  198. }
  199. int some_queue::add(int value){
  200. element *new_one=new element;
  201. if(new_one==NULL) return 0;
  202. new_one->set_value(value);
  203. new_one->set_next(NULL);
  204. if(queue_empty()){
  205. set_start(new_one);
  206. set_curent(new_one);
  207. set_sfarsit(new_one);
  208. set_lenght(1);
  209. }
  210. else{
  211. reveal_sfarsit()->set_next(new_one);
  212. set_curent(new_one);
  213. set_sfarsit(new_one);
  214. set_lenght(reveal_lenght()+1);
  215. }
  216. return 1;
  217. }
  218.  
  219. int some_queue::serve(int *old_value){
  220. if(queue_empty()) return 0;
  221. if (reveal_lenght()==1){
  222. *old_value=reveal_sfarsit()->reveal_value();
  223. delete reveal_sfarsit();
  224. set_lenght(0);
  225. set_start(NULL);
  226. set_curent(NULL);
  227. set_sfarsit(NULL);
  228. return 1;
  229. }
  230. else{
  231. *old_value=reveal_start()->reveal_value();
  232. element *new_start=reveal_start()->reveal_next();
  233. delete reveal_start();
  234. set_start(new_start);
  235. set_lenght(reveal_lenght()-1);
  236. return 1;
  237. }
  238. }
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245. //3. Simple lists as NoRestriction
  246. #include<iostream>
  247. using namespace std;
  248.  
  249. class element{
  250. private: int info;
  251. class element *next;
  252. public: element(){ next=NULL;};
  253. ~element(){ };
  254. int set_value(int this_value){ info=this_value; }
  255. int reveal_value(){ return info; }
  256. int set_next(element *next_element){ next=next_element; }
  257. element *reveal_next(){ return next; }
  258. };
  259.  
  260. class some_list{
  261. private: element *start,*curent,*sfarsit;
  262. int lungime;
  263. public: some_list(){ start=curent=sfarsit=NULL;lungime=0; }
  264. int set_lenght(int new_lenght){ lungime=new_lenght; }
  265. int set_start(element *new_start){ start=new_start; }
  266. int set_curent(element *new_curent){ curent=new_curent; }
  267. int set_end(element *new_sfarsit){ sfarsit=new_sfarsit; }
  268. element *reveal_end(){ return sfarsit; }
  269. element *reveal_start(){ return start; }
  270. int reveal_lenght(){ return lungime; }
  271. element *reveal_current(){ return curent; }
  272. int print_me(){ cout<<endl;element *walker=start; cout<<"START : ";
  273. int counter=reveal_lenght();
  274. if (counter>=1){while(counter>=2){
  275. cout<<walker->reveal_value()<<" -> ";
  276. walker=walker->reveal_next();counter--;}
  277. cout<<walker->reveal_value();}
  278. cout<<" : STOP ";return 1;}
  279. };
  280.  
  281. class no_strategy:public some_list{
  282. public: int find_value(int this_value);
  283. int addsome ( int value, int direction );
  284. int delsome(int *old_value,int direction);
  285. int list_empty();
  286. };
  287. no_strategy L;
  288.  
  289. int main(){
  290. int old;
  291. L.addsome(1,1);
  292. L.addsome(2,1);
  293. L.addsome(3,1);
  294. L.print_me();
  295. L.find_value(2);
  296. L.delsome(&old,0);
  297. L.print_me();
  298. }
  299.  
  300. int no_strategy::list_empty(){
  301. if(reveal_lenght()>=1) return 0;
  302. return 1;
  303. }
  304.  
  305. int no_strategy::find_value(int this_value){
  306. element *walker=reveal_start();
  307. if (reveal_lenght()>=1) {
  308. while(walker->reveal_next()!=NULL){
  309. if (walker->reveal_value()==this_value) break ;
  310. walker=walker->reveal_next();
  311. }
  312. if (walker->reveal_value()==this_value) {
  313. set_curent(walker);
  314. return 1;
  315. }
  316. }
  317. else return 0;
  318. }
  319.  
  320. int no_strategy::addsome(int value,int direction){
  321. element *new_one=new element;
  322. new_one->set_value(value);
  323. if (list_empty()){
  324. new_one->set_next(NULL);
  325. set_lenght(1);
  326. set_curent(new_one);
  327. set_start(new_one);
  328. set_end(new_one);
  329. return -1;
  330. }
  331. if(direction==0){
  332. if (reveal_current()==reveal_start()){
  333. new_one->set_next(reveal_current());
  334. set_curent(new_one);
  335. set_start(new_one);
  336. set_lenght(reveal_lenght()+1);
  337. return -1;
  338. }
  339. else{
  340. element *walker=reveal_start();
  341. while(walker->reveal_next()!=reveal_current()) walker=walker->reveal_next();
  342. new_one->set_next(reveal_current());
  343. walker->set_next(new_one);
  344. set_lenght(reveal_lenght()+1);
  345. return 1;
  346. }
  347. }
  348. else{
  349. if(reveal_current()->reveal_next()==NULL){
  350. if (new_one==NULL) return 0;
  351. new_one->set_next(NULL);
  352. reveal_end()->set_next(new_one);
  353. set_curent(new_one);
  354. set_end(new_one);
  355. set_lenght(reveal_lenght()+1);
  356. return -1;
  357. }
  358. else{
  359. new_one->set_next(reveal_current()->reveal_next());
  360. reveal_current()->set_next(new_one);
  361. set_curent(new_one);
  362. set_lenght(reveal_lenght()+1);
  363. return 1;
  364. }
  365. }
  366. }
  367.  
  368. int no_strategy::delsome(int *old_value,int direction){
  369. if(list_empty()) return 0;
  370. if(reveal_lenght()==1){
  371. *old_value=reveal_current()->reveal_value();
  372. delete reveal_current();
  373. set_curent(NULL);
  374. set_start(NULL);
  375. set_end(NULL);
  376. set_lenght(0);
  377. return -1;
  378. }
  379. if(direction==0){
  380. if (reveal_current()==reveal_start()) {
  381. *old_value=reveal_current()->reveal_value();
  382. element *aux,*aux1;
  383. int old_lenght;
  384. old_lenght=reveal_lenght();
  385. aux1=reveal_current();
  386. aux=reveal_current();
  387. aux=aux->reveal_next();
  388. set_curent(aux);
  389. set_start(aux);
  390. set_lenght(old_lenght-1);
  391. delete aux1;
  392. return 1;
  393. }
  394. if (reveal_current()==reveal_end()) {
  395. *old_value=reveal_current()->reveal_value();
  396. int old_lenght;
  397. old_lenght=reveal_lenght();
  398. element *walker=reveal_start();
  399. while(walker->reveal_next()!=reveal_end())
  400. walker=walker->reveal_next();
  401. element *aux=new element;
  402. aux=reveal_current();
  403. set_curent(walker);
  404. set_end(walker);
  405. set_lenght(old_lenght-1);
  406. delete aux;
  407. return 1;
  408. }
  409. *old_value=reveal_current()->reveal_value();
  410. int old_lenght;
  411. old_lenght=reveal_lenght();
  412. element *walker=reveal_start();
  413. while(walker->reveal_next()!=reveal_current()) walker=walker->reveal_next();
  414. element *some_fork=reveal_current()->reveal_next();
  415. delete reveal_current();
  416. walker->set_next(some_fork);
  417. set_curent(walker);
  418. set_lenght(old_lenght-1);
  419. }
  420. else{
  421. if(reveal_current()==reveal_start()){
  422. *old_value=reveal_current()->reveal_value();
  423. element *dead_one=reveal_start();
  424. set_curent(reveal_start()->reveal_next());
  425. set_start(reveal_current());
  426. set_lenght(reveal_lenght()-1);
  427. delete dead_one;
  428. return 1;
  429. }
  430. if(reveal_current()==reveal_end()){
  431. *old_value=reveal_current()->reveal_value();
  432. element *aux=new element;
  433. aux=reveal_current();
  434. element *walker=reveal_start();
  435. while(walker->reveal_next()!=reveal_current())
  436. walker=walker->reveal_next();
  437. set_curent(walker);
  438. set_end(walker);
  439. set_lenght(reveal_lenght()-1);
  440. delete aux;
  441. return 1;
  442. }
  443. else{
  444. *old_value=reveal_current()->reveal_value();
  445. element *aux=new element;
  446. aux=reveal_current();
  447. element *walker=reveal_start();
  448. while(walker->reveal_next()!=reveal_current())
  449. walker=walker->reveal_next();
  450. element *some_fork=reveal_current()->reveal_next();
  451. walker->set_next(some_fork);
  452. set_curent(some_fork);
  453. set_lenght(reveal_lenght()-1);
  454. delete aux;
  455. return 1;
  456. }
  457. }
  458. }
  459.  
  460.  
  461.  
  462.  
  463. //4. program #include<iostream>
  464. using namespace std;
  465. class element
  466. {
  467. private:
  468. int info;
  469. class element *next;
  470. public:
  471. element(){ next=NULL; }
  472. int set_value(int this_value){ info=this_value; }
  473. int reveal_value(){ return info; }
  474. int set_next(element *next_element){ next=next_element; }
  475. element *reveal_next(){ return next; }
  476. };
  477. class some_list
  478. {
  479. private:
  480. element *start,*curent,*sfarsit;
  481. int lungime;
  482. public:
  483. some_list()
  484. {
  485. start=curent=sfarsit=NULL;
  486. lungime=0;
  487. }
  488. int set_lenght(int new_lenght){ lungime=new_lenght; }
  489. int set_start(element *new_start){ start=new_start; }
  490. int set_curent(element *new_curent){ curent=new_curent; }
  491. int set_sfarsit(element *new_sfarsit){ sfarsit=new_sfarsit; }
  492. element *reveal_sfarsit(){ return sfarsit; }
  493. int reveal_lenght(){ return lungime; }
  494. element *reveal_current(){ return curent; }
  495. int print_me(){
  496. cout<<endl;
  497. element *walker=start;
  498. cout<<" START : ";
  499. if (reveal_lenght()>=1){
  500. while(walker->reveal_next()!=NULL){
  501. cout<<walker->reveal_value()<<" -> ";
  502. walker=walker->reveal_next();
  503. }
  504. cout<<walker->reveal_value();
  505. cout<<" : STOP ";
  506. return 1;
  507. }
  508. cout<<" : STOP ";return 0;
  509. }
  510. element *reveal_start(){ return start; }
  511. };
  512. class some_stack:public some_list
  513. {
  514. public:
  515. some_stack()
  516. {
  517. set_start(NULL);
  518. set_curent(NULL);
  519. set_sfarsit(NULL);
  520. set_lenght(0);
  521. };
  522. int push(int value);
  523. int stack_empty();
  524. };
  525. class some_queue:public some_list{
  526. public:
  527. some_queue(){
  528. set_start(NULL);
  529. set_curent(NULL);
  530. set_sfarsit(NULL);
  531. set_lenght(0);
  532. };
  533. int add(int value);
  534. int serve(int *old_topvalue);
  535. int queue_empty();
  536. };
  537. class no_strategy:public some_stack,public some_queue{
  538. public :
  539. int find_value(int this_value) {
  540. element *walker=some_stack::reveal_start();
  541. if (some_stack::reveal_lenght()>=1) {
  542. while(walker->reveal_next()!=NULL){
  543. if (walker->reveal_value()==this_value) break ;
  544. walker=walker->reveal_next();
  545. }
  546. if (walker->reveal_value()==this_value) {
  547. some_stack::set_curent(walker);
  548. return 1;
  549. }
  550. }
  551. else return 0;
  552. }
  553. int addsome ( int value, int direction ){
  554. if (stack_empty()){
  555. element *new_one;
  556. new_one->set_value(value);
  557. new_one->set_next(NULL);
  558. some_stack::set_lenght(1);
  559. some_stack::set_curent(new_one);
  560. some_stack::set_start(new_one);
  561. some_stack::set_sfarsit(new_one);
  562. return -1;
  563. }
  564. if (direction==0) {
  565. if (some_stack::reveal_current()==some_stack::reveal_start()){
  566. element *new_one;
  567. new_one->set_value(value);
  568. new_one->set_next(some_stack::reveal_current());
  569. some_stack::set_curent(new_one);
  570. some_stack::set_start(new_one);
  571. some_stack::set_lenght(some_stack::reveal_lenght()+1);
  572. return -1;
  573. }
  574. else {
  575. element *new_one=new element;
  576. new_one->set_value(value);
  577. new_one->set_next(some_stack::reveal_current());
  578. element *walker=some_stack::reveal_start();
  579. while(walker->reveal_next()!=some_stack::reveal_current())
  580. walker=walker->reveal_next();
  581. new_one->set_next(some_stack::reveal_current());
  582. walker->set_next(new_one);
  583. some_stack::set_curent(new_one);
  584. some_stack::set_lenght(some_stack::reveal_lenght()+1);
  585. return 1;
  586. }
  587. }
  588. else{
  589. if(some_stack::reveal_current()==some_stack::reveal_sfarsit()){
  590. element *new_one=new element;
  591. new_one->set_value(value);
  592. new_one->set_next(NULL);
  593. some_stack::reveal_sfarsit()->set_next(new_one);
  594. some_stack::set_curent(new_one);
  595. some_stack::set_sfarsit(new_one);
  596. some_stack::set_lenght(some_stack::reveal_lenght()+1);
  597. return -1;
  598. }
  599. else{
  600. element *new_one=new element;
  601. new_one->set_value(value);
  602. new_one->set_next(some_stack::reveal_current()->reveal_next());
  603. some_stack::reveal_current()->set_next(new_one);
  604. some_stack::set_curent(new_one);
  605. some_stack::set_lenght(some_stack::reveal_lenght()+1);
  606. return 1;
  607. }
  608. }
  609. return 1;
  610. }
  611. };
  612. no_strategy L;
  613. int main(){
  614. L.push(1);
  615. L.push(3);
  616. L.push(4);
  617. L.some_stack::print_me();
  618. L.find_value(1);
  619. L.addsome(2,1);
  620. L.some_stack::print_me();
  621. return 0;
  622. }
  623. int some_stack::stack_empty(){
  624. if(this->reveal_lenght()>=1) return 0;
  625. return 1;
  626. }
  627. int some_stack::push(int value)
  628. {
  629. element *new_one=new element;
  630. if (new_one==NULL) return 0;
  631. new_one->set_value(value);
  632. new_one->set_next(NULL);
  633. if (reveal_lenght()==0){
  634. set_start(new_one);
  635. set_curent(new_one);
  636. set_sfarsit(new_one);
  637. set_lenght(1);}
  638. else{
  639. reveal_sfarsit()->set_next(new_one);
  640. set_curent(new_one);
  641. set_sfarsit(new_one);
  642. set_lenght(reveal_lenght()+1);}
  643. return 1;
  644. }
  645.  
  646.  
  647.  
  648.  
  649. //5.#include<iostream>
  650. using namespace std;
  651. class element{
  652. private:
  653. int info;
  654. class element *next;
  655. public:
  656. element(){ next=NULL; }
  657. int set_value(int this_value){ info=this_value; }
  658. int reveal_value(){ return info; }
  659. int set_next(element *next_element){ next=next_element; }
  660. element *reveal_next(){ return next; }
  661. };
  662.  
  663. class some_list{
  664. private:
  665. element *start,*curent,*sfarsit;
  666. int lungime;
  667. public:
  668. some_list(){
  669. start=curent=sfarsit=NULL;
  670. lungime=0;
  671. }
  672. int set_lenght(int new_lenght){ lungime=new_lenght; }
  673. int set_start(element *new_start){ start=new_start; }
  674. int set_curent(element *new_curent){ curent=new_curent; }
  675. int set_sfarsit(element *new_sfarsit){ sfarsit=new_sfarsit; }
  676.  
  677. element *reveal_sfarsit(){ return sfarsit; }
  678. int reveal_lenght(){ return lungime; }
  679. element *reveal_current(){ return curent; }
  680. int print_me(){ cout<<endl;
  681. element *walker=start; cout<<" START : ";
  682. if (reveal_lenght()>=1) {
  683. while(walker->reveal_next()!=NULL){
  684. cout<<walker->reveal_value()<<" -> ";
  685. walker=walker->reveal_next();}
  686. cout<<walker->reveal_value(); cout<<" : STOP ";return 1;}
  687.  
  688. cout<<" : STOP ";return 0;}
  689. element *reveal_start(){ return start; }
  690. };
  691.  
  692. class some_stack:public some_list{
  693. public:
  694. some_stack(){
  695. set_start(NULL);
  696. set_curent(NULL);
  697. set_sfarsit(NULL);
  698. set_lenght(0);
  699. };
  700. int push(int value);
  701. // int pop(int *old_topvalue);
  702. int stack_empty();
  703. };
  704.  
  705. class some_queue:public some_list{
  706. public:
  707. some_queue(){
  708. set_start(NULL);
  709. set_curent(NULL);
  710. set_sfarsit(NULL);
  711. set_lenght(0);
  712. };
  713. int add(int value); // identica cu push
  714. int serve(int *old_topvalue); // se elimina doar primul element din lista
  715. int queue_empty(); // identica cu stack_empty
  716. };
  717.  
  718. class no_strategy:public some_stack,public some_queue
  719. {
  720. public :
  721. // find_value cauta incepand cu primul element pana la ultimul o anumita valoare
  722. // daca valoare NU este gasit metoda intoarce FALSE 0
  723. // pentru prima situatie (!) cand valoarea este gasita pozitionam element curent
  724. // in acel element si metoda intoarce TRUE 1
  725. int find_value(int this_value) {
  726. element *walker=some_stack::reveal_start();
  727. if (some_stack::reveal_lenght()>=1) {
  728. while(walker->reveal_next()!=NULL){
  729. if (walker->reveal_value()==this_value) break ;
  730. walker=walker->reveal_next();
  731. }
  732. if (walker->reveal_value()==this_value) {
  733. some_stack::set_curent(walker);
  734. return 1;
  735. }
  736. }
  737.  
  738. else return 0;
  739. }
  740.  
  741.  
  742. int addsome ( int value, int direction )
  743. {
  744. if (stack_empty()) {
  745. element *new_one;
  746. new_one->set_value(value);
  747. new_one->set_next(NULL);
  748. some_stack::set_lenght(1);
  749. some_stack::set_curent(new_one);
  750. some_stack::set_start(new_one);
  751. some_stack::set_sfarsit(new_one);
  752. return -1;
  753. }
  754. if (direction==0) {
  755. if (some_stack::reveal_current()==some_stack::reveal_start())
  756. {
  757. // .... (!) // pentru curent pe primul element
  758. return -1;
  759. }
  760. else {
  761. element *new_one=new element;
  762. new_one->set_value(value);
  763. new_one->set_next(some_stack::reveal_current());
  764. element *walker=some_stack::reveal_start();
  765. while(walker->reveal_next()!=some_stack::reveal_current())
  766. walker=walker->reveal_next();
  767. new_one->set_next(some_stack::reveal_current());
  768. walker->set_next(new_one);
  769. some_stack::set_lenght(some_stack::reveal_lenght()+1);
  770. return 1;
  771. }
  772. }
  773.  
  774. //.... (!) pentru direction!=0 adica adaugarea la dreapta
  775. return 1;
  776. }
  777. };
  778.  
  779. no_strategy L;
  780. int main(){
  781. L.push(1);
  782. L.push(2);
  783. L.push(3);
  784. // lista are valorile 1 2 3
  785. // a fost construita prin apeluri push => deci "curent" este pe ultimul element
  786. L.addsome(0,0);
  787. // adaug 0 la stanga element
  788. // deci lista trebuie sa fie 1 2 0 3
  789. L.some_stack::print_me();
  790. // apelez "print_me" la care am acces prin mostenire public la ambii parinti
  791. // some_queue si some_stack
  792. // si utilizez rezolutie :: pentru a referi acea metoda "print_me" din
  793. // some_stack
  794. L.find_value(3); // !!!
  795. cout<<endl<<(L.some_stack::reveal_current())->reveal_value()<<endl;
  796. L.addsome(0,0);
  797. L.some_stack::print_me(); // sa avem 1 0 2 0 3
  798.  
  799. return 0;
  800. }
  801.  
  802. int some_stack::stack_empty(){
  803. if(this->reveal_lenght()>=1) return 0;
  804. return 1;
  805. }
  806.  
  807. int some_stack::push(int value)
  808. {
  809. element *new_one=new element;
  810. if (new_one==NULL) return 0;
  811. new_one->set_value(value);
  812. new_one->set_next(NULL);
  813. if (reveal_lenght()==0){
  814. set_start(new_one);
  815. set_curent(new_one);
  816. set_sfarsit(new_one);
  817. set_lenght(1);}
  818. else{
  819. reveal_sfarsit()->set_next(new_one);
  820. set_curent(new_one);
  821. set_sfarsit(new_one);
  822. set_lenght(reveal_lenght()+1);}
  823. return 1;
  824. }
  825.  
  826.  
  827.  
  828.  
  829.  
  830.  
  831. //6.#include<iostream>
  832. using namespace std;
  833. class element{
  834. private:
  835. int info;
  836. class element *next;
  837. public:
  838. element(){ next=NULL; }
  839. int set_value(int this_value){ info=this_value; }
  840. int reveal_value(){ return info; }
  841. int set_next(element *next_element){ next=next_element; }
  842. element *reveal_next(){ return next; }
  843. };
  844.  
  845. class some_list{
  846. private:
  847. element *start,*curent,*sfarsit;
  848. int lungime;
  849. public:
  850. some_list(){
  851. start=curent=sfarsit=NULL;
  852. lungime=0;
  853. }
  854. int set_lenght(int new_lenght){ lungime=new_lenght; }
  855. int set_start(element *new_start){ start=new_start; }
  856. int set_curent(element *new_curent){ curent=new_curent; }
  857. int set_sfarsit(element *new_sfarsit){ sfarsit=new_sfarsit; }
  858.  
  859. element *reveal_sfarsit(){ return sfarsit; }
  860. int reveal_lenght(){ return lungime; }
  861. element *reveal_current(){ return curent; }
  862. int print_me(){ cout<<endl;
  863. element *walker=start; cout<<" START : ";
  864. if (reveal_lenght()>=1) {
  865. while(walker->reveal_next()!=NULL){
  866. cout<<walker->reveal_value()<<" -> ";
  867. walker=walker->reveal_next();}
  868. cout<<walker->reveal_value(); cout<<" : STOP ";return 1;}
  869.  
  870. cout<<" : STOP ";return 0;}
  871. element *reveal_start(){ return start; }
  872. };
  873.  
  874. class some_stack:public some_list{
  875. public:
  876. some_stack(){
  877. set_start(NULL);
  878. set_curent(NULL);
  879. set_sfarsit(NULL);
  880. set_lenght(0);
  881. };
  882. int push(int value);
  883. // int pop(int *old_topvalue);
  884. int stack_empty();
  885. };
  886.  
  887. class some_queue:public some_list{
  888. public:
  889. some_queue(){
  890. set_start(NULL);
  891. set_curent(NULL);
  892. set_sfarsit(NULL);
  893. set_lenght(0);
  894. };
  895. int add(int value); // identica cu push
  896. int serve(int *old_topvalue); // se elimina doar primul element din lista
  897. int queue_empty(); // identica cu stack_empty
  898. };
  899.  
  900. class no_strategy:public some_stack,public some_queue
  901. {
  902. public :
  903. // find_value cauta incepand cu primul element pana la ultimul o anumita valoare
  904. // daca valoare NU este gasit metoda intoarce FALSE 0
  905. // pentru prima situatie (!) cand valoarea este gasita pozitionam element curent
  906. // in acel element si metoda intoarce TRUE 1
  907. int find_value(int this_value) {
  908. element *walker=some_stack::reveal_start();
  909. if (some_stack::reveal_lenght()>=1) {
  910. while(walker->reveal_next()!=NULL){
  911. if (walker->reveal_value()==this_value) break ;
  912. walker=walker->reveal_next();
  913. }
  914. if (walker->reveal_value()==this_value) {
  915. some_stack::set_curent(walker);
  916. return 1;
  917. }
  918. }
  919.  
  920. else return 0;
  921. }
  922.  
  923.  
  924. int addsome ( int value, int direction )
  925. {
  926. if (stack_empty()) {
  927. element *new_one;
  928. new_one->set_value(value);
  929. new_one->set_next(NULL);
  930. some_stack::set_lenght(1);
  931. some_stack::set_curent(new_one);
  932. some_stack::set_start(new_one);
  933. some_stack::set_sfarsit(new_one);
  934. return -1;
  935. }
  936. if (direction==0) {
  937. if (some_stack::reveal_current()==some_stack::reveal_start())
  938. {
  939. // .... (!) // pentru curent pe primul element
  940. return -1;
  941. }
  942. else {
  943. element *new_one=new element;
  944. new_one->set_value(value);
  945. new_one->set_next(some_stack::reveal_current());
  946. element *walker=some_stack::reveal_start();
  947. while(walker->reveal_next()!=some_stack::reveal_current())
  948. walker=walker->reveal_next();
  949. new_one->set_next(some_stack::reveal_current());
  950. walker->set_next(new_one);
  951. some_stack::set_lenght(some_stack::reveal_lenght()+1);
  952. return 1;
  953. }
  954. }
  955.  
  956. //.... (!) pentru direction!=0 adica adaugarea la dreapta
  957. return 1;
  958. }
  959. };
  960.  
  961. no_strategy L;
  962. int main(){
  963. L.push(1);
  964. L.push(2);
  965. L.push(3);
  966. // lista are valorile 1 2 3
  967. // a fost construita prin apeluri push => deci "curent" este pe ultimul element
  968. L.addsome(0,0);
  969. // adaug 0 la stanga element
  970. // deci lista trebuie sa fie 1 2 0 3
  971. L.some_stack::print_me();
  972. // apelez "print_me" la care am acces prin mostenire public la ambii parinti
  973. // some_queue si some_stack
  974. // si utilizez rezolutie :: pentru a referi acea metoda "print_me" din
  975. // some_stack
  976. L.find_value(3); // !!!
  977. cout<<endl<<(L.some_stack::reveal_current())->reveal_value()<<endl;
  978. L.addsome(0,0);
  979. L.some_stack::print_me(); // sa avem 1 0 2 0 3
  980.  
  981. return 0;
  982. }
  983.  
  984. int some_stack::stack_empty(){
  985. if(this->reveal_lenght()>=1) return 0;
  986. return 1;
  987. }
  988.  
  989. int some_stack::push(int value)
  990. {
  991. element *new_one=new element;
  992. if (new_one==NULL) return 0;
  993. new_one->set_value(value);
  994. new_one->set_next(NULL);
  995. if (reveal_lenght()==0){
  996. set_start(new_one);
  997. set_curent(new_one);
  998. set_sfarsit(new_one);
  999. set_lenght(1);}
  1000. else{
  1001. reveal_sfarsit()->set_next(new_one);
  1002. set_curent(new_one);
  1003. set_sfarsit(new_one);
  1004. set_lenght(reveal_lenght()+1);}
  1005. return 1;
  1006. }
  1007.  
  1008.  
  1009.  
  1010.  
  1011.  
  1012.  
  1013. //7. functii cu numar variabil de parametri
  1014. #include<iostream>
  1015. #include<stdarg.h>
  1016. using namespace std;
  1017. int aduna(int n,...)
  1018. {
  1019. va_list VA;
  1020. int response=0;
  1021. va_start(VA,n);// de initializat lista , apel de va_start // de parcurs lista prin va_arg
  1022. int val;
  1023. for(int i=1;i<=n;i++){
  1024. val=va_arg(VA,int);
  1025. response+=val;
  1026. }
  1027. va_end(VA); // destructor , dezaloca lista
  1028. return response;
  1029. }
  1030. int main(){// prim parametru indica numarul valorilor transmise
  1031. cout<<aduna(3,1,2,3) ; // 6 !
  1032. cout<<endl<<aduna(4,1,2,3,4); // 10 !
  1033. return 0;
  1034. }
  1035.  
  1036.  
  1037.  
  1038.  
  1039.  
  1040.  
  1041.  
  1042. //8.#include<iostream>
  1043. #include<algorithm>
  1044. using namespace std;
  1045. int V[100]={2,1,3};
  1046. int n=3;
  1047. int (*nume)(int*,int);
  1048. int afisez(int V[], int dim){
  1049. cout<<"Found order !"<<endl;
  1050. }
  1051. int sortez(int V[],int dim){
  1052. cout<<"No order !"<<endl;
  1053. sort(V,V+dim);
  1054. }
  1055. int ma_adaptez(int V[], int dim){
  1056. int response=1;
  1057. for(int i=0;i<dim-1;i++){
  1058. if(V[i]<=V[i+1]){
  1059. response=1;
  1060. }
  1061. else{
  1062. response=0;
  1063. break;
  1064. }
  1065. }
  1066. if(response==1) nume=&afisez;
  1067. else nume=&sortez;
  1068. }
  1069. int main(){
  1070. ma_adaptez(V,n);
  1071. (*nume)(V,n);
  1072. ma_adaptez(V,n);
  1073. (*nume)(V,n);
  1074. return 0;
  1075. }
  1076.  
  1077.  
  1078.  
  1079.  
  1080.  
  1081.  
  1082.  
  1083. //9.#include<iostream>
  1084. #include<string>
  1085. using namespace std;
  1086.  
  1087. int main(int argc, char *argv[]){
  1088. cout<<" having "<<argc<<" parameters !";
  1089. int sum=0;
  1090. for(int i=1;i<=argc;i++){
  1091. if(argv[i]!=NULL){
  1092. int term=stoi(argv[i]);
  1093. sum+=term;
  1094. }
  1095. }
  1096. cout<<endl<<" Suma = "<<sum;
  1097. return 0;
  1098. }
  1099.  
  1100.  
  1101.  
  1102.  
  1103.  
  1104.  
  1105.  
  1106. //10.#include<iostream>
  1107. #include<thread>
  1108. #include<limits.h>
  1109. using namespace std;
  1110. int V[100]={5,2,3,4,1};
  1111. int n=5;
  1112. int mid=n/2;
  1113. int min1,min2;
  1114. int first_half(){
  1115. min1=INT_MAX;
  1116. for(int i=0;i<mid;i++){
  1117. if(V[i]<min1){
  1118. min1=V[i];
  1119. }
  1120. }
  1121. return min1;
  1122. }
  1123. int second_half(){
  1124. min2=INT_MAX;
  1125. for(int i=mid-1;i<n;i++){
  1126. if(V[i]<min2) min2=V[i];
  1127. }
  1128. return min2;
  1129. }
  1130. thread t1(first_half);
  1131. thread t2(second_half);
  1132. int main(){
  1133. t1.join();
  1134. t2.join();
  1135. int minim;
  1136. if(min1<min2) minim=min1;
  1137. else minim=min2;
  1138. cout<<endl<<"Minim="<<minim;
  1139. return 0;
  1140. }
  1141.  
  1142.  
  1143.  
  1144.  
  1145.  
  1146.  
  1147. //11.#include<iostream>
  1148. #include<fstream>
  1149. using namespace std;
  1150. int main(){
  1151. fstream f;
  1152. fstream g;
  1153. f.open("hello.exe",ios::in|ios::binary);
  1154. g.open("copy_hello.exe",ios::out|ios::binary);
  1155. char *buffer=new char;;
  1156. while(!f.eof()){
  1157. f.read(buffer,sizeof(char));
  1158. g.write(buffer,sizeof(char));
  1159. }
  1160. return 0;
  1161. }
  1162.  
  1163.  
  1164.  
  1165.  
  1166.  
  1167.  
  1168. //12.#include<iostream>
  1169. #include<fstream>
  1170. #include<string.h>
  1171. using namespace std;
  1172. char* the_string=new char;
  1173. char* the_copy=new char;
  1174. void read_data(){
  1175. ifstream f("input.dat");
  1176. f.getline(the_string,100);
  1177. }
  1178.  
  1179. void break_str(){
  1180. char* piece=new char;
  1181. piece=strtok(the_string," ");
  1182. while(piece){
  1183. cout<<piece<<endl;
  1184. piece=strtok(NULL," ");
  1185. }
  1186. }
  1187. int main(){
  1188. read_data();
  1189. break_str();
  1190. return 0;
  1191. }
  1192.  
  1193.  
  1194.  
  1195.  
  1196.  
  1197.  
  1198.  
  1199. //13.#include<iostream>
  1200. #include<fstream>
  1201. #include<string.h>
  1202. using namespace std;
  1203. char* the_string=new char;
  1204. char* the_copy=new char;
  1205. void read_data(){
  1206. ifstream f("input.dat");
  1207. f.getline(the_string,255);
  1208. }
  1209. int main(){
  1210. read_data();
  1211. strcpy(the_copy,the_string);
  1212. char* piece=new char;
  1213. char* words[100];
  1214. piece=strtok(the_string," ");
  1215. int nr=1;
  1216. while(piece){
  1217. words[nr]=piece;
  1218. piece=strtok(NULL," ");
  1219. nr++;
  1220. }
  1221. nr--;
  1222. for(int i=1;i<=nr;i++){
  1223. int freq=0;
  1224. for(int j=1;j<=nr;j++){
  1225. if(strcmp(words[i],words[j])==0){
  1226. freq++;
  1227. }
  1228. }
  1229. cout<<words[i]<<" "<<freq<<endl;
  1230. }
  1231. return 0;
  1232. }
  1233.  
  1234.  
  1235.  
  1236.  
  1237.  
  1238.  
  1239.  
  1240.  
  1241. //14.#include<iostream>
  1242. #include<fstream>
  1243. #include<string.h>
  1244. char the_string[]="Ana are mere Ana";
  1245. char position[]="Ana";
  1246. using namespace std;
  1247. int results()
  1248. {
  1249. int counter=0;
  1250. char *piece=strtok(the_string," ");
  1251. while(piece!=NULL){
  1252. if(strcmp(piece,position)==0) counter++;
  1253. piece=strtok(NULL," ");
  1254. }
  1255. return counter; // intoarce numarul de aparitii in sirul "in_care_caut"
  1256. // al subsirului "pe_care_caut"
  1257. }
  1258.  
  1259. int main()
  1260. {
  1261. //read_data(); // "abc aBc abc"
  1262. // "abc"
  1263. cout<<results(); // 2
  1264. return 0;
  1265. }
  1266.  
  1267.  
  1268.  
  1269.  
  1270.  
  1271.  
  1272.  
  1273.  
  1274. //15.#include<iostream>
  1275. #include<fstream>
  1276. #include<string.h>
  1277. using namespace std;
  1278. char* the_string=new char;
  1279. char* the_copy=new char;
  1280. void read_data(){
  1281. ifstream f("input.dat");
  1282. f.getline(the_string,100);
  1283. }
  1284. void break_str(){
  1285. char* piece=new char;
  1286. piece=strtok(the_string," ");
  1287. while(piece){
  1288. cout<<piece<<" ";
  1289. piece=strtok(NULL," ");
  1290. }
  1291. }
  1292. int main(){
  1293. read_data();
  1294. break_str();
  1295. return 0;
  1296. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement