Sunday, 30 October 2011


C++ Matrix Calculator

#include<iostream.h>
#include<graphics.h>
#include<conio.h>
#include<dos.h>
#include<stdlib.h>
#include<stdio.h>
#include<time.h>
#include<iomanip.h>
void mtrxshp();

int i,j,xf1=200,xf2=230,chf1=0,chf2=0,ex1=0,ex2=0,x1=0,x2=0,y1=0,y2=0;


      void sum(int [3][3],int [3][3]);
      void sub(int [3][3],int [3][3]);
      void mul(int[3][3], int [3][3]);
      void transa(int[3][3]);
      void transb(int[3][3]);



  void main()
  {clrscr();
  int gdriver = DETECT, gmode, errorcode;
  initgraph(&gdriver, &gmode, "c:\\tc\\bgi");

  mtrxshp();

     int a[3][3],b[3][3];
      char ch,h;
////////////////////////////////A

      for(i=0; i<3; i++)
      {gotoxy(25,9+i);
for(j=0; j<3; j++)
{ cin>>a[i][j];
  cout<<"   ";
}
cout<<"\n\n";
      }

//----------------Matrix B--------------------------------\n\n";


for(i=0; i<3; i++)
{ gotoxy(43,9+i);

for(j=0; j<3; j++)
{
cin>>b[i][j];
   }
   cout<<"\n";
   }


////////////////////////////////////////////////////////////////////////////////

do
 { mtrxshp();
   if(ch==72)
   {y1-=30;
    y2-=30;}

 if(ch==80)
   {y1+=30;
    y2+=30;}

 if(ch==75)
   { x1-=80;
     x2-=80;}

 if(ch==77)
    {x1+=80;
     x2+=80;}

 if(x1>160 ||y1>30)
   { x1=0;
     x2=0;
     y1=0;
     y2=0;}

 if(x1<0||y1<0)
   { x1=160;
     x2=160;
     y1=30;
     y2=30;}
/////////////////////////////////////////////////////////////////////////////
   setcolor(WHITE);
   rectangle(180+x1,330+y1,260+x2,360+y2);

   ch=getche();
    }
    while(ch!=13);


//////////////////////////////////////////////////////////////////////////


    if(x1==0)
    {ch=1;}
    if(x1==80)
    {ch=2;}
    if(x1==160)
    {ch=3;}
    if(y1==30&&x1==0)
    {ch=4;}
    if(y1==30&&x1==80)
    {ch=5;}
    if(y1==30&&x1==160)
    {ch=6;}


switch(ch)
{
      case 1:

   sum(a,b);

   break;


case 2:
   sub(a,b);
   break;

      case 3:

 mul(a,b);
break;
case 4:

 transa(a);

break;


case 5:

 transb(b);

break;


case 6:
     //////////ext msg from main
  break;

      }

  getche();
  }

////////////////////////////////////////////////////////////////////////
    void mtrxshp()
    {
  setcolor(RED);
  settextstyle(4,0,6);
  outtextxy(120,4,"Matrix Calculator");
  rectangle(90,10,530,70);
  setcolor(RED);
  rectangle(10,425 ,620,477);
  settextstyle(0,0,1);
  outtextxy(16,430,"NAME: Muhammad Waqas");
  outtextxy(16,445,"Roll # 9275");
  outtextxy(16,460,"Class : BCs-A(2011 - 2014)");
  outtextxy(400,430,"S u b m e t t e d  T o");
  outtextxy(380,460,"S i r .  U m e r  H a y a t");
  settextstyle(4,0,16);
  outtextxy(30,90,"V      V");
   settextstyle(0,0,0);
////////////////////////////////////////////////////////////
  rectangle(160,120,260,190);
  rectangle(310,120,410,190);
  rectangle(230,215,340,300);

  outtextxy(177,110,"Matrix A");
  outtextxy(337,110,"Matrix B");
  rectangle(170,320,430,400);

  rectangle(180,330,260,360);
  rectangle(260,330,340,360);
  rectangle(340,330,420,360);

  rectangle(180,360,260,390);
  rectangle(260,360,340,390);
  rectangle(340,360,420,390);
  outtextxy(210,340,"+");
  outtextxy(290,340,"-");
  outtextxy(370,340,"*");

  outtextxy(210,370,"T A");
  outtextxy(290,370,"T B");
  outtextxy(370,370,"Exit");


  }


      void sum(int a[3][3],int b[3][3])
      {
 int s[3][3];
for (int i = 0; i<3; i++)
{
  gotoxy(31,16+i);
for (int j=0; j<3;j++)
{

s[i][j]=a[i][j] + b[i][j];

cout<<"  "<<s[i][j];

}
cout<<"\n";
}

      }
      void sub(int a[3][3],int b[3][3])
      {     int st[3][3];
 for(i=0; i<3; i++)
{  gotoxy(31,16+i);
 for(j=0; j<3; j++)
 {
     st[i][j]=a[i][j]-b[i][j];

cout<<" " <<st[i][j];
 }
 cout<<"\n";
 }

      }
   void mul(int a[3][3],int b[3][3])
      {
      int mu[3][3],t,n,p;
for(i=0; i<3; i++)
{  gotoxy(31,16+i);
 for(j=0; j<3; j++)
 { t=0;
 for( n=0; n<3; n++)
 {

p=a[i][n]*b[n][j];
t=t+p;
mu[i][j]=t;
}
cout<<"  "<<mu[i][j];
 }
 cout<<"\n";
 }
}


void transa(int a[3][3])
      {
 for(i=0; i<3; i++)
{  gotoxy(31,16+i);
  for(j=0; j<3; j++)
 {
 cout<<"  "<<a[j][i];
 }
 cout<<"\n";
 }
}

void transb(int b[3][3])
      {

 for(i=0; i<3; i++)
{  gotoxy(31,16+i);
 for(j=0; j<3; j++)
 {

 cout<<"  "<<b[j][i];
 }
 cout<<"\n";
 }
}






   Copy this code and save in notepad and save as .cpp extension in bin directory then run with turbo C




No comments:

Post a Comment