Ошибки в программе
03.05.2014, 12:43. Показов 443. Ответов 3
С++ только начал изучать... Цель-переписать с паскаля на С++. это сделал но не удовлетворен работой... не могу разобраться( в док..файле код паскаля и С++ ..Помогите ПЛИЗ.
ПАСКАЛЬ abc
Pascal | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
| uses
crt;
type
int = 0..10;
vec = array [0..10] of real;
matr = array [0..10, 0..10] of real;
var
x, x_1, ax, ax_1, c: vec;
e, a, a_1, b, b_1, br, br_1: matr;
{преобразование строковой дроби в числовой вид (1/2 --> 0.5)}
function drob(var s: string): double;
var
a, b, c: integer;
begin
if s.Split('/').Length > 1 then begin
if integer.TryParse(s.Split('/')[0], c) then
a := c
else begin
writeln('Error!');
end;
if integer.TryParse(s.Split('/')[1], c) then
b := c
else begin
writeln('Error!');
end;
result := a / b;
end else
if integer.TryParse(s, c) then
result := c
else begin
writeln('Error!');
exit;
end;
end;
procedure readvec(var x: vec; name: char; n: int);
var
i: int;
s: string;
begin
writeln(' Ввести вектор ', name, ' размера n=', n); for i := 0 to n - 1 do
begin
write(' ', name, '[', i:2, ']=');
readln(s);
writeln('Получившаяся дробь: ', drob(s));
{вызов функции преобразования}
x[i] := drob(s);
end;
end;
procedure writevec(var x: vec; name: char; n: int);
var
i: int;
begin
writeln(' Вывести вектор ', name, ' размера n=', n); for i := 0 to n - 1 do
begin
write(' ', name, '[', i:2, ']=');
writeln(x[i]:10:5)
end;
end;
procedure readmatr(var y: matr; name: char; n, m: int);
var
i, j: int;
s: string;
begin
writeln(' Ввести матрицу ', name,
' размера : n=', n, ', * m=', m); for i := 0 to n - 1 do
for j := 0 to m - 1 do
begin
write(' ', name, '[', i:2, ' ,', j:2, ']= ');
readln(s);
writeln('Получившаяся дробь: ', drob(s));
{вызов функции преобразования}
y[i, j] := drob(s);
end;
end;
procedure writematr(var y: matr; name: char; n, m: int);
var
i, j: int;
begin
writeln(' Вывести матрицу ', name,
' размера: n=', n, ', * m=', m);
for i := 0 to n - 1 do
for j := 0 to m - 1 do
begin
write(' ', name, '[', i:2, ' ,', j:2, ']= ');
writeln(y[i, j]:8:4)
end;
end;
procedure Em(var E: matr; n: int);
var
i, j: int;
begin
for i := 0 to n do
for j := 0 to n do
if i = j then E[i, j] := 1 else E[i, j] := 0;
end;
procedure Rmatr(var a, b, c: matr; n, m: int);
var
i, j: int;
begin
for i := 0 to n do
for j := 0 to m do
c[i, j] := a[i, j] - b[i, j];
end;
procedure obrmatr(var AIS, AP: matr; n: int);
var
i, j, k: integer;
a: matr;
begin
a := AIS;
n := n - 1;
for k := 0 to n do
begin
for j := 0 to n do
if (j <> k) then AP[k, j] := -a[k, j] / a[k, k];
for i := 0 to n do
if (i <> k) then AP[i, k] := a[i, k] / a[k, k];
for i := 0 to n do
for j := 0 to n do
if (i <> k) and (j <> k)
then AP[i, j] := a[i, j] - a[i, k] * a[k, j] / a[k, k];
AP[k, k] := 1 / a[k, k]; a := AP;
end;
end;
procedure matrvec(var b: matr; var c, x: vec; n: int);
var
i, j: int;
begin
for j := 0 to n - 1 do
begin
x[j] := 0;
for i := 0 to n - 1 do
x[j] := x[j] + b[i, j] * c[i]
end;
end;
begin
clrscr;
readvec(c, 'C', 2);
readmatr(a, 'A', 2, 2);
readmatr(a_1, 'Z', 2, 2);
Em(e, 2);
rmatr(e, a, br, 2, 2); rmatr(e, a_1, br_1, 2, 2);
obrmatr(br, b, 2); obrmatr(br_1, b_1, 2);
matrvec(b, c, x, 2); matrvec(b_1, c, x_1, 2); matrvec(a, x, ax, 2); matrvec(a_1, x_1, ax_1, 2);
if (ax[0] <= c[0]) and (ax[1] <= c[1]) then
writeln('Экономика матрица A - эффективна') else
writeln('Экономика матрица A - не эффективна');
if (ax_1[0] <= c[0]) and (ax_1[1] <= c[1]) then
writeln('Экономика матрица A_1 - эффективна') else
writeln('Экономика матрица A_1 - не эффективна');
readln();
writematr(b, 'B', 2, 2);
writematr(b_1, 'Z', 2, 2);
writevec(x, 'x', 2);
writevec(x_1, 'y', 2);
end. |
|
С++
C++ | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
| #include <iostream>;
// VEC
double x[10], x_1[10], ax[10], ax_1[10], c[10];
// MATR
double e[10][10], a[10][10], a_1[10][10], b[10][10], b_1[10][10], br[10][10], br_1[10][10];
double drob(int a, int b) {
return static_cast<double>(a)/b;
}
void readvec(char *name, int n) {
int a,b;
std::cout << "Vvesti vektor " << name << " razmera " << n << "\n";
for (int i=0; i < n;i++) {
std::cout << " " << name << "[" << i << "]=";
std::cin >> a >> b;
std::cout << "Poluchivsshazsa drob: " << drob(a,b) << "\n";
x[i] = drob(a,b);
}
std::cout << "\n";
}
void readmatr(char *name, int n, int m, char *type) {
int c,b;
std::cout << "Vvesti matricu " << name << " razmera " << n << "x" << m <<"\n";
for (int i=0; i < n;i++) {
for (int j=0; j < m;j++) {
std::cout << " " << name << "[" << i << ", " << j << "]=";
std::cin >> c >> b;
std::cout << "Poluchivsshazsa drob: " << drob(c,b) << "\n";
if (strcmp(type,"A") == 0)
a[i][j] = drob(c,b);
if (strcmp(type,"B") == 0)
a_1[i][j] = drob(c,b);
}
}
std::cout << "\n";
}
void Em(int n) {
for (int i=0; i==n;i++) {
for (int j=0; j==n;j++) {
if (i == j)
e[i][j] = 1;
else
e[i][j] = 0;
}
}
}
void Rmatr(double a[10][10], double b[10][10], int n, int m, char *type) {
for (int i=0; i==n;i++) {
for (int j=0; j==m;j++) {
if (strcmp(type,"A") == 0)
br[i][j] = a[i][j] - b[i][j];
if (strcmp(type,"B") == 0)
br_1[i][j] = a[i][j] - b[i][j];
}
}
}
void obrmatr(double AIS[10][10], double AP[10][10], int n, char *type) {
double a[10][10];
for (int i=0; i<10;i++)
for (int j=0; j<10;j++)
a[i][j] = AIS[i][j];
for (int k=0; k<n;k++){
for (int j=0;j<n;j++)
if (j != k){
if (strcmp(type,"A") == 0)
b[k][j] = -a[k][j] / a[k][k];
if (strcmp(type,"B") == 0)
b_1[k][j] = -a[k][j] / a[k][k];
}
for (int i=0;i<n;i++)
if (i != k){
if (strcmp(type,"A") == 0)
b[i][k] = a[i][k] / a[k][k];
if (strcmp(type,"B") == 0)
b_1[i][k] = a[i][k] / a[k][k];
}
for (int i=0;i<n;i++)
for (int j=0;j<n;j++)
if (i != k && j != k){
if (strcmp(type,"A") == 0)
b[i][j] = a[i][j] - a[i][k] * a[k][j] / a[k][k];
if (strcmp(type,"B") == 0)
b_1[i][j] = a[i][j] - a[i][k] * a[k][j] / a[k][k];
}
if (strcmp(type,"A") == 0)
b[k][k] = 1 / a[k][k];
if (strcmp(type,"B") == 0)
b_1[k][k] = 1 / a[k][k];
for (int i=0; i<10;i++)
for (int j=0; j<10;j++)
a[i][j] = AP[i][j];
}
}
void matrvec(double b[10][10], double c[10], int n, char *type) {
for (int j=0;j<n;j++) {
if (strcmp(type,"A") == 0)
x[j] = 0;
if (strcmp(type,"B") == 0)
x_1[j] = 0;
if (strcmp(type,"C") == 0)
ax[j] = 0;
if (strcmp(type,"D") == 0)
ax_1[j] = 0;
for (int i=0;i<n;i++){
if (strcmp(type,"A") == 0)
x[j] = x[j] + b[i][j] * c[i];
if (strcmp(type,"B") == 0)
x_1[j] = x_1[j] + b[i][j] * c[i];
if (strcmp(type,"C") == 0)
ax[j] = ax[j] + b[i][j] * c[i];
if (strcmp(type,"D") == 0)
ax_1[j] = ax_1[j] + b[i][j] * c[i];
}
}
}
void writematr(double y[10][10], char *name, int n, int m) {
std::cout << "Vivesti matricu " << name << " razmera " << n << "x" << m << "\n";
for (int i=0;i<n; i++)
for (int j=0;j<m;j++) {
std::cout << " " << name << "[" << i << ", " << j << "]=" << y[i][j] << "\n";
}
}
void writevec(double x[10], char *name, int n) {
std::cout << "Vivesti vektor " << name << " razmera " << n << "\n";
for (int i=0;i<n; i++) {
std::cout << " " << name << "[" << i << "]=" << x[i] << "\n";
}
}
int main() {
readvec("C", 2);
readmatr("A", 2, 2, "A");
readmatr("Z", 2, 2, "B");
Em(2);
Rmatr(e, a, 2, 2, "A");
Rmatr(e, a_1, 2, 2, "B");
obrmatr(br, b, 2, "A");
obrmatr(br_1, b_1, 2, "B");
matrvec(b, c, 2, "A");
matrvec(b_1, c, 2, "B");
matrvec(a, x, 2, "C");
matrvec(a_1, x_1, 2, "D");
if (ax[0] <= c[0] && ax[1] <= c[1])
std::cout << "Aconomika matrica A - affektivna \n";
else
std::cout << "Aconomika matrica A - ne affektivna \n";
if (ax_1[0] <= c[0] && ax_1[1] <= c[1])
std::cout << "Aconomika matrica A_1 - affektivna \n";
else
std::cout << "Aconomika matrica A_1 - ne affektivna \n";
std::cout << "\n";
writematr(b, "B", 2, 2);
writematr(b_1, "Z", 2, 2);
writevec(x, "x", 2);
writevec(x_1, "y", 2);
system("pause");
return 0;
} |
|
Вложение 394481
0
|