Изменение кода с С++ на Visual Basic - VB
Формулировка задачи:
Листинг программы
- #include "stdafx.h"
- #include <iostream.h>
- #include <fstream>
- #include <math.h>
- using namespace std;
- int main()
- {
- int n, i, j, k;
- int max;
- ifstream fin("in.txt");
- ofstream fout;
- fout.open("out.txt");
- fin>>n;
- double **matrix = new double*[n];
- double *x=new double[n];
- for(i=0; i<n; i++)
- {
- matrix[i] = new double[n+1];
- }
- double *r;
- for(i=0; i<n; i++){
- for(j=0; j<=n; j++){
- fin>>matrix[i][j];
- }
- }
- for(i=0; i<n; i++)
- {
- //Початок
- max=i;
- for(j=i+1;j<n;j++)
- {
- if(fabs(matrix[j][i])>fabs(matrix[max][i]))
- {
- max=j;
- }
- }
- if(max!=i)
- {
- r=matrix[i];
- matrix[i]=matrix[max];
- matrix[max]=r;
- }
- if(matrix[i][i]==0)
- {
- cout<<"Sustema nevuznachena!!!"<<endl<<endl;
- break;
- }
- //Кiнець
- for(j=n;j>=i;j--)
- {
- matrix[i][j]/=matrix[i][i];
- }
- for(j=i+1; j<n; j++)
- {
- for(k=n; k>=i; k--)
- {
- matrix[j][k]-=matrix[j][i]*matrix[i][k];
- }
- }
- }
- /*Вывод треугольной матрицы
- for(i=0;i<n;i++){
- for(j=0;j<=n;j++){
- fout<<matrix[i][j]<<" ";
- }
- fout<<endl;
- }
- fout<<endl; */
- for(i=n-1;i>=0;i--)
- {
- x[i]=matrix[i][n];
- for(j=n-1;j>i;j--)
- {
- x[i]-=x[j]*matrix[i][j];
- }
- }
- for(i=0;i<n;i++)
- {
- fout<<"x"<<i+1<<"="<<x[i]<<endl;
- }
- for(i=0;i<n;i++){
- delete matrix[i];
- cout<<"x"<<i+1<<"="<<x[i]<<endl;
- }
- delete matrix;
- system("PAUSE");
- return 0;
- }
Решение задачи: «Изменение кода с С++ на Visual Basic»
textual
Листинг программы
- Option Explicit
- Option Base 0
- Function main() As Long
- Dim n As Long
- Dim i As Long
- Dim j As Long
- Dim k As Long
- Dim max As Long
- Open "in.txt" For Input As #1
- Open "out.txt" For Output As #2
- Input #1, n
- Dim matrix() As Double
- ReDim matrix(n - 1, n - 1)
- Dim x() As Double
- ReDim x(n - 1)
- Dim r As Double
- For i = 0 To n - 1
- For j = 0 To n - 1
- Input #1, matrix(i, j)
- Next
- Next
- For i = 0 To n - 1
- 'Початок
- max = i
- For j = i + 1 To n - 1
- If Abs(matrix(j, i)) > Abs(matrix(max, i)) Then max = j
- Next
- If max <> i Then
- r = matrix(i)
- matrix(i) = matrix(max)
- matrix(max) = r
- End If
- If matrix(i, i) = 0 Then
- Write #2, "Sustema nevuznachena!!!"
- Close #1
- Close #2
- Exit Function
- End If
- 'Кiнець
- For j = n To i Step -1
- matrix(i, j) = matrix(i, j) / matrix(i, i)
- Next
- For j = i + 1 To n - 1
- For k = n To i Step -1
- matrix(j, k) = matrix(j, k) - matrix(j, i) * matrix(i, k)
- Next
- Next
- Next
- '*Вывод треугольной матрицы
- 'For i = 0 To n - 1
- ' For j = 0 To n - 1
- ' Write #2, matrix(i, j)
- ' Next
- ' Write #2, vbLf
- 'Next
- 'Write #2, vbLf
- For i = n - 1 To 0 Step -1
- x(i) = matrix(i, n)
- For j = n - 1 To i + 1 Step -1
- x(i) = x(i) - x(j) * matrix(i, j)
- Next
- Next
- For i = 0 To n - 1
- Write #2, "x" & CStr(i + 1) & "="; CStr(x(i)) & vbLf
- Next
- Close #1
- Close #2
- main = 0
- End Function
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д