#include <bits/stdc++.h>
#include <thread>
#include <mutex>
using namespace std;
mutex vote_lock;
condition_variable cv;
int turn= 0 ;
class Poll{
public :
string PollId;
string question;
vector< string> options;
map< string,vector< pair< string,string>>> votes;
map< string,int > results;
Poll( string PollId,string question,vector< string> options) {
this- > PollId= PollId;
this- > question= question;
this- > options= options;
votes.clear ( ) ;
results.clear ( ) ;
}
Poll( ) = default ;
} ;
unordered_map< string,Poll> polls;
void createPoll( string question, vector< string> options) {
int sz = polls.size ( ) + 1 ;
string pollId = "poll" + to_string( sz) ;
Poll poll = Poll( pollId,question,options) ;
polls[ pollId] = poll;
cout << "Poll created successfully" << '\n ' ;
}
void updatePoll( string pollId, string question, vector< string> options) {
Poll * poll = & polls[ pollId] ;
( * poll) .options = options;
( * poll) .question = question;
cout << "Poll updates successfully" << '\n ' ;
}
void deletePoll( string pollId) {
polls.erase ( pollId) ;
}
void voteInPoll( string pollId, string userId, string option) {
lock_guard< mutex> lock( vote_lock) ;
Poll * poll = & polls[ pollId] ;
( * poll) .votes [ option] .push_back ( { userId,option} ) ;
string optionIndex = "" ;
optionIndex + = option[ 6 ] ;
// cout<<"hi "<<optionIndex<<'\n';
string val = ( * poll) .options [ stoi( optionIndex) - 1 ] ;
cout << val<< '\n ' ;
( * poll) .results [ val] ++ ;
cout << "Vote cast successfully" << '\n ' ;
turn++ ;
if ( turn== 3 ) {
cv.notify_all ( ) ;
}
}
void viewPollResults( string pollId) {
unique_lock< mutex> lock( vote_lock) ;
while ( turn! = 3 ) {
cv.wait ( lock) ;
}
Poll * poll = & polls[ pollId] ;
cout << "PollId: " << ( * poll) .PollId << '\n ' ;
cout << "Question: " << ( * poll) .question << '\n ' ;
cout << "Results: " << '\n ' ;
for ( auto it: ( * poll) .options ) {
cout << it<< ": " << ( * poll) .results [ it] << '\n ' ;
}
}
int main( ) {
createPoll( "What is your favorite color?" , { "Red" , "Blue" , "Green" , "Yellow" } ) ;
thread t1( voteInPoll,"poll1" , "user1" , "Option1" ) ;
thread t2( voteInPoll,"poll1" , "user2" , "Option2" ) ;
thread t3( voteInPoll,"poll1" , "user3" , "Option1" ) ;
viewPollResults( "poll1" ) ;
t1.join ( ) ;
t2.join ( ) ;
t3.join ( ) ;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNpbmNsdWRlIDx0aHJlYWQ+CiNpbmNsdWRlIDxtdXRleD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKbXV0ZXggdm90ZV9sb2NrOwpjb25kaXRpb25fdmFyaWFibGUgY3Y7CmludCB0dXJuPTA7CmNsYXNzIFBvbGx7CnB1YmxpYzoKICAgIHN0cmluZyBQb2xsSWQ7CiAgICBzdHJpbmcgcXVlc3Rpb247CiAgICB2ZWN0b3I8c3RyaW5nPm9wdGlvbnM7CiAgICBtYXA8c3RyaW5nLHZlY3RvcjxwYWlyPHN0cmluZyxzdHJpbmc+Pj52b3RlczsKICAgIG1hcDxzdHJpbmcsaW50PnJlc3VsdHM7CiAgICBQb2xsKHN0cmluZyBQb2xsSWQsc3RyaW5nIHF1ZXN0aW9uLHZlY3RvcjxzdHJpbmc+b3B0aW9ucyl7CiAgICAgICAgdGhpcy0+UG9sbElkPVBvbGxJZDsKICAgICAgICB0aGlzLT5xdWVzdGlvbj1xdWVzdGlvbjsKICAgICAgICB0aGlzLT5vcHRpb25zPW9wdGlvbnM7CiAgICAgICAgdm90ZXMuY2xlYXIoKTsKICAgICAgICByZXN1bHRzLmNsZWFyKCk7CiAgICB9CiAgICBQb2xsKCk9ZGVmYXVsdDsKfTsKdW5vcmRlcmVkX21hcDxzdHJpbmcsUG9sbD5wb2xsczsKdm9pZCBjcmVhdGVQb2xsKHN0cmluZyBxdWVzdGlvbiwgdmVjdG9yPHN0cmluZz5vcHRpb25zKXsKICAgIGludCBzeiA9IHBvbGxzLnNpemUoKSsxOwogICAgc3RyaW5nIHBvbGxJZCA9ICJwb2xsIit0b19zdHJpbmcoc3opOwogICAgUG9sbCBwb2xsID0gUG9sbChwb2x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