gpt4 book ai didi

c++添加操作菜单

转载 作者:行者123 更新时间:2023-11-30 04:05:40 24 4
gpt4 key购买 nike

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

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

#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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com