L
L
Lyudmila Nesmachnaya2018-12-10 21:26:59
C++ / C#
Lyudmila Nesmachnaya, 2018-12-10 21:26:59

How to access structure elements in c#?

I am translating a program from C++ to C#. There was an appeal to the elements of the complex structure, as to an array. C# gives error CS0021 Cannot apply indexing via [] to expression of type 'float'.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace spectr_Furie
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void выходToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void открытьToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // открытие изображения
            Bitmap image;
            OpenFileDialog open_dialog = new OpenFileDialog();
            open_dialog.Filter = "Image Files(*.BMP;*.JPG;*.GIF;*.PNG)|*.BMP;*.JPG;*.GIF;*.PNG|All files (*.*)|*.*"; //формат загружаемого файла
            if (open_dialog.ShowDialog() == DialogResult.OK) //если в окне была нажата кнопка "ОК"
            {
                try
                {
                    image = new Bitmap(open_dialog.FileName);
                    pictureBox1.Size = image.Size;
                    pictureBox1.Image = image;
                    pictureBox1.Invalidate();
                }
                catch
                {
                    DialogResult rezult = MessageBox.Show("Невозможно открыть выбранный файл",
                    "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void выполнитьАнализToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int n = pictureBox1.Height; // n строк изображения, m столбцов
            int m = n;
            double N_1 = Math.Pow(n,-1); // множитель перед суммой
            double pi_2_N = 2 * Math.PI/n;
            int j, k, u, v;
            float[] furie = new float[2];
            float sum_real, sum_imaging; // суммы действительной и мнимой частей
            float arg; // аргумент sin, cos
            Bitmap image = new Bitmap(pictureBox1.Image);

            for (u = 0; u < 20; u++)
                for (v = 0; v < 20; v++)
                {
                    sum_real = sum_imaging = 0;
                    for (j = 0; j < n; j++)
                        for (k = 0; k < n; k++)
                        {
                            if (image.GetPixel(j,k) != Color.White)
                            {
                                arg = (float)pi_2_N * (u * j + v * k);
                                sum_real += (float)Math.Cos(arg);
                                sum_imaging += (-1) * (float)Math.Sin(arg);
                            }
                        }
                    furie[u][v].Re = N_1 * sum_real;
                    furie[u][v].Im = N_1 * sum_imaging;
                }
        }

        struct complex //комплексное число
        {
            public float Re;
            public float Im;
        }
    }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
#
#, 2018-12-10
@nesmachnaja

int n = pictureBox1.Height; // n строк изображения, m столбцов
int m = n;
oh well... ?!!!!!!!!!!... why not
int n = pictureBox1.Height; // n строк изображения, m столбцов
int m = pictureBox1.Width;
how do you know that there is an exact square? .. but these are trifles compared to the rest
struct complex //комплексное число
        {
            public float Re;
            public float Im;
        }
and
furie[u][v].Re = ...
furie[u][v].Im = ...
well then write exactly complex .. !!! .. next.. what dimension?...
furie[u][v].Re = ...
furie[u][v].Im = ...

wherein
for (u = 0; u < 20; u++)
    for (v = 0; v < 20; v++)

where are these 20 points from? you iterate over indices from 0 to 19 along two dimensions.. result: ... but this does not guarantee anything in the sense of some kind of correct operation. God knows how many more not explicit jambs you have .. is the code that you translate correct? .. but the compiler will miss the declared errors .. ))

D
Dasha Tsiklauri, 2018-12-10
@dasha_programmist

well, you first wrote 2 elements in the array float[] furie = new float[2];
and then you turn in the for loop (u = 0; u < 20; u++) furie[u] - going beyond the array,
but in general some kind of stupid code furie[u][v].Re - why take?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question