hqt

temporaryShit

hqt
Oct 4th, 2012
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.51 KB | None | 0 0
  1. /* Relation.cpp
  2. In this file, a data type representing a relation using a matrix of
  3. character bits are declared and some operations on relation is
  4. implemented as samples. Other operations are implemented by yourself .
  5. */
  6. #include "CharBit.cpp"
  7. #include <string.h>
  8. // Data type for a relation using a matrix of character bits.
  9. struct Relation {
  10.    int numRow, numCol; // number of rows and columns
  11.    CharBit** values;  // a matrix of character bits
  12. };
  13. // 3 versions for initializations a relation
  14. void init (Relation& r) { // Empty relation
  15.    r.numRow = r.numCol=0;
  16.    r.values=NULL;
  17. }
  18.  void init (Relation& r, int rr, int cc) { // rr x cc relation
  19.    if (rr<=0 || cc <=0) {
  20.      init(r);
  21.      return;
  22.    }
  23.    r.numRow=rr; r.numCol=cc;
  24.    if (rr==0 || cc==0) r.values=NULL;
  25.    else {
  26.      r.values = new CharBit*[r.numRow];
  27.      for (int i=0;i<r.numRow;i++) {
  28.        r.values[i]= new CharBit[r.numCol];
  29.        for (int j=0;j<r.numCol;j++) clear(r.values[i][j]);
  30.      }
  31.    }  
  32. }
  33. void init (Relation& r, int n) {   // n x n relation
  34.    init (r,n,n);  
  35. }
  36. // De-allocate memory of a relation
  37. void dispose(Relation& r){
  38.    for (int i=0;i<r.numRow;i++) delete[] r.values[i];
  39.    delete[] r.values;
  40.    r.values=NULL;  
  41.    r.numRow=r.numCol=0;
  42. }
  43. // set a row of a relation using a bit string
  44. void set ( int row, Relation& r, char* vals){
  45.   int L= strlen(vals);
  46.   if (L==0 || row<0 || row>=r.numRow) return;
  47.   int minCols= r.numCol<L? r.numCol : L;
  48.   int j;
  49.   // copy bits
  50.   for (j=0;j<minCols; j++) set(r.values[row][j], vals[j]);
  51.   // clear extra bits
  52.   for (j=minCols; j<r.numCol;j++) clear(r.values[row][j]);
  53. }
  54. // Clear input buffer
  55. void clear() {
  56.   while (getchar()!='\n');
  57. }
  58. // input a relation from the keyboard
  59. void input (Relation& r){
  60.    char S[80];
  61.    printf("Relation is a matrix of %dx%d values\n", r.numRow, r.numCol);
  62.    for (int i=0;i<r.numRow; i++){
  63.      printf("Enter a bit string for the row %d: ", (i+1));
  64.      gets(S);
  65.      set(i,r,S);
  66.    }
  67. }
  68. // print a relation to the screen
  69. void output (Relation&r){
  70.   if (r.numRow==0) printf("Empty relation!\n");
  71.   else {
  72.      for (int i=0;i<r.numRow; i++) {
  73.        for (int j=0;j<r.numCol;j++) print(r.values[i][j],2);
  74.        putchar('\n');  
  75.      }
  76.   }  
  77. }
  78. // Checking whether a relation is reflexive or not.
  79. int isReflexive(Relation& r){
  80.     if (r.numRow!=r.numCol) return 0; // It is not a relation a set
  81.     for (int i=0;i<r.numRow;i++)
  82.       if (get(r.values[i][i])!='1') return 0; // not reflexive
  83.     return 1; // reflexive
  84. }
  85. // Finding union of two relations
  86. Relation unionRelation(Relation& r1, Relation& r2) {
  87.     Relation result;
  88.     if (r1.numRow != r2.numRow || r1.numCol!=r2.numCol) {
  89.         init(result);
  90.     }
  91.     else {
  92.        init(result,r1.numRow, r1.numCol);
  93.        for (int i=0; i<result.numRow; i++)
  94.          for (int j=0; j<result.numCol; j++)
  95.             result.values[i][j]=r1.values[i][j]+r2.values[i][j];
  96.     }
  97.     return result;
  98. }
  99. // OTHER OPERATIONS ON RELATION ARE PUT HERE BY YOURSELF
  100.  
  101. Relation inverse(Relation& r1) {
  102.  
  103.     Relation result = NULL;
  104.    
  105.  
  106.     init(result, r1.numRow, r1.numCol);
  107.    
  108.     for (int i = 0; i < result.numRow; i++) {
  109.         for (int j = 0; j < result.numCol; j++) {
  110.             if (i != j) {
  111.                 if ((r1.values[i][j] == '0')) {
  112.                         result.values[i][j] = '1';
  113.                 }
  114.                 else {
  115.                     result.values[i][j] = '0';
  116.                 }
  117.             }
  118.         }
  119.     }
  120.    
  121.     return result;
  122.  
  123. }
Advertisement
Add Comment
Please, Sign In to add comment