c++添加操作菜单

标签 c++ arrays matrix menu operations

下面是我目前使用的代码,我的问题是如何从这里开始包含一个菜单,以便可以对从文本文件读取的矩阵执行操作?

操作示例可以是行列式、加法、减法等

#include <iostream>  //declaring variables
#include <iomanip>
#include <string>
#include <fstream>

using namespace std;
string code(string& line);
int main()
{
    int MatrixA[3][3] = {{1,0,0},{0,2,0},{0,0,3}};

    ofstream outf;
    ifstream myfile;
    string infile;
    string line;
    string outfile;

    cout << "Please enter an input file (A.txt) for Matrix A or (B.txt) for Matrix B" << endl;
    cin >> infile;   //prompts user for input file

    if (infile == "A.txt")
    {      //read whats in it and write to screen

        myfile.open("A.txt");
        cout << endl;
        while (getline (myfile, line))
        cout << line << endl;

        //float elements[9];
        //ifstream myfile("A.txt");
        //myfile >> elements[0] >> elements[1] >> elements[2];
        //myfile >> elements[3] >> elements[4] >> elements[5];
        //myfile >> elements[6] >> elements[7] >> elements[8];

        //myfile.close();


    }
    else
        if (infile == "B.txt")
        {
            myfile.open("B.txt");
            cout << endl;
            while (getline (myfile, line))
            cout << line << endl;
        }
        else
    { 
        cout << "Unable to open file." << endl;
    }
        //{
            //while("Choose next operation");

        //}


    return 0;
}

最佳答案

你可以使用枚举和 switch 语句来写这样的东西:

enum options { Operation1, Operation2, Operation3 };

cout << "Operations:\n\n";
cout << "Operation1 - " << Operation1 << "\n";
cout << "Operation2 - " << Operation2 << "\n";
cout << "Operation3 - " << Operation3 << "\n";
cout << "Your choice: ";
int choice;
cin >> choice;

switch (choice)
{
case Operation1:
    cout << "You picked Operation1.\n";
    break;
case Operation2:
    cout << "You picked Operation2.\n";
    break;
case Operation3:
    cout << "You picked Operation3.\n";
    break;
default:
    cout << "You made an illegal choice.\n";
    break;
}

完成后,您可以在需要时将其放入 while/for 循环,并在需要时退出...

关于c++添加操作菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23198822/

相关文章:

c++ - OpenGL 属性偏移量未按预期工作

javascript - Lodash _.hasIntersection?

arrays - 包含在数字范围内的组哈希键

arrays - NumPy 数组在赋值/复制时丢失维度,为什么?

c++ - 编译单个大头文件(VS : C1063)

c++ - 我该如何解决这个缓冲区溢出问题?

r - 如何创建一个对角矩阵,对角线上有 block ,每个 block 有一个不同的矩阵?

c++ - OpenCV 断言失败 - convertTo

c++ - 在构造函数中初始化 vector C++

r - 如何检查矩阵是否是矩阵列表的元素?在 R