1: #include <stdio.h>
2: #include <iostream>
3: #include <string>
4: #ifndef DEBUG
5: #define DEBUG 1
6: #endif
7: FILE *fr;
8: using namespace std;
9: int gcd (int m, int n)
10: {
11: int ret;
12: if (m==0 || n==0 ) return 0;
13: if (m % n == 0)
14: ret = n;
15: else
16: ret = gcd (n,m%n);
17: return ret;
18: }
19: //recursive GCD
20: class frac{
21: public:
22: frac(string str){
23: a = atoi(str.c_str()); // string 입력(원소단위) delimiter '/'
24: int i = str.find('/');
25: if( i <= 0 ){
26: b=1;
27: }
28: else if( i > 0){
29: a= atoi(str.c_str());
30: if( strlen(str.c_str()+i+1) > 0){
31: b = atoi(str.c_str()+i+1);
32: }
33: else b = 1;
34: reduce();
35: }
36: }
37: frac(int _a,int _b){
38: int i = 0 ;
39: if( _a==0 || _b == 0 ) a=b=0;
40: else{
41: while(1 != abs(( i=gcd(_a,_b))) ) {
42: _a /= i;
43: _b /= i;
44: }
45: a = _a;
46: b = _b;
47: reduce();
48: }
49: }
50: frac(){
51: a = 0;
52: b = 0;
53: }
54: frac operator + (frac _in);
55: frac operator - (frac _in);
56: frac operator * (frac _in);
57: frac operator / (frac _in);
58: void reduce(){
59: if( a == 0 || b == 0) a = b = 0;
60: else if(a == b){
61: if( a > 0 ){
62: a = 1;
63: b = 1;
64: }
65: else {
66: a = -1;
67: b = 1;
68: }
69: }
70: else if( b < 0) {
71: b *= -1;
72: a *= -1;
73: }
74: };
75: void print(){
76: reduce();
77: if( a == b){
78: if( a==0) cout<<0;
79: else if( a<0 || b<0) cout<<-1;
80: else cout<<1;
81: }
82: else {
83: cout<<a;
84: if( b != 1 ) cout<<"/"<<b;
85: }
86: }
87: int get_b(){return b;}
88: int get_a(){return a;}
89: private:
90: int a;
91: int b;
92: };
93: //end of frac class
94: frac frac::operator +(frac _in){
95: int ta = (a==0)? _in.a : ( _in.a==0)? a : a*_in.b + _in.a*b;
96: int tb = (b==0)? _in.b : ( _in.b==0)? b : b*_in.b;
97: return frac(ta,tb);
98: }
99: frac frac::operator -(frac _in){
100: int ta = (a==0)? _in.a : ( _in.a==0)? a : a*_in.b - _in.a*b;
101: int tb = (b==0)? _in.b : ( _in.b==0)? b : b*_in.b;
102: return frac(ta,tb);
103: }
104: frac frac::operator *(frac _in){
105: int ta = a*_in.a;
106: int tb = b*_in.b;
107: return frac(ta,tb);
108: }
109: frac frac::operator /(frac _in){
110: int ta = a*_in.b;
111: int tb = b*_in.a;
112: return frac(ta,tb);
113: }
114: //end of operator..
115: void help(){
116: cout<<"***********************************************************"<<endl;
117: cout<<"********** Reduced Echelon Form with m*n Matrix ***********"<<endl;
118: cout<<"***********************************************************"<<endl<<endl;
119: cout<<"* input.txt 파일 형식은 m n a11 a12 a13 a14 a21 a22 .....**"<<endl;
120: cout<<"* ex) 3x4 행렬 [ 1 1/2 1/3 1/4 ...]일 때 input.txt파일 ***"<<endl;
121: cout<<"3 4"<<endl;
122: cout<<"1 1/2 1/3 1/4"<<endl;
123: cout<<"1 1/2 1/3 1/4"<<endl;
124: cout<<"1 1/2 1/3 1/4"<<endl<<endl<<endl;
125: }
126: //usage
127: void Debug_print(frac mat[32][32], int i, int c, int k, int m, int n){
128: cout<<"cursor"<<i<<"행"<<c<<"열 :: "<<k<<"행연산"<<endl;
129: int j = 0;
130: for( i = 0 ; i < m ; i++){
131: for( j = 0 ; j < n ; j++){
132: mat[i][j].print();
133: cout<<'\t';
134: }
135: cout<<endl;
136: }
137: }
138: //pragma
139: int ref (void){
140: frac mat[32][32];
141: char ch[16];
142: int i=0,j=0,m=0,n=0,k=0,c=0;
143: help();
144: if( 0 > fopen_s(&fr,"input.txt","r")) exit(-1);
145: ch[0]=0;
146: fscanf(fr,"%d %d",&m,&n);
147: cout<<"("<<m<<" x "<<n<<") matrix"<<endl;
148: cout<<"elements :"<<endl;
149: for(i=0;i<m;i++){
150: for(j=0;j<n;j++){
151: fscanf(fr,"%s",ch);
152: mat[i][j] = frac(ch);
153: mat[i][j].print();
154: cout<<'\t';
155: }
156: cout<<endl;
157: }
158: cout<<"---------------------------------------------------------------------------"<<endl;
159: i=j=0;
160: for(i=0;i<m;i++){ // cursor 행
161: int tb=0;
162: int ta=0;
163: c=i; // cursor 열
164: while(mat[i][c].get_a() == 0 && mat[i][c].get_b() == 0 ) {
165: c++;
166: }
167: tb = mat[i][c].get_b();
168: ta = mat[i][c].get_a();
169: for(j=c;j<n;j++){
170: mat[i][j] = mat[i][j] * frac(tb,ta); // i 행 A[i] = A[i] * A[i][c]
171: }
172: for(k=0;k<m;k++){ // k행 연산
173: ta = mat[k][c].get_a();
174: tb = mat[k][c].get_b();
175: if( ta != 0 && tb != 0 && k != i){ // 행렬 원소가 0이 아님 && cursor행이 아님
176: for(j=c;j<n;j++){
177: mat[k][j] = mat[k][j] + ( mat[i][j] * frac(-1*ta,tb) ); // A[k] -= A[i] * (A[i][c]/A[k][c])
178: }
179: }
180: if(DEBUG)
181: Debug_print(mat, i,c,k,m,n);
182: }
183: }
184: cout<<"결과"<<endl;
185: for( i = 0 ; i < m ; i++){
186: for( j = 0 ; j < n ; j++){
187: mat[i][j].print();
188: cout<<'\t';
189: }
190: cout<<endl;
191: }
192: return 0;
193: }