Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.12 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class Nod{
  6.  
  7. char val;
  8. Nod * urm;
  9.  
  10. public:
  11.  
  12. char getVal(){return val; }
  13. Nod *getUrm() { return urm; }
  14.  
  15. friend class Coada;
  16.  
  17. };
  18.  
  19. class Coada{
  20. Nod *prim;
  21. Nod *ultim;
  22.  
  23. public:
  24.  
  25. void push (char x);
  26. void pop ();
  27. char top ();
  28. void afis();
  29. Coada() { prim = NULL; ultim = NULL; }
  30. Nod* get_prim(){ return prim; }
  31.  
  32. friend ostream &operator<<(ostream &o, Coada &c);
  33. friend istream &operator>>(istream &i, Coada &c);
  34.  
  35. Coada operator+( Coada B);
  36. Coada operator-( Coada &B);
  37.  
  38. };
  39.  
  40.  
  41. void Coada::push(char x){
  42.  
  43. if( prim == NULL){
  44. prim=new Nod;
  45. prim->val=x;
  46. prim->urm=NULL;
  47. ultim=prim;
  48. }
  49.  
  50. else{
  51. Nod *nou;
  52. nou=new Nod;
  53. nou->val=x;
  54. nou->urm=NULL;
  55. ultim->urm=nou;
  56. ultim=nou;
  57. }
  58. }
  59.  
  60. void Coada::pop(){
  61.  
  62. if(prim==NULL)
  63. cout<<"Coada e goala.";
  64.  
  65. else{
  66.  
  67. Nod *nou=prim;
  68. nou->urm=prim->urm;
  69. prim=prim->urm;
  70.  
  71. delete nou;
  72.  
  73. }
  74. }
  75.  
  76. char Coada::top(){
  77.  
  78. if(prim!=NULL)
  79. return prim->val;
  80. else
  81. return 0;
  82. }
  83. ostream &operator<<(ostream &o, Coada &c){
  84.  
  85. if( c.prim == NULL)
  86. o<<"Coada e goala."<<endl;
  87.  
  88. else {
  89.  
  90. Nod *nou= c.get_prim();
  91.  
  92. while (nou != NULL) {
  93. o<<nou->getVal();
  94. nou=nou->getUrm();
  95. c.pop();
  96. }
  97. }
  98.  
  99. return o;
  100. }
  101. istream &operator>>(istream &i,Coada &c)
  102. {
  103. cout<<"Numarul de caractere ale cozii:";
  104. int n;
  105. i>>n;
  106. char caracter;
  107.  
  108. for(int k=0;k<n;k++)
  109. {
  110. i>>caracter;
  111. c.push(caracter);
  112. }
  113.  
  114. return i;
  115.  
  116. }
  117.  
  118. Coada Coada::operator+( Coada B)
  119. {
  120. Nod *nou=B.prim;
  121. this->ultim->urm=nou;
  122.  
  123. return *this;
  124.  
  125. }
  126.  
  127. Coada Coada :: operator-(Coada &B)
  128. {
  129. while( this->top()==B.top())
  130. {
  131. this->pop();
  132. B.pop();
  133. }
  134. return *this;
  135. }
  136.  
  137. int main()
  138. {
  139.  
  140.  
  141. cout<<"Tastati 1 pentru citire coada de caractere."<<endl;
  142. cout<<"Tastati 2 pentru push. "<<endl;
  143. cout<<"Tastati 3 pentru pop. "<<endl;
  144. cout<<"Tastati 4 pentru top."<<endl;
  145. cout<<"Tastati 5 afisare."<<endl;
  146. cout<<"Tastati 6 pentru concatenare."<<endl;
  147. cout<<"Tastati 7 pentru diferenta."<<endl;
  148. Coada c;
  149.  
  150. int cerinta;
  151. cout<<endl<<"Alege cerinta:"<<endl;
  152. cin>>cerinta;
  153.  
  154.  
  155. do
  156. {
  157.  
  158. switch(cerinta){
  159.  
  160. case 1:
  161. {
  162.  
  163. cin>>c;
  164. break;
  165. }
  166. case 2:
  167. {
  168. cout<<"Adaugam elementul cu valoarea:";
  169. char x;
  170. cin>>x;
  171. c.push(x);
  172. cout<<"Am adaugat "<<x<<" la finalul cozii.";
  173. break;
  174. }
  175. case 3:
  176. {
  177. c.pop();
  178. cout<<"Am apelat functia pop.";
  179. break;
  180. }
  181. case 4:
  182. {
  183. cout<<"Varful cozii este "<<c.top()<<".";
  184. break;
  185. }
  186. case 5:
  187. {
  188. cout<<"Coada este "<<c<<".";
  189. break;
  190. }
  191. case 6:
  192. {
  193. Coada a,b;
  194. cout<<"Primul sir:"<<endl;
  195. cin>>a;
  196. cout<<"Al doilea sir:"<<endl;
  197. cin>>b;
  198. a=a+b;
  199. cout<<"Sirul a+b este "<<a;
  200. break;
  201.  
  202. }
  203. case 7:
  204. {
  205. Coada a,b;
  206. cout<<"Primul sir:"<<endl;
  207. cin>>a;
  208. cout<<"Al doilea sir:"<<endl;
  209. cin>>b;
  210. a=a-b;
  211. cout<<"Sirul a-b este "<<a;
  212. break;
  213. }
  214. default:
  215. {
  216. break;
  217. }
  218. }
  219. cout<<endl;
  220. cout<<"Alege cerinta:"<<endl;
  221. cin>>cerinta;
  222. }
  223. while(cerinta>=1 && cerinta <=7);
  224.  
  225. return 0;
  226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement