You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// For this challenge you will be parsing a command line string and counting its characters.
2
+
/*
3
+
have the function CommandLine(str) take the str parameter being passed which represents the parameters given to a command in an old PDP system. The parameters are alphanumeric tokens (without spaces) followed by an equal sign and by their corresponding value. Multiple parameters/value pairs can be placed on the command line with a single space between each pair. Parameter tokens and values cannot contain equal signs but values can contain spaces. The purpose of the function is to isolate the parameters and values to return a list of parameter and value lengths. It must provide its result in the same format and in the same order by replacing each entry (tokens and values) by its corresponding length.
4
+
5
+
For example, if str is: "SampleNumber=3234 provider=Dr. M. Welby patient=John Smith priority=High" then your function should return the string "12=4 8=12 7=10 8=4" because "SampleNumber" is a 12 character token with a 4 character value ("3234") followed by "provider" which is an 8 character token followed by a 12 character value ("Dr. M. Welby"), etc.
6
+
*/
7
+
8
+
#include<iostream>
9
+
#include<string>
10
+
#include<sstream>
11
+
usingnamespacestd;
12
+
13
+
/*
14
+
One approach is to use the equal sings as a break point to split it into substrings
15
+
16
+
We can extract the first token by taking all characters prior to the first equal sign
17
+
We can extract the last value by taking all characters after the last equal sign
18
+
19
+
To collect the tokens and values in the middle we can reach a break point
20
+
check the characters prior that are not a space and those will result in our token
21
+
for the value we can subtract the current equal sign index by the start of the previous equal sign and the length of token
22
+
*/
23
+
24
+
// method to collect the length of both the token and the value
25
+
voidanalyzeToken(int previousBreak, int currentBreak, string str, string& result)
26
+
{
27
+
string temp;
28
+
stringstream convert;
29
+
30
+
// loop will backtrack from the breakpoint and collect the token
31
+
// if we reach a space than a the token has been finalized
32
+
for (int x = currentBreak-1; x >= 0; x--)
33
+
{
34
+
if (str[x] == '')
35
+
{
36
+
break;
37
+
}
38
+
else
39
+
{
40
+
temp.push_back(str[x]);
41
+
}
42
+
}
43
+
44
+
// get the length of the current token
45
+
int currentTokenLength = temp.length();
46
+
47
+
if (previousBreak != 0)
48
+
{
49
+
// get the length of the previous value
50
+
int previousValueLength = currentBreak - currentTokenLength - previousBreak -2;
51
+
52
+
// converting the value length to string and adding to our final result
// For this challenge you will be calculating how to display a 5 star rating.
2
+
/*
3
+
have the function StarRating(str) take the str parameter being passed which will be an average rating between 0.00 and 5.00, and convert this rating into a list of 5 image names to be displayed in a user interface to represent the rating as a list of stars and half stars. Ratings should be rounded up to the nearest half. There are 3 image file names available: "full.jpg", "half.jpg", "empty.jpg". The output will be the name of the 5 images (without the extension), from left to right, separated by spaces. For example: if str is "2.36" then this should be displayed by the following image:
4
+
5
+
So your program should return the string "full full half empty empty".
6
+
*/
7
+
8
+
#include<iostream>
9
+
#include<string>
10
+
#include<sstream>
11
+
usingnamespacestd;
12
+
13
+
14
+
/*
15
+
first we would convert the string value to a float
16
+
we can set a loop to continuously subtract 1 or its fractional amount from the total value
17
+
For instance if value originally is 1.45
18
+
we subtract by 1 in first iteration
19
+
than subtract by .45
20
+
21
+
*/
22
+
string StarRating(string str)
23
+
{
24
+
// converting the string
25
+
istringstream convert(str);
26
+
float value;
27
+
convert >> value;
28
+
29
+
string result="";
30
+
int starCount = 0;
31
+
32
+
// subtract by either 1 or a fraction of its value until we reach zero
33
+
while (starCount < 5)
34
+
{
35
+
// if greater than or equal to one this will result in a full star
36
+
if (value >= 1)
37
+
{
38
+
result += "full ";
39
+
40
+
// update our value
41
+
value -= 1;
42
+
}
43
+
elseif (value > 0)
44
+
{
45
+
if (value+.25 >= 1)
46
+
{
47
+
result += "full ";
48
+
}
49
+
elseif (value+.25 >= .5)
50
+
{
51
+
result += "half ";
52
+
}
53
+
else
54
+
{
55
+
result += "empty ";
56
+
}
57
+
58
+
value -= value;
59
+
}
60
+
else
61
+
{
62
+
// empty star
63
+
result += "empty ";
64
+
}
65
+
66
+
starCount++;
67
+
}
68
+
69
+
return result;
70
+
}
71
+
72
+
intmain()
73
+
{
74
+
cout << StarRating("2.36") << endl; // full full half empty empty
0 commit comments