C#
- 43 ответа
- 0 вопросов
47
Вклад в тег
var data = "0, 1.1, 1.2, 1.3, name, true";
var myClass = MyClass.Parse(data);
MyClass.SeeChanges(myClass);
public class MyClass
{
private int Id { get; set; }
private double X { get; set; }
private double Y { get; set; }
private double Z { get; set; }
private string Name { get; set; }
private bool Active { get; set; }
public static MyClass Parse(string data)
{
var myClass = new MyClass();
var properties = data.Split(',');
var props = myClass.GetType().GetProperties(BindingFlags.NonPublic | BindingFlags.Public
| BindingFlags.Instance | BindingFlags.Static);
for (int i = 0; i < props.Length; i++)
{
var prop = props[i];
var type = prop.PropertyType;
prop.SetValue(myClass, Convert.ChangeType(properties[i], type, CultureInfo.InvariantCulture));
}
return myClass;
}
public static void SeeChanges(MyClass myClass)
{
var props = myClass.GetType().GetProperties(BindingFlags.NonPublic | BindingFlags.Public
| BindingFlags.Instance | BindingFlags.Static);
foreach (var prop in props)
{
Debug.WriteLine("{0} = {1}", prop.Name, prop.GetValue(myClass, null));
}
}
}
#include "stdafx.h"
#include <iostream>
#include "list"
#include <algorithm>
#include <sstream>
#include <string>
using namespace std;
class Car {
public:
enum Type
{
Passenger,
Cargo
};
int Price;
string Model;
Type TypeAuto;
Car(int price, string model, Type type) {
this->Model = model;
this->Price = price;
this->TypeAuto = type;
}
string ToString() {
std::stringstream result;
result << "Model: " << this->Model <<
", Price: " << this->Price <<
", Type: " << this->TypeAuto;
return result.str();
}
};
class FilterCars {
public:
list<Car> Cars;
int AveragePrice;
FilterCars(list<Car> cars, int averagePrice) {
this->Cars = cars;
this->AveragePrice = averagePrice;
}
};
void ShowCars(list<Car> cars) {
for (auto car : cars) {
std::cout << car.ToString() << endl;;
}
}
FilterCars AveragePriceByType(list<Car> cars, Car::Type type) {
list<Car> selectCars;
std::copy_if(cars.begin(), cars.end(), std::back_inserter(selectCars), [type](Car car) {
return car.TypeAuto == type;
});
auto prices = 0;
for (auto car : selectCars) {
prices += car.Price;
}
auto average = prices / selectCars.size();
return FilterCars(selectCars, average);
}
int main()
{
setlocale(LC_ALL, "russian");
auto cars = {
Car(40000, "Tesla", Car::Type::Passenger),
Car(34000, "Chevrolet", Car::Type::Passenger),
Car(324000, "Mercedes ", Car::Type::Passenger),
Car(114000, "Volvo ", Car::Type::Cargo),
Car(222000, "BMW ", Car::Type::Cargo),
};
auto cargo = AveragePriceByType(cars, Car::Type::Cargo);
auto passenger = AveragePriceByType(cars, Car::Type::Passenger);
cout << "Вывод грузовых автомобилей: " << endl;
ShowCars(cargo.Cars);
cout << "Средняя цена грузовых автомобилей: " << cargo.AveragePrice << endl << endl;
cout << "Вывод легковых автомобилей: " << endl;
ShowCars(passenger.Cars);
cout << "Средняя цена легковых автомобилей: " << passenger.AveragePrice << endl << endl;
system("pause");
return 0;
}