Title: weight conversion

Posted by Shaudh on Tue, 25 Jan 2022 02:22:58 +0100

Weight conversion

Title Description

At present, the most basic unit of international weight is gram. In ancient times, the weight units of various countries were different.

China uses Jin, Liang and money to express weight, where 1 jin = 10 Liang and 1 liang = 10 money

The relationship between Chinese weight unit and gram is: 1 jin = 500g, 1 liang = 50g, 1 money = 5g

In Britain, pounds, ounces and talans are used to express weight, where 1 pound = 16 ounces and 1 ounce = 16 talans

The relationship between British weight unit and gram is: 1 pound = 512 grams, 1 ounce = 32 grams, 1 Dalan = 2 grams

The following reference code contains the abstract class Weight, which is inherited by both Chinese Weight and British Weight.

China's weight category adds three attributes: Jin, Liang and Qian, and an operation: weight conversion Convert.

Convert can convert the input grams into Chinese weight, for example, 1234 grams into 2 kilograms, 4 liang, 6 money and 4 grams, and put the values into the four attributes of Jin, 2, money and G

The British weight category adds three attributes: pound, ounce and Talan, and two operations:

1. The function of weight conversion Convert is similar to the above. For example, 2345g is converted into 4 pounds, 9 ounces, 4 Dalan and 1g, and the value is put into the corresponding four attributes

2. Weight equivalence, overloading the type conversion operator to convert the object of the UK weight class into the object of the Chinese weight class. For example, the UK weight class object en (2 pounds, 2 ounces, 11 Dalan 1g) is equivalent to (converted to) the Chinese weight class object cn (2 pounds, 2 ounces, 1 gram).

The procedure requirements are as follows

1. The reference code framework cannot be modified. Add code where required

2. The main function cannot be modified

The above data is purely designed for the subject, which is convenient for calculation. The actual converted data are different.

All the above class attributes are not public, and the above requirements are implemented with object-oriented idea and C + + language

----Reference code----

class CN; //Advance declaration

class EN; //Advance declaration

class Weight{ //abstract class

protected:

char kind[20]; //Weight type

int gram; //gram

public:

Weight (char tk[]="no name", int tg=0)

{ strcpy(kind, tk);

gram = tg;

}

virtual void Print(ostream & out) = 0; //Output different types of weight information

};

class CN: public Weight { //China weight

//.... Class definitions are written by yourself

};

class EN: public Weight { //British weight

//.... Class definitions are written by yourself

}

//Overload the output operator as a global function, lines 3-5 Self written

//The overloaded function contains two parameters: ostream stream object and Weight class object. The parameters can be object or object reference

//Overloaded functions must call the Print method of the Weight object

int main()//Main function

{int tw;

//Create a Chinese weight object cn

//The construction parameters correspond to Jin, Liang, Qian, gram and type, where gram and type correspond to the base class attributes gram and kind

CN cn(0,0,0,0, "China weight");

cin>>tw;

cn.Convert(tw); //Convert the input grams into Chinese weight

cout<<cn;

//Create UK weight class object en

//The construction parameters correspond to pound, ounce, Dalan, gram and type, where gram and type correspond to the base class attributes gram and kind

EN en(0,0,0,0,"British weight");

cin>>tw;

en.Convert(tw); //Convert the entered grams to British weight

cout<<en;

cn=en; //Convert British weight to Chinese weight

cout<<cn;

return 0;

}

input

In the first line, enter a gram and call the Chinese weight conversion to convert the gram into Chinese weight

In the second line, enter a gram, call the British weight conversion, convert the gram to the British weight, and call the weight equivalent to convert the British weight to the Chinese weight

output

Run the output according to the main function

Sample input

1234
2345

Sample output

China's weight: 2 kg, 4 liang, 6 yuan and 4 g
British weight: 4 pounds, 9 ounces, 4 Dylan, 1 gram
China's weight: 4 kg, 6 Liang, 9 yuan and 0 gram

#include<iostream>
#include<cstring>
using namespace std;
class CN; //Advance declaration
class EN; //Advance declaration
class Weight{ //abstract class

	protected:
		char kind[20]; //Weight type
		int gram; //gram
	public:
		Weight (char tk[]="no name", int tg=0)
		{ 
			strcpy(kind, tk);
			gram = tg;
		}
	virtual void Print(ostream & out) = 0; //Output different types of weight information
};

class CN: public Weight { //China weight
//.... Class definitions are written by yourself
	public:
		CN(int jin1,int liang1,int qian1,int gram1,char kind1[]):Weight(kind1,gram1)
		{
			jin=jin1;
			liang=liang1;
			qian=qian1;
		}
		void Convert(int sum)
		{
			jin=sum/500;
			liang=sum%500/50;
			qian=sum%50/5;
			gram=sum%5;
		}
		void Print(ostream & out)
		{
			cout<<kind<<":"<<jin<<"Jin"<<liang<<"two"<<qian<<"money"<<gram<<"gram"<<endl;
		}
	protected:
		int jin;
		int liang;
		int qian;

};

class EN: public Weight { //British weight

//.... Class definitions are written by yourself
	public:
		EN(int pound1,int ans1,int dalan1,int gram1,char kind1[]):Weight(kind1,gram1)
		{
			pound=pound1;
			ans=ans1;
			dalan=dalan1;
		}
		void Convert(int sum)
		{
			pound=sum/512;
			ans=sum%512/32;
			dalan=sum%32/2;
			gram=sum%2;
		}
		operator CN()
		{
			int sum;
			sum=pound*512+ans*32+dalan*2+gram;
			int a,b,c,d;
			a=sum/500;
			b=sum%500/50;
			c=sum%50/5;
			d=sum%5;
			CN cnn(a,b,c,d,"China weight");
			return cnn;
		}
		void Print(ostream & out)
		{
			cout<<kind<<":"<<pound<<"pound"<<ans<<"ounce"<<dalan<<"Dalan"<<gram<<"gram"<<endl;
		}
	private:
		int pound;
		int ans;
		int dalan;
};

//Overload the output operator as a global function, lines 3-5 Self written

//The overloaded function contains two parameters: ostream stream object and Weight class object. The parameters can be object or object reference

//Overloaded functions must call the Print method of the Weight object

ostream & operator << (ostream &output,Weight  &a)//Do not add const to the Weight object reference 
{
	a.Print(output);
	return output;
}

int main()//Main function

{int tw;

//Create a Chinese weight object cn

//The construction parameters correspond to Jin, Liang, Qian, gram and type, where gram and type correspond to the base class attributes gram and kind

CN cn(0,0,0,0, "China weight");

cin>>tw;

cn.Convert(tw); //Convert the input grams into Chinese weight

cout<<cn;

//Create UK weight class object en

//The construction parameters correspond to pound, ounce, Dalan, gram and type, where gram and type correspond to the base class attributes gram and kind

EN en(0,0,0,0,"British weight");

cin>>tw;

en.Convert(tw); //Convert the entered grams to British weight

cout<<en;

cn=en; //Convert British weight to Chinese weight

cout<<cn;

return 0;

}

Topics: C++