Skip to content

Commit d072272

Browse files
author
Carlos Leonard
committed
update
1 parent 920ecb6 commit d072272

12 files changed

+198
-8
lines changed

79_CommandLine.cpp

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
// 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+
using namespace std;
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+
void analyzeToken(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
53+
convert << previousValueLength;
54+
result+=convert.str();
55+
result.push_back(' ');
56+
convert.str("");
57+
}
58+
59+
// converting the token length to a string
60+
convert << currentTokenLength;
61+
result+=convert.str();
62+
result.push_back('=');
63+
}
64+
65+
string CommandLine(string str)
66+
{
67+
string result="";
68+
69+
// setting the break points
70+
int breakPoint1 = str.find('=');
71+
int breakPoint2 = 0;
72+
73+
// first step to analyze the first token
74+
analyzeToken(breakPoint2, breakPoint1, str, result);
75+
76+
77+
// loop to continue to check the string base on the amount of tokens found
78+
while (str.find('=', breakPoint1 + 1) >= 0 && str.find('=', breakPoint1 + 1) <= str.length() - 1)
79+
{
80+
// updating the break points based on the current string
81+
breakPoint2 = breakPoint1;
82+
breakPoint1 = str.find('=',breakPoint1+1);
83+
84+
analyzeToken(breakPoint2, breakPoint1, str, result);
85+
}
86+
87+
// last step to analyze the last value if any are found
88+
string temp ="";
89+
for (int x = breakPoint1 + 1; x < str.length(); x++)
90+
{
91+
temp.push_back(str[x]);
92+
}
93+
94+
// getting the length and converting back to a string
95+
int valueLength = temp.length();
96+
stringstream convert;
97+
convert << valueLength;
98+
result+=convert.str();
99+
100+
return result;
101+
}
102+
103+
int main()
104+
{
105+
cout << CommandLine("SampleNumber=3234 provider=Dr. M. Welby patient=John Smith priority=High") << endl; // 12=4 8=12 7=10 8=4
106+
cout << CommandLine("letters=A B Z T numbers=1 2 26 20 combine=true") << endl; // 7=7 7=9 7=4
107+
cout << CommandLine("a=3 b=4 a=23 b=a 4 23 c=") << endl; // 1=1 1=1 1=2 1=6 1=0
108+
109+
return 0;
110+
}

80_StarRating.cpp

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// 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+
using namespace std;
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+
else if (value > 0)
44+
{
45+
if (value+.25 >= 1)
46+
{
47+
result += "full ";
48+
}
49+
else if (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+
int main()
73+
{
74+
cout << StarRating("2.36") << endl; // full full half empty empty
75+
cout << StarRating("0.38") << endl; // half empty empty empty empty
76+
cout << StarRating("4.5") << endl; // full full full full half
77+
cout << StarRating("3.02") << endl; // full full full empty empty
78+
cout << StarRating("2.75") << endl; // full full full empty empty
79+
80+
return 0;
81+
}

Debug/Fun Practice.log

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
Build started 8/29/2017 3:48:41 PM.
1+
Build started 11/14/2017 4:45:06 PM.
22
1>Project "C:\Users\gutty333\Documents\Visual Studio 2013\Projects\Fun Practice\Fun Practice\Fun Practice.vcxproj" on node 2 (Build target(s)).
33
1>ClCompile:
4-
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\CL.exe /c /ZI /nologo /W3 /WX- /sdl /Od /Oy- /D WIN32 /D _DEBUG /D _CONSOLE /D _LIB /D _UNICODE /D UNICODE /Gm /EHsc /RTC1 /MDd /GS /fp:precise /Zc:wchar_t /Zc:forScope /Fo"Debug\\" /Fd"Debug\vc120.pdb" /Gd /TP /analyze- /errorReport:prompt 78_RemoveBrackets.cpp
5-
78_RemoveBrackets.cpp
6-
1>c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\78_removebrackets.cpp(24): warning C4018: '<' : signed/unsigned mismatch
4+
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\CL.exe /c /ZI /nologo /W3 /WX- /sdl /Od /Oy- /D WIN32 /D _DEBUG /D _CONSOLE /D _LIB /D _UNICODE /D UNICODE /Gm /EHsc /RTC1 /MDd /GS /fp:precise /Zc:wchar_t /Zc:forScope /Fo"Debug\\" /Fd"Debug\vc120.pdb" /Gd /TP /analyze- /errorReport:prompt 80_StarRating.cpp
5+
80_StarRating.cpp
76
Link:
8-
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\link.exe /ERRORREPORT:PROMPT /OUT:"C:\Users\gutty333\Documents\Visual Studio 2013\Projects\Fun Practice\Debug\Fun Practice.exe" /INCREMENTAL /NOLOGO kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /MANIFEST /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /manifest:embed /DEBUG /PDB:"C:\Users\gutty333\Documents\Visual Studio 2013\Projects\Fun Practice\Debug\Fun Practice.pdb" /SUBSYSTEM:CONSOLE /TLBID:1 /DYNAMICBASE /NXCOMPAT /IMPLIB:"C:\Users\gutty333\Documents\Visual Studio 2013\Projects\Fun Practice\Debug\Fun Practice.lib" /MACHINE:X86 Debug\78_RemoveBrackets.obj
7+
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\link.exe /ERRORREPORT:PROMPT /OUT:"C:\Users\gutty333\Documents\Visual Studio 2013\Projects\Fun Practice\Debug\Fun Practice.exe" /INCREMENTAL /NOLOGO kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /MANIFEST /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /manifest:embed /DEBUG /PDB:"C:\Users\gutty333\Documents\Visual Studio 2013\Projects\Fun Practice\Debug\Fun Practice.pdb" /SUBSYSTEM:CONSOLE /TLBID:1 /DYNAMICBASE /NXCOMPAT /IMPLIB:"C:\Users\gutty333\Documents\Visual Studio 2013\Projects\Fun Practice\Debug\Fun Practice.lib" /MACHINE:X86 Debug\80_StarRating.obj
98
Fun Practice.vcxproj -> C:\Users\gutty333\Documents\Visual Studio 2013\Projects\Fun Practice\Debug\Fun Practice.exe
109
1>Done Building Project "C:\Users\gutty333\Documents\Visual Studio 2013\Projects\Fun Practice\Fun Practice\Fun Practice.vcxproj" (Build target(s)).
1110

1211
Build succeeded.
1312

14-
Time Elapsed 00:00:03.18
13+
Time Elapsed 00:00:00.98
24.6 KB
Binary file not shown.
2.8 KB
Binary file not shown.
1.63 KB
Binary file not shown.
1.44 KB
Binary file not shown.
-408 Bytes
Binary file not shown.
-8 Bytes
Binary file not shown.

Debug/vc120.idb

8 KB
Binary file not shown.

0 commit comments

Comments
 (0)