[C++] Đọc ma trận kề, danh sách kề bằng fstream, sử dụng tham số dòng lệnh

Tác giả:

  • Trần Hán Huy – tranhanhuy.wordpress.com

Đề bài

  • Đọc ghi tập tin với tham số dòng lệnh:

Cấu trúc tham số dòng lệnh:
<Tên chuong trinh.exe> <tham số : L – ds kề, m – ma trận kề > <tên file input> <tên file output>

in1 : ma tran ke
4
0 1 5 1
1 0 0 2
1 0 0 0
1 1 0 0

in2 : ds ke
2 3 4
1 2 3
1 3 5
2 4 6

output
Dinh 1 noi voi dinh 2: chi phi
…..

Linkdown code demo:
http://www.mediafire.com/file/pnxbejrpi2cjfxj/LTDT-DocFile-fstream.rar

Code Class DSKe:

#pragma once
#include <stdio.h>
#include <conio.h>
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

struct Canh
{
   int DinhDau;
   int DinhCuoi;
   int TrongLuong;
};

class DSKe
{
private:
   vector<Canh> m_DSKe;
public:
   DSKe(void);
   ~DSKe(void);
public:
   void DocFileDSKe(char *TenFile);
   void GhiFileDSKe(char *TenFile);
};
DSKe::DSKe(void)
{
}
DSKe::~DSKe(void)
{
}
void DSKe:: DocFileDSKe(char *TenFile)
{
   ifstream DocTep(TenFile);
   if (!DocTep.is_open())
   {
      cout << "Khong mo file duoc!, conloyal";
      return;
   }
   else
   {
      Canh Tam;
      while (!DocTep.eof())
      {
          DocTep >> Tam.DinhDau;
          DocTep >> Tam.DinhCuoi;
          DocTep >> Tam.TrongLuong;
          m_DSKe.push_back(Tam);
          cout << Tam.DinhDau << " - " << Tam.DinhCuoi << " : " << Tam.TrongLuong << endl;
      }
      DocTep.close();
   }
}

void DSKe:: GhiFileDSKe(char *TenFile)
{
   ofstream MoTep(TenFile);
   if (!MoTep.is_open())
   {
       cout << "Khong tao file duoc!";
       return;
   }
   else
   {
       for (int i=0; i<(signed)m_DSKe.size(); i++)
       {
           MoTep << "Dinh " << m_DSKe[i].DinhDau << " noi voi dinh " << m_DSKe[i].DinhCuoi << " voi chi phi: " << m_DSKe[i].TrongLuong << "\n";
       }
       MoTep.close();
   }
}

Code Class MaTranKe

#pragma once
#include <stdio.h>
#include <conio.h>
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

#define MAX 100

class MaTranKe
{
private:
    vector<vector<int>> m_Matrix;
public:
   MaTranKe(void);
   ~MaTranKe(void);
public:
   void DocFileMaTran(char *TenFile);
   void GhiFileMaTran(char *TenFile);
};

MaTranKe::MaTranKe(void)
{
}

MaTranKe::~MaTranKe(void)
{
}
void MaTranKe:: DocFileMaTran(char *TenFile)
{
   ifstream DocTep(TenFile);
   if (!DocTep.is_open())
   {
        cout << "Khong mo file duoc!";
        return;
   }
   else
   {
        int n;
        DocTep >> n;
        m_Matrix.resize(n);

        cout << " " << n << endl;

        for (int i=0 ; i<(signed) m_Matrix.size(); i++)
        {
             m_Matrix[i].resize(m_Matrix.size());
             for(int j=0; j< (signed)m_Matrix[i].size(); j++)
             {
                 DocTep >> m_Matrix[i][j];
                 cout << " " << m_Matrix[i][j];
             }
             cout << endl;
        }
        DocTep.close();
    }
}

void MaTranKe:: GhiFileMaTran(char *TenFile)
{
    ofstream MoTep(TenFile);
    if (!MoTep.is_open())
    {
         cout << "Khong tao file duoc!, conloyal";
         return;
    }
    else
    {
         MoTep << "Co " << m_Matrix.size() << " dinh\n" ;

         for (int i=0 ; i<(signed) m_Matrix.size(); i++)
         {
             for(int j=0; j< (signed)m_Matrix[i].size(); j++)
             {
                  if ( m_Matrix[i][j] != 0)
                  {
                      MoTep << "Dinh " << i+1 << " noi voi dinh " << j+1 << " voi chi phi: " << m_Matrix[i][j] << "\n";
                  }
             }
        }
        MoTep.close();
    }
}

Code main

#include "MaTranKe.h"
#include "DSKe.h"
void main(int n, char* a[])
{
    if (strcmp(a[1], "m") == 0)
    {
        cout << "Ma tran ke: " << endl;
        MaTranKe dt;
        dt.DocFileMaTran(a[2]);
        dt.GhiFileMaTran(a[3]);
    }
    else if (strcmp(a[1], "l") == 0)
    {
        cout << "Danh sach ke: " << endl;
        DSKe ds;
        ds.DocFileDSKe(a[2]);
        ds.GhiFileDSKe(a[3]);
    }
    else
    {
        cout << "du lieu dua vao khong hop le, conloyal";
    }
}

One response to “[C++] Đọc ma trận kề, danh sách kề bằng fstream, sử dụng tham số dòng lệnh”

  1. Kem Cute hehe says :

    tks anh,tèn ten ten 😀

Leave a comment