JAVA入坑之nefu往年瑞格练习2

目录

题目一text7187

基于C++实现text7187

基于python实现text7187

题目二text7190

基于C++实现text7190

基于python实现text7190

题目三text7188

基于c++的text7188

 基于python的text7188

题目四text7202

基于c++的text7202

基于python的text7202

题目五text7201

基于C++的text7201

基于python的text7201

题目六text7198

基于C++的text7198

基于python的text7198

 题目七text7200​编辑

基于c++的text7200

基于python的text7200

题目八text7199

基于C++的text7199

基于python的text7199

题目九text7191

基于c++的text7191

基于python的text7191


题目一text7187

在这里插入图片描述

import java.util.Scanner;
public class Main {
    public static class Cuboid {
        private int length;
        private int width;
        private int height;
        public void setDemo(int x, int y, int z) {
            length = x;
            width = y;
            height = z;
        }
        public int calculateVolume() {
            return length * width * height;
        }
    }
    public static void main(String[] args) {
        Cuboid cuboid = new Cuboid();
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入立方体的长、宽、高(用空格隔开):");
        int length = scanner.nextInt();
        int width = scanner.nextInt();
        int height = scanner.nextInt();
        cuboid.setDemo(length, width, height);
        int volume = cuboid.calculateVolume();
        System.out.println("体积=" + volume);
    }
}

优化

import java.util.Scanner;
public class Main {
    public static class Cuboid {
        private int length;
        private int width;
        private int height;
        public Cuboid(int length, int width, int height) {
            this.length = length;
            this.width = width;
            this.height = height;
        }
        public int calculateVolume() {
            return length * width * height;
        }
    }
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入立方体的长、宽、高(用空格隔开):");
        int length = scanner.nextInt();
        int width = scanner.nextInt();
        int height = scanner.nextInt();
        Cuboid cuboid = new Cuboid(length, width, height);
        int volume = cuboid.calculateVolume();
        System.out.println("体积=" + volume);
    }
}

基于C++实现text7187

#include <iostream>

using namespace std;

class Cuboid {
private:
    int length;
    int width;
    int height;
public:
    Cuboid(int length, int width, int height) {
        this->length = length;
        this->width = width;
        this->height = height;
    }
    int calculateVolume() {
        return length * width * height;
    }
};

int main() {
    int length, width, height;
    cout << "请输入立方体的长、宽、高(用空格隔开):";
    cin >> length >> width >> height;
    Cuboid cuboid(length, width, height);
    int volume = cuboid.calculateVolume();
    cout << "体积=" << volume << endl;
    return 0;
}

基于python实现text7187

class Cuboid:
    def __init__(self, length, width, height):
        self.length = length
        self.width = width
        self.height = height

    def calculateVolume(self):
        return self.length * self.width * self.height

length, width, height = input("请输入立方体的长、宽、高(用空格隔开):").split()
cuboid = Cuboid(int(length), int(width), int(height))
volume = cuboid.calculateVolume()
print("体积=", volume)

题目二text7190

在这里插入图片描述

import java.util.Scanner;
public class Main {
    public static class Add {
        int firstItem;
        int numberOfTerms;
        int tolerance;
        void evaluation(int a,int b,int c){
             firstItem = a;
             numberOfTerms = b;
             tolerance = c;
        }
        int getSum(){
            int sum = 0;
            for(int i = firstItem, j = 1; j <= numberOfTerms; j ++, i += tolerance) {
                sum += i;
            }
            return sum;
        }
    }
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入等差数列的首项,项数和公差(用空格隔开):");
        int a = scanner.nextInt();
        int b = scanner.nextInt();
        int c = scanner.nextInt();
        Add Exa = new Add();
        Exa.evaluation(a, b, c);
        int sum = Exa.getSum();
        System.out.print("和=" + sum);
    }
}

修改优化

import java.util.Scanner;
public class Main {
    public static class Add {
        private int firstTerm;
        private int numTerms;
        private int commonDifference;
        public Add(int firstTerm, int numTerms, int commonDifference) {
            this.firstTerm = firstTerm;
            this.numTerms = numTerms;
            this.commonDifference = commonDifference;
        }

        public int  getSum(){
            return numTerms * (2 * firstTerm + (numTerms - 1) * commonDifference) / 2;
        }
    }
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入等差数列的首项,项数和公差(用空格隔开):");
        int a = scanner.nextInt();
        int b = scanner.nextInt();
        int c = scanner.nextInt();
        Add add = new Add(a,b,c);
        int sum = add.getSum();
        System.out.print("和=" + sum);
    }
}

基于C++实现text7190

#include <iostream>

using namespace std;

class Add {
private:
    int firstTerm;
    int numTerms;
    int commonDifference;
public:
    Add(int firstTerm, int numTerms, int commonDifference) {
        this->firstTerm = firstTerm;
        this->numTerms = numTerms;
        this->commonDifference = commonDifference;
    }

    int getSum() {
        return numTerms * (2 * firstTerm + (numTerms - 1) * commonDifference) / 2;
    }
};

int main() {
    int a, b, c;
    cout << "请输入等差数列的首项,项数和公差(用空格隔开):";
    cin >> a >> b >> c;
    Add add(a, b, c);
    int sum = add.getSum();
    cout << "和=" << sum << endl;
    return 0;
}

基于python实现text7190

class Add:
    def __init__(self, firstTerm, numTerms, commonDifference):
        self.firstTerm = firstTerm
        self.numTerms = numTerms
        self.commonDifference = commonDifference

    def getSum(self):
        return self.numTerms * (2 * self.firstTerm + (self.numTerms - 1) * self.commonDifference) / 2

a, b, c = input("请输入等差数列的首项,项数和公差(用空格隔开):").split()
add = Add(int(a), int(b), int(c))
sum = add.getSum()
print("和=", sum)

题目三text7188

import java.util.Scanner;
public class Main {
    public static class Student {
        private String studentId;
        private String name;
        private String gender;
        private int age;

        public Student(String studentId, String name, String gender, int age) {
            this.studentId = studentId;
            this.name = name;
            this.gender = gender;
            this.age = age;
        }

        public String getStudentId() {
            return studentId;
        }

        public void setStudentId(String studentId) {
            this.studentId = studentId;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getGender() {
            return gender;
        }

        public void setGender(String gender) {
            this.gender = gender;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }
    }

    public static void main(String[] args) {
        Student student = new Student("2046", "张凡", "male", 18);
        // 修改学生的学号
        student.setStudentId("2059");
        // 打印学生对象的属性值
        System.out.println("学号:" + student.getStudentId());
        System.out.println("姓名:" + student.getName());
        System.out.println("性别:" + student.getGender());
        System.out.println("年龄:" + student.getAge());
    }
}

基于c++的text7188

#include <iostream>

using namespace std;

class Student {
private:
    string studentId;
    string name;
    string gender;
    int age;

public:
    Student(string studentId, string name, string gender, int age) {
        this->studentId = studentId;
        this->name = name;
        this->gender = gender;
        this->age = age;
    }

    string getStudentId() {
        return studentId;
    }

    void setStudentId(string studentId) {
        this->studentId = studentId;
    }

    string getName() {
        return name;
    }

    void setName(string name) {
        this->name = name;
    }

    string getGender() {
        return gender;
    }

    void setGender(string gender) {
        this->gender = gender;
    }

    int getAge() {
        return age;
    }

    void setAge(int age) {
        this->age = age;
    }
};

int main() {
    Student student("2046", "张凡", "male", 18);
    // 修改学生的学号
    student.setStudentId("2059");
    // 打印学生对象的属性值
    cout << "学号:" << student.getStudentId() << endl;
    cout << "姓名:" << student.getName() << endl;
    cout << "性别:" << student.getGender() << endl;
    cout << "年龄:" << student.getAge() << endl;

    return 0;
}

 基于python的text7188

class Student:
    def __init__(self, studentId, name, gender, age):
        self.studentId = studentId
        self.name = name
        self.gender = gender
        self.age = age

    def getStudentId(self):
        return self.studentId

    def setStudentId(self, studentId):
        self.studentId = studentId

    def getName(self):
        return self.name

    def setName(self, name):
        self.name = name

    def getGender(self):
        return self.gender

    def setGender(self, gender):
        self.gender = gender

    def getAge(self):
        return self.age

    def setAge(self, age):
        self.age = age


student = Student("2046", "张凡", "male", 18)
# 修改学生的学号
student.setStudentId("2059")
# 打印学生对象的属性值
print("学号:", student.getStudentId())
print("姓名:", student.getName())
print("性别:", student.getGender())
print("年龄:", student.getAge())

题目四text7202

在这里插入图片描述

import java.util.Scanner;
public class Main {
    public static class Complex {
        private double a; // 实部
        private double b; // 虚部

        // 默认构造方法
        public Complex() {
            this.a = 0;
            this.b = 0;
        }

        // 带参构造方法
        public Complex(double a, double b) {
            this.a = a;
            this.b = b;
        }

        // 拷贝构造方法
        public Complex(Complex c) {
            this.a = c.a;
            this.b = c.b;
        }

        // 复数加法
        public Complex add(Complex c) {
            double new_a = this.a + c.a;
            double new_b = this.b + c.b;
            return new Complex(new_a, new_b);
        }

        // 复数减法
        public Complex subtract(Complex c) {
            double new_a = this.a - c.a;
            double new_b = this.b - c.b;
            return new Complex(new_a, new_b);
        }

        // 打印复数
        public void print() {
            if (this.a == 0 && this.b == 0) {
                System.out.println("0");
            } else if (this.a == 0) {
                System.out.println(this.b + "i");
            } else if (this.b == 0) {
                System.out.println(this.a);
            } else {
                System.out.println(this.a + "+" + this.b + "i");
            }
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        double a1 = scanner.nextDouble();
        double b1 = scanner.nextDouble();
        double a2 = scanner.nextDouble();
        double b2 = scanner.nextDouble();
        Complex c1 = new Complex(a1, b1);
        Complex c2 = new Complex(a2, b2);
        Complex c3 = c1.add(c2);
        Complex c4 = c1.subtract(c2);
        c3.print();
        c4.print();
    }
}

基于c++的text7202

#include <iostream>

using namespace std;

class Complex {
private:
    double a; // 实部
    double b; // 虚部

public:
    // 默认构造方法
    Complex() {
        this->a = 0;
        this->b = 0;
    }

    // 带参构造方法
    Complex(double a, double b) {
        this->a = a;
        this->b = b;
    }

    // 拷贝构造方法
    Complex(const Complex& c) {
        this->a = c.a;
        this->b = c.b;
    }

    // 复数加法
    Complex add(Complex c) {
        double new_a = this->a + c.a;
        double new_b = this->b + c.b;
        return Complex(new_a, new_b);
    }

    // 复数减法
    Complex subtract(Complex c) {
        double new_a = this->a - c.a;
        double new_b = this->b - c.b;
        return Complex(new_a, new_b);
    }

    // 打印复数
    void print() {
        if (this->a == 0 && this->b == 0) {
            cout << "0" << endl;
        } else if (this->a == 0) {
            cout << this->b << "i" << endl;
        } else if (this->b == 0) {
            cout << this->a << endl;
        } else {
            cout << this->a << "+" << this->b << "i" << endl;
        }
    }
};

int main() {
    double a1, b1, a2, b2;
    cin >> a1 >> b1 >> a2 >> b2;
    Complex c1(a1, b1);
    Complex c2(a2, b2);
    Complex c3 = c1.add(c2);
    Complex c4 = c1.subtract(c2);
    c3.print();
    c4.print();
    return 0;
}

基于python的text7202

class Student:
    def __init__(self, studentId, name, gender, age):
        self.studentId = studentId
        self.name = name
        self.gender = gender
        self.age = age

    def getStudentId(self):
        return self.studentId

    def setStudentId(self, studentId):
        self.studentId = studentId

    def getName(self):
        return self.name

    def setName(self, name):
        self.name = name

    def getGender(self):
        return self.gender

    def setGender(self, gender):
        self.gender = gender

    def getAge(self):
        return self.age

    def setAge(self, age):
        self.age = age


student = Student("2046", "张凡", "male", 18)
# 修改学生的学号
student.setStudentId("2059")
# 打印学生对象的属性值
print("学号:", student.getStudentId())
print("姓名:", student.getName())
print("性别:", student.getGender())
print("年龄:", student.getAge())

题目五text7201

在这里插入图片描述

import java.util.Scanner;
class Point {
    private  int x;
    private  int y;
    public Point(int x,int y) {
        this.x=x;
        this.y=y;
    }
    public Point(Point B) {
        this.x=B.x;
        this.y=B.y;
    }
    public int getX() {
        return x;
    }
    public int getY() {
        return y;
    }
    public static void fun1(Point p) {
        System.out.println(p.getX());
    }
    public static Point fun2() {
        Point A =new Point(1,2);
        return A;
    }

}
public class Main{
    public static void main(String[] args) {
        int a, b;
        Scanner scanner = new Scanner(System.in);
        a = scanner.nextInt();
        b = scanner.nextInt();
        Point A = new Point(a, b);
        Point B = new Point(A);
        System.out.println(B.getX());
        B.fun1(B);
        B = B.fun2();
        System.out.println(B.getX());
    }
}

基于C++的text7201

#include <iostream>
using namespace std;

class Point {
private:
    int x;
    int y;

public:
    Point(int x, int y) {
        this->x = x;
        this->y = y;
    }

    Point(Point& B) {
        this->x = B.x;
        this->y = B.y;
    }

    int getX() {
        return x;
    }

    int getY() {
        return y;
    }

    static void fun1(Point* p) {
        cout << p->getX() << endl;
    }

    static Point fun2() {
        Point A(1, 2);
        return A;
    }
};

int main() {
    int a, b;
    cin >> a >> b;
    Point A(a, b);
    Point B(A);
    cout << B.getX() << endl;
    B.fun1(&B);
    B = B.fun2();
    cout << B.getX() << endl;
    return 0;
}

基于python的text7201

class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y
    
    def getX(self):
        return self.x
    
    def getY(self):
        return self.y
    
    def fun1(p):
        print(p.getX())
    
    def fun2():
        A = Point(1, 2)
        return A

a, b = map(int, input().split())
A = Point(a, b)
B = Point(A.x,A.y)
print(B.getX())
Point.fun1(B)
B = Point.fun2()
print(B.getX())

题目六text7198

在这里插入图片描述

import java.util.Scanner;
class Dog {
    private  String name;
    private  String color;
    private  int age;

    Dog(){
        name = "dog1";
        color = "white";
        age = 1;
    }
    Dog(String name,String color,int age){
        this.name = name;
        this.color = color;
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public String getcolor() {
        return color;
    }
    public int getAge() {
        return age;
    }
}
public class Main{
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String printName = in.next();
        String printColor = in.next();
        int printAge = in.nextInt();
        Dog d1 = new Dog();
        Dog d2 = new Dog(printName, printColor, printAge);
        System.out.println("name=" + d1.getName() + ",color=" +d1.getcolor()+",age="+d1.getAge());
        System.out.println("name=" + d2.getName() + ",color=" +d2.getcolor()+",age="+d2.getAge());
    }
}

基于C++的text7198

#include <iostream>
#include <string>

using namespace std;

class Dog {
private:
    string name;
    string color;
    int age;
public:
    Dog() {
        name = "dog1";
        color = "white";
        age = 1;
    }

    Dog(string name, string color, int age) {
        this->name = name;
        this->color = color;
        this->age = age;
    }

    string getName() {
        return name;
    }

    string getColor() {
        return color;
    }

    int getAge() {
        return age;
    }
};

int main() {
    string printName, printColor;
    int printAge;

    cin >> printName >> printColor >> printAge;

    Dog d1;
    Dog d2(printName, printColor, printAge);

    cout << "name=" << d1.getName() << ",color=" << d1.getColor() << ",age=" << d1.getAge() << endl;
    cout << "name=" << d2.getName() << ",color=" << d2.getColor() << ",age=" << d2.getAge() << endl;

    return 0;
}

基于python的text7198

class Dog:
    def __init__(self, name="dog1", color="white", age=1):
        self.name = name
        self.color = color
        self.age = age
        
    def getName(self):
        return self.name
    
    def getColor(self):
        return self.color
    
    def getAge(self):
        return self.age
    
name = input()
color = input()
age = int(input())
d1 = Dog()
d2 = Dog(name, color, age)
print("name={},color={},age={}".format(d1.getName(), d1.getColor(), d1.getAge()))
print("name={},color={},age={}".format(d2.getName(), d2.getColor(), d2.getAge()))

 题目七text7200在这里插入图片描述

import java.util.Scanner;
class MyClass {
    private int int1;
    private int int2;
    private double double1;
    private double double2;
    private double double3;
    private String string1;
    private String string2;

    public MyClass(int int1, int int2) {
        this.int1 = int1;
        this.int2 = int2;
    }

    public MyClass(double double1, double double2, double double3) {
        this.double1 = double1;
        this.double2 = double2;
        this.double3 = double3;
    }

    public MyClass(String string1, String string2) {
        this.string1 = string1;
        this.string2 = string2;
    }

    public int findLargerInt() {
        return Math.max(int1, int2);
    }

    public double calculateProduct() {
        return double1 * double2 * double3;
    }

    public boolean checkIfStringsEqual() {
        return string1.equals(string2);
    }
}
    public class Main{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int int1 = scanner.nextInt();
        int int2 = scanner.nextInt();
        double double1 = scanner.nextDouble();
        double double2 = scanner.nextDouble();
        double double3 = scanner.nextDouble();
        String string1 = scanner.next();
        String string2 = scanner.next();
        MyClass obj1 = new MyClass(int1, int2);
        System.out.println("Larger value: " + obj1.findLargerInt());
        MyClass obj2 = new MyClass(double1, double2, double3);
        System.out.printf("a*b*c = %.2f\n", obj2.calculateProduct());
        MyClass obj3 = new MyClass(string1, string2);
        System.out.println("Are equal: " + obj3.checkIfStringsEqual());
    }
}

基于c++的text7200

#include <iostream>
#include <string>
#include <cmath>
using namespace std;

class MyClass {
private:
    int int1;
    int int2;
    double double1;
    double double2;
    double double3;
    string string1;
    string string2;

public:
    MyClass(int int1, int int2) {
        this->int1 = int1;
        this->int2 = int2;
    }

    MyClass(double double1, double double2, double double3) {
        this->double1 = double1;
        this->double2 = double2;
        this->double3 = double3;
    }

    MyClass(string string1, string string2) {
        this->string1 = string1;
        this->string2 = string2;
    }

    int findLargerInt() {
        return max(int1, int2);
    }

    double calculateProduct() {
        return double1 * double2 * double3;
    }

    bool checkIfStringsEqual() {
        return string1 == string2;
    }
};

int main() {
    int int1, int2;
    double double1, double2, double3;
    string string1, string2;

    cin >> int1 >> int2 >> double1 >> double2 >> double3 >> string1 >> string2;

    MyClass obj1(int1, int2);
    cout << "Larger value: " << obj1.findLargerInt() << endl;

    MyClass obj2(double1, double2, double3);
    printf("a*b*c = %.2f\n", obj2.calculateProduct());

    MyClass obj3(string1, string2);
    cout << "Are equal: " << obj3.checkIfStringsEqual() << endl;

    return 0;
}

基于python的text7200

import sys
import math

class MyClass:
    def __init__(self, int1=None, int2=None, double1=None, double2=None, double3=None, string1=None, string2=None):
        self.int1 = int1
        self.int2 = int2
        self.double1 = double1
        self.double2 = double2
        self.double3 = double3
        self.string1 = string1
        self.string2 = string2

    def findLargerInt(self):
        return max(self.int1, self.int2)

    def calculateProduct(self):
        return self.double1 * self.double2 * self.double3

    def checkIfStringsEqual(self):
        return self.string1 == self.string2
int1, int2 = map(int, input().split())
double1, double2, double3 = map(float, input().split())
string1, string2 = input().split()
obj1 = MyClass(int1=int1, int2=int2)
print("Larger value:", obj1.findLargerInt())
obj2 = MyClass(double1=double1, double2=double2, double3=double3)
print("a*b*c = {:.2f}".format(obj2.calculateProduct()))
obj3 = MyClass(string1=string1, string2=string2)
print("Are equal:", obj3.checkIfStringsEqual())

题目八text7199

在这里插入图片描述

import java.util.Scanner;

class Prime {
    private int number;

    public Prime(int number) {
        this.number = number;
    }

    public boolean isPrime() {
        if (number == 1) {
            return false;
        } else if (number == 2) {
            return true;
        } else if (number % 2 == 0) {
            return false;
        } else {
            int sqrt = (int) Math.sqrt(number);
            for (int i = 3; i <= sqrt; i += 2) {
                if (number % i == 0) {
                    return false;
                }
            }
            return true;
        }
    }
}

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入一个自然数n:");
        int n = scanner.nextInt();
        Prime num = new Prime(n);
        if (num.isPrime() == true) {
            System.out.println("YES");
        } else {
            System.out.println("NO");
        }
    }
}

基于C++的text7199

#include <iostream>
#include <cmath>
using namespace std;

class Prime {
private:
    int number;

public:
    Prime(int number) {
        this->number = number;
    }

    bool isPrime() {
        if (number == 1) {
            return false;
        } else if (number == 2) {
            return true;
        } else if (number % 2 == 0) {
            return false;
        } else {
            int sqrtNum = (int) sqrt(number);
            for (int i = 3; i <= sqrtNum; i += 2) {
                if (number % i == 0) {
                    return false;
                }
            }
            return true;
        }
    }
};

int main() {
    int n;
    cout << "请输入一个自然数n:";
    cin >> n;
    Prime num(n);
    if (num.isPrime()) {
        cout << "YES" << endl;
    } else {
        cout << "NO" << endl;
    }
    return 0;
}

基于python的text7199

import math

class Prime:
    def __init__(self, number):
        self.number = number

    def is_prime(self):
        if self.number == 1:
            return False
        elif self.number == 2:
            return True
        elif self.number % 2 == 0:
            return False
        else:
            sqrt = int(math.sqrt(self.number))
            for i in range(3, sqrt+1, 2):
                if self.number % i == 0:
                    return False
            return True

n = int(input("请输入一个自然数n:"))
num = Prime(n)
if num.is_prime():
    print("YES")
else:
    print("NO")

题目九text7191

// 商品类
class Product {
    private String name;

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}

// 超市类
class Market {
    private String name;
    private Product[] products;

    public Market(String name, int capacity) {
        this.name = name;
        products = new Product[capacity];
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Product getProduct(int index) {
        return products[index];
    }

    public void setProduct(int index, Product product) {
        products[index] = product;
    }

    public String sell(String productName) {
        for (int i = 0; i < products.length; i++) {
            if (products[i] != null && products[i].getName().equals(productName)) {
                return productName;
            }
        }
        return null;
    }
}

// 购物者类
class Person {
    private String name;

    public Person(String name) {
        this.name = name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public String shopping(Market market, String productName) {
        String result = market.sell(productName);
        if (result == null) {
            return getName() + "所需" + productName + "无货";
        } else {
            return getName() + "买到了" + productName;
        }
    }
}

// 测试类
public class Main {
    public static void main(String[] args) {
        // 创建商品
        Product tv = new Product();
        tv.setName("电视机");
        Product washer = new Product();
        washer.setName("洗衣机");
        Product soyMilkMaker = new Product();
        soyMilkMaker.setName("豆浆机");
        Product printer = new Product();
        printer.setName("打印机");

        // 创建超市
        Market market = new Market("家家乐福", 4);
        market.setProduct(0, tv);
        market.setProduct(1, washer);
        market.setProduct(2, soyMilkMaker);
        market.setProduct(3, printer);

        // 创建购物者
        Person person = new Person("张乐");

        // 进行购物
        String productName = "电视机";
        String result = person.shopping(market, productName);

        // 输出结果
        System.out.println(result);
    }
}

基于c++的text7191

#include <iostream>
#include <string>
using namespace std;

// 商品类
class Product {
private:
    string name;

public:
    void setName(string name) {
        this->name = name;
    }

    string getName() {
        return name;
    }
};

// 超市类
class Market {
private:
    string name;
    Product* products;

public:
    Market(string name, int capacity) {
        this->name = name;
        products = new Product[capacity];
    }

    string getName() {
        return name;
    }

    void setName(string name) {
        this->name = name;
    }

    Product getProduct(int index) {
        return products[index];
    }

    void setProduct(int index, Product product) {
        products[index] = product;
    }

    string sell(string productName) {
        for (int i = 0; i < sizeof(products) / sizeof(products[0]); i++) {
            if (products[i].getName() == productName) {
                return productName;
            }
        }
        return "";
    }
};

// 购物者类
class Person {
private:
    string name;

public:
    Person(string name) {
        this->name = name;
    }

    void setName(string name) {
        this->name = name;
    }

    string getName() {
        return name;
    }

    string shopping(Market market, string productName) {
        string result = market.sell(productName);
        if (result == "") {
            return getName() + "所需" + productName + "无货";
        } else {
            return getName() + "买到了" + productName;
        }
    }
};

// 测试类
int main() {
    // 创建商品
    Product tv;
    tv.setName("电视机");
    Product washer;
    washer.setName("洗衣机");
    Product soyMilkMaker;
    soyMilkMaker.setName("豆浆机");
    Product printer;
    printer.setName("打印机");

    // 创建超市
    Market market("家家乐福", 4);
    market.setProduct(0, tv);
    market.setProduct(1, washer);
    market.setProduct(2, soyMilkMaker);
    market.setProduct(3, printer);

    // 创建购物者
    Person person("张乐");

    // 进行购物
    string productName = "电视机";
    string result = person.shopping(market, productName);

    // 输出结果
    cout << result << endl;

    return 0;
}

基于python的text7191

# 商品类
class Product:
    def __init__(self):
        self.name = ""

    def set_name(self, name):
        self.name = name

    def get_name(self):
        return self.name

# 超市类
class Market:
    def __init__(self, name, capacity):
        self.name = name
        self.products = [None] * capacity

    def get_name(self):
        return self.name

    def set_name(self, name):
        self.name = name

    def get_product(self, index):
        return self.products[index]

    def set_product(self, index, product):
        self.products[index] = product

    def sell(self, product_name):
        for product in self.products:
            if product is not None and product.get_name() == product_name:
                return product_name
        return None

# 购物者类
class Person:
    def __init__(self, name):
        self.name = name

    def set_name(self, name):
        self.name = name

    def get_name(self):
        return self.name

    def shopping(self, market, product_name):
        result = market.sell(product_name)
        if result is None:
            return self.get_name() + "所需" + product_name + "无货"
        else:
            return self.get_name() + "买到了" + product_name

# 测试类
if __name__ == "__main__":
    # 创建商品
    tv = Product()
    tv.set_name("电视机")
    washer = Product()
    washer.set_name("洗衣机")
    soy_milk_maker = Product()
    soy_milk_maker.set_name("豆浆机")
    printer = Product()
    printer.set_name("打印机")

    # 创建超市
    market = Market("家家乐福", 4)
    market.set_product(0, tv)
    market.set_product(1, washer)
    market.set_product(2, soy_milk_maker)
    market.set_product(3, printer)

    # 创建购物者
    person = Person("张乐")

    # 进行购物
    product_name = "电视机"
    result = person.shopping(market, product_name)

    # 输出结果
    print(result)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

烟雨平生9527

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值