This start file already contains a struct for the name/value pairs, an array, an
ID: 3700164 • Letter: T
Question
This start file already contains a struct for the name/value pairs, an array, and two functions, one of which (parse()) is almost already complete.
Complete the following:
Add code to the parse function, which will populate the name_value_pairs array.
Complete the function 'param()' based on the prototype, calls, and definition, which are already in place. The function 'param()'s job is to match the field name passed in its parameter to the corresponding array struct element. Once the appropriate structure is matched, the value for that field can be returned.
Return "" (an empty string) if the element for that name is not found in the array - (no match from the html form).
Use your own fields by modifying the html code appropriately (first, last, and color are not required, just examples) .
As a final touch, return the data received back to the browser in some creative way. For example the color chosen by the user could be used to change the background color of the page. Again, this is only an example - be creative.
Explanation / Answer
// File: retrieve_form_start.cpp
// Author:
// cs102 Online
// Date:
// Description: This program retrieves three form fields and sends the result
// back to the browser
#include <iomanip>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <string>
using namespace std;
struct FIELDS
{
string name;
string value;
};
const int cnt = 3; //cnt should be set to the number of fields the html form contains
// Prototypes
void parse(string, FIELDS []);
string param(string, FIELDS [], int);
//main begins
int main()
{
FIELDS name_value_pairs [cnt];
string qs(getenv("QUERY_STRING"));
//string qs("first=fred&last=flint&color=red");
cout << "Content-type:text/html ";
cout << "debug with qs: " << qs << "<p>" << endl;
parse(qs, name_value_pairs);
// debug to show content of name_value_pairs
cout << "debug to show content of name_value_pairs array: " << endl << "<br>";
for (int index = 0; index<cnt; index++) {
cout << "name: " << name_value_pairs[index].name << " ";
cout << "value: " << name_value_pairs[index].value << endl << "<br>";
}
/*
// Three fields data are retrieved from the param function
string first = param("first", name_value_pairs, cnt);
string last = param("last", name_value_pairs, cnt);
string color = param("color", name_value_pairs, cnt);
*/
// code an HTML page, which includes the three fields
// received.
return 0;
}
//*******************************************
//******** Functions begin ******************
//*******************************************
//*******************************************
// parse()
// This will separate the name/value pairs found after the ? in
// the URL and populate the name_value_pairs array of structures
//*******************************************
void parse (string qs, FIELDS f_name_value_pairs [])
{
cout << "debug in parse<br> " << endl;
string name, value;
int start_pos = 0, pos;
for (int counter=0; counter < cnt; counter++) {
pos = qs.find("=", start_pos);
name = qs.substr(start_pos, pos - start_pos);
cout << "name: " << name << "<br>" << endl;
start_pos = pos + 1;
pos = qs.find("&", start_pos);
if (pos == string::npos) {
pos = qs.length();
}
value = qs.substr(start_pos, pos - start_pos);
cout << "value: " << value << "<br>" << endl;
start_pos = pos + 1;
}
}
//*******************************************
// param()
// This will find the field value based on the
// field name
//*******************************************
string param(string lookUp, FIELDS f_name_value_pairs[], int f_cnt)
{
}
#include <bits/stdc++.h>
#ifdef PRINTERS
#include "printers.hpp"
using namespace printers;
#define tr(a) cerr<<#a<<" : "<<a<<endl;
#else
#define tr(a)
#endif
#define ll long long
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define vi vector<int>
#define all(a) (a).begin(),(a).end()
#define F first
#define S second
#define sz(x) (int)x.size()
#define hell 1000000007
#define endl ' '
#define rep(i,a,b) for(int i=a;i<b;i++)
using namespace std;
vector<pii>adj[1005];
int dp[1005][2000];
void solve(){
memset(dp,-1,sizeof dp);
int N,M,K;
cin>>N>>M>>K;
vi p(N+1);
rep(i,1,N+1){
int k;
cin>>k;
while(k--){
int temp;
cin>>temp;
temp--;
p[i]|=(1<<temp);
}
}
priority_queue<pair<int,pair<int,int>>,vector<pair<int,pair<int,int>>>,greater<pair<int,pair<int,int>>>>pq;
rep(i,0,M){
int a,b,c;
cin>>a>>b>>c;
adj[a].pb(mp(b,c));
adj[b].pb(mp(a,c));
}
pq.push(mp(0,mp(1,p[1])));
while(!pq.empty()){
auto cur=pq.top();
pq.pop();
if(dp[cur.S.F][cur.S.S]>=0)continue;
dp[cur.S.F][cur.S.S]=cur.F;
for(auto i:adj[cur.S.F]){
pq.push(mp(i.S+cur.F,mp(i.F,cur.S.S|p[i.F])));
}
}
int ans=INT_MAX;
rep(i,0,1<<K){
rep(j,0,1<<K){
if((i|j)==(1<<K)-1 and dp[N][i]!=-1 and dp[N][j]!=-1)
ans=min(max(dp[N][i],dp[N][j]),ans);
}
}
cout<<ans;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t=1;
// cin>>t;
while(t--){
solve();
}
return 0;
}S
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.