void main ()
{
bool start_over = true;
while(start_over) .
{
system(“cls”); //clear the screen.
int choice = 0;
//main menu.
cout
<< “\n1: Add two Matrices” <<endl
<< “2: Subtract two Matrices” <<endl
<< “3: Multiply two Matrices” <<endl
<< “4: Transpose of a two Matrix” <<endl
<< “5: Inverse of a two Matirx” <<endl
<< “7: Exit”<<endl
<< “\nPlease enter your choice. (1 – 7)”<<endl;
cin >> choice;
while (cin.fail() || choice<1 || choice>7) //validate input
{
cout << “Invalid Input!”;
cin.clear(); //clear in stream flag.
cin.ignore(0xFFFF, ‘\n’); //ignore bad bits
cin >> choice;
}
switch(choice)
{
case 1:
{
system(“cls”);
cout << “Operation 1: Addition of two matrices” <<endl;
double elements[9]={1,2,3,4,5,6,7,8,9};
Matrix33d a(elements), b(elements);
cout << “\nElements of the first matrix: A” << endl;
cout << a; //<< operator is overloaded, displays first matrix on the screen
cout << “\nElements of the second matrix: B” << endl;
cout << b; //<<operator is overloaded, displays second matrix on the screen
cout << “\nElements of the resultant matrix: A + B” << endl;
cout << a.Add(a,b); //call add function and display the result on the screen
cout << “\nWhat do you want to do next?”<<endl; //ask the user for his choice
cout << “1: Back to main menu”<<endl;
cout << “2: Exit”<<endl; //exit the program
cin >> choice;
while (cin.fail() || choice<1 || choice>2)
{
cout << “Invalid Input!”;
cin.clear();
cin.ignore(0xFFFF, ‘\n’);
cin >> choice;
}
if (choice == 2)
start_over = false; .
break;
}
case 2:
{
system(“cls”);
cout << “Operation 2: Subtraction of two matrices” <<endl;
double elements[9]={1,2,3,4,5,6,7,8,9};
Matrix33d a(elements), b(elements);
cout << “\nElements of the first matrix: A” << endl;
cout << a;
cout << “\nElements of the second matrix: B” << endl;
cout << b;
cout << “\nElements of the resultant matrix: A – B” << endl;
//subtract function to subtract the matrices, result is shown on the screen after calculation.
cout << a.Subtract(a,b);
cout << “\nWhat do you want to do next?”<<endl;
cout << “1: Back to main menu”<<endl;
cout << “2: Exit”<<endl;
cin >> choice;
while (cin.fail() || choice<1 || choice>2)
{
cout << “Invalid Input!”;
cin.clear();
cin.ignore(0xFFFF, ‘\n’);
cin >> choice;
}
if (choice == 2)
start_over = false;
break;
}
case 3:
{
system(“cls”);
cout << “Operation 3: Multiplication of two matrices” <<endl;
double elements[9]={1,2,3,4,5,6,7,8,9};
Matrix33d a(elements), b(elements);
cout << “\nElements of the first matrix: A” << endl;
cout << a;
cout << “\nElements of the second matrix: B” << endl;
cout << b;
cout << “\nElements of the resultant matrix: A * B” << endl;
cout << a*b;
cout << “\nWhat do you want to do next?”<<endl;
cout << “1: Back to main menu”<<endl;
cout << “2: Exit”<<endl;
cin >> choice;
while (cin.fail() || choice<1 || choice>2)
{
cout << “Invalid Input!”;
cin.clear();
cin.ignore(0xFFFF, ‘\n’);
cin >> choice;
}
if (choice == 2)
start_over = false;
break;
}
|
دیدگاهتان را بنویسید