0% found this document useful (0 votes)
14 views2 pages

CalcRainwater (Final)

The document contains a C++ program that calculates the total volume and number of areas formed by slopes and steeps represented by characters in a string. It uses stacks and queues to manage the slopes and steeps, computes the total volume, and prints the results. The program handles input validation and outputs the calculated volumes and area counts.

Uploaded by

jackx607
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views2 pages

CalcRainwater (Final)

The document contains a C++ program that calculates the total volume and number of areas formed by slopes and steeps represented by characters in a string. It uses stacks and queues to manage the slopes and steeps, computes the total volume, and prints the results. The program handles input validation and outputs the calculated volumes and area counts.

Uploaded by

jackx607
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <iostream>

#include <stack>
#include <queue>
#include <string>

using namespace std;

typedef pair<int, int> pii;

int totalVolume;
int areaNumber;
stack<int> areaVolume;

void printResult() {
cout << "V = " <<totalVolume << ", n = " << areaNumber << endl;
int i=1;
while (!areaVolume.empty()) {
cout << "v" << i << " = " << areaVolume.top() << endl;
areaVolume.pop();
i++;
}
cout << endl;
}

void calcTotalVolume(stack<int>& Slopes, queue<int>& Steeps, stack<pii>&


Checkpoints) {
while (!Slopes.empty() && !Steeps.empty()) {
int startPoint = Slopes.top(); Slopes.pop();
int endPoint = Steeps.front(); Steeps.pop();

Checkpoints.push({startPoint, endPoint});

totalVolume += (endPoint - startPoint);


}

/*If there's no slopes, steeps become redundant. So, we just clear them*/
if (Slopes.empty()) {
while (!Steeps.empty()) Steeps.pop();
}
}

void calcAreaVolume(stack<pii>& Checkpoints) {


if (Checkpoints.empty()) return;

int tempVolume = 0;
int pos = Checkpoints.top().first;

while (!Checkpoints.empty()) {
int startPoint = Checkpoints.top().first;
int endPoint = Checkpoints.top().second;
Checkpoints.pop();

if (startPoint < pos) {


areaNumber++;
areaVolume.push(tempVolume);
tempVolume = 0;
pos = startPoint;
}
tempVolume += (endPoint - startPoint);

if (Checkpoints.empty()) {
areaVolume.push(tempVolume);
areaNumber++;
}
}
}

int main() {
/*Input & check for valid length*/
string s;
getline(cin, s);
if (s.length() < 2) {
printResult();
return 0;
}

/*Initialize containers for slope('\'), steep('/') and pairs of checkpoints*/


stack<int> Slopes;
queue<int> Steeps;
stack<pii> Checkpoints;

/*Start algorithm*/
bool hadSlope = false;
bool hadSteep = false;
for (size_t i = 0; i < s.length(); i++) {
if (s[i] == '/') {
hadSteep = true;
if (hadSlope) {
Steeps.push(i);
}
}
if (s[i] == '\\') {
if (hadSteep) {
calcTotalVolume(Slopes, Steeps, Checkpoints);
hadSteep = false;
}
hadSlope = true;
Slopes.push(i);
}
if (i + 1 == s.length()) {
calcTotalVolume(Slopes, Steeps, Checkpoints);
}
}
calcAreaVolume(Checkpoints);

/*Print result*/
printResult();
return 0;
}

You might also like