问题描述
/*
Name : paintRoomCalc in C++
Author : Аїӡек Меѥҏ
Version : v1.0
License : N/A
*/
#include <cmath>
#include <iostream>
using namespace std;
static void getLayers(float roomWidth,float roomDepth,float roomHeight,float paintThick) {
float edgeArea,edgeSideDepthArea,edgeSideDepthVol,edgeSideWidthArea,edgeSideWidthVol,edgeUpArea,edgeUpVol,edgeVol,roomVol,wallArea,wallFive,wallOne,wallThree,wallVol;
double i;
double t;
i = 0;
t = 0;
roomWidth = (roomWidth * 12);
roomDepth = (roomDepth * 12);
roomHeight = (roomHeight * 12);
roomVol = ((roomWidth * roomDepth) * roomHeight);
while ((roomVol >= 0)) {
// account for walls
wallOne = float(roomWidth * roomHeight);
wallThree = float(roomDepth * roomHeight);
wallFive = float(roomDepth * roomWidth);
wallVol = float((((wallOne * 2) + (wallThree * 2)) + wallFive) * paintThick);
wallArea = float(((wallOne * 2) + (wallThree * 2)) + wallFive);
roomWidth = float(roomWidth - (paintThick * 2));
roomDepth = float(roomDepth - (paintThick * 2));
roomHeight = float(roomHeight - (paintThick * 2));
// account for edges going up & down
edgeUpVol = float(roomHeight * pow(paintThick,2));
edgeUpVol = float(edgeUpVol * 2);
edgeUpArea = float(roomHeight * paintThick);
edgeUpVol = float(edgeUpArea * 2);
// account for edges going side to side {on ceiling}
//// account for edges going side to side {depth}
edgeSideDepthVol = float(roomDepth * pow(paintThick,2));
edgeSideDepthVol = float(edgeSideDepthVol * 2);
edgeSideDepthArea = float(roomDepth * paintThick);
//// account for edges going side to side {width}
edgeSideWidthVol = float(roomWidth * pow(paintThick,2));
edgeSideWidthVol = float(edgeSideWidthVol * 2);
edgeSideWidthArea = float(roomWidth * paintThick);
edgeVol = float((edgeUpVol + edgeSideWidthVol) + edgeSideDepthVol);
// add edges
edgeArea = float((edgeUpArea + edgeSideWidthArea) + edgeSideDepthArea);
// calculate final values
roomVol = float((roomVol - wallVol) - edgeVol);
wallArea = float(wallArea - edgeArea);
i += 1;
t += wallArea;
}
cout << i << " layers to fill your room with paint!!" << endl;
cout << "& " << t / 4800 << " gallons of paint" << endl;
}
int main() {
float roomWidth,roomDepth,roomHeight,paintThickMil;
float paintThick;
cout << "Paint Layer Calculator" << endl;
cout << "by : izder456" << endl;
cout << "" << endl;
//get input for room dimentions
cout << "Room Width in Feet? ";
cin >> roomWidth;
cout << "Room Depth in Feet? ";
cin >> roomDepth;
cout << "Room Height in Feet? ";
cin >> roomHeight;
cout << endl;
cout << "1 mil is 1/1000 of an inch" << endl;
cout << "Paint Thickness in Mils? ";
cin >> paintThickMil;
paintThick = paintThickMil / 1000;
cout << endl;
cout << "Calculating..." << endl;
getLayers(roomWidth,paintThick);
cin.get();
return 0;
}
我转换为C的
/*
Name : paintRoomCalc in C
Author : Аїӡек Меѥҏ
Version : b0.1
License : N/A
*/
#include <stdio.h>
#include <math.h>
void getLayers(float roomWidth,wallVol;
double i;
double t;
i = 0;
t = 0;
roomWidth = (roomWidth * 12);
roomDepth = (roomDepth * 12);
roomHeight = (roomHeight * 12);
roomVol = ((roomWidth * roomDepth) * roomHeight);
while (roomVol >= 0) {
// account for walls
wallOne = (float)(roomWidth * roomHeight);
wallThree = (float)(roomDepth * roomHeight);
wallFive = (float)(roomDepth * roomWidth);
wallVol = (float)((((wallOne * 2) + (wallThree * 2)) + wallFive) * paintThick);
wallArea = (float)(((wallOne * 2) + (wallThree * 2)) + wallFive);
roomWidth = (float)(roomWidth - (paintThick * 2));
roomDepth = (float)(roomDepth - (paintThick * 2));
roomHeight = (float)(roomHeight - (paintThick * 2));
// account for edges going up & down
edgeUpVol = (float)(roomHeight * pow(paintThick,2));
edgeUpVol = (float)(edgeUpVol * 2);
edgeUpArea = (float)(roomHeight * paintThick);
edgeUpVol = (float)(edgeUpArea * 2);
// account for edges going side to side {on ceiling}
//// account for edges going side to side {depth}
edgeSideDepthVol = (float)(roomDepth * pow(paintThick,2));
edgeSideDepthVol = (float)(edgeSideDepthVol * 2);
edgeSideDepthArea = (float)(roomDepth * paintThick);
//// account for edges going side to side {width}
edgeSideWidthVol = (float)(roomWidth * pow(paintThick,2));
edgeSideWidthVol = (float)(edgeSideWidthVol * 2);
edgeSideWidthArea = (float)(roomWidth * paintThick);
edgeVol = (float)((edgeUpVol + edgeSideWidthVol) + edgeSideDepthVol);
// add edges
edgeArea = (float)((edgeUpArea + edgeSideWidthArea) + edgeSideDepthArea);
// calculate final values
roomVol = (float)((roomVol - wallVol) - edgeVol);
wallArea = (float)(wallArea - edgeArea);
i += 1;
t += wallArea;
}
printf("%f layers to fill your room with paint!!\n",i);
printf("& %f gallons of paint",((float)t/(float)4800));
};
int main() {
float roomWidth,paintThickMil;
float paintThick;
printf("Paint Layer Calculator\n");
printf("by : izder456\n");
printf("\n");
//get input for room dimentions
printf("Room Width in Feet? ");
scanf("%f",&roomWidth);
printf("Room Depth in Feet? ");
scanf("%f",&roomDepth);
printf("Room Height in Feet? ");
scanf("%f",&roomHeight);
printf("1 mil is 1/1000 of an inch\n");
printf("Paint Thickness in Mils? ");
scanf("%f",&paintThickMil);
paintThick = ((float)(paintThickMil))/((float)(1000));
printf("\n");
printf("Calculating...\n");
getLayers(roomWidth,paintThick);
while(getchar() != '\n');
return 0;
}
在CLion IDE中使用C99标准进行编译和运行时,我得到的输出很奇怪;像这样:
Paint Layer Calculator
by : izder456
Room Width in Feet?1
Room Depth in Feet?2
Room Height in Feet?3
1 mil is 1/1000 of an inch
Paint Thickness in Mils?2
Calculating...
3332.000000 layers to fill your room with paint!!
& 1039.159058 gallons of paint
Process finished with exit code 0
我希望它看起来像这样:
Paint Layer Calculator
by : izder456
Room Width in Feet? 1
Room Depth in Feet? 2
Room Height in Feet? 3
1 mil is 1/1000 of an inch
Paint Thickness in Mils? 2
Calculating...
3332.000000 layers to fill your room with paint!!
& 1039.159058 gallons of paint
Process finished with exit code 0
我希望在提示和输入之间插入空格;我不希望他们像这样在行的开头。我还希望在倒数第二个问题和最后一个问题之前的行之间有一个空格,例如在C ++版本中。
我如何在scanf
中格式化printf
和main()
以获得这种I / O格式?
您的帮助将不胜感激!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)