I am trying to convert this Node.js script over to C# :
/*
Name : paintRoomCalc in node.js
Author : Аїӡек Меѥҏ
Version : v1.0
License : N/A
*/
const prompt = require('prompt-sync')();
const colors = require('colors');
function getLayers(roomWidth, roomDepth, roomHeight, paintThick) {
var edgeArea, edgeSideDepthArea, edgeSideDepthVol, edgeSideWidthArea, edgeSideWidthVol, edgeUpArea, edgeUpVol, edgeVol, i, roomVol, t, wallArea, wallFive, wallOne, wallThree, wallVol;
i = 0;
t = 0;
roomWidth = (roomWidth * 12);
roomDepth = (roomDepth * 12);
roomHeight = (roomHeight * 12);
roomVol = ((roomWidth * roomDepth) * roomHeight);
while ((roomVol >= 0)) {
wallOne = (roomWidth * roomHeight);
wallThree = (roomDepth * roomHeight);
wallFive = (roomDepth * roomWidth);
wallVol = ((((wallOne * 2) + (wallThree * 2)) + wallFive) * paintThick);
wallArea = (((wallOne * 2) + (wallThree * 2)) + wallFive);
roomWidth = (roomWidth - (paintThick * 2));
roomDepth = (roomDepth - (paintThick * 2));
roomHeight = (roomHeight - (paintThick * 2));
edgeUpVol = (roomHeight * Math.pow(paintThick, 2));
edgeUpVol = (edgeUpVol * 2);
edgeUpArea = (roomHeight * paintThick);
edgeUpVol = (edgeUpArea * 2);
edgeSideDepthVol = (roomDepth * Math.pow(paintThick, 2));
edgeSideDepthVol = (edgeSideDepthVol * 2);
edgeSideDepthArea = (roomDepth * paintThick);
edgeSideWidthVol = (roomWidth * Math.pow(paintThick, 2));
edgeSideWidthVol = (edgeSideWidthVol * 2);
edgeSideWidthArea = (roomWidth * paintThick);
edgeVol = ((edgeUpVol + edgeSideWidthVol) + edgeSideDepthVol);
edgeArea = ((edgeUpArea + edgeSideWidthArea) + edgeSideDepthArea);
roomVol = ((roomVol - wallVol) - edgeVol);
wallArea = (wallArea - edgeArea);
i += 1;
t += wallArea;
}
console.log(colors.magenta(i.toString() + " layers to fill your room with paint!!"));
console.log(colors.magenta(("& " + (t / 4800).toString()) + " gallons of paint"));
}
console.log(colors.bgBlack.green("Paint Layer Calculator"));
console.log(colors.bgBlack.green("by : Аїӡек Меѥҏ"));
console.log("\n")
let roomWidth = prompt(colors.blue("Room Width in Feet? "));
roomWidth = Number(roomWidth);
let roomDepth = prompt(colors.blue("Room Depth in Feet? "));
roomDepth = Number(roomDepth);
let roomHeight = prompt(colors.blue("Room Height in Feet? "));
roomHeight = Number(roomHeight);
console.log("\n");
console.log(colors.red("1 mil is 1/1000 of an inch"));
let paintThickMil = prompt(colors.blue("Paint Thickness in Mils? "));
paintThickMil = Number(paintThickMil);
paintThick = (paintThickMil / 1000);
console.log("\n");
console.log(colors.bgBlack.green("Calculating..."));
getLayers(roomWidth, roomDepth, roomHeight, paintThick);
so far I've gotten this far :
/*
Name : paintRoomCalc in C#
Author : Аїӡек Меѥҏ
Version : b0.1
License : N/A
*/
using System;
class MainClass {
public void getLayers(int roomWidth, int roomDepth, int roomHeight, int paintThick) {
int edgeArea, edgeSideDepthArea, edgeSideDepthVol, edgeSideWidthArea, edgeSideWidthVol, edgeUpArea, edgeUpVol, edgeVol, roomVol, wallArea, wallFive, wallOne, wallThree, wallVol;
double i = 0;
double t = 0;
roomWidth = (roomWidth * 12);
roomDepth = (roomDepth * 12);
roomHeight = (roomHeight * 12);
roomVol = ((roomWidth * roomDepth) * roomHeight);
while ((roomVol >= 0)) {
wallOne = (roomWidth * roomHeight);
wallThree = (roomDepth * roomHeight);
wallFive = (roomDepth * roomWidth);
wallVol = ((((wallOne * 2) + (wallThree * 2)) + wallFive) * paintThick);
wallArea = (((wallOne * 2) + (wallThree * 2)) + wallFive);
roomWidth = (roomWidth - (paintThick * 2));
roomDepth = (roomDepth - (paintThick * 2));
roomHeight = (roomHeight - (paintThick * 2));
edgeUpVol = (roomHeight * Math.Pow(paintThick, 2));
edgeUpVol = (edgeUpVol * 2);
edgeUpArea = (roomHeight * paintThick);
edgeUpVol = (edgeUpArea * 2);
edgeSideDepthVol = (roomDepth * Math.Pow(paintThick, 2));
edgeSideDepthVol = (edgeSideDepthVol * 2);
edgeSideDepthArea = (roomDepth * paintThick);
edgeSideWidthVol = (roomWidth * Math.Pow(paintThick, 2));
edgeSideWidthVol = (edgeSideWidthVol * 2);
edgeSideWidthArea = (roomWidth * paintThick);
edgeVol = ((edgeUpVol + edgeSideWidthVol) + edgeSideDepthVol);
edgeArea = ((edgeUpArea + edgeSideWidthArea) + edgeSideDepthArea);
roomVol = ((roomVol - wallVol) - edgeVol);
wallArea = (wallArea - edgeArea);
i += 1;
t += wallArea;
}
Console.WriteLine(ToString(i) + " layers to fill your room with paint!!");
Console.WriteLine(("& " + ToString(t / 4800)) + " gallons of paint");
}
public static void Main (String[] args) {
int roomWidth, roomDepth, roomHeight, paintThickMil, paintThick;
Console.WriteLine("Paint Layer Calculator");
Console.WriteLine("by : Аїӡек Меѥҏ");
Console.WriteLine();
//get input for room dimentions
Console.Write("Room Width in Feet? ");
roomWidth = Console.ReadLine();
roomWidth = ToInt32(roomWidth);
Console.Write("Room Depth in Feet? ");
roomDepth = Console.ReadLine();
roomDepth = ToInt32(roomDepth);
Console.Write("Room Height in Feet? ");
roomHeight = Console.ReadLine();
roomHeight = ToInt32(roomHeight);
Console.WriteLine();
Console.WriteLine("1 mil is 1/1000 of an inch");
Console.Write("Paint Thickness in Mils? ");
paintThickMil = Console.ReadLine();
paintThickMil = ToInt32(paintThickMil);
paintThick = (paintThickMil / 1000);
Console.WriteLine();
Console.WriteLine("Calculating...");
getLayers(roomWidth, roomDepth, roomHeight, paintThick);
}
}
I get this error multiple times when trying to compile with mcs
:
Cannot implicitly convert type `string' to `int'
I also get this error as well :
Cannot implicitly convert type `double' to `int'. An explicit conversion exists (are you missing a cast?)
it seems as if I'm converting vars incorrectly
I've tried the method approach : int.ToString()
&& string.ToInt32()
like in Node.js, but to no avail
Thanks for your help ahead of time!
Cheers!
EDIT : it's been solved : working code :
/*
Name : paintRoomCalc in C#
Author : Аїӡек Меѥҏ
Version : v1.0
License : N/A
*/
using System;
class MainClass {
public static void getLayers(int roomWidth, int roomDepth, int roomHeight, int paintThick) {
int edgeArea, edgeSideDepthArea, edgeSideDepthVol, edgeSideWidthArea, edgeSideWidthVol, edgeUpArea, edgeUpVol, edgeVol, roomVol, wallArea, wallFive, wallOne, wallThree, wallVol;
double i = 0;
double t = 0;
roomWidth = (roomWidth * 12);
roomDepth = (roomDepth * 12);
roomHeight = (roomHeight * 12);
roomVol = ((roomWidth * roomDepth) * roomHeight);
while ((roomVol >= 0)) {
wallOne = (roomWidth * roomHeight);
wallThree = (roomDepth * roomHeight);
wallFive = (roomDepth * roomWidth);
wallVol = ((((wallOne * 2) + (wallThree * 2)) + wallFive) * paintThick);
wallArea = (((wallOne * 2) + (wallThree * 2)) + wallFive);
roomWidth = (roomWidth - (paintThick * 2));
roomDepth = (roomDepth - (paintThick * 2));
roomHeight = (roomHeight - (paintThick * 2));
edgeUpVol = (roomHeight * (int)Math.Pow(paintThick, 2));
edgeUpVol = (edgeUpVol * 2);
edgeUpArea = (roomHeight * paintThick);
edgeUpVol = (edgeUpArea * 2);
edgeSideDepthVol = (roomDepth * (int)Math.Pow(paintThick, 2));
edgeSideDepthVol = (edgeSideDepthVol * 2);
edgeSideDepthArea = (roomDepth * paintThick);
edgeSideWidthVol = (roomWidth * (int)Math.Pow(paintThick, 2));
edgeSideWidthVol = (edgeSideWidthVol * 2);
edgeSideWidthArea = (roomWidth * paintThick);
edgeVol = ((edgeUpVol + edgeSideWidthVol) + edgeSideDepthVol);
edgeArea = ((edgeUpArea + edgeSideWidthArea) + edgeSideDepthArea);
roomVol = ((roomVol - wallVol) - edgeVol);
wallArea = (wallArea - edgeArea);
i += 1;
t += wallArea;
}
Console.WriteLine(Convert.ToString(i) + " layers to fill your room with paint!!");
Console.WriteLine(("& " + Convert.ToString(t / 4800)) + " gallons of paint");
}
public static void Main (String[] args) {
string roomWidth, roomDepth, roomHeight, paintThickMil;
int roomWidthInt, roomDepthInt, roomHeightInt, paintThickMilInt, paintThickInt;
Console.WriteLine("Paint Layer Calculator");
Console.WriteLine("by : Аїӡек Меѥҏ");
Console.WriteLine();
//get input for room dimentions
Console.Write("Room Width in Feet? ");
roomWidth = Console.ReadLine();
roomWidthInt = Convert.ToInt32(roomWidth);
Console.Write("Room Depth in Feet? ");
roomDepth = Console.ReadLine();
roomDepthInt = Convert.ToInt32(roomDepth);
Console.Write("Room Height in Feet? ");
roomHeight = Console.ReadLine();
roomHeightInt = Convert.ToInt32(roomHeight);
Console.WriteLine();
Console.WriteLine("1 mil is 1/1000 of an inch");
Console.Write("Paint Thickness in Mils? ");
paintThickMil = Console.ReadLine();
paintThickMilInt = Convert.ToInt32(paintThickMil);
paintThickInt = (paintThickMilInt / 1000);
Console.WriteLine();
Console.WriteLine("Calculating...");
getLayers(roomWidthInt, roomDepthInt, roomHeightInt, paintThickInt);
}
}
to summarize for future readers, Math.Pow
cannot do operations on doubles so you have to cast it like this :
(int)Math.Pow(<double>);
also, variables stay one type once declared and you cannot reassign types so you have to have a variable for each type, at least how I did it.
EDIT :
I got the code working as if it was written in Node.js : division of two integers causes problems so the final variables, in getLayers()
weren't subtracting because it was subtracting NaN from the final volume.
here's the completely working code using various tips from very nice stack overflow members, and my spaghetti code additions. :
/*
Name : paintRoomCalc in C#
Author : Аїӡек Меѥҏ
Version : v1.1
License : N/A
*/
using System;
class MainClass {
static public 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 = 0;
double 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 * Math.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 * Math.Pow(paintThick, 2) );
edgeSideDepthVol = (float)( edgeSideDepthVol * 2 );
edgeSideDepthArea = (float)( roomDepth * paintThick );
//// account for edges going side to side {width}
edgeSideWidthVol = (float)( roomWidth * Math.Pow(paintThick, 2) );
edgeSideWidthVol = (float)( edgeSideWidthVol * 2 );
edgeSideWidthArea = (float)( roomWidth * paintThick );
// add edges
edgeVol = (float)( edgeUpVol + edgeSideWidthVol + edgeSideDepthVol );
edgeArea = (float)( edgeUpArea + edgeSideWidthArea + edgeSideDepthArea );
// calculate final values
roomVol = (float)( roomVol - wallVol - edgeVol );
wallArea = (float)( wallArea - edgeArea );
i += 1;
t += wallArea;
}
Console.WriteLine(i + " layers to fill your room with paint!!");
Console.WriteLine("& " + t / 4800 + " gallons of paint");
}
static public void Main(string[] args) {
float roomWidth, roomDepth, roomHeight, paintThickMil;
float paintThick;
Console.WriteLine("Paint Layer Calculator");
Console.WriteLine("by : Аїӡек Меѥҏ");
Console.WriteLine();
//get input for room dimensions
Console.Write("Room Width in Feet? ");
roomWidth = float.Parse(Console.ReadLine());
Console.Write("Room Depth in Feet? ");
roomDepth = float.Parse(Console.ReadLine());
Console.Write("Room Height in Feet? ");
roomHeight = float.Parse(Console.ReadLine());
Console.WriteLine();
Console.WriteLine("1 mil is 1/1000 of an inch");
Console.Write("Paint Thickness in Mils? ");
paintThickMil = float.Parse(Console.ReadLine());
paintThick = paintThickMil / 1000;
Console.WriteLine();
Console.WriteLine("Calculating...");
getLayers(roomWidth, roomDepth, roomHeight, paintThick);
Console.ReadKey();
}
}
I could ( probably ) not cast the (float)
to each and every one of the math operations; would that still work?
also here's my github repo for this project to see the other various versions of this code https://github.com/Izder456/paintRoomCalc