SHOW:
|
|
- or go back to the newest paste.
1 | #include <stdio.h> | |
2 | - | #include <time.h> |
2 | + | #include <iostream> |
3 | - | #include <stdlib.h> |
3 | + | #include<locale.h> |
4 | - | #include <math.h> |
4 | + | #include<conio.h> |
5 | - | #define M 5 |
5 | + | using namespace std; |
6 | - | #define N 7 |
6 | + | |
7 | - | #define A -50 |
7 | + | struct EquationSystem //structura start |
8 | - | #define B 50 |
8 | + | |
9 | int id; | |
10 | - | void Init(int a[][N],int m , int n) |
10 | + | double Matrix[6][6]; |
11 | double Vector[6]; | |
12 | int EquationCount; | |
13 | - | for(i = 0 ; i < m ; i++) |
13 | + | }; |
14 | - | for(j = 0 ; j < n ; j++) |
14 | + | struct EquationSystemRoot //structura finish |
15 | - | a[i][j]=A+rand()%(B-A+1); |
15 | + | { |
16 | int id; | |
17 | - | void Print(int a[][N],int m,int n) |
17 | + | double Root[6]; |
18 | int EquationCount; | |
19 | }; | |
20 | - | for(i = 0 ; i < m; i++) |
20 | + | |
21 | int Init(char *fname)//Vvod dannix | |
22 | - | for(j = 0 ; j < n ; j++) |
22 | + | |
23 | - | printf("%4d",a[i][j]); |
23 | + | |
24 | - | printf("\n"); |
24 | + | double sum=0; |
25 | char c; | |
26 | - | printf("\n"); |
26 | + | EquationSystem d; |
27 | FILE *f; | |
28 | - | int Prostoe(int a) |
28 | + | if((f = fopen(fname,"ab"))==NULL) |
29 | { | |
30 | - | a=abs(a); |
30 | + | printf("Eror open File\n"); |
31 | return -1; | |
32 | - | if (a==2) |
32 | + | |
33 | - | return 1; |
33 | + | |
34 | - | if (a==0 || a==1 || a%2==0) |
34 | + | do |
35 | - | return 0; |
35 | + | |
36 | - | int i=3; |
36 | + | printf("Nomer Zapisy:"); |
37 | - | while(i <= a/2 && a%i != 0) |
37 | + | scanf("%d",&d.id); |
38 | - | i+=2; |
38 | + | printf("Vvedy Kolvo Uravnen\nNo Ne Bolshe 6\n"); |
39 | - | return i>a/2; |
39 | + | scanf("%d",&d.EquationCount); |
40 | while(d.EquationCount > 6) | |
41 | - | int CountStr(int a[][N],int m,int n) |
41 | + | |
42 | printf("Vvedy Kolvo Uravnen\nNo Ne Bolshe 6\n"); | |
43 | - | int i,j,count=0,flag=0,max=0,buf=0; |
43 | + | scanf("%d",&d.EquationCount); |
44 | - | for( i = 0 ; i < m; i++) |
44 | + | |
45 | for(i=0;i<d.EquationCount;i++) | |
46 | - | for(j = 0 ; j < n ; j++) |
46 | + | |
47 | - | if(Prostoe(a[i][j])==1) |
47 | + | for(j=0;j<d.EquationCount;j++) |
48 | { | |
49 | - | printf("==%4d",a[i][j]); |
49 | + | printf("Vvedy Koef"); |
50 | - | count++; |
50 | + | cin>>d.Matrix[i][j]; |
51 | - | flag=1; |
51 | + | while(d.Matrix[0][0]==0) |
52 | { | |
53 | - | if(count>=max) |
53 | + | printf("Matix[0][0] Ne Mozhet = 0\nVvedy Zanogo\n"); |
54 | cin>>d.Matrix[0][0]; | |
55 | - | max=count; |
55 | + | } |
56 | - | buf=i; |
56 | + | |
57 | printf("Vectora"); | |
58 | - | count=0; |
58 | + | cin>> d.Vector[i]; |
59 | } | |
60 | - | if (flag==0) |
60 | + | fwrite(&d,sizeof(d),1,f); |
61 | ||
62 | - | if (flag) |
62 | + | printf("Äîáàâèòü åùå çàïèñü? (+,-)\n"); |
63 | - | return buf; |
63 | + | c=getchar(c); |
64 | } | |
65 | while (c!='-'); | |
66 | ||
67 | - | int a[M][N],b[M],buf; |
67 | + | fclose(f); |
68 | - | srand(time(NULL));; |
68 | + | |
69 | - | Init(a,M,N); |
69 | + | int Read(char *fname) |
70 | - | Print(a,M,N); |
70 | + | |
71 | - | buf=CountStr(a,M,N); |
71 | + | |
72 | - | if(buf==-1) |
72 | + | EquationSystem d; |
73 | - | printf("Net Stroki s prostimi 4islami"); |
73 | + | FILE *f; |
74 | - | else |
74 | + | if((f = fopen(fname,"rb"))==NULL) |
75 | - | printf("\nNomer Stroki=%d\n",buf); |
75 | + | |
76 | printf("Eror open File:Net Takogo FILE\n"); | |
77 | return -1; | |
78 | } | |
79 | while(fread(&d,sizeof(d),1,f)) | |
80 | { | |
81 | printf("\nID==%d",d.id); | |
82 | printf("\nEquationCount==%d\n",d.EquationCount); | |
83 | printf("\nMatrica= \n"); | |
84 | for(i = 0 ; i < d.EquationCount ; i++) | |
85 | { | |
86 | printf("("); | |
87 | for(j = 0 ; j < d.EquationCount ; j++) | |
88 | printf(" %7.2f",d.Matrix[i][j]); | |
89 | printf(" |%7.2f)\n",d.Vector[i]); | |
90 | } | |
91 | } | |
92 | } | |
93 | double Sum ( double *a, int n) | |
94 | { | |
95 | int i; | |
96 | double sum=0; | |
97 | for( i = 0 ; i < n ; i++) | |
98 | sum+=a[i]; | |
99 | return sum; | |
100 | } | |
101 | long Size(char *fname) | |
102 | { | |
103 | FILE *f; | |
104 | long n; | |
105 | if((f = fopen(fname,"rb"))==NULL) | |
106 | return -1; | |
107 | fseek(f,0,SEEK_END); | |
108 | n = ftell(f); | |
109 | return n; | |
110 | } | |
111 | int Obrabotka(char *fname, char *fname2) | |
112 | { | |
113 | int sum,k,i,j,l,b,m; | |
114 | double buf,koef; | |
115 | EquationSystem d; | |
116 | EquationSystemRoot d2; | |
117 | FILE *f,*f2; | |
118 | if((f = fopen(fname,"rb"))==NULL) | |
119 | return -1; | |
120 | if((f2 =fopen(fname2,"ab"))==NULL) | |
121 | { | |
122 | fclose(f); | |
123 | return -2; | |
124 | } | |
125 | fclose(f2); | |
126 | while( fread(&d,sizeof(d),1,f)) | |
127 | { | |
128 | for(m = 0 ; m < d.EquationCount - 1; m++)//Stup Vid1 | |
129 | for(i = 1 + m ; i < d.EquationCount ; i++) | |
130 | { | |
131 | if( d.Matrix[m][m] == 0) | |
132 | continue; | |
133 | koef = d.Matrix[i][m] / d.Matrix[m][m] * (-1); | |
134 | for(j = m ; j < d.EquationCount ; j++) | |
135 | d.Matrix[i][j] = koef * d.Matrix[m][j] +d.Matrix[i][j]; | |
136 | d.Vector[i]=d.Vector[m]*koef+d.Vector[i]; | |
137 | ||
138 | } | |
139 | for(i=0 ,k=d.EquationCount-1; i<d.EquationCount && i<=k;i++,k--)//perestanovka1 | |
140 | { | |
141 | buf=d.Vector[i]; | |
142 | d.Vector[i]=d.Vector[k]; | |
143 | d.Vector[k]=buf; | |
144 | if (i<k) | |
145 | for(j=0,l=d.EquationCount-1;j<d.EquationCount ;j++,l--) | |
146 | { | |
147 | buf= d.Matrix[i][j]; | |
148 | d.Matrix[i][j]=d.Matrix[k][l]; | |
149 | d.Matrix[k][l]=buf; | |
150 | } | |
151 | else | |
152 | for(j=0,l=d.EquationCount-1;j<=l;j++,l--) | |
153 | { | |
154 | buf= d.Matrix[i][j]; | |
155 | d.Matrix[i][j]=d.Matrix[k][l]; | |
156 | d.Matrix[k][l]=buf; | |
157 | } | |
158 | } | |
159 | for(m = 0 ; m < d.EquationCount - 1; m++)//Stup Vid2 | |
160 | for(i = 1 + m ; i < d.EquationCount ; i++) | |
161 | { | |
162 | if( d.Matrix[m][m] == 0) | |
163 | { | |
164 | printf("Continue!!!"); | |
165 | continue; | |
166 | } | |
167 | koef = d.Matrix[i][m] / d.Matrix[m][m] * (-1); | |
168 | for(j = m ; j < d.EquationCount ; j++) | |
169 | d.Matrix[i][j] = koef * d.Matrix[m][j] +d.Matrix[i][j]; | |
170 | d.Vector[i]=d.Vector[m]*koef+d.Vector[i]; | |
171 | } | |
172 | for(i=0 ,k=d.EquationCount-1; i<d.EquationCount && i<=k;i++,k--)//Perestanovka2 | |
173 | { | |
174 | buf=d.Vector[i]; | |
175 | d.Vector[i]=d.Vector[k]; | |
176 | d.Vector[k]=buf; | |
177 | if (i<k) | |
178 | for(j=0,l=d.EquationCount-1;j<d.EquationCount ;j++,l--) | |
179 | { | |
180 | buf= d.Matrix[i][j]; | |
181 | d.Matrix[i][j]=d.Matrix[k][l]; | |
182 | d.Matrix[k][l]=buf; | |
183 | } | |
184 | else | |
185 | for(j=0,l=d.EquationCount-1;j<=l;j++,l--) | |
186 | { | |
187 | buf= d.Matrix[i][j]; | |
188 | d.Matrix[i][j]=d.Matrix[k][l]; | |
189 | d.Matrix[k][l]=buf; | |
190 | } | |
191 | } | |
192 | ||
193 | // printf("\n\n\nPosle Smeni===\n"); //vivod | |
194 | // for(i=0;i<d.EquationCount;i++)//vivod | |
195 | // { | |
196 | // for(j=0;j<d.EquationCount;j++)//vivdo | |
197 | // printf("%5.2f",d.Matrix[i][j]);//vivod | |
198 | // printf("|%5.2f",d.Vector[i]); | |
199 | // printf("\n"); | |
200 | // } | |
201 | f2=fopen(fname2 , "ab"); | |
202 | for(i = d.EquationCount-1 ; i >= 0; i--) | |
203 | { | |
204 | if(Sum(d.Matrix[i],d.EquationCount) == 0 ) | |
205 | { | |
206 | d2.Root[i]=1; | |
207 | continue; | |
208 | } | |
209 | else | |
210 | d2.Root[i]=(d.Vector[i]-(Sum(d.Matrix[i],d.EquationCount)-d.Matrix[i][i]))/d.Matrix[i][i]; | |
211 | } | |
212 | d2.id=d.id; | |
213 | d2.EquationCount=d.EquationCount; | |
214 | fwrite(&d2,sizeof(d2),1,f2); | |
215 | fclose(f2); | |
216 | } | |
217 | ||
218 | fclose(f); | |
219 | f2=fopen(fname2 , "r"); | |
220 | while(fread(&d2,sizeof(d2),1,f2)) | |
221 | { | |
222 | printf("\nID==%d",d2.id); | |
223 | printf("\nEquationCount==%d\n",d2.EquationCount); | |
224 | printf("Korni= "); | |
225 | for(i = 0 ; i < d2.EquationCount ; i++) | |
226 | { | |
227 | printf(" %5.2f",d2.Root[i]); | |
228 | } | |
229 | } | |
230 | fclose(f2); | |
231 | ||
232 | if((Size(fname)) == 0) | |
233 | puts("Ôàéë ïóñò!"); | |
234 | } | |
235 | int main() | |
236 | { | |
237 | setlocale(LC_ALL,"Rus"); | |
238 | EquationSystem d; | |
239 | EquationSystemRoot d2; | |
240 | int c; | |
241 | int sum,k,i,j,b,m,l; | |
242 | double buf,koef; | |
243 | char fname[50],fname2[50]; | |
244 | //char fname[]="Z:\\fail\\second.txt"; | |
245 | //char fname[]="D:\\univer\\C\\programki\\laba3.txt"; | |
246 | //char fname2[]="D:\\univer\\C\\programki\\laba3.1.txt"; | |
247 | FILE *f,*f2; | |
248 | //f=fopen(fname,"rb"); | |
249 | while(1) | |
250 | { | |
251 | puts("\t\t\t***ÌÅÍÞ***"); | |
252 | puts("1 - Äîáàâèòü çàïèñü â ôàéë èëè ñîçäàòü íîâûé,åñëè åãî íåò"); | |
253 | puts("2 - ×òåíèå âõîäíîãî ôàéëà"); | |
254 | puts("3 - Îáðàáîòêà"); | |
255 | puts("0 - Âûõîä"); | |
256 | fflush(stdin); | |
257 | c = getch(); | |
258 | printf("Podtvedite vibor\n"); | |
259 | //c=getch(); | |
260 | switch(c) | |
261 | { | |
262 | ||
263 | case '1': | |
264 | printf("Vvedite FIlE Name: ");scanf("%s",fname); | |
265 | Init(fname); | |
266 | break; | |
267 | case '2': | |
268 | printf("Ââåäèòå íàçâàíèå ôàéëà: ");scanf("%s",fname); | |
269 | Read(fname); | |
270 | break; | |
271 | case '3': | |
272 | printf("Ââåäèòå íàçâàíèå âõîäíîãî ôàéëà: ");scanf("%s",fname); | |
273 | printf("Ââåäèòå íàçâàíèå âûõîäíîãî ôàéëà: ");scanf("%s",fname2); | |
274 | Obrabotka(fname,fname2); | |
275 | break; | |
276 | case '0': | |
277 | return 0; | |
278 | } | |
279 | } | |
280 | ||
281 | ||
282 | ||
283 | ||
284 | ||
285 | ||
286 | printf("\nKonec\n"); | |
287 | return 0; | |
288 | } |