Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Relation.cpp
- In this file, a data type representing a relation using a matrix of
- character bits are declared and some operations on relation is
- implemented as samples. Other operations are implemented by yourself .
- */
- #include "CharBit.cpp"
- #include <string.h>
- // Data type for a relation using a matrix of character bits.
- struct Relation {
- int numRow, numCol; // number of rows and columns
- CharBit** values; // a matrix of character bits
- };
- // 3 versions for initializations a relation
- void init (Relation& r) { // Empty relation
- r.numRow = r.numCol=0;
- r.values=NULL;
- }
- void init (Relation& r, int rr, int cc) { // rr x cc relation
- if (rr<=0 || cc <=0) {
- init(r);
- return;
- }
- r.numRow=rr; r.numCol=cc;
- if (rr==0 || cc==0) r.values=NULL;
- else {
- r.values = new CharBit*[r.numRow];
- for (int i=0;i<r.numRow;i++) {
- r.values[i]= new CharBit[r.numCol];
- for (int j=0;j<r.numCol;j++) clear(r.values[i][j]);
- }
- }
- }
- void init (Relation& r, int n) { // n x n relation
- init (r,n,n);
- }
- // De-allocate memory of a relation
- void dispose(Relation& r){
- for (int i=0;i<r.numRow;i++) delete[] r.values[i];
- delete[] r.values;
- r.values=NULL;
- r.numRow=r.numCol=0;
- }
- // set a row of a relation using a bit string
- void set ( int row, Relation& r, char* vals){
- int L= strlen(vals);
- if (L==0 || row<0 || row>=r.numRow) return;
- int minCols= r.numCol<L? r.numCol : L;
- int j;
- // copy bits
- for (j=0;j<minCols; j++) set(r.values[row][j], vals[j]);
- // clear extra bits
- for (j=minCols; j<r.numCol;j++) clear(r.values[row][j]);
- }
- // Clear input buffer
- void clear() {
- while (getchar()!='\n');
- }
- // input a relation from the keyboard
- void input (Relation& r){
- char S[80];
- printf("Relation is a matrix of %dx%d values\n", r.numRow, r.numCol);
- for (int i=0;i<r.numRow; i++){
- printf("Enter a bit string for the row %d: ", (i+1));
- gets(S);
- set(i,r,S);
- }
- }
- // print a relation to the screen
- void output (Relation&r){
- if (r.numRow==0) printf("Empty relation!\n");
- else {
- for (int i=0;i<r.numRow; i++) {
- for (int j=0;j<r.numCol;j++) print(r.values[i][j],2);
- putchar('\n');
- }
- }
- }
- // Checking whether a relation is reflexive or not.
- int isReflexive(Relation& r){
- if (r.numRow!=r.numCol) return 0; // It is not a relation a set
- for (int i=0;i<r.numRow;i++)
- if (get(r.values[i][i])!='1') return 0; // not reflexive
- return 1; // reflexive
- }
- // Finding union of two relations
- Relation unionRelation(Relation& r1, Relation& r2) {
- Relation result;
- if (r1.numRow != r2.numRow || r1.numCol!=r2.numCol) {
- init(result);
- }
- else {
- init(result,r1.numRow, r1.numCol);
- for (int i=0; i<result.numRow; i++)
- for (int j=0; j<result.numCol; j++)
- result.values[i][j]=r1.values[i][j]+r2.values[i][j];
- }
- return result;
- }
- // OTHER OPERATIONS ON RELATION ARE PUT HERE BY YOURSELF
- Relation inverse(Relation& r1) {
- Relation result = NULL;
- init(result, r1.numRow, r1.numCol);
- for (int i = 0; i < result.numRow; i++) {
- for (int j = 0; j < result.numCol; j++) {
- if (i != j) {
- if ((r1.values[i][j] == '0')) {
- result.values[i][j] = '1';
- }
- else {
- result.values[i][j] = '0';
- }
- }
- }
- }
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment