离散实践1
1 #include<iostream> 2 using namespace std; 3 void main() 4 { 5 int M,N,p,q,temp=1; 6 char i,j; 7 while(temp) //控制程序是否要执行 8 { 9 cout<<"请输入你的选择temp:\n"; 10 cout<<"您输入0时:不再运行程序,直接退出程序。\n"; 11 cout<<"您输入1时:运行程序即输出二元关系矩阵。\n"; 12 cin>>temp; 13 switch(temp) 14 { 15 case 0: temp=0;break; 16 case 1: 17 cout<<"请输入两个数组的大小M,N:"<<endl; 18 cin>>M>>N; 19 char *arr1=new char[M]; 20 char *arr2=new char[N]; 21 cout<<"请输入第一个集合arr1:"<<endl; 22 for(p=0;p<M;p++) 23 { 24 cin>>arr1[p]; 25 } 26 cout<<"请输入第二个集合arr2:"<<endl; 27 for(p=0;p<N;p++) 28 { 29 cin>>arr2[p]; 30 } 31 int** arr=new int*[M];//开辟一个动态二维数组,高M,宽N 32 for(int K=0;K<M;K++) 33 { 34 arr[K]=new int[N]; 35 } 36 for(q=0;q<M;q++)//数组初始化全为0 37 { 38 for( p=0;p<N;p++) 39 { 40 arr[q][p]=0; 41 } 42 } 43 cout<<"请输入二元关系以0结束"<<endl; //输入关系后,以输入0代表结束 44 while(1) 45 { 46 cin>>i; 47 if(i=='0') 48 break; 49 else 50 { 51 cin>>j; 52 if(i>='a'&&i<='z'&&(j<'a'||j>'z')) 53 { 54 i=i-'0';//0的ascii码为48,a的ascii码是65,z的ascii码90 55 arr[(i-'0')-1][(j-'0')-1]=1; 56 } 57 if(j>='a'&&j<='z'&&(i<'a'||i>'z')) 58 { 59 j=j-'0'; 60 arr[(i-'0')-1][(j-'0')-1]=1; 61 } 62 else if(i>='a'&&i<='z'&&j>='a'&&j<='z') 63 { 64 p=i-'0'-'0'; 65 q=j-'0'-'0'; 66 arr[p-1][q-1]=1; 67 } 68 else 69 arr[(i-'0')-1][(j-'0')-1]=1; 70 } 71 } 72 cout<<"该二元关系的矩阵为:"<<endl; 73 for(q=0;q<M;q++) 74 { 75 for(p=0;p<N;p++) 76 { 77 cout<<arr[q][p]<<" "; 78 } 79 cout<<endl; 80 }break; 81 } 82 } 83 }
#include<iostream> using namespace std; void output(int i,int j,int **a); void choose(int **arr); void main() { int M,N; //int M=0,N=0; //int *array1=new int[M]; //int *array2=new int[N]; cout<<"Please input sizes of array1 and array2: "<<endl; cin>>M>>N; int** arr=new int*[M];//开辟一个动态二维数组,高M,宽N for(int k=0;k<N;k++) arr[M]=new int[N]; for(int i=0;i<M;i++) { for(int j=0;j<N;j++) arr[i][j]=0; } choose(arr); output(M,N,arr); } void output(int M,int N,int **arr) { for(int i=0;i<M;i++) for(int j=0;j<N;j++) cout<<arr[i][j]<<"-"; } void choose(int **arr) { int i,j; cout<<"Please input the two relations:"<<endl; cin>>i>>j; arr[i][j]=0; }
#include<iostream> using namespace std; void output(int i,int j,int **a); void choose(int **arr); void main() { int M,N,temp1,temp2; //int M=0,N=0; //int *array1=new int[M]; //int *array2=new int[N]; end:cout<<"Please enter 1(continue) or 2(break) to choose:"<<endl; cin>>temp1; switch(temp1) { case 1: { cout<<"Please input sizes of height and width: "<<endl; cin>>M>>N; int** arr=new int*[M];//??????????,?M,?N for(int k=0;k<M;k++) arr[k]=new int[N]; for(int i=0;i<M;i++) { for(int j=0;j<N;j++) arr[i][j]=0; } choose(arr); output(M,N,arr); cout<<"Please input number 1 to again or 2 to exit:"<<endl; cin>>temp2; switch(temp2) { case 1: goto end; case 2: exit(1); } system("pause"); } case 2: break; } } void choose(int **arr) { int i,j; cout<<"Please input the two relations:"<<endl; cin>>i>>j; i--; j--; arr[i][j]=1; } void output(int M,int N,int **arr) { for(int i=0;i<M;i++) { for(int j=0;j<N;j++) { cout<<arr[i][j]<<"-"; } cout<<endl; } }