Answer :
To implement a simplified version of a crypto broker platform. Use #include<bits/stdc++.h>
using namespace std;
//This is cryptoTrading function
vector<int> cryptoTrading(vector<int> deposits, vector<string> operations) {
//res vector will store the final result
vector<int>res;
//This unordered map value will store the current deposit of each person
unordered_map<int,int>value;
//This unordered map value will store the current number of stock of each person
unordered_map<int,int>stock;
//First traverse the deposits vector
for(int i=0;i<deposits.size();i++){
//Here,person number which is (i+1) is key, and it's corresponding deposit is value
value[(i+1)]=deposits[i];
}
//Now start traversing the operations vector
for(int i=0;i<operations.size();i++){
//task string will store the operation to perform, initially it is an empty string
string task="";
//index variable will keep track the spaces in sentence
int index=-1;
//Now start traverse the current operation sentence
for(int j=0;j<operations[i].length();j++){
//It will stop when a space is found
if(operations[i][j]==' '){
//And store the next index of that space in index variable
index=j+1;
break;
}
//Otherwise concatenate the character with task
task=task+operations[i][j];
}
//If task is "buy" then
if(task=="buy"){
//person variable will store the person number
int person=0;
//Again traverse the operation sentence, from index
for(int j=index;j<operations[i].length();j++){
//It will stop when a space is found
if(operations[i][j]==' '){
//store the next index of that space in index variable
index=j+1;
break;
}
//Otherwise add numbers with person variable
person=(person*10)+(operations[i][j]-'0');
}
//num will store the number of stock to buy
}
//Otherwise add numbers with num variable
num=(num*10)+(operations[i][j]-'0');
}
//val will store the value of that stock
int val=0;
//Repeat the same operations
for(int j=index;j<operations[i].length();j++){
if(operations[i][j]==' '){
break;
}
//add numbers with val
val=(val*10)+(operations[i][j]-'0');
}
//if that person's current deposit is less than (num*val) then
if(value[person]<(num*val)){
//push that person's current deposit in res vector
res.push_back(value[person]);
}
//Otherwise
else if(value[person]>=(num*val)){
//subtract (num*val) from that person's current deposit
value[person]=value[person]-(num*val);
//And add num with person's current stock
stock[person]=stock[person]+num;
//push that person's current deposit in res vector
res.push_back(value[person]);
}
}
//If task is "sell" then
else if(task=="sell"){
//Repeat the same process to find person's number
int person=0;
for(int j=index;j<operations[i].length();j++){
if(operations[i][j]==' '){
index=j+1;
break;
}
person=(person*10)+(operations[i][j]-'0');
}
//Repeat the same process to find number of stocks
int num=0;
for(int j=index;j<operations[i].length();j++){
if(operations[i][j]==' '){
index=j+1;
break;
}
num=(num*10)+(operations[i][j]-'0');
}
//Repeat the same process to find value of stock
int val=0;
for(int j=index;j<operations[i].length();j++){
if(operations[i][j]==' '){
break;
}
val=(val*10)+(operations[i][j]-'0');
}
//if that person's current stock is less than num then
if(stock[person]<num){
//push that person's current deposit in res vector
res.push_back(value[person]);
}
//Otherwise
else if(stock[person]>=num){
//subtract num from that person's current stock
stock[person]=stock[person]-num;
//And add (num*val) with person's current deposit
value[person]=value[person]+(num*val);
//push that person's current deposit in res vector
res.push_back(value[person]);
}
}
//If task is "deposit" then
else if(task=="deposit"){
//Repeat the same process to find person's number
int person=0;
for(int j=index;j<operations[i].length();j++){
if(operations[i][j]==' '){
index=j+1;
break;
}
person=(person*10)+(operations[i][j]-'0');
}
//Repeat the same process to find value to deposit
int val=0;
for(int j=index;j<operations[i].length();j++){
if(operations[i][j]==' '){
break;
}
//At the end return res vector
return res;
}
//This is main function
int main()
{
//Do a sample run of above method
vector<int>deposits{9,7,12};
vector<string>operations{"buy 1 3 2","sell 1 4 10","deposit 2 12","buy 2 10 2","buy 2 6 3"};
vector<int>res=cryptoTrading(deposits,operations);
//Display the result
cout<<"Result: ";
for(int i=0;i<res.size();i++){
cout<<res[i]<<" ";
}
return 0;
}
To learn more about crypto
https://brainly.com/question/15084188
#SPJ4
