Thursday, 10 September 2015

first assignment

  1. include<iostream.h>
  2. #include<string.h>
  3. #include<conio.h>

  4. class vegetable
  5. {
  6. public:
  7. char v[30];
  8. int vegcount;
  9. void getdata(char *s, int p)
  10.     {
  11. strcpy(v,s);
  12. vegcount=p;
  13.     }
  14. void display1(void)
  15.     {
  16. cout<<"\n";
  17. cout<<v;
  18. cout<<"\t\t"<<vegcount;
  19.     }
  20. };
  21. class fruits
  22. {
  23. public:
  24. char f[30];
  25. int fruitcount;

  26. void getdata(char *s, int p)
  27.     {
  28. strcpy(f,s);
  29. fruitcount=p;
  30.     }
  31. void display2(void)
  32.     {
  33. cout<<"\n";
  34. cout<<f;
  35. cout<<"\t\t"<<fruitcount;
  36.     }
  37. };
  38. void arrange( vegetable vobj[30], fruits fobj[30],int n)
  39. {
  40. int temp;
  41. char *s;
  42. int i,j;

  43. for(i=0; i<n-1;i++)
  44.      {
  45. for(j=0;j<n-i-1;j++)
  46.        {
  47.     if(vobj[j].vegcount<vobj[j+1].vegcount)
  48.      {
  49.     temp=vobj[j].vegcount;
  50.     strcpy(s,vobj[j].v);

  51.     vobj[j].vegcount=vobj[j+1].vegcount;
  52.     strcpy(vobj[j].v,vobj[j+1].v);
  53.     vobj[j+1].vegcount=temp;
  54.     strcpy(vobj[j+1].v,s);
  55.       }
  56.     }
  57.       }

  58. for(i=0; i<n-1;i++)
  59.       {
  60.     for(j=0;j<n-i-1;j++)
  61.     {
  62.     if(fobj[j].fruitcount<fobj[j+1].fruitcount)
  63.       {
  64.     temp=fobj[j].fruitcount;
  65.     strcpy(s, fobj[j].f);
  66.     fobj[j].fruitcount=fobj[j+1].fruitcount;
  67.     strcpy(fobj[j].f, fobj[j+1].f);
  68.     fobj[j+1].fruitcount=temp;
  69.     strcpy(fobj[j+1].f,s);
  70.        }
  71.      }
  72.       }
  73. cout<<"\n Vegetables \t\t Fruits";
  74. cout<<"\n---------------";
  75. for(i=0;i<n;i++)
  76.       {
  77.     cout<<"\n";
  78.     cout<<vobj[i].v<<"\t\t"<<fobj[i].f;
  79.       }
  80.     }
  81. void main()
  82.     {
  83. vegetable veg[30];
  84. char *vname="";
  85. int vcount;
  86. fruits fruit[30];
  87. char *fname="";
  88. int fcount,i,n;
  89. void arrange(vegetable veg[30], fruits fruit[30], int n);
  90. clrscr();
  91. cout<<"\n \t\t Vegetable and Fruit Purchase Pattern";
  92. cout<<"\nHow many items?";
  93. cin>>n;
  94. cout<<"\n Enter Vegetable details\n";
  95. for(i=0;i<n;i++)
  96.       {
  97.     cout<<"\nItem :";
  98.     cin>>vname;
  99.     cout<<"\nPurchase Count";
  100.     cin>>vcount;

  101.     veg[i].getdata(vname,vcount);
  102.       }
  103. cout<< "\n Enter Fruit details\n";
  104. for(i=0;i<n;i++)
  105.       {
  106.     cout<<"\nItem : ";
  107.     cin>>fname;
  108.     cout<<"\nPurchase Count";
  109.     cin>>fcount;
  110.     fruit[i].getdata(fname,fcount);
  111.       }
  112. cout<<"\n Vegetable \tPurchase Count";
  113. cout<<"\n......................";
  114. for(i=0;i<n;i++)
  115.       {
  116.     veg[i].display1();
  117.       }
  118. cout<<"\n\nFruit\t\tPurchase Count";
  119. cout<<"\n......................";
  120. for(i=0;i<n;i++)
  121.       {
  122.     fruit[i].display2();
  123.       }
  124. cout<<"\n Arranging according to Purchase Pattern..";
  125. arrange(veg,fruit,n);
  126. getch();
  127.    }

No comments:

Post a Comment