CODE IS IN C++ Please use code attached and update areas with \"classes\" and on
ID: 3744835 • Letter: C
Question
CODE IS IN C++
Please use code attached and update areas with "classes" and only use arrays please. Right after the first if statement inside the bool has the first class and they're called throughout the program. These need to be updated to arrays or strings or char. Thank you!
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
const char ENDOFBEAT = '/';
bool Syntax(string song)
{
if (song.size() == 0)
return true;
if (song.at(song.size() - 1) != ENDOFBEAT)
return false;
size_t k = 0;
while (k != song.size())
{
if (song.at(k) == ENDOFBEAT)
{
k++;
continue;
}
if (isdigit(song.at(k)))
{
k++;
if (isdigit(song.at(k)))
k++;
}
char color = tolower(song.at(k));
if (color != 'g' && color != 'r' && color != 'y' &&
color != 'b' && color != 'o')
return false;
k++;
if (song.at(k) != ENDOFBEAT)
return false;
k++;
}
return true;
}
int tSong(string song, string& instructions, int& badBeat)
{
const int RETOK = 0;
const int RETNOTWELLFORMED = 1;
const int RETSUSTAINEDINTERRUPTED = 2;
const int RETPREMATUREEND = 3;
const int RETBADSUSTAINEDLENGTH = 4;
if (!Syntax(song))
return RETNOTWELLFORMED;
string result;
int sustainedRemaining = 0;
char sustainedColor;
int beatNumber;
size_t k = 0;
for (beatNumber = 1; k != song.size(); beatNumber++)
{
if (song.at(k) == ENDOFBEAT)
{
if (sustainedRemaining > 0)
{
// Continue with sustained note
result += sustainedColor;
sustainedRemaining--;
}
else
result += 'x';
k++;
continue;
}
if (sustainedRemaining > 0)
{
badBeat = beatNumber;
return RETSUSTAINEDINTERRUPTED;
}
if (isdigit(song.at(k)))
{
sustainedRemaining = song.at(k) - '0';
k++;
if (isdigit(song.at(k)))
{
sustainedRemaining = 10 * sustainedRemaining + song.at(k) - '0';
k++;
}
if (sustainedRemaining < 2)
{
badBeat = beatNumber;
return RETBADSUSTAINEDLENGTH;
}
sustainedColor = toupper(song.at(k));
result += sustainedColor;
sustainedRemaining--;
}
else
{
result += tolower(song.at(k));
}
k += 2;
}
if (sustainedRemaining > 0)
{
badBeat = beatNumber;
return RETPREMATUREEND;
}
instructions = result;
return RETOK;
}
Explanation / Answer
#include <iostream>
#include <string.h>
#include<cstdlib>
#include <sstream>
using namespace std;
const char ENDOFBEAT = '/';
//updating all area's of strings with arrays
bool Syntax(char song[])//changing to array
{
//finding length of the string ........ which is in the form of array
int size = strlen(song);
if (size == 0)//if size is zero
return true;
if (song[size - 1] != ENDOFBEAT)
return false;
size_t k = 0;
while (k != size)
{
if (song[k] == ENDOFBEAT)
{
k++;
continue;
}
if (isdigit(song[k]))
{
k++;
if (isdigit(song[k]))
k++;
}
char color = tolower(song[k]);
if (color != 'g' && color != 'r' && color != 'y' &&
color != 'b' && color != 'o')
return false;
k++;
if (song[k] != ENDOFBEAT)
return false;
k++;
}
return true;
}
int tSong(char song[], char instructions[], int& badBeat)
{
const int RETOK = 0;
const int RETNOTWELLFORMED = 1;
const int RETSUSTAINEDINTERRUPTED = 2;
const int RETPREMATUREEND = 3;
const int RETBADSUSTAINEDLENGTH = 4;
if (!Syntax(song))
return RETNOTWELLFORMED;
char result[1000];//converted to array
int r=0;
int sustainedRemaining = 0;
char sustainedColor;
int beatNumber;
size_t k = 0;
int size = strlen(song);
for (beatNumber = 1; k != size; beatNumber++)
{
if (song[k] == ENDOFBEAT)
{
if (sustainedRemaining > 0)
{
// Continue with sustained note
result[r++] = sustainedColor;
sustainedRemaining--;
}
else
result[r++]= 'x';
k++;
continue;
}
if (sustainedRemaining > 0)
{
badBeat = beatNumber;
return RETSUSTAINEDINTERRUPTED;
}
if (isdigit(song[k]))
{
sustainedRemaining = song[k] - '0';
k++;
if (isdigit(song[k]))
{
sustainedRemaining = 10 * sustainedRemaining + song[k] - '0';
k++;
}
if (sustainedRemaining < 2)
{
badBeat = beatNumber;
return RETBADSUSTAINEDLENGTH;
}
sustainedColor = toupper(song[k]);
result[r++]= sustainedColor;
sustainedRemaining--;
}
else
{
result[r++]= tolower(song[k]);
}
k += 2;
}
if (sustainedRemaining > 0)
{
badBeat = beatNumber;
return RETPREMATUREEND;
}
// instructions = result;
//copying result to instructions....
strcpy(instructions,result);
return RETOK;
}
int main()
{
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.