C++基础编程任务二:动态矩阵(12月26日更新)

c++_21任务要求:

  1. 编写矩阵类,成员包括行数和列数:m,n
  2. 用m,n的构造函数
  3. 输入函数:从文件中读取矩阵
  4. 输出函数:向文件中保存矩阵
  5. 求矩阵的+-*/运算
  6. 求矩阵的秩,标准形
  7. 求逆

注:

程序基于Linux平台,移植到Windows请更改 system("clear")和 anyKey(void) 函数,还有一些文字格式的标记

程序在 openSUSE Leap 42.1 with g++ 4.8.5 环境下测试成功,欢迎留言反馈出现的问题

12.26更新内容:

  1. 基本用户菜单(1,2,a,0可用)
  2. Linux下实现“按任意键继续”的 anyKey(void) 函数
  3. 多文件:matrix.h(类的定义) matrix.cpp(类成员函数定义) main.cpp(主程序)

程序代码:

这里只写了前4个任务,后面的都是算法的问题,未完待续

matrix.h

#include <vector>
#include <iostream>

using namespace std;

class matrix{
//矩阵数据和操作
    int countOfRow;        //行数
    int countOfColumn;     //列数

    vector<int> *row;
public:
    //空构造函数
    matrix();

    //手动输入矩阵
    matrix(int& _rowNum, int& _columnNum);

    //从文件读取矩阵
    matrix(const char* fileName);

    ~matrix();
    //int getcountOfRow();
    //int getcountOfColumn();

    //修改某个位置的值
    void modify(int&, int&, int&);

    //从文件中读取矩阵
    void getDataFromFile(const char * );
    
    //由用户输入矩阵
    void getDataFromUser(void);

    //将当前矩阵保存到文件中
    void saveToFile(const char * );

    friend ostream& operator<<(ostream&, matrix*);

    friend void show(matrix *M);
};

class Menus{
//用户菜单
public:
    void showMainMenu(matrix *A, matrix *B);
    void showInputMenu(matrix *M);
private:
    char choice;
    char fileName[255];
    void closeMenu();
};

mairix.cpp

#include "matrix.h"
#include <fstream>
#include <cstdlib>

using namespace std;

void anyKey(){
	cout<<"\n按任意健继续...\n";
	//char key;
	
	getchar();
}

matrix::matrix(){

}

//手动输入矩阵
matrix::matrix(int& _rowNum, int& _columnNum)
        :countOfRow(_rowNum),countOfColumn(_columnNum){

    row = new vector<int>[countOfRow];
    int tmp;
    for(int i=0; i<countOfRow; i++){
        cout<<"输入第"<<i+1<<"行:";
        for(int j=0; j<countOfColumn; j++){
            cin>>tmp;
            row[i].push_back(tmp);
        }
        cin.ignore(1024,'\n');
        //清空超出行长度的数据
    }
}

//从文件
matrix::matrix(const char* fileName){
    getDataFromFile(fileName);
}


matrix::~matrix(){
    //晚点写,先占个位
}

void matrix::modify(int& _rowNum, int& _columnNum, int& _item){
    if(_rowNum<countOfRow && _rowNum>0
        && _columnNum<countOfColumn && _columnNum>0){
            row[_rowNum-1][_columnNum-1] = _item;
            cout<<"Warning:修改成功,记得手动保存到文件!\n";
    }
}


void matrix::getDataFromFile(const char * fileName){
    fstream fileStream;
    fileStream.open(fileName,ios::in);

    //确定文件中矩阵的列数
    int *tmp = new int;
    char *c = new char;
    int  count = 0;
    for(int i=0;i<10000;i++){
        fileStream>>(*tmp);
        //cout<<(*tmp);
        ++count;
        (*c)=fileStream.peek();
        if((*c)=='\n')
        {
            break;
        }
    }
    countOfColumn = count;
    //

    delete tmp;
    delete c;
    count = 0;

    //确定文件中矩阵的行数
    fileStream.seekg(0);        //指针归位
    string *temp = new string;
	if(fileStream){
        while(getline(fileStream,*temp,'\n')){
            count++;
        }
	    fileStream.close();
    }
    countOfRow = count;
    delete temp;

	cout<<"\n读取到"<<countOfRow<<"行,"<<countOfColumn<<"列的矩阵:\n";
	
    //读取文件内容并存储到内存中
    tmp = new int;
    row = new vector<int>[countOfRow];
   

    fileStream.open(fileName);
    for(int i=0; i<countOfRow; i++){
        for(int j=0; j<countOfColumn; j++){
            fileStream>>(*tmp);
                cout.width(6);
                cout<<*tmp;
            row[i].push_back(*tmp);
            (*c)=fileStream.peek();
            if((*c)=='\n')
            {
                fileStream.seekg(1,ios::cur);
            }
        }
        cout<<'\n';
    }
    delete tmp;
	
	return ;
}

void matrix::getDataFromUser(void){
	cout<<"请输入行数:";	cin>>this->countOfRow;
	cin.ignore(1024,'\n');
	cout<<"请输入列数:";	cin>>this->countOfColumn;
	cin.ignore(1024,'\n');
	
	cout<<"\n请输入矩阵数据,每行"
		<<this->countOfColumn
		<<"个数字,共"
		<<this->countOfRow
		<<"行,\nWarning:多余数据会被忽略:\n";
	this->row = new vector<int>[countOfRow];
    int tmp;
    for(int i=0; i<this->countOfRow; i++){
        cout<<"输入第"<<i+1<<"行:";
        for(int j=0; j<this->countOfColumn; j++){
            cin>>tmp;
            this->row[i].push_back(tmp);
        }
        //清空超出行长度的数据
        cin.ignore(1024,'\n');
    }
    return ;
}

void matrix::saveToFile(const char * fileName){
    fstream fileStream;
    fileStream.open(fileName,ios::out);
    for(int i=0; i<countOfRow; i++){
        for(int j=0; j<countOfColumn; j++){
            fileStream.width(8);
            fileStream<<row[i][j];
        }
        fileStream<<'\n';
    }
}

ostream& operator<<(ostream& stream, matrix* m){
    for(int i=0; i<m->countOfRow; i++){
        for(int j=0; j<m->countOfColumn; j++){
            stream.width(6);
            stream<<m->row[i][j]<<' ';
        }
        cout<<'\n';
    }
    return stream;
}

void show(matrix *M)
{
	for(int i=0; i<M->countOfRow; i++){
        for(int j=0; j<M->countOfColumn; j++){
            cout.width(6);
            cout<<M->row[i][j]<<' ';
        }
        cout<<'\n';
    }
}



/*********************************************************************************************/
/*********************************************************************************************/

void Menus::showMainMenu(matrix *A, matrix *B){

    while(1){
        system("clear");
        cout<<"*******************************************************\n";
        cout<<"*                                                     *\n";
        cout<<"*                  本程序提供矩阵的基础计算           *\n";
        cout<<"*                 注意:如果从文件中读取矩阵,        *\n";
        cout<<"*                   文件中\033[36m行尾不得有空格\033[0m              *\n";
        cout<<"*                                                     *\n";
        cout<<"*******************************************************\n";
        
		cout<<"                 1.输入矩阵A\n";
        cout<<"                 2.输入矩阵B\n";
        cout<<"                 3.计算A+B\n";
        cout<<"                 4.计算A-B\n";
        cout<<"                 5.计算A*B\n";
        cout<<"                 6.计算A/B\n";
        cout<<"                 7.计算秩\n";
        cout<<"                 8.计算标准形\n";
        cout<<"                 9.计算逆矩阵\n";
		cout<<"                 a.输出矩阵A和B\n";
		cout<<"                 0.退出程序\n";
		
        cout<<"请输入你的选择:";
            cin>>choice;
        switch (choice) {
			case '0':
				exit(0);
            case '1':
                //choice = '0';
                showInputMenu(A);
                break;
			case '2':
				showInputMenu(B);
				break;
			case 'a':
			case 'A':
				
				system("clear");
				cout<<"矩阵A:\n";
				cout<<A;
				cout<<"矩阵B:\n";
				cout<<B;
				getchar();
				anyKey();
				
				break;

        }
    }
}

void Menus::showInputMenu(matrix *M){

    while(1){
        system("clear");
        cout<<"*******************************************************\n";
        cout<<"*                                                     *\n";
        cout<<"*                 注意:如果从文件中读取矩阵,        *\n";
        cout<<"*                   文件中\033[36m行尾不得有空格\033[0m              *\n";
        cout<<"*                                                     *\n";
        cout<<"*******************************************************\n";
        cout<<"请选择输入矩阵的方式:\n";
        cout<<"                 1.从文件读取\n";
        cout<<"                 2.手动输入\n";
		cout<<"                 0.返回上级菜单\n";
        cout<<"你的选择:";
        cin>>choice;
        switch (choice) {
			case '0':
				//showMainMenu();
				return ;
				break;
            case '1':
                //choice = '0';
                system("clear");
                cout<<"如果不与程序处于同级目录,需要文件的绝对路径\n";
                cout<<"请输入文件名(长度不超过255):";

                cin>>fileName;
                    M->getDataFromFile(fileName);

                cin.get();
                anyKey();
				return ;
                break;
            case '2':
                system("clear");
				M->getDataFromUser();
				
				//cin.get();
				anyKey();
				return ;
                break;

        }
    }
}

 

main.cpp

#include "matrix.cpp"
//#include <iostream>
using namespace std;

using namespace std;

int main(){
    matrix *A = new matrix;
    matrix *B = new matrix;

    Menus menu;
    menu.showMainMenu(A, B);
	
	exit(0);


    /*
    int choice;
    //cout.unsetf();
    matrix m("MATRIX.DAT");
    cout<<"创建好的矩阵为:\n";
    cout<<m;
    int a=2,b=3,c=34;
    m.modify(a,b,c);
    cout<<m;
    //m.saveToFile("test1");
    */
    
}

 

标签: C++

精彩评论
  1. 大哥带我飞

发表评论: