I have calculated costs from each floor but now I need to calculate total cost for all floors how would I do this?

조회 수: 1 (최근 30일)
This code all works but Im not sure how to combine each floors total cost to get the overall building cost.(Im sorry its long.)
choice9 = questdlg('Would you like to calculate costs for spaces?','question','yes','no','yes');
switch choice9
case 'no'
disp('not calculating cost')
case 'yes'
ResidentialCost=input('Please enter the building cost for residential spaces per m^2: ');
OfficeCost=input('Please enter the building cost for office spaces per m^2: ');
EducationalCost=input('Please enter the building cost for educational spaces per m^2: ');
ToiletCost=input('Please enter the building cost for toilet spaces per m^2: ');
StorageCost=input('Please enter the building cost for Storage spaces per m^2: ');
if ResidentialArea<=0 || ResidentialCost<=0
TotalResidentialCost=0;
disp('No cost for residential spaces')
else
TotalResidentialCost= ResidentialCost*ResidentialArea;
disp("Your total cost for floor "+c+" residential spaces is:"+TotalResidentialCost)
end
if OfficeArea<=0 || OfficeCost<=0
TotalOfficeCost=0;
disp('No cost for office spaces')
else
TotalOfficeCost= OfficeCost*OfficeArea;
disp("Your total cost for floor "+c+" office spaces is:"+TotalOfficeCost)
end
if EducationalArea<=0 || EducationalCost<=0
TotalEducationalCost=0;
disp('No cost for educational spaces')
else
TotalEducationalCost= EducationalCost*EducationalArea;
disp("Your total cost for floor "+c+" educational spaces is:"+TotalEducationalCost)
end
if ToiletArea<=0 || ToiletCost<=0
TotalToiletCost=0;
disp('No cost for educational spaces')
else
TotalToiletCost= ToiletCost*ToiletArea;
disp("Your total cost for floor "+c+" toilet spaces is:"+TotalToiletCost)
end
if StorageArea<=0 || StorageCost<=0
TotalStorageCost=0;
disp('No cost for storage spaces:')
else
TotalStorageCost= StorageCost*StorageArea;
disp("Your total cost for floor "+c+" storage spaces is:"+TotalStorageCost)
end
OverallCost=0;
OverallCost=TotalStorageCost+TotalToiletCost+TotalEducationalCost+TotalOfficeCost+TotalResidentialCost+OverallCost;
disp("Your running total for floor "+c+" is: "+OverallCost)
end
end

답변 (1개)

Umur Ayberk
Umur Ayberk 2022년 5월 5일
It is hard to tell without seeing the rest of the code but I believe you can use a variable to store the total cost of floors from 1 to c outside the loop and keep adding the overall floor cost to it.
% I assume there is some way to input the number of floors and areas for
% each floor but I just used sample values here.
nFloors = 3;
RunningBuildingCost = 0;
for c = 1:nFloors
ResidentialArea = 1;
OfficeArea =1;
EducationalArea = 1;
ToiletArea = 1;
StorageArea = 1;
%%
choice9 = questdlg('Would you like to calculate costs for spaces?','question','yes','no','yes');
switch choice9
case 'no'
disp('not calculating cost')
case 'yes'
ResidentialCost=input('Please enter the building cost for residential spaces per m^2: ');
OfficeCost=input('Please enter the building cost for office spaces per m^2: ');
EducationalCost=input('Please enter the building cost for educational spaces per m^2: ');
ToiletCost=input('Please enter the building cost for toilet spaces per m^2: ');
StorageCost=input('Please enter the building cost for Storage spaces per m^2: ');
if ResidentialArea<=0 || ResidentialCost<=0
TotalResidentialCost=0;
disp('No cost for residential spaces')
else
TotalResidentialCost= ResidentialCost*ResidentialArea;
disp("Your total cost for floor "+c+" residential spaces is:"+TotalResidentialCost)
end
if OfficeArea<=0 || OfficeCost<=0
TotalOfficeCost=0;
disp('No cost for office spaces')
else
TotalOfficeCost= OfficeCost*OfficeArea;
disp("Your total cost for floor "+c+" office spaces is:"+TotalOfficeCost)
end
if EducationalArea<=0 || EducationalCost<=0
TotalEducationalCost=0;
disp('No cost for educational spaces')
else
TotalEducationalCost= EducationalCost*EducationalArea;
disp("Your total cost for floor "+c+" educational spaces is:"+TotalEducationalCost)
end
if ToiletArea<=0 || ToiletCost<=0
TotalToiletCost=0;
disp('No cost for educational spaces')
else
TotalToiletCost= ToiletCost*ToiletArea;
disp("Your total cost for floor "+c+" toilet spaces is:"+TotalToiletCost)
end
if StorageArea<=0 || StorageCost<=0
TotalStorageCost=0;
disp('No cost for storage spaces:')
else
TotalStorageCost= StorageCost*StorageArea;
disp("Your total cost for floor "+c+" storage spaces is:"+TotalStorageCost)
end
OverallCost=0;
OverallCost=TotalStorageCost+TotalToiletCost+TotalEducationalCost+TotalOfficeCost+TotalResidentialCost+OverallCost;
disp("Your running total for floor "+c+" is: "+OverallCost)
end
%%
RunningBuildingCost = RunningBuildingCost + OverallCost;
disp("Your running total for floors upto "+c+" is: "+RunningBuildingCost)
end

카테고리

Help CenterFile Exchange에서 Descriptive Statistics에 대해 자세히 알아보기

태그

제품


릴리스

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by