Advertisement
handtevada

Matrix by C++

Mar 1st, 2013
1,095
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 20.20 KB | None | 0 0
  1. //***************************************************
  2. //www.engineerindy.com
  3. //@ Tutorial
  4. //@ DevC++ 4.9.9.2
  5. //@ Windows7
  6. ///**************************************************
  7.  
  8. #include <iostream>
  9. #include <fstream>
  10. #include <conio.h>
  11.  
  12. using namespace std;
  13.  
  14. struct matrix
  15. {
  16.     char name;
  17.     int x,y;
  18.     int a[20][20];      
  19. };
  20.  
  21. struct matrix one[10];
  22. struct matrix tran[5];
  23. struct matrix result[5];
  24. int z=0,y=0,t=0,g=0;
  25.  
  26. void insert()
  27. {
  28.      int number;
  29.      int r=1,c=1;
  30.     //row and column
  31.    
  32.     cout<<"          Please input name of matrix [1 charecter] : ";  
  33.     cin>>one[z].name;
  34.     cout<<"          Please input size of matrix [row] : ";
  35.     cin>>one[z].x;
  36.     cout<<"          Please input size of matrix [column] : ";
  37.     cin>>one[z].y;
  38.     cout<<"\n";
  39.     for(int i=0;i<(one[z].x*one[z].y);i++)
  40.     {
  41.        cout<<"          Please input value position "<<r<<","<<c<<" : ";
  42.        cin>>one[z].a[r][c];
  43.        c++;
  44.        if(c>one[z].y)
  45.        {
  46.           c=1;
  47.           r++;
  48.        }
  49.     }
  50.     cout<<"\n          matrix "<<one[z].name<<"["<<one[z].x<<"]["<<one[z].y<<"]\n";
  51.     cout<<"          ";
  52.     for(int j=1;j<=(one[z].x);j++)
  53.     {
  54.        for(int n=1;n<=(one[z].y);n++)    
  55.        {
  56.           cout<<one[z].a[j][n]<<" ";
  57.        }  
  58.        cout<<"\n          ";
  59.     }
  60.     cout<<"\n";
  61.     z++;
  62. }
  63.  
  64. void showresult()
  65. {
  66.      if(result[g].x!=0)
  67.      {
  68.      cout<<"\n          matrix "<<result[g].name<<"["<<result[g].x<<"]["<<result[g].y<<"]\n";
  69.      cout<<"          ";
  70.      for(int j=1;j<=(result[g].x);j++)
  71.     {
  72.        for(int n=1;n<=(result[g].y);n++)    
  73.        {
  74.           cout<<result[g].a[j][n]<<" ";
  75.        }  
  76.        cout<<"\n          ";
  77.     }
  78.     cout<<"\n";
  79.     g++;
  80.     }
  81.     else
  82.     cout<<"\n          size of matrix Unequal or don't have variable\n\n";
  83. }
  84.  
  85. void cal()
  86. {
  87.     int j,i,n,m;
  88.     char x[3];
  89.     cout<<"          Please Input LOGIC : ";
  90.     cin>>x;
  91.     cout<<"          Please enter name of result matrix : ";
  92.     cin>>result[g].name;
  93.     //cout<<x;
  94.     if(((x[1]=='+')&&(x[2]!=0))||((x[1]=='-')&&(x[2]!=0)))
  95.     {
  96.        //size of matrix must sam  
  97.        for(i=0;i<10;i++)
  98.        {
  99.           if(one[i].name==x[0])//first
  100.           {
  101.                for(j=0;j<10;j++)
  102.                {
  103.                    if(one[j].name==x[2])//second
  104.                    {
  105.                        if((one[i].x==one[j].x)&&(one[i].y==one[j].y))//check colum and row must sam together
  106.                        {  
  107.                             result[g].x=one[j].x;
  108.                             result[g].y=one[j].y;                                                                        
  109.                        for(m=1;m<=one[i].x;m++)//row
  110.                        {
  111.                           for(n=1;n<=one[i].y;n++)//column
  112.                           {
  113.                              if(x[1]=='+')
  114.                              {    
  115.                                 result[g].a[m][n]=(one[i].a[m][n]+one[j].a[m][n]);
  116.                                
  117.                              }
  118.                              else if(x[1]=='-')
  119.                              {    
  120.                                 result[g].a[m][n]=(one[i].a[m][n]-one[j].a[m][n]);
  121.                              }    
  122.                           }
  123.                                
  124.                        }  
  125.                           break;
  126.                        }                          
  127.                    }
  128.                    else if(tran[j].name==x[2])
  129.                    {  
  130.                        if((one[i].x==tran[j].x)&&(one[i].y==tran[j].y))//check colum and row must sam together
  131.                        {
  132.                               result[g].x=tran[j].x;
  133.                               result[g].y=tran[j].y;                                                
  134.                        for(m=1;m<=tran[i].x;m++)//row
  135.                        {
  136.                           for(n=1;n<=tran[i].y;n++)//column
  137.                           {
  138.                              if(x[1]=='+')
  139.                              {    
  140.                                 result[g].a[m][n]=(one[i].a[m][n]+tran[j].a[m][n]);
  141.                              }
  142.                              else if(x[1]=='-')
  143.                              {    
  144.                                 result[g].a[m][n]=(one[i].a[m][n]-tran[j].a[m][n]);
  145.                              }    
  146.                           }
  147.                                
  148.                        }
  149.                        break;
  150.                        }
  151.                    }
  152.                    else if(result[j].name==x[2])
  153.                    {  
  154.                        if((one[i].x==result[j].x)&&(one[i].y==result[j].y))//check colum and row must sam together
  155.                        {
  156.                               result[g].x=one[i].x;
  157.                               result[g].y=one[i].y;                                                
  158.                        for(m=1;m<=result[i].x;m++)//row
  159.                        {
  160.                           for(n=1;n<=result[i].y;n++)//column
  161.                           {
  162.                              if(x[1]=='+')
  163.                              {    
  164.                                 result[g].a[m][n]=(one[i].a[m][n]+result[j].a[m][n]);
  165.                              }
  166.                              else if(x[1]=='-')
  167.                              {    
  168.                                 result[g].a[m][n]=(one[i].a[m][n]-result[j].a[m][n]);
  169.                              }    
  170.                           }
  171.                                
  172.                        }
  173.                        break;
  174.                        }
  175.                    }
  176.                }    
  177.           }
  178.           else if(tran[i].name==x[0])
  179.           {
  180.                for(j=0;j<10;j++)
  181.                {
  182.                    if(tran[j].name==x[2])//second
  183.                    {
  184.                        if((tran[i].x==tran[j].x)&&(tran[i].y==tran[j].y))//check colum and row must sam together
  185.                        {  
  186.                             result[g].x=tran[j].x;
  187.                             result[g].y=tran[j].y;                                                                        
  188.                        for(m=1;m<=tran[i].x;m++)//row
  189.                        {
  190.                           for(n=1;n<=tran[i].y;n++)//column
  191.                           {
  192.                              if(x[1]=='+')
  193.                              {    
  194.                                 result[g].a[m][n]=(tran[i].a[m][n]+tran[j].a[m][n]);
  195.                                
  196.                              }
  197.                              else if(x[1]=='-')
  198.                              {    
  199.                                 result[g].a[m][n]=(tran[i].a[m][n]-tran[j].a[m][n]);
  200.                              }    
  201.                           }
  202.                                
  203.                        }  
  204.                        
  205.                           break;
  206.                        }
  207.                                                      
  208.                    }
  209.                    else if(one[j].name==x[2])
  210.                    {  
  211.                        if((tran[i].x==one[j].x)&&(tran[i].y==one[j].y))//check colum and row must sam together
  212.                        {
  213.                               result[g].x=tran[j].x;
  214.                               result[g].y=tran[j].y;                                                
  215.                        for(m=1;m<=one[i].x;m++)//row
  216.                        {
  217.                           for(n=1;n<=one[i].y;n++)//column
  218.                           {
  219.                              if(x[1]=='+')
  220.                              {    
  221.                                 result[g].a[m][n]=(tran[i].a[m][n]+one[j].a[m][n]);
  222.                              }
  223.                              else if(x[1]=='-')
  224.                              {    
  225.                                 result[g].a[m][n]=(tran[i].a[m][n]-one[j].a[m][n]);
  226.                              }    
  227.                           }
  228.                                
  229.                        }
  230.                        break;
  231.                        }
  232.                    }
  233.                    else if(result[j].name==x[2])
  234.                    {  
  235.                        if((tran[i].x==result[j].x)&&(tran[i].y==result[j].y))//check colum and row must sam together
  236.                        {
  237.                               result[g].x=tran[i].x;
  238.                               result[g].y=tran[i].y;                                                
  239.                        for(m=1;m<=result[i].x;m++)//row
  240.                        {
  241.                           for(n=1;n<=result[i].y;n++)//column
  242.                           {
  243.                              if(x[1]=='+')
  244.                              {    
  245.                                 result[g].a[m][n]=(tran[i].a[m][n]+result[j].a[m][n]);
  246.                              }
  247.                              else if(x[1]=='-')
  248.                              {    
  249.                                 result[g].a[m][n]=(tran[i].a[m][n]-result[j].a[m][n]);
  250.                              }    
  251.                           }
  252.                                
  253.                        }
  254.                        break;
  255.                        }
  256.                    }
  257.                }    
  258.           }
  259.           else if(result[i].name==x[0])
  260.           {
  261.                for(j=0;j<10;j++)
  262.                {
  263.                    if(tran[j].name==x[2])//second
  264.                    {
  265.                        while((result[i].x==tran[j].x)&&(result[i].y==tran[j].y))//check colum and row must sam together
  266.                        {  
  267.                             result[g].x=tran[j].x;
  268.                             result[g].y=tran[j].y;                                                                        
  269.                        for(m=1;m<=result[i].x;m++)//row
  270.                        {
  271.                           for(n=1;n<=result[i].y;n++)//column
  272.                           {
  273.                              if(x[1]=='+')
  274.                              {    
  275.                                 result[g].a[m][n]=(result[i].a[m][n]+tran[j].a[m][n]);
  276.                                
  277.                              }
  278.                              else if(x[1]=='-')
  279.                              {    
  280.                                 result[g].a[m][n]=(result[i].a[m][n]-tran[j].a[m][n]);
  281.                              }    
  282.                           }
  283.                                
  284.                        }  
  285.                        
  286.                           break;
  287.                        }
  288.                                                      
  289.                    }
  290.                    else if(one[j].name==x[2])
  291.                    {  
  292.                        while((result[i].x==one[j].x)&&(result[i].y==one[j].y))//check colum and row must sam together
  293.                        {
  294.                               result[g].x=result[i].x;
  295.                               result[g].y=result[i].y;                                                
  296.                        for(m=1;m<=result[i].x;m++)//row
  297.                        {
  298.                           for(n=1;n<=result[i].y;n++)//column
  299.                           {
  300.                              if(x[1]=='+')
  301.                              {    
  302.                                 result[g].a[m][n]=(result[i].a[m][n]+one[j].a[m][n]);
  303.                              }
  304.                              else if(x[1]=='-')
  305.                              {    
  306.                                 result[g].a[m][n]=(result[i].a[m][n]-one[j].a[m][n]);
  307.                              }    
  308.                           }
  309.                                
  310.                        }
  311.                        break;
  312.                        }
  313.                    }
  314.                    else if(result[j].name==x[2])
  315.                    {  
  316.                        while((result[i].x==result[j].x)&&(result[i].y==result[j].y))//check colum and row must sam together
  317.                        {
  318.                               result[g].x=result[j].x;
  319.                               result[g].y=result[j].y;                                                
  320.                        for(m=1;m<=result[i].x;m++)//row
  321.                        {
  322.                           for(n=1;n<=result[i].y;n++)//column
  323.                           {
  324.                              if(x[1]=='+')
  325.                              {    
  326.                                 result[g].a[m][n]=(result[i].a[m][n]+result[j].a[m][n]);
  327.                              }
  328.                              else if(x[1]=='-')
  329.                              {    
  330.                                 result[g].a[m][n]=(result[i].a[m][n]-result[j].a[m][n]);
  331.                              }    
  332.                           }
  333.                                
  334.                        }
  335.                        break;
  336.                        }
  337.                    }
  338.                }    
  339.           }
  340.        //3
  341.        }
  342.        showresult();                      
  343.     }
  344.     else if(((x[1]=='*')&&(x[2]!=0))||((x[1]=='/')&&(x[2]!=0)))
  345.     {
  346.         //column of first must sam with row of secen
  347.         cout<<"\n          We are currently developing.\n\n";
  348.     }
  349.     else
  350.     {
  351.         cout<<"\n          Syntax of LOGIC error\n";
  352.         showresult();
  353.     }
  354.      
  355. }
  356.  
  357. void trans()
  358. {
  359.     char v;
  360.     cout<<"          Transpose matrix : ";
  361.     cin>>v;
  362.     for(int i=0;i<10;i++)
  363.     {
  364.        if((one[i].name)==v)
  365.        {
  366.           for(int j=1;j<=one[i].x;j++)
  367.           {
  368.               for(int n=1;n<=one[i].y;n++)  
  369.               {
  370.                    tran[y].a[n][j]=one[i].a[j][n];
  371.               }      
  372.           }
  373.           tran[y].x=one[i].y;
  374.           tran[y].y=one[i].x;
  375.          
  376.           cout<<"\n          Enter name of Transpose Matrix : ";
  377.           cin>>tran[y].name;                
  378.           cout<<"\n          Transpose Matrix "<<tran[y].name<<"["<<tran[y].x<<"]["<<tran[y].y<<"]\n";
  379.           break;              
  380.        }
  381.        else if((result[i].name)==v)
  382.        {
  383.           for(int j=1;j<=result[i].x;j++)
  384.           {
  385.               for(int n=1;n<=result[i].y;n++)  
  386.               {
  387.                    tran[y].a[n][j]=result[i].a[j][n];
  388.               }      
  389.           }
  390.           tran[y].x=result[i].y;
  391.           tran[y].y=result[i].x;
  392.          
  393.           cout<<"\n          Enter name of Transpose Matrix : ";
  394.           cin>>tran[y].name;                
  395.           cout<<"\n          Transpose Matrix "<<tran[y].name<<"["<<tran[y].x<<"]["<<tran[y].y<<"]\n";
  396.           break;              
  397.        }
  398.        else if((tran[i].name)==v)
  399.        {
  400.           for(int j=1;j<=tran[i].x;j++)
  401.           {
  402.               for(int n=1;n<=tran[i].y;n++)  
  403.               {
  404.                    tran[y].a[n][j]=tran[i].a[j][n];
  405.               }      
  406.           }
  407.           tran[y].x=tran[i].y;
  408.           tran[y].y=tran[i].x;
  409.          
  410.           cout<<"\n          Enter name of Transpose Matrix : ";
  411.           cin>>tran[y].name;                
  412.           cout<<"\n          Transpose Matrix "<<tran[y].name<<"["<<tran[y].x<<"]["<<tran[y].y<<"]\n";
  413.           break;              
  414.        }
  415.        else
  416.        {
  417.           cout<<"\n          Don't have this matrix\n";
  418.           break;
  419.        }
  420.     }
  421.    
  422.     cout<<"          ";
  423.     for(int o=1;o<=(tran[y].x);o++)
  424.     {
  425.        for(int p=1;p<=(tran[y].y);p++)    
  426.        {
  427.           cout<<tran[y].a[o][p]<<" ";
  428.        }  
  429.        cout<<"\n          ";
  430.     }
  431.     cout<<"\n";
  432.     y++;  
  433. }
  434.  
  435. void constant()
  436. {
  437.     char b,c;
  438.     int a,i,n,m;
  439.    
  440.     cout<<"          Enter constant number : ";
  441.     cin>>a;
  442.     cout<<"          ['*'= Mutiply]['/'= Devide] : ";
  443.     cin>>b;
  444.    
  445.     if(b=='*')
  446.     {
  447.        cout<<"          Enter matrix that you want mutiply : ";
  448.        cin>>c;
  449.        cout<<"          Enter name of result matrix : ";
  450.        cin>>result[g].name;
  451.        for(i=0;i<10;i++)
  452.        {
  453.            if(one[i].name==c)
  454.            {
  455.                for(m=1;m<=one[i].x;m++)//row
  456.                {
  457.                   for(n=1;n<=one[i].y;n++)//column
  458.                   {    
  459.                         result[g].a[m][n]=(one[i].a[m][n]*a);  
  460.                   }        
  461.                }
  462.                result[g].x=one[i].x;
  463.                result[g].y=one[i].y;
  464.                showresult();
  465.                break;            
  466.            }
  467.            else if(result[i].name==c)
  468.            {
  469.                for(m=1;m<=result[i].x;m++)//row
  470.                {
  471.                   for(n=1;n<=result[i].y;n++)//column
  472.                   {    
  473.                         result[g].a[m][n]=(result[i].a[m][n]*a);  
  474.                   }        
  475.                }
  476.                result[g].x=result[i].x;
  477.                result[g].y=result[i].y;
  478.                showresult();
  479.                break;              
  480.            }
  481.            else if(tran[i].name==c)
  482.            {
  483.                for(m=1;m<=tran[i].x;m++)//row
  484.                {
  485.                   for(n=1;n<=tran[i].y;n++)//column
  486.                   {    
  487.                         result[g].a[m][n]=(tran[i].a[m][n]*a);  
  488.                   }        
  489.                }
  490.                result[g].x=tran[i].x;
  491.                result[g].y=tran[i].y;
  492.                showresult();
  493.                break;              
  494.            }                    
  495.        }
  496.     }
  497.     else if(b=='/')
  498.     {
  499.        cout<<"          Enter matrix that you want devide : ";
  500.        cin>>c;
  501.        cout<<"          Enter name of result matrix : ";
  502.        cin>>result[g].name;
  503.        for(i=0;i<10;i++)
  504.        {
  505.            if(one[i].name==c)
  506.            {
  507.                for(m=1;m<=one[i].x;m++)//row
  508.                {
  509.                   for(n=1;n<=one[i].y;n++)//column
  510.                   {    
  511.                         result[g].a[m][n]=(one[i].a[m][n]/a);  
  512.                   }        
  513.                }
  514.                result[g].x=one[i].x;
  515.                result[g].y=one[i].y;
  516.                showresult();
  517.                break;            
  518.            }
  519.            else if(result[i].name==c)
  520.            {
  521.                for(m=1;m<=result[i].x;m++)//row
  522.                {
  523.                   for(n=1;n<=result[i].y;n++)//column
  524.                   {    
  525.                         result[g].a[m][n]=(result[i].a[m][n]/a);  
  526.                   }        
  527.                }
  528.                result[g].x=result[i].x;
  529.                result[g].y=result[i].y;
  530.                showresult();
  531.                break;              
  532.            }
  533.            else if(tran[i].name==c)
  534.            {
  535.                for(m=1;m<=tran[i].x;m++)//row
  536.                {
  537.                   for(n=1;n<=tran[i].y;n++)//column
  538.                   {    
  539.                         result[g].a[m][n]=(tran[i].a[m][n]/a);  
  540.                   }        
  541.                }
  542.                result[g].x=tran[i].x;
  543.                result[g].y=tran[i].y;
  544.                showresult();
  545.                break;              
  546.            }                  
  547.        }
  548.     }
  549.     else
  550.     {
  551.        cout<<"\n          You must Enter ['*' or '/']\n\n";    
  552.     }
  553. }
  554.  
  555. int main()
  556. {
  557.     char number;
  558.  
  559.     cout<<"\n                 WELCOME TO CALCULATED OF MATRIX PROGEAM\n\n";
  560.     cout<<"          ****************************************************\n";
  561.     cout<<"                                MENUS\n";
  562.     cout<<"                           1.Insert Matrix\n";
  563.     cout<<"                           2.Calculated\n";
  564.     cout<<"                           3.Transpose\n";
  565.     cout<<"                           4.Compute with constant\n";
  566.     cout<<"                           5.End progrem\n";
  567.     cout<<"          ****************************************************\n\n\n";
  568.    
  569.     do
  570.     {
  571.        cout<<"          Please enter number : ";
  572.        cin>>number;
  573.        if(number=='1')
  574.        {
  575.           insert();
  576.        }
  577.        else if(number=='2')
  578.        {
  579.           cal();
  580.        }
  581.        else if(number=='3')
  582.        {
  583.           trans();
  584.        }
  585.        else if(number=='4')
  586.        {
  587.           constant();
  588.        }
  589.        else if(number=='5')
  590.        {
  591.           cout<<"\n                            T-H-A-N-K-Y-O-U";
  592.           break;    
  593.        }
  594.        else
  595.        {
  596.           cout<<"          E-R-R-O-R\n";
  597.        }
  598.     }
  599.     while(number!='5');
  600.     getch();
  601.     return 0;  
  602. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement