Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // TranTrung.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include "iostream"
- #include "fstream"
- #include "sstream"
- using namespace std;
- #define sign ' '
- #define filename "data.txt"
- #define MAX 100
- int numberOfPoints = 0;
- int toInt(string c)//Chuyen tu Char sang int
- {
- int r = (c[0] - '0')%48;
- return r;
- }
- int countofString(string source)//Tinh so ki tu khac ' ' trong 1 chuoi
- {
- int r = 0;
- for (int i = 0; i < source.length(); i++)
- {
- if (source[i] != ' ')
- r++;
- }
- return r;
- }
- void splitString(string source, int target[])//Cat chuoi thanh cac so va them vao mang
- {
- istringstream tmp(source);
- string temp;
- for (int i = 0; i<countofString(source); i++)
- {
- getline(tmp, temp, sign);
- target[i] = toInt(temp);
- }
- }
- void loadFromFile(int target[][MAX])//Lay du lieu tu file text vao mang
- {
- fstream fs;
- string str;
- int tmp[MAX];
- fs.open(filename, ios::in);
- if (fs.is_open())
- {
- int i = 0;
- while (fs.good() && !fs.eof())
- {
- getline(fs, str);
- if (str == "")
- break;
- else
- {
- splitString(str, tmp);
- for (int j = 0; j<countofString(str); j++)
- {
- target[i][j] = tmp[j];
- }
- i++;
- }
- }
- numberOfPoints = i;
- fs.close();
- }
- }
- void countRank(int source[][MAX], int target[])//Tinh toan bac cua cac phan tu trong mang roi luu vao 1 mang khac
- {
- for (int i = 0; i < numberOfPoints; i++)
- {
- int tmpcount = 0;
- for (int j = 0; j < numberOfPoints; j++)
- {
- if (source[j][i] == 1)
- tmpcount++;
- }
- target[i] = tmpcount;
- }
- }
- int maxOfRank(int rank[])
- {
- int max = rank[0];
- for (int i = 1; i < numberOfPoints; i++)
- {
- if (rank[i]>max)
- max = rank[i];
- }
- return max;
- }
- void deleteRowandCollumn(int input[][MAX], int pos)//Xoa dong va cot cua 1 phan tu trong mang tai vi tri pos
- {
- for (int i = pos; i < numberOfPoints; i++)//Xoa dong
- {
- for (int j = 0; j < numberOfPoints; j++)
- {
- input[i][j] = input[i + 1][j];
- }
- }
- for (int j = pos; j < numberOfPoints; j++)//Xoa cot
- {
- for (int i = 0; i < numberOfPoints; i++)
- {
- input[i][j] = input[i][j + 1];
- }
- }
- numberOfPoints--;//Giam so phan tu di 1
- }
- void decreaseRank(int source[][MAX], int rank[])//Giam bac cua cac diem co lien quan voi diem bi xoa
- {
- int rankmax=maxOfRank(rank);//lay gia tri bac cao nhat
- for (int i = 0; i < numberOfPoints; i++)
- {
- if (rank[i] == rankmax)//lay vi tri cua phan tu co bac cao nhat
- {
- deleteRowandCollumn(source, i);//Xoa phan tu co bac cao nhat ra khoi mang
- }
- }
- countRank(source, rank);//Giam bac cua cac phan tu co lien quan(tinh lai bac theo bang moi)
- }
- void showArr(int input[][MAX], int cnt)
- {
- for (int i = 0; i < cnt; i++)
- {
- for (int j = 0; j < cnt; j++)
- {
- cout << input[i][j] << " ";
- }
- cout << endl;
- }
- cout << endl;
- }
- void showArr(int input[], int cnt)
- {
- for (int i = 0; i < cnt; i++)
- cout << input[i] << " ";
- cout << " RANK" << endl << endl;
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- int arr[MAX][MAX];
- int rank[MAX];
- //cout << "Nhap n: "; cin >> n;
- loadFromFile(arr);
- showArr(arr, numberOfPoints);
- countRank(arr, rank);
- showArr(rank, numberOfPoints);
- decreaseRank(arr, rank);
- showArr(arr, numberOfPoints);
- showArr(rank, numberOfPoints);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment