detective1711

Ma tran vuong, bac cua ma tran

Mar 27th, 2014
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.28 KB | None | 0 0
  1. // TranTrung.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "iostream"
  6. #include "fstream"
  7. #include "sstream"
  8.  
  9.  
  10. using namespace std;
  11.  
  12. #define sign ' '
  13. #define filename "data.txt"
  14. #define MAX 100
  15.  
  16. int numberOfPoints = 0;
  17. int toInt(string c)//Chuyen tu Char sang int
  18. {
  19.     int r = (c[0] - '0')%48;
  20.     return r;
  21. }
  22. int countofString(string source)//Tinh so ki tu khac ' ' trong 1 chuoi
  23. {
  24.     int r = 0;
  25.     for (int i = 0; i < source.length(); i++)
  26.     {
  27.         if (source[i] != ' ')
  28.             r++;
  29.     }
  30.     return r;
  31. }
  32. void splitString(string source, int target[])//Cat chuoi thanh cac so va them vao mang
  33. {
  34.     istringstream tmp(source);
  35.     string temp;
  36.     for (int i = 0; i<countofString(source); i++)
  37.     {
  38.         getline(tmp, temp, sign);
  39.         target[i] = toInt(temp);
  40.     }
  41.  
  42. }
  43. void loadFromFile(int target[][MAX])//Lay du lieu tu file text vao mang
  44. {
  45.     fstream fs;
  46.     string str;
  47.     int tmp[MAX];
  48.     fs.open(filename, ios::in);
  49.     if (fs.is_open())
  50.     {
  51.         int i = 0;
  52.         while (fs.good() && !fs.eof())
  53.         {
  54.             getline(fs, str);
  55.             if (str == "")
  56.                 break;
  57.             else
  58.             {      
  59.                 splitString(str, tmp);
  60.                 for (int j = 0; j<countofString(str); j++)
  61.                 {
  62.                     target[i][j] = tmp[j];
  63.                 }
  64.                 i++;
  65.             }
  66.         }
  67.         numberOfPoints = i;
  68.         fs.close();
  69.     }
  70. }
  71. void countRank(int source[][MAX], int target[])//Tinh toan bac cua cac phan tu trong mang roi luu vao 1 mang khac
  72. {
  73.     for (int i = 0; i < numberOfPoints; i++)
  74.     {
  75.         int tmpcount = 0;
  76.         for (int j = 0; j < numberOfPoints; j++)
  77.         {
  78.             if (source[j][i] == 1)
  79.                 tmpcount++;
  80.         }
  81.         target[i] = tmpcount;
  82.     }
  83. }
  84. int maxOfRank(int rank[])
  85. {
  86.     int max = rank[0];
  87.     for (int i = 1; i < numberOfPoints; i++)
  88.     {
  89.         if (rank[i]>max)
  90.             max = rank[i];
  91.     }
  92.     return max;
  93. }
  94. void deleteRowandCollumn(int input[][MAX], int pos)//Xoa  dong va cot cua 1 phan tu trong mang tai vi tri pos
  95. {
  96.     for (int i = pos; i < numberOfPoints; i++)//Xoa dong
  97.     {
  98.         for (int j = 0; j < numberOfPoints; j++)
  99.         {
  100.             input[i][j] = input[i + 1][j];
  101.         }
  102.     }
  103.     for (int j = pos; j < numberOfPoints; j++)//Xoa cot
  104.     {
  105.         for (int i = 0; i < numberOfPoints; i++)
  106.         {
  107.             input[i][j] = input[i][j + 1];
  108.         }
  109.     }
  110.     numberOfPoints--;//Giam so phan tu di 1
  111. }
  112. void decreaseRank(int source[][MAX], int rank[])//Giam bac cua cac diem co lien quan voi diem bi xoa
  113. {
  114.     int rankmax=maxOfRank(rank);//lay gia tri bac cao nhat
  115.     for (int i = 0; i < numberOfPoints; i++)
  116.     {
  117.         if (rank[i] == rankmax)//lay vi tri  cua phan tu co bac cao nhat
  118.         {
  119.             deleteRowandCollumn(source, i);//Xoa phan tu co bac cao nhat ra khoi mang
  120.         }
  121.     }
  122.     countRank(source, rank);//Giam bac cua cac phan tu co lien quan(tinh lai bac theo bang moi)
  123. }
  124. void showArr(int input[][MAX], int cnt)
  125. {
  126.     for (int i = 0; i < cnt; i++)
  127.     {
  128.         for (int j = 0; j < cnt; j++)
  129.         {
  130.             cout << input[i][j] << " ";
  131.         }
  132.         cout << endl;
  133.     }
  134.     cout << endl;
  135. }
  136. void showArr(int input[], int cnt)
  137. {
  138.     for (int i = 0; i < cnt; i++)
  139.         cout << input[i] << " ";
  140.     cout << "   RANK" << endl << endl;
  141. }
  142. int _tmain(int argc, _TCHAR* argv[])
  143. {
  144.     int arr[MAX][MAX];
  145.     int rank[MAX];
  146.     //cout << "Nhap n: "; cin >> n;
  147.     loadFromFile(arr);
  148.     showArr(arr, numberOfPoints);
  149.     countRank(arr, rank);
  150.     showArr(rank, numberOfPoints);
  151.  
  152.     decreaseRank(arr, rank);
  153.     showArr(arr, numberOfPoints);
  154.     showArr(rank, numberOfPoints);
  155.     return 0;
  156. }
Advertisement
Add Comment
Please, Sign In to add comment