将 MKS 转换为 CGS 并将 CGS 转换为 MKS 的菜单驱动程序
先决条件: C/C++ 中的切换案例
问题陈述:
使用 Switch case 编写一个菜单驱动程序,将 CGS 系统转换为 MKS 系统,反之亦然。
方法:
MKS(Meter-Kilogram-Second) 系统: MKS 单位系统是指物理测量系统,其中尺寸长度、重量和时间分别以米、公斤和秒为单位测量。
MKS System:
Length - meters (m)
Weight - kilograms (kg)
Time - seconds (s)
CGS(Centimeter-Gram-Second) 系统: CGS 单位系统是指物理测量系统,其中尺寸长度、重量和时间分别以厘米、克和秒来衡量。
CGS System:
Length - centimeters (cm)
Weight - grams (g)
Time - seconds (s)
如何将 MKS 转换为 CGS?
为了将MKS系统转换为CGS系统,转换过程如下:
For Length:
Since,
1 meter = 100 centimeter
Therefore,
multiply the given length value by 100.
For Weight:
Since,
1 kilogram = 1000 gram
Therefore,
multiply the given weight value by 1000.
For Time:
Since,
1 second = 1 second
Therefore,
The time value will remain the same.
如何将 CGS 转换为 MKS?
为了将CGS系统转换为MKS系统,转换过程如下:
For Length:
Since,
1 centimeter = 1/100 met
Therefore,
divide the given length value by 100.
For Weight:
Since,
1 gram = 1/1000 kilogram
Therefore,
divide the given weight value by 1000.
For Time:
Since,
1 second = 1 second
Therefore,
The time value will remain the same.
下面是上述方法的实现:
程序:
CPP
// C++ menu-driven program using the Switch case
// to convert the CGS system to MKS system
// and vice versa
#include
using namespace std;
// declare mks class pre-hand for reference
// to mks class for friend function in cgs.
class mks;
class cgs {
private:
// cgs units: centimeter, gram, second.
//(length, weight, time)
float cm, gm, sec;
public:
// Default constructor
cgs()
{
cm = 0;
gm = 0;
sec = 0;
}
// Parameterized constructor
cgs(float x, float y, float z)
{
cm = x;
gm = y;
sec = z;
}
void disp_cgs()
{
cout << cm << "\t" << gm
<< "\t" << sec << "\n";
}
void disp_cgs1()
{
cout << cm;
}
void disp_cgs2()
{
cout << gm;
}
void disp_cgs3()
{
cout << sec;
}
// friend functions:
friend mks cgs2mks(cgs);
friend cgs mks2cgs(mks);
};
class mks {
private:
// mks units: meter, kilogram, second.
//(length, weight, time)
float m, kg, sec;
public:
// Default constructor
mks()
{
m = 0;
kg = 0;
sec = 0;
}
// Parameterized constructor
mks(float x, float y, float z)
{
m = x;
kg = y;
sec = z;
}
void disp_mks()
{
cout << m << "\t" << kg
<< "\t" << sec << "\n";
}
void disp_mks1()
{
cout << m;
}
void disp_mks2()
{
cout << kg;
}
void disp_mks3()
{
cout << sec;
}
friend mks cgs2mks(cgs);
friend cgs mks2cgs(mks);
};
// mks->cgs conversion:
mks cgs2mks(cgs c)
{
mks t;
t.m = c.cm / 100;
t.kg = c.gm / 1000;
t.sec = c.sec;
return t;
}
// cgs->mks conversion:
cgs mks2cgs(mks d)
{
cgs t;
t.cm = d.m * 100;
t.gm = d.kg * 1000;
t.sec = d.sec;
return t;
}
// Code for menu
void menu(int mainMenuChoice, int parameterChoice)
{
float distanceValue = 30, weightValue = 300, timeValue = 3000;
int continueChoice = 0;
cout << "MKS <-> CGS converter menu:\n";
do {
cout << "Enter 1 for CGS to MKS conversion.\n"
<< "Enter 2 for MKS to CGS conversion.\n";
cout << "\nYou have chosen: "
<< mainMenuChoice
<< "\n\n";
switch (mainMenuChoice) {
case 1: {
cout << "Enter 0 to convert all three parameters"
<< "- distance, weight and time.\n"
<< "Enter 1 for converting distance\n"
<< "Enter 2 for converting weight\n"
<< "Enter 3 for converting time\n";
cout << "\nYou have chosen: "
<< parameterChoice
<< "\n\n";
switch (parameterChoice) {
case 0: {
cout << "Enter values for distance unit in centimeter.\n";
distanceValue;
cout << endl;
cout << "Enter values for weight unit in grams.\n";
weightValue;
cout << endl;
cout << "Enter values for time unit in seconds.\n";
timeValue;
cout << endl;
cout << "The entered values in CGS system are: \n";
cgs c(distanceValue, weightValue, timeValue);
c.disp_cgs();
cout << "Corresponding values in MKS system: \n";
mks m = cgs2mks(c);
m.disp_mks();
cout << endl;
break;
}
case 1: {
cout << "Enter values for distance unit in centimeter.\n";
distanceValue;
cout << endl;
cout << "The entered values in CGS system are: \n";
cgs c(distanceValue, 0, 0);
c.disp_cgs();
cout << "Corresponding values in MKS system: \n";
mks m = cgs2mks(c);
m.disp_mks();
cout << endl;
break;
}
case 2: {
cout << "Enter values for weight unit in grams.\n";
weightValue;
cout << endl;
cout << "The entered values in CGS system are: \n";
cgs c(0, weightValue, 0);
c.disp_cgs();
cout << "Corresponding values in MKS system: \n";
mks m = cgs2mks(c);
m.disp_mks();
cout << endl;
break;
}
case 3: {
cout << "Enter values for time unit in seconds.\n";
timeValue;
cout << endl;
cout << "The entered values in CGS system are: \n";
cgs c(0, 0, timeValue);
c.disp_cgs();
cout << "Corresponding values in MKS system: \n";
mks m = cgs2mks(c);
m.disp_mks();
cout << endl;
break;
}
}
break;
}
case 2: {
cout << "Enter 0 to convert all three parameters"
<< "- distance, weight and time.\n"
<< "Enter 1 for converting distance\n"
<< "Enter 2 for converting weight\n"
<< "Enter 3 for converting time\n";
cout << "\nYou have chosen: "
<< parameterChoice
<< "\n\n";
switch (parameterChoice) {
case 0: {
cout << "Enter values for distance unit in meter.\n";
distanceValue;
cout << endl;
cout << "Enter values for weight unit in kilogram.\n";
weightValue;
cout << endl;
cout << "Enter values for time unit in second.\n";
timeValue;
cout << endl;
cout << "The entered values in MKS system are: \n";
mks m(distanceValue, weightValue, timeValue);
m.disp_mks();
cout << "Corresponding values in CGS system: \n";
cgs c = mks2cgs(m);
c.disp_cgs();
cout << endl;
break;
}
case 1: {
cout << "Enter values for distance unit in meter.\n";
distanceValue;
cout << endl;
cout << "The entered values in MKS system are: \n";
mks m(distanceValue, 0, 0);
m.disp_mks();
cout << "Corresponding values in CGS system: \n";
cgs c = mks2cgs(m);
c.disp_cgs();
cout << endl;
break;
}
case 2: {
cout << "Enter values for weight unit in kilogram.\n";
weightValue;
cout << endl;
cout << "The entered values in MKS system are: \n";
mks m(0, weightValue, 0);
m.disp_mks();
cout << "Corresponding values in CGS system: \n";
cgs c = mks2cgs(m);
c.disp_cgs();
cout << endl;
break;
}
case 3: {
cout << "Enter values for time unit in second.\n";
timeValue;
cout << endl;
cout << "The entered values in MKS system are: \n";
mks m(0, 0, timeValue);
m.disp_mks();
cout << "Corresponding values in CGS system: \n";
cgs c = mks2cgs(m);
c.disp_cgs();
cout << endl;
break;
}
}
}
}
cout << "Enter 1 to continue, or\n"
<< "Press any other key to exit.\n";
} while (continueChoice == 1);
}
// Driver code
int main()
{
cout << "Check for CGS to MKS of all units.\n\n";
menu(1, 0);
cout << "\n-------------------------------------------------\n";
cout << "\nCheck for CGS to MKS of only distance unit.\n\n";
menu(1, 1);
cout << "\n-------------------------------------------------\n";
cout << "\nCheck for CGS to MKS of only weight unit.\n\n";
menu(1, 2);
cout << "\n-------------------------------------------------\n";
cout << "\nCheck for CGS to MKS of only time unit.\n\n";
menu(1, 3);
cout << "\n-------------------------------------------------\n";
cout << "Check for MKS to CGS of all units.\n\n";
menu(2, 0);
cout << "\n-------------------------------------------------\n";
cout << "\nCheck for MKS to CGS of only distance unit.\n\n";
menu(2, 1);
cout << "\n-------------------------------------------------\n";
cout << "\nCheck for MKS to CGS of only weight unit.\n\n";
menu(2, 2);
cout << "\n-------------------------------------------------\n";
cout << "\nCheck for MKS to CGS of only time unit.\n\n";
menu(2, 3);
cout << "\n-------------------------------------------------\n";
return 0;
}
输出
Check for CGS to MKS of all units.
MKS <-> CGS converter menu:
Enter 1 for CGS to MKS conversion.
Enter 2 for MKS to CGS conversion.
You have chosen: 1
Enter 0 to convert all three parameters- distance, weight and time.
Enter 1 for converting distance
Enter 2 for converting weight
Enter 3 for converting time
You have chosen: 0
Enter values for distance unit in centimeter.
Enter values for weight unit in grams.
Enter values for time unit in seconds.
The entered values in CGS system are:
30 300 3000
Corresponding values in MKS system:
0.3 0.3 3000
Enter 1 to continue, or
Press any other key to exit.
-------------------------------------------------
Check for CGS to MKS of only distance unit.
MKS <-> CGS converter menu:
Enter 1 for CGS to MKS conversion.
Enter 2 for MKS to CGS conversion.
You have chosen: 1
Enter 0 to convert all three parameters- distance, weight and time.
Enter 1 for converting distance
Enter 2 for converting weight
Enter 3 for converting time
You have chosen: 1
Enter values for distance unit in centimeter.
The entered values in CGS system are:
30 0 0
Corresponding values in MKS system:
0.3 0 0
Enter 1 to continue, or
Press any other key to exit.
-------------------------------------------------
Check for CGS to MKS of only weight unit.
MKS <-> CGS converter menu:
Enter 1 for CGS to MKS conversion.
Enter 2 for MKS to CGS conversion.
You have chosen: 1
Enter 0 to convert all three parameters- distance, weight and time.
Enter 1 for converting distance
Enter 2 for converting weight
Enter 3 for converting time
You have chosen: 2
Enter values for weight unit in grams.
The entered values in CGS system are:
0 300 0
Corresponding values in MKS system:
0 0.3 0
Enter 1 to continue, or
Press any other key to exit.
-------------------------------------------------
Check for CGS to MKS of only time unit.
MKS <-> CGS converter menu:
Enter 1 for CGS to MKS conversion.
Enter 2 for MKS to CGS conversion.
You have chosen: 1
Enter 0 to convert all three parameters- distance, weight and time.
Enter 1 for converting distance
Enter 2 for converting weight
Enter 3 for converting time
You have chosen: 3
Enter values for time unit in seconds.
The entered values in CGS system are:
0 0 3000
Corresponding values in MKS system:
0 0 3000
Enter 1 to continue, or
Press any other key to exit.
-------------------------------------------------
Check for MKS to CGS of all units.
MKS <-> CGS converter menu:
Enter 1 for CGS to MKS conversion.
Enter 2 for MKS to CGS conversion.
You have chosen: 2
Enter 0 to convert all three parameters- distance, weight and time.
Enter 1 for converting distance
Enter 2 for converting weight
Enter 3 for converting time
You have chosen: 0
Enter values for distance unit in meter.
Enter values for weight unit in kilogram.
Enter values for time unit in second.
The entered values in MKS system are:
30 300 3000
Corresponding values in CGS system:
3000 300000 3000
Enter 1 to continue, or
Press any other key to exit.
-------------------------------------------------
Check for MKS to CGS of only distance unit.
MKS <-> CGS converter menu:
Enter 1 for CGS to MKS conversion.
Enter 2 for MKS to CGS conversion.
You have chosen: 2
Enter 0 to convert all three parameters- distance, weight and time.
Enter 1 for converting distance
Enter 2 for converting weight
Enter 3 for converting time
You have chosen: 1
Enter values for distance unit in meter.
The entered values in MKS system are:
30 0 0
Corresponding values in CGS system:
3000 0 0
Enter 1 to continue, or
Press any other key to exit.
-------------------------------------------------
Check for MKS to CGS of only weight unit.
MKS <-> CGS converter menu:
Enter 1 for CGS to MKS conversion.
Enter 2 for MKS to CGS conversion.
You have chosen: 2
Enter 0 to convert all three parameters- distance, weight and time.
Enter 1 for converting distance
Enter 2 for converting weight
Enter 3 for converting time
You have chosen: 2
Enter values for weight unit in kilogram.
The entered values in MKS system are:
0 300 0
Corresponding values in CGS system:
0 300000 0
Enter 1 to continue, or
Press any other key to exit.
-------------------------------------------------
Check for MKS to CGS of only time unit.
MKS <-> CGS converter menu:
Enter 1 for CGS to MKS conversion.
Enter 2 for MKS to CGS conversion.
You have chosen: 2
Enter 0 to convert all three parameters- distance, weight and time.
Enter 1 for converting distance
Enter 2 for converting weight
Enter 3 for converting time
You have chosen: 3
Enter values for time unit in second.
The entered values in MKS system are:
0 0 3000
Corresponding values in CGS system:
0 0 3000
Enter 1 to continue, or
Press any other key to exit.
-------------------------------------------------