20_6_7 2
function.h
#pragma once
#ifndef FUNCTION_H
#define FUNCTION_H
class volume {
private:
double length, width, height;
public:
volume() {};
volume(double l, double w, double h) {
length = l;
width = w;
height = h;
}
double cube_volume() {//立方体的体积
double v = length * width * height;
return v;
}
double outside_area() {//立方体的表面积
double s;
s = 2 * (length * height + length * width + width * height);
return s;
}
double repair() {
double real;
real = 2 * (length * height + length * width + width * height) - length * width;
return real;
}
void input();//输入函数
};
void volume::input() {
std::cin >> length >> width >> height;
}
#endif
menu.h
#pragma once
#ifndef MENU_H
#define MENU_H
#include<iostream>
using namespace std;
void menu() {
cout << "****************************" << endl;
cout << "1.求房间的体积" << endl;
cout << "2.求房间的表面积" << endl;
cout << "3.求房间的粉刷成本" << endl;
cout << "0.退出" << endl;
cout << "****************************" << endl;
}
void exit_() {
cout << "****************************" << endl;
cout << "欢迎使用本程序" << endl;
cout << "退出成功" << endl;
cout << "欢迎下次使用" << endl;
cout << "****************************" << endl;
}
#endif
main.cpp
#include<iostream>
#include<stdlib.h>
#include"function.h"
#include"menu.h"
#define COST 20
using namespace std;
int main(void) {
volume B;
int c;
menu();
while (1) {
cout << "输入房间的长、宽、高的值:";
B.input();
cout << "请选择:" << endl;
cin >> c;
switch (c)
{
case 1:
cout << "1.求房间的体积:" << B.cube_volume() << endl;
break;
case 2:
cout << "2.求房间的表面积:" << B.outside_area() << endl;
break;
case 3:
cout << "3.求房间的粉刷成本:" << B.repair() *COST<< endl;
break;
case 0:
system("cls");
exit_();
exit(1);
break;
default:
cout << "无此选项!";
}
}
system("pause");
return 0;
}

浙公网安备 33010602011771号