upsidedown

Water jug + O/P

Aug 1st, 2012
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.99 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3. #include<conio.h>
  4.  
  5. int jar1=0,jar2=0;
  6. void fill_jar1();
  7. void fill_jar2();
  8. void jar1_jar2();
  9. void jar2_jar1();
  10. void empty_jar1();
  11. void empty_jar2();
  12. void show_jars();
  13.  
  14. void main()
  15. {
  16.     int ch;
  17.     clrscr();
  18.     cout<<"capacity of jar 1 is 4 litre"<<endl;
  19.     cout<<"capacity of jar 2 is 3 litre";
  20.     do
  21.     {
  22.     cout<<"\n"<<endl;
  23.     cout<<"1.fill jar"<<endl;
  24.     cout<<"2.empty jar"<<endl;
  25.     cout<<"3.pour from jar 1 to jar 2"<<endl;
  26.     cout<<"4.pour from jar 2 to jar 1"<<endl;
  27.     cout<<"5.show jars"<<endl;
  28.     cout<<"enter choice:";
  29.     cin>>ch;
  30.     switch(ch)
  31.     {
  32.      case 1:int x;
  33.         cout<<"\nenter 1 to fill jar 1"<<endl;
  34.         cout<<"enter 2 to fill jar 2"<<endl;
  35.         cout<<"enter choice:";
  36.         cin>>x;
  37.         if(x==1)
  38.          fill_jar1();
  39.         else if(x==2)
  40.          fill_jar2();
  41.         break;
  42.      case 2:int y;
  43.         cout<<"\nenter 1 to empty jar 1"<<endl;
  44.         cout<<"enter 2 to empty jar 2"<<endl;
  45.         cout<<"enter choice:";
  46.         cin>>y;
  47.         if(y==1)
  48.          empty_jar1();
  49.         else if(y==2)
  50.          empty_jar2();
  51.         break;
  52.      case 3:jar1_jar2();
  53.         break;
  54.      case 4:jar2_jar1();
  55.         break;
  56.      case 5:show_jars();
  57.         break;
  58.      default:break;
  59.     }
  60.     }
  61.     while((jar1!=2)&&(jar2<=3));
  62.     cout<<"\nJar 1 contains 2 litres"<<endl;
  63.     cout<<"problem solved"<<endl;
  64.     getch();
  65. }
  66.  
  67. void fill_jar1()
  68. {
  69.  jar1=4;
  70. }
  71.  
  72. void fill_jar2()
  73. {
  74.  jar2=3;
  75. }
  76.  
  77. void jar1_jar2()
  78. {
  79.  if((jar1==0)&&(jar2==0))
  80.   cout<<"both jars are empty:";
  81.  if((jar1==4)&&(jar2==3))
  82.   cout<<"both jars are full:";
  83.  while((jar1!=0)&&(jar2!=3))
  84.  {
  85.   jar1--;
  86.   jar2++;
  87.  }
  88.  if(jar2==3)
  89.   cout<<"jar2 is full";
  90. }
  91.  
  92. void jar2_jar1()
  93. {
  94.  if((jar1==0 )&&(jar2==0))
  95.   cout<<"jars are empty:";
  96.  if((jar1==4)&&(jar2==3))
  97.   cout<<"both jars are full:";
  98.  while((jar1!=4)&&(jar2!=0))
  99.  {
  100.   jar1++;
  101.   jar2--;
  102.  }
  103.  if(jar1==4)
  104.   cout<<"jar1 is full";
  105. }
  106.  
  107. void empty_jar1()
  108. {
  109.  jar1=0;
  110.  cout<<"jar 1 is empty";
  111. }
  112.  
  113. void empty_jar2()
  114. {
  115.  jar2=0;
  116.  cout<<"jar 2 is empty";
  117. }
  118.  
  119. void show_jars()
  120. {
  121.  cout<<"\n"<<endl;
  122.  cout<<"|     |"<<"\t"<<"|     |"<<endl;
  123.  cout<<"|  "<<jar1<<"  |"<<"\t"<<"|  "<<jar2<<"  |"<<endl;
  124.  cout<<"|_____|"<<"\t"<<"|_____|"<<endl;
  125.  cout<<" jar 1"<<"\t"<<" jar 2 "<<endl;
  126. }
  127.  
  128. output:
  129. 1.fill jar
  130. 2.empty jar
  131. 3.pour from jar 1 to jar 2
  132. 4.pour from jar 2 to jar 1
  133. 5.show jars
  134. enter choice:5
  135.  
  136. |     | |     |
  137. |  0  | |  0  |
  138. |_____| |_____|
  139.  jar 1   jar 2
  140.  
  141. 1.fill jar
  142. 2.empty jar
  143. 3.pour from jar 1 to jar 2
  144. 4.pour from jar 2 to jar 1
  145. 5.show jars
  146. enter choice:1
  147.  
  148. enter 1 to fill jar 1
  149. enter 2 to fill jar 2
  150. enter choice:1
  151.  
  152. 1.fill jar
  153. 2.empty jar
  154. 3.pour from jar 1 to jar 2
  155. 4.pour from jar 2 to jar 1
  156. 5.show jars
  157. enter choice:5
  158.  
  159.  
  160. |     | |     |
  161. |  4  | |  0  |
  162. |_____| |_____|
  163.  jar 1   jar 2
  164.  
  165. 1.fill jar
  166. 2.empty jar
  167. 3.pour from jar 1 to jar 2
  168. 4.pour from jar 2 to jar 1
  169. 5.show jars
  170. enter choice:1
  171.  
  172. enter 1 to fill jar 1
  173. enter 2 to fill jar 2
  174. enter choice:2
  175.  
  176. 1.fill jar
  177. 2.empty jar
  178. 3.pour from jar 1 to jar 2
  179. 4.pour from jar 2 to jar 1
  180. 5.show jars
  181. enter choice:5
  182.  
  183.  
  184. |     | |     |
  185. |  4  | |  3  |
  186. |_____| |_____|
  187.  jar 1   jar 2
  188.  
  189. 1.fill jar
  190. 2.empty jar
  191. 3.pour from jar 1 to jar 2
  192. 4.pour from jar 2 to jar 1
  193. 5.show jars
  194. enter choice:2
  195.  
  196. enter 1 to empty jar 1
  197. enter 2 to empty jar 2
  198. enter choice:1
  199.  
  200. 1.fill jar
  201. 2.empty jar
  202. 3.pour from jar 1 to jar 2
  203. 4.pour from jar 2 to jar 1
  204. 5.show jars
  205. enter choice:5
  206.  
  207.  
  208. |     | |     |
  209. |  0  | |  3  |
  210. |_____| |_____|
  211.  jar 1   jar 2
  212.  
  213. 1.fill jar
  214. 2.empty jar
  215. 3.pour from jar 1 to jar 2
  216. 4.pour from jar 2 to jar 1
  217. 5.show jars
  218. enter choice:4
  219.  
  220. 1.fill jar
  221. 2.empty jar
  222. 3.pour from jar 1 to jar 2
  223. 4.pour from jar 2 to jar 1
  224. 5.show jars
  225. enter choice:5
  226.  
  227.  
  228. |     | |     |
  229. |  3  | |  0  |
  230. |_____| |_____|
  231.  jar 1   jar 2
  232.  
  233. 1.fill jar
  234. 2.empty jar
  235. 3.pour from jar 1 to jar 2
  236. 4.pour from jar 2 to jar 1
  237. 5.show jars
  238. enter choice:1
  239.  
  240. enter 1 to fill jar 1
  241. enter 2 to fill jar 2
  242. enter choice:2
  243.  
  244. 1.fill jar
  245. 2.empty jar
  246. 3.pour from jar 1 to jar 2
  247. 4.pour from jar 2 to jar 1
  248. 5.show jars
  249. enter choice:5
  250.  
  251.  
  252. |     | |     |
  253. |  3  | |  3  |
  254. |_____| |_____|
  255.  jar 1   jar 2
  256.  
  257. 1.fill jar
  258. 2.empty jar
  259. 3.pour from jar 1 to jar 2
  260. 4.pour from jar 2 to jar 1
  261. 5.show jars
  262. enter choice:4
  263. jar1 is full
  264.  
  265. 1.fill jar
  266. 2.empty jar
  267. 3.pour from jar 1 to jar 2
  268. 4.pour from jar 2 to jar 1
  269. 5.show jars
  270. enter choice:5
  271.  
  272.  
  273. |     | |     |
  274. |  4  | |  2  |
  275. |_____| |_____|
  276.  jar 1   jar 2
  277.  
  278. 1.fill jar
  279. 2.empty jar
  280. 3.pour from jar 1 to jar 2
  281. 4.pour from jar 2 to jar 1
  282. 5.show jars
  283. enter choice:2
  284.  
  285. enter 1 to empty jar 1
  286. enter 2 to empty jar 2
  287. enter choice:1
  288.  
  289. 1.fill jar
  290. 2.empty jar
  291. 3.pour from jar 1 to jar 2
  292. 4.pour from jar 2 to jar 1
  293. 5.show jars
  294. enter choice:5
  295.  
  296.  
  297. |     | |     |
  298. |  0  | |  2  |
  299. |_____| |_____|
  300.  jar 1   jar 2
  301.  
  302. 1.fill jar
  303. 2.empty jar
  304. 3.pour from jar 1 to jar 2
  305. 4.pour from jar 2 to jar 1
  306. 5.show jars
  307. enter choice:4
  308.  
  309. 1.fill jar
  310. 2.empty jar
  311. 3.pour from jar 1 to jar 2
  312. 4.pour from jar 2 to jar 1
  313. 5.show jars
  314. enter choice:5
  315.  
  316.  
  317. |     | |     |
  318. |  2  | |  0  |
  319. |_____| |_____|
  320.  jar 1   jar 2
  321.  
  322. Jar 1 contains 2 litres
  323. problem solved
Advertisement
Add Comment
Please, Sign In to add comment