Перевести программу из C++ в C#
Формулировка задачи:
Помогите пожалуйста перевести программу из C++ в C#.
#include <iostream.h> #include <iomanip.h> #include <conio.h> #include <stdlib.h> class Massiv { int *a,*b,n,min; public: void Sozd(); void Vvod_Sl(); void Form(int n1, int n2); void Form_2(int n3, int n4); void Print(); void Print_2(int number); void Print_3(int rec, int field); }; void main() { Massiv a,b; int number,punkt,rec,field,kol,n1,n2,n,n3,n4; clrscr(); cout<<"1 - sozdanie massiva";cout<<endl; cout<<"2 - pechat vseh strok";cout<<endl; cout<<"3 - pechat stroki po indeksu";cout<<endl; cout<<"4 - sceplenie dvuh massivov";cout<<endl; cout<<"5 - sliyanie dvuh massivov";cout<<endl; cout<<"6 - pechat konkretnogo elementa konkretnoi stroki";cout<<endl; cout<<"7 - vihod";cout<<endl; cout<<"Vvedite punkt menu "; cin>> punkt; while (punkt!=7) { switch(punkt) { case 1: {randomize(); //sozdanie massiva a.Vvod_Sl(); cout<<endl; a.Print(); cout<<endl; cout<<"1 - sozdanie massiva";cout<<endl; cout<<"2 - pechat vseh strok";cout<<endl; cout<<"3 - pechat stroki po indeksu";cout<<endl; cout<<"4 - sceplenie dvuh massivov";cout<<endl; cout<<"5 - sliyanie dvuh massivov";cout<<endl; cout<<"6 - pechat konkretnogo elementa konkretnoi stroki";cout<<endl; cout<<"7 - vihod";cout<<endl; cout<<"Vvedite punkt menu "; cin>> punkt; } break; case 2: {a.Print(); //pechat massiva cout<<endl; cout<<"1 - sozdanie massiva";cout<<endl; cout<<"2 - pechat vseh strok";cout<<endl; cout<<"3 - pechat stroki po indeksu";cout<<endl; cout<<"4 - sceplenie dvuh massivov";cout<<endl; cout<<"5 - sliyanie dvuh massivov";cout<<endl; cout<<"6 - pechat konkretnogo elementa konkretnoi stroki";cout<<endl; cout<<"7 - vihod";cout<<endl; cout<<"Vvedite punkt menu "; cin>> punkt; } break; case 3: { //pechat stroki po indeksu cout<<"Vvedite nomer massiva (numeracia s 0!): ";cin>>number; cout<<endl; a.Print_2(number); cout<<"1 - sozdanie massiva";cout<<endl; cout<<"2 - pechat vseh strok";cout<<endl; cout<<"3 - pechat stroki po indeksu";cout<<endl; cout<<"4 - sceplenie dvuh massivov";cout<<endl; cout<<"5 - sliyanie dvuh massivov";cout<<endl; cout<<"6 - pechat konkretnogo elementa konkretnoi stroki";cout<<endl; cout<<"7 - vihod";cout<<endl; cout<<"Vvedite punkt menu "; cin>> punkt; } break; case 4: { //sceplenie dvuh massivov cout<<"Vvrdite nomer 1-go massiva(numeracia s 0!): ";cin>>n1;cout<<endl; cout<<"Vvedite nomer 2-go massiva(numeracia s 0!): ";cin>>n2;cout<<endl; a.Form(n1,n2); cout<<endl; cout<<"1 - sozdanie massiva";cout<<endl; cout<<"2 - pechat vseh strok";cout<<endl; cout<<"3 - pechat stroki po indeksu";cout<<endl; cout<<"4 - sceplenie dvuh massivov";cout<<endl; cout<<"5 - sliyanie dvuh massivov";cout<<endl; cout<<"6 - pechat konkretnogo elementa konkretnoi stroki";cout<<endl; cout<<"7 - vihod";cout<<endl; cout<<"Vvedite punkt menu "; cin>> punkt;} break; case 5: { //sliyanie dvuh massivov cout<<"Vvrdite nomer 1-go massiva(numeracia s 0!): ";cin>>n3;cout<<endl; cout<<"Vvedite nomer 2-go massiva(numeracia s 0!): ";cin>>n4;cout<<endl; a.Form_2(n3,n4); cout<<endl; cout<<"1 - sozdanie massiva";cout<<endl; cout<<"2 - pechat vseh strok";cout<<endl; cout<<"3 - pechat stroki po indeksu";cout<<endl; cout<<"4 - sceplenie dvuh massivov";cout<<endl; cout<<"5 - sliyanie dvuh massivov";cout<<endl; cout<<"6 - pechat konkretnogo elementa konkretnoi stroki";cout<<endl; cout<<"7 - vihod";cout<<endl; cout<<"Vvedite punkt menu "; cin>> punkt;} break; case 6: { //pechat konkretnogo elementa konkretnoi stroki cout<<"Vvedite nomer stroki massiva(numeraciya s 0!): ";cin>>rec; cout<<endl; cout<<"Vvedite nomer stolbca: ";cin>>field; cout<<endl; a.Print_3(rec,field); cout<<"1 - sozdanie massiva";cout<<endl; cout<<"2 - pechat vseh strok";cout<<endl; cout<<"3 - pechat stroki po indeksu";cout<<endl; cout<<"4 - sceplenie dvuh massivov";cout<<endl; cout<<"5 - sliyanie dvuh massivov";cout<<endl; cout<<"6 - pechat konkretnogo elementa konkretnoi stroki";cout<<endl; cout<<"7 - vihod";cout<<endl; cout<<"Vvedite punkt menu "; cin>> punkt;} break; case 7: break; } } } void Massiv :: Sozd() { int m; cout<<"kolvo strok?";cin>>n; int min=0; for (int i=0;i<n;i++) { cout<<"kolvo elementov v "<< i+1 <<" stroke?";cin>>m; *(b+i)=m; if (min<m) min=m; } a=(int*)malloc(sizeof(int)*n*min); } void Massiv::Vvod_Sl() { Sozd(); for(int i=0;i<n;i++) for (int j=0; j<*(b+i); j++) { *(a+i+j)=-20+random(41); } } void Massiv :: Print() { for(int i=0;i<n;i++) { for (int j=0; j<*(b+i);j++) { cout<<setw(6)<<*(a+i+j); } cout<<endl; } } void Massiv :: Print_2(int number) { for(int i=0;i<n;i++) { for (int j=0; j<*(b+i);j++) { if (i==number) cout<<setw(6)<<*(a+i+j); } cout<<endl; } } void Massiv :: Print_3(int rec, int field) { for(int i=0;i<n;i++) { for (int j=0; j<*(b+i);j++) { if ((i==rec)&&(j==field)) cout<<"Danii element= "<<setw(6)<<*(a+i+j); else "Massiv s takimi dannimi ne naiden!"; } cout<<endl; } } void Massiv :: Form(int n1,int n2) { int m1,m2,k,j,l,p; int c[20]; //zapominaem kolvo elementov v strokah m1=*(b+n1); m2=*(b+n2); // *(b+n1)=m1+m2; int m3=m1+m2; k=0; for (l=0;l<m3;l++) if(l<m1) {c[l]=*(a+n1+l); cout<<setw(6)<<c[l]; } else if((l>=m1)&&(l<m3)) { c[l]=*(a+n2+k); cout<<setw(6)<<c[l]; k++; } } void Massiv :: Form_2(int n3,int n4) { int m1,m2,k,j,l,p,l1; int c[20],f[20]; //zapominaem kolvo elementov v strokah m1=*(b+n3); m2=*(b+n4); int m3=m1+m2; k=0; for (l=0;l<m3;l++) { if(l<m1) {c[l]=*(a+n3+l); } else if((l>=m1)&&(l<m3)) { c[l]=*(a+n4+k); k++; } } l1=0; f[l1]=c[l1]; cout<<setw(6)<<f[l1]; //sliyanie for(p=1;p<m3;p++) if(f[l1]!=c[p]) { l++; f[l1]=c[p]; cout<<setw(6)<<f[l1]; } }
Решение задачи: «Перевести программу из C++ в C#»
textual
Листинг программы
using System; public static class Program { private static Massiv array = new Massiv(); private static int menuOption; public static void Main() { do { Console.WriteLine(); Console.WriteLine("1 - sozdanie massiva"); Console.WriteLine("2 - pechat vseh strok"); Console.WriteLine("3 - pechat stroki po indeksu"); Console.WriteLine("4 - sceplenie dvuh massivov"); Console.WriteLine("5 - sliyanie dvuh massivov"); Console.WriteLine("6 - pechat konkretnogo elementa konkretnoi stroki"); Console.WriteLine("7 - vihod"); Console.WriteLine(); Console.Write("Vvedite punkt menu "); menuOption = int.Parse(Console.ReadLine()); } while (HandleMenu()); } private static bool HandleMenu() { switch (menuOption) { case 1: //sozdanie massiva array.Vvod_Sl(); Console.WriteLine(); array.Print(); break; case 2: array.Print(); //pechat massiva break; case 3: //pechat stroki po indeksu int number = ReadInteger("Vvedite nomer massiva (numeracia s 0!): "); array.Print_2(number); break; case 4: //sceplenie dvuh massivov int n1 = ReadInteger("Vvrdite nomer 1-go massiva(numeracia s 0!): "); int n2 = ReadInteger("Vvedite nomer 2-go massiva(numeracia s 0!): "); array.Form(n1, n2); break; case 5: //sliyanie dvuh massivov int n3 = ReadInteger("Vvrdite nomer 1-go massiva(numeracia s 0!): "); int n4 = ReadInteger("Vvedite nomer 2-go massiva(numeracia s 0!): "); array.Form_2(n3, n4); break; case 6: //pechat konkretnogo elementa konkretnoi stroki int rec = ReadInteger("Vvedite nomer stroki massiva(numeraciya s 0!): "); int field = ReadInteger("Vvedite nomer stolbca: "); array.Print_3(rec, field); break; case 7: return false; } return true; } private static int ReadInteger(string label) { Console.Write(label); int value = int.Parse(Console.ReadLine()); Console.WriteLine(); return value; } } public class Massiv { private static readonly Random Randomizer = new Random(); private int[][] data; public void Sozd() { Console.Write("kolvo strok?"); data = new int[int.Parse(Console.ReadLine())][]; for (var i = 0; i < data.Length; i++) { Console.Write("kolvo elementov v {0} stroke?", i + 1); data[i] = new int[int.Parse(Console.ReadLine())]; } } public void Vvod_Sl() { Sozd(); foreach (int[] row in data) for (var i = 0; i < row.Length; i++) row[i] = Randomizer.Next(-20, 21); } public void Form(int n1, int n2) { foreach (int field in data[n1]) Console.Write("{0,6}", field); foreach (int field in data[n2]) Console.Write("{0,6}", field); } public void Form_2(int n3, int n4) { int previous = int.MinValue; foreach (int field in data[n3]) if (field != previous) Console.Write("{0,6}", field); foreach (int field in data[n4]) if (field != previous) Console.Write("{0,6}", field); } public void Print() { for (var i = 0; i < data.Length; i++) Print_2(i); } public void Print_2(int row) { foreach (int field in data[row]) Console.Write("{0,6}", field); Console.WriteLine(); } public void Print_3(int row, int column) { if (row >= 0 && row < data.Length && column >= 0 && column < data[row].Length) Console.WriteLine("Danii element= {0,6}", data[row][column]); else Console.WriteLine("Massiv s takimi dannimi ne naiden!"); } }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д