Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 100 additions & 0 deletions homework/82070/cpp2html/cpp2html.flex
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/* scanner for a C++ language */

%{
#define YY_NO_UNISTD_H
%}

DIGIT [0-9]
ID [a-zA-Z_$][a-zA-Z0-9_$]*
CHARSEQ '([^\']|(\\\'))*'
STR \"([^\"]|(\\\"))*\"
COMMENT \/\/.*\n|\/\*.*\*\/

%%

{DIGIT}+ {
printf("<span class=\"number\">%s</span>", yytext);
}

{DIGIT}+"."{DIGIT}* {
printf("<span class=\"number\">%s</span>", yytext);
}

alignas|alignof|and|and_eq|asm|atomic_cancel|atomic_commit|atomic_noexcept|auto|bitand|bitor|bool|break|case|catch|char|char8_t|char16_t|char32_t|class|compl|concept|const|consteval|constexpr|constinit|const_cast|continue|co_await|co_return|co_yield|decltype|default|delete|do|double|dynamic_cast|else|enum|explicit|export|extern|false|float|for|friend|goto|if|inline|int|long|mutable|namespace|new|noexcept|not|not_eq|nullptr|operator|or|or_eq|private|protected|public|reflexpr|register|reinterpret_cast|requires|return|short|signed|sizeof|static|static_assert|static_cast|struct|switch|synchronized|template|this|thread_local|throw|true|try|typedef|typeid|typename|union|unsigned|using|virtual|void|volatile|wchar_t|while|xor|xor_eq {
printf("<span class=\"keyword\">%s</span>", yytext);
}

#if|#elif|#else|#endif|#ifdef|#ifndef|#elifdef|#elifndef|#define|#undef|#include|#line|#error|#warning|#pragma|#defined|#__has_include|#__has_cpp_attribute|#export|#import|#module {
printf("<span class=\"preprocesor\">%s</span>", yytext);
}

{COMMENT} printf("<span class=\"comment\">%s</span>", yytext);

"<"{ID}">"|"<"{ID}"."[a-zA-Z0-9_$]*">" printf("<span class=\"inclusion\">&lt;%s</span>", yytext+1);

{ID} printf("<span class=\"identifier\">%s</span>", yytext);

{CHARSEQ}|{STR} printf("<span class=\"string\">%s</span>", yytext);

"."|"+"|"-"|"*"|"/"|"="|"!"|">"|"<"|"!="|"=="|">="|"<="|"++"|"--"|"%"|"+="|"-="|"*="|"/="|"%="|">>="|"<<="|"&="|"^="|"|="|"&&"|"||"|"&"|"|"|"^"|"~"|"<<"|">>" printf("<span class=\"operator\">%s</span>", yytext);

[;(){}:? \t\n,\[\]]+ printf("%s", yytext); /* echo the rest */

. printf( "Unrecognized character: %s\n", yytext );

%%

int yywrap()
{
return 1;
}


int main(int argc, const char* argv[])
{
++argv, --argc; /* skip over program name */
if ( argc > 0 )
yyin = fopen( argv[0], "r" );
else
yyin = stdin;

puts(
"<!doctype>"
"<html>"
"<head>"
" <title>");
puts(argv[0]);
puts("</title>"
" <style>"
" .keyword {"
" color: blue;"
" }"
" .number {"
" color: red;"
" }"
" .string {"
" color: orange;"
" }"
" .operator {"
" font-weight: 900;"
" }"
" .preprocesor {"
" color: purple;"
" }"
" .comment {"
" color: green;"
" }"
" .inclusion {"
" color: teal;"
" }"
" </style>"
"</head>"
"<body>"
" <pre class=\"code\">"
);
yylex();
puts("</pre></body></html>");
if ( argc > 0 )
fclose(yyin);
return 0;
}
79 changes: 79 additions & 0 deletions homework/82070/cpp2html/exampleInput.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#include <iostream>
#include <cmath>
using namespace std;

struct point {
double x;
double y;
void set_point(double, double);
void print();
};

int main() {
const int maxPoints = 32;
int counterPoints = 0, counterTriangle = 0, counterPokeball = 0, outside;
point arrPoints[maxPoints];
for (int i = 0; i < maxPoints; ++i) {
double x, y;
cin >> x >> y;
if (cin.good()) {
arrPoints[i].set_point(x, y);
counterPoints++;
}
else {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
break;
}
}
for (int i = 0; i < counterPoints; ++i) {
bool insideTriangle = false, insidePokeball = false;
point triangleVertex, lineCrossOx, lineCrossOy, pokeballCenter;
triangleVertex.set_point(2, 2);
lineCrossOx.set_point(6, 0);
lineCrossOy.set_point(0, 6);
pokeballCenter.set_point(-9, 0);
if (arrPoints[i].x >= triangleVertex.x
&& arrPoints[i].y >= triangleVertex.y
&& arrPoints[i].x * lineCrossOy.y + arrPoints[i].y * lineCrossOx.x <= lineCrossOx.x * lineCrossOy.y) {
insideTriangle = true;
counterTriangle++;
}
else {
const double smallCircleR = 1, largeCircleR = 3;
double distanceX = abs(arrPoints[i].x - pokeballCenter.x);
double distanceY = abs(arrPoints[i].y - pokeballCenter.y);
double distanceFromPokeballCenter = sqrt(distanceX * distanceX + distanceY * distanceY);
if (arrPoints[i].x >= pokeballCenter.x - largeCircleR
&& arrPoints[i].x <= pokeballCenter.x + largeCircleR
&& arrPoints[i].y >= pokeballCenter.y
&& arrPoints[i].y <= pokeballCenter.y + largeCircleR
&& distanceFromPokeballCenter >= smallCircleR && distanceFromPokeballCenter <= largeCircleR) {
insidePokeball = true;
counterPokeball++;
}
}
arrPoints[i].print();
if (insideTriangle) {
cout << "inside, " << i + 1 << " in the list of points (It is inside the triangle)." << endl;
}
else if (insidePokeball) {
cout << "inside, " << i + 1 << " in the list of points (It is inside the pokeball)." << endl;
}
else {
cout << "outside, " << i + 1 << " in the list of points" << endl;
}
}
outside = counterPoints - (counterTriangle + counterPokeball);
cout << counterTriangle << " points inside triangle, " << counterPokeball << " points inside pokeball, " << outside << " points outside out of " << counterPoints << " points." << endl;
return 0;
}

void point::set_point(double x, double y) {
this->x = x;
this->y = y;
}

void point::print() {
cout << "<" << this->x << ", " << this->y << ">: ";
}
Loading