1 // 离散实验程序1.cpp : Defines the entry point for the console application.
2 //
3
4 #include "stdafx.h"
5
6 #include<iostream>
7 using namespace std;
8 #define MAX 50
9 int M[MAX][MAX];
10 int i,j,p,q,n,l;
11 char A[MAX]; // 存放集合
12 char B[MAX]; //存放关系
13 void relation() //输入关系
14 {
15 gets(B);
16 l=strlen(B);
17 cout<<"您输入的关系为"<<endl;
18 cout<<"R={";
19 for(j=0;j<l;j=j+2)
20 {
21 cout<<"<";
22 cout<<B[j];
23 cout<<","<<B[j+1];
24 cout<<">";
25 cout<<"}\n";
26 }
27 }
28
29 void translate() //关系转化为关系矩阵的函数
30 {
31 int p,q,i=0,j;
32 while(B[i]!='\0')
33 {
34
35
36 for(j=0;j<n;j++)
37 {
38 if (B[i]==A[j])
39 {
40 p=j;
41 break;
42 }
43 }
44 i++;
45 while(B[i]!='\0')
46 {
47 for(j=0;j<n;j++)
48 if (B[i]==A[j])
49 q=j;
50 M[p][q]=1;
51 break;
52 }
53 if(j==n)
54 i++;
55 else
56 {i++;
57 break;
58 }
59 }
60 i++;
61 }
62
63 void display() //输出关系矩阵
64 {
65 int i,j;
66 for(i=0;i<n;i++)
67 {
68 for(j=0;j<n;j++)
69 {
70 cout<<M[i][j];
71 }
72 cout<<"\n";
73 }
74
75 }
76
77 void main()
78 {
79 cout<<"请输入一个有限集合A"<<endl;
80 gets(A);
81 n=strlen(A);
82 cout<<"您输入的集合为"<<endl;
83 cout<<"A{";
84 for(i=0;i<n;i++)
85 cout<<A[i];
86 cout<<"}\n";
87 cout<<"请输入此集合上的一个关系R";
88 relation();
89 translate();
90 cout<<"R的关系矩阵为\n";
91 display();
92
93 }
1 //失败的实验
2 #include<iostream>
3 using namespace std;
4
5 void output();
6 void choose();
7 void change();
8
9 int n;
10 int *arr1=new int[n];
11 int **arr=new int*[n];
12 for(int i=0;i<n;i++)
13 {
14 arr[i]=new int[n];
15 }//??????????,?M,?N
16 void main()
17 {
18 int n;
19
20 //int M=0,N=0;
21 //int *array1=new int[M];
22 //int *array2=new int[N];
23 cout<<"Please input the length:"<<endl;
24 cin>>n;
25 cout<<"Please input some numbers:"<<endl;
26 for(int i=0;i<n;i++)
27 {cin>>arr1[i];
28 }
29 choose();
30 output();
31 /*end:cout<<"Please enter 1(continue) or 2(break) to choose:"<<endl;
32 cin>>temp1;
33 switch(temp1)
34 {
35 case 1:
36 {
37 cout<<"Please input sizes of height and width: "<<endl;
38 cin>>M>>N;
39
40 for(int k=0;k<M;k++)
41 arr[k]=new int[N];
42 for(int i=0;i<M;i++)
43 {
44 for(int j=0;j<N;j++)
45 arr[i][j]=0;
46 }
47 choose(arr);
48 output(M,N,arr);
49 cout<<"Please input number 1 to again or 2 to exit:"<<endl;
50 cin>>temp2;
51 switch(temp2)
52 {
53 case 1:
54 goto end;
55 case 2:
56 exit(1);
57 }
58 system("pause");
59 }
60 case 2:
61 break;
62 }*/
63 }
64
65 void change()
66 {
67 for(int i=0;i<n;i++)
68 for(int j=0;j<n;j++)
69 {if(arr1[i]>arr1[j])
70 {
71 arr[i][j]=1;
72 }
73 }
74 }
75
76
77
78
79 void choose()
80 {
81
82 int i,j,k,h;
83 cout<<"Please input the two relations:"<<endl;
84 cin>>i>>j;
85 cin>>k>>h;
86 if((i>j)&&(k>h))
87 {
88 change();
89 }
90
91 }
92
93 void output()
94 {
95 for(int i=0;i<sizeof(arr1);i++)
96 {
97 for(int j=0;j<sizeof(arr1);j++)
98 {
99 cout<<arr[i][j]<<"-";
100 }
101 cout<<endl;
102 }
103 }