본문 바로가기
study/자료구조

[클래스 코드]

by 메이02 2023. 4. 15.

//내 코드

#include <iostream>
using namespace std;

class Rectangle {
private:
	int width;
	int height;

public:
	Rectangle();
	Rectangle(int, int);
	int evalArea(int, int);
	int evalPerimeter(int, int);
};

Rectangle::Rectangle(int w, int h) {
	width = w;
	height = h;
}

int Rectangle::evalArea(int width, int height)
{
	int area = width * height;
	return area;
}

int Rectangle :: evalPerimeter(int width, int height) 
{
	int peri = (width + height) * 2;
	return peri;
}

int main()
{
	int h = 0;
	int w = 0;
	int n = 0;
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> w;
		cin >> h;
		Rectangle rec(w, h);
		cout << rec.evalArea(w, h) << endl;
		cout << rec.evalPerimeter(w, h) << endl;	
	}
	return 0;
}

 

#include <iostream>
using namespace std;
#pragma warning(disable:4996)

class Goods {
private:
	char name[40];
	char maker[40];
	double price;
	double rate;
public:
	char getName();
	void setName(char*);
	char getMaker();
	void setMaker(char*);
	double getPrice();
	void setPrice(double);
	double getRate();
	void setRate(int);
	void evalPrice();
};

char Goods::getName() {
	return name[40];
}
void Goods::setName(char* newn) {
	strcpy(name, newn);
}
char Goods::getMaker() {
	return maker[40];
}
void Goods::setMaker(char* newm) {
	strcpy(maker, newm);
}
double Goods::getPrice() {
	return price;
}
void Goods::setPrice(double newp) {
	price = newp;
}
double Goods::getRate() {
	return rate;
}
void Goods::setRate(int newr) {
	rate = newr;
}
void Goods::evalPrice() {
	cout.precision(2);
	double result = price * (1 - (rate / 100));
	int final = result;
	cout <<  final;
}


int main() {
	int n = 0;
	cin >> n;
	char na[40];
	char m[40];
	double p;
	double r;
	for (int i = 0; i < n; i++)
	{
		cin >> na >> m >> p >> r;
		Goods good;
		good.setPrice(p);
		good.setRate(r);
		good.evalPrice();
	}

	return 0;
}

 

//정답 코드

#include <iostream>
using namespace std;
#define MAX_SIZE 100

class Rectangle {
private:
	int width;
	int height;
public:
	Rectangle() { };
	~Rectangle() { };
	int getWidth() { return width; }
	int getHeight(int w) { return height; }
	void setWidth(int w) { width = w; }
	void setHeight(int h) { height = h; }

	double evalPerimeter()
	{
		return (width + height) * 2;
	}

	int evalArea()
	{
		return width * height;

	}
};

class Goods {
private:
	string name;
	string maker;
	double price;
	double rate;
public:
	Goods() { };
	~Goods() { };
	string getName() { return name; }
	string getMaker() { return maker; }
	double getPrice() { return price; }
	double getRate() { return rate; }

	void setName(string n) { name = n; }
	void setMaker(string m) { maker = m; }
	void setPrice(double p) { price = p; }
	void setRate(double r) { rate = r; }

	double evalPrice()
	{
		return price - (price * (rate / 100.0f));
	}
};

int main()
{
	int n;

	// CLASS RECTANGLE
	Rectangle rect[MAX_SIZE];
	int w, h;

	cin >> n;
	for (int i = 0; i < n; i++)
	{
		cin >> w >> h;

		rect[i].setWidth(w);
		rect[i].setHeight(h);
	}
	for (int i = 0; i < n; i++)
		cout << endl << rect[i].evalArea() << endl << rect[i].evalPerimeter();
	cout << endl << endl;


	// CLASS GOODS
	Goods g[MAX_SIZE];
	string name;
	string maker;
	double price;
	double rate;

	cin >> n;
	for (int i = 0; i < n; i++)
	{
		cin >> name >> maker >> price >> rate;
		g[i].setName(name);
		g[i].setMaker(maker);
		g[i].setPrice(price);
		g[i].setRate(rate);
	}
	for (int i = 0; i < n; i++)
		cout << g[i].evalPrice() << endl;
	cout << endl;
}

'study > 자료구조' 카테고리의 다른 글

[큐 코드]  (0) 2023.04.15
[스택 코드]  (0) 2023.04.15
[Chap5] 연결 리스트 과제  (0) 2023.04.06
[Chap5] 포인터와 연결리스트  (0) 2023.04.05
[Chap4] 과제  (0) 2023.03.28