Форум программистов, компьютерный форум, киберфорум
С++ для начинающих
Войти
Регистрация
Восстановить пароль
Карта форума Темы раздела Блоги Сообщество Поиск Заказать работу  
 
Рейтинг 4.86/7: Рейтинг темы: голосов - 7, средняя оценка - 4.86
1 / 1 / 0
Регистрация: 17.11.2020
Сообщений: 154
1

Перевод из Java в C++

14.10.2021, 15:53. Показов 1414. Ответов 6

Author24 — интернет-сервис помощи студентам
Народ, здравствуйте, кому не сложно, поможете с переводом задания кода из Java в C++?
Java
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
public class Product {
 
    private int id;
 
    private String name;
 
    private int UPC;
 
    private String manufacturer;
 
    private double price;
 
    private int shelf_life;
 
    private int number;
 
 
    public Product(){
        this.id = 0;
        this.name = "";
        this.UPC = 0;
        this.manufacturer = "";
        this.price = 0;
        this.shelf_life = 0;
        this.number = 0;
    }
 
    public Product(int id, String name, int UPC, String manufacturer, double price, int shelf_life, int number) {
        this.id = id;
        this.name = name;
        this.UPC = UPC;
        this.manufacturer = manufacturer;
        this.price = price;
        this.shelf_life = shelf_life;
        this.number = number;
    }
 
    public int getId() {
        return id;
    }
 
    public void setId(int id) {
        this.id = id;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public int getUPC() {
        return UPC;
    }
 
    public void setUPC(int UPC) {
        if(UPC>0) {
            this.UPC = UPC;
        } else System.out.println("UPC не может быть отрицательным или нулевым!!");
    }
 
    public String getManufacturer() {
        return manufacturer;
    }
 
    public void setManufacturer(String manufacturer) {
        this.manufacturer = manufacturer;
    }
 
    public double getPrice() {
        return price;
    }
 
    public void setPrice(double price) {
        if(price>0) {
            this.price = price;
        } else System.out.println("Цена не может быть отрицательной или нулевой!!");
    }
 
    public int getShelf_life() {
        return shelf_life;
    }
 
    public void setShelf_life(int shelf_life) {
        if(shelf_life>0) {
            this.shelf_life = shelf_life;
        } else System.out.println("Срок хранения не может быть отрицательным или нулевым!!");
    }
 
    public int getNumber() {
        return number;
    }
 
    public void setNumber(int number) {
        if (number>0) {
            this.number = number;
        } else System.out.println("Количество товара не может быть отрицательным или нулевым!!");
    }
 
    @Override
    public String toString() {
        return "Продукт --- " +
                "id=" + id +
                ", Имя=" + name +
                ", UPC=" + UPC +
                ", Производитель=" + manufacturer  +
                ", Цена=" + price +
                ", Срок годности=" + shelf_life +
                ", Количество=" + number;
    }
}
По заданию :Разработать классы для описанных ниже объектов. Включить вкласс методы set (…), get (…), show (…). Определить другие методы.
Product: Наименование, Производитель, Цена, Срок хранения,
Количество. Создать массив объектов. Вывести:
а) список товаров для заданного наименования;
б) список товаров для заданного наименования, цена которых непревышает указанной;
в) список товаров, срок хранения которых больше заданного.
0
Лучшие ответы (1)
Programming
Эксперт
94731 / 64177 / 26122
Регистрация: 12.04.2006
Сообщений: 116,782
14.10.2021, 15:53
Ответы с готовыми решениями:

Аналог MoveNext на Java. Перевод программы с C# на Java
Написала программу на C#. Перевожу ее на Java. Проблема возникла в переводе нижеследующего куска...

Перевод кода с Pascal на Java - Java SE
var a:Int64; begin Reset(Input,'input.txt'); Rewrite(Output,'output.txt'); read(a);...

Перевод код С++ на Java - Java SE
#include <fstream> using namespace std; ifstream cin ("input.txt"); ofstream cout...

Перевод java.sql.date -> java.util.date?
Перевод java.sql.date -> java.util.date?

Перевод с Си на Java
Помогите пожалуйста перевести программу с Си на Java. #include <stdio.h> #include <math.h> ...

6
-87 / 21 / 8
Регистрация: 11.10.2021
Сообщений: 122
14.10.2021, 16:06 2
Этот код почти дословно переводится. В чем именно проблема?
0
1 / 1 / 0
Регистрация: 17.11.2020
Сообщений: 154
14.10.2021, 16:14  [ТС] 3
Честно, не разбираюсь
0
-87 / 21 / 8
Регистрация: 11.10.2021
Сообщений: 122
14.10.2021, 16:20 4
Лучший ответ Сообщение было отмечено vadyusharamzes2 как решение

Решение

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
#include <string>
#include <iostream>
 
class Product {
 
    private: int id;
 
    private: std::string name;
 
    private: int UPC;
 
    private: std::string manufacturer;
 
    private: double price;
 
    private: int shelf_life;
 
    private: int number;
 
 
    public: Product(){
        this->id = 0;
        this->name = "";
        this->UPC = 0;
        this->manufacturer = "";
        this->price = 0;
        this->shelf_life = 0;
        this->number = 0;
    }
 
    public: Product(int id, std::string name, int UPC, std::string manufacturer, double price, int shelf_life, int number) {
        this->id = id;
        this->name = name;
        this->UPC = UPC;
        this->manufacturer = manufacturer;
        this->price = price;
        this->shelf_life = shelf_life;
        this->number = number;
    }
 
    public: int getId() {
        return id;
    }
 
    public: void setId(int id) {
        this->id = id;
    }
 
    public: std::string getName() {
        return name;
    }
 
    public: void setName(std::string name) {
        this->name = name;
    }
 
    public: int getUPC() {
        return UPC;
    }
 
    public: void setUPC(int UPC) {
        if(UPC>0) {
            this->UPC = UPC;
        } else std::cout << "UPC не может быть отрицательным или нулевым!!" << std::endl;
    }
 
    public: std::string getManufacturer() {
        return manufacturer;
    }
 
    public: void setManufacturer(std::string manufacturer) {
        this->manufacturer = manufacturer;
    }
 
    public: double getPrice() {
        return price;
    }
 
    public: void setPrice(double price) {
        if(price>0) {
            this->price = price;
        } else std::cout << "Цена не может быть отрицательной или нулевой!!" << std::endl;
    }
 
    public: int getShelf_life() {
        return shelf_life;
    }
 
    public: void setShelf_life(int shelf_life) {
        if(shelf_life>0) {
            this->shelf_life = shelf_life;
        } else std::cout << "Срок хранения не может быть отрицательным или нулевым!!" << std::endl;
    }
 
    public: int getNumber() {
        return number;
    }
 
    public: void setNumber(int number) {
        if (number>0) {
            this->number = number;
        } else std::cout << "Количество товара не может быть отрицательным или нулевым!!" << std::endl;
    }
 
    public: std::string toString() {
        return "Продукт --- " +
                "id=" + id +
                ", Имя=" + name +
                ", UPC=" + UPC +
                ", Производитель=" + manufacturer  +
                ", Цена=" + price +
                ", Срок годности=" + shelf_life +
                ", Количество=" + number;
    }
};
Не проверял, но вроде ничего не изменил, а только перевел.
1
1 / 1 / 0
Регистрация: 17.11.2020
Сообщений: 154
14.10.2021, 16:57  [ТС] 5
Код то правильный но почему то в строчке
C++
1
2
return "Продукт --- " +
        ", id=" + id +
Пишет что id должно относится к целочисленному типу или типу перечислений без области видимости
А также в строках
C++
1
2
3
4
5
6
7
8
9
public: Product() {
    this->id = 0;
    this->name = "";
    this->UPC = 0;
    this->manufacturer = "";
    this->price = 0;
    this->shelf_life = 0;
    this->number = 0;
}
C++
1
2
3
4
5
6
7
8
9
public: Product(int id, std::string name, int UPC, std::string manufacturer, double price, int shelf_life, int number) {
    this->id = id;
    this->name = name;
    this->UPC = UPC;
    this->manufacturer = manufacturer;
    this->price = price;
    this->shelf_life = shelf_life;
    this->number = number;
}
Не найдено определение функции для Patient и getName,setSurname
0
-87 / 21 / 8
Регистрация: 11.10.2021
Сообщений: 122
14.10.2021, 17:04 6
Цитата Сообщение от black-icarus Посмотреть сообщение
C++
1
2
3
4
5
6
7
8
9
10
public: std::string toString() {
 return "Продукт --- " +
 "id=" + std::string(id) +
 ", Имя=" + name +
 ", UPC=" + std::string(UPC) +
 ", Производитель=" + manufacturer +
 ", Цена=" + std::string(price) +
 ", Срок годности=" + std::string(shelf_life) +
 ", Количество=" + std::string(number);
 }
Попробуй так, а про функции, то я просто перевел код без изменений.
0
1 / 1 / 0
Регистрация: 17.11.2020
Сообщений: 154
14.10.2021, 17:06  [ТС] 7
БОЛЬШОООООЕ ВАМ СПАСИБОООООООО
Оно работает
0
14.10.2021, 17:06
IT_Exp
Эксперт
87844 / 49110 / 22898
Регистрация: 17.06.2006
Сообщений: 92,604
14.10.2021, 17:06
Помогаю со студенческими работами здесь

Перевод с С# на Java
using System; using System.Drawing; using System.Threading; using System.Windows.Forms; ...

Перевод с java на С++
Всем доброго времени суток. Возможно ли перевести код с java на С. Я попробовал сам,начал,но там...

Перевод с c++ на java
Собственно весь код на c++ #include &lt;fstream&gt; #include &lt;stdio.h&gt; using namespace std;...

Перевод с Java на C++
Помогите перевести следующий код с Java на C++: package weaver; import java.util.ArrayList;...

Перевод из с++ на java
Люди помогите пожалуйста.Нужно перевести данный код из С++ на Java: // lab3.cpp : Defines the...


Искать еще темы с ответами

Или воспользуйтесь поиском по форуму:
7
Ответ Создать тему
КиберФорум - форум программистов, компьютерный форум, программирование
Powered by vBulletin
Copyright ©2000 - 2024, CyberForum.ru