Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/issue#28 c06 exercises #35

Open
wants to merge 9 commits into
base: develop
Choose a base branch
from
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
12 changes: 11 additions & 1 deletion src/capitulo_06/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,14 @@ project(capitulo_06)
add_subdirectory(tente_isto/t02)
add_subdirectory(tente_isto/t03)

add_subdirectory(pratica/p01)
add_subdirectory(pratica/p01)

add_subdirectory(exercicios/e02)
add_subdirectory(exercicios/e03)
add_subdirectory(exercicios/e04)
add_subdirectory(exercicios/e05)
add_subdirectory(exercicios/e06)
add_subdirectory(exercicios/e07)
add_subdirectory(exercicios/e08)
add_subdirectory(exercicios/e09)
add_subdirectory(exercicios/e10)
14 changes: 14 additions & 0 deletions src/capitulo_06/exercicios/e02/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
cmake_minimum_required(VERSION 3.12.0)

set(thisProject c06e02)

project(${thisProject})

add_executable(${thisProject})

target_include_directories(${thisProject} PRIVATE
${lib_path})

target_sources(${thisProject} PRIVATE
${thisProject}.cpp
${lib_file})
3 changes: 3 additions & 0 deletions src/capitulo_06/exercicios/e02/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Exercícios 2

Acrescente a capacidade de usar **{ }** e **( )** no programa, de modo que **{(4+5)\*6} / (3+4)** seja uma expressão válida.
203 changes: 203 additions & 0 deletions src/capitulo_06/exercicios/e02/c06e02.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
#include "std_lib_facilities.h"

//------------------------------------------------------------------------------

class Token{
public:
char kind; // what kind of token
double value; // for numbers: a value
Token(char ch) // make a Token from a char
:kind(ch), value(0) { }
Token(char ch, double val) // make a Token from a char and a double
:kind(ch), value(val) { }
};

//------------------------------------------------------------------------------

class Token_stream {
public:
Token_stream(); // make a Token_stream that reads from cin
Token get(); // get a Token (get() is defined elsewhere)
void putback(Token t); // put a Token back
private:
bool full; // is there a Token in the buffer?
Token buffer; // here is where we keep a Token put back using putback()
};

//------------------------------------------------------------------------------

// The constructor just sets full to indicate that the buffer is empty:
Token_stream::Token_stream()
:full(false), buffer(0) // no Token in buffer
{
}

//------------------------------------------------------------------------------

// The putback() member function puts its argument back into the Token_stream's buffer:
void Token_stream::putback(Token t)
{
if (full) error("putback() into a full buffer");
buffer = t; // copy t to buffer
full = true; // buffer is now full
}

//------------------------------------------------------------------------------

Token Token_stream::get()
{
if (full) { // do we already have a Token ready?
// remove token from buffer
full = false;
return buffer;
}

char ch;
cin >> ch; // note that >> skips whitespace (space, newline, tab, etc.)

switch (ch) {
case '=': // for "print"
case 'x': // for "quit"
case '(': case ')': case '+': case '-': case '*': case '/': case '{': case '}':
return Token(ch); // let each character represent itself
case '.':
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
{
cin.putback(ch); // put digit back into the input stream
double val;
cin >> val; // read a floating-point number
return Token('8', val); // let '8' represent "a number"
}
default:
error("Bad token");
}
}

//------------------------------------------------------------------------------

Token_stream ts; // provides get() and putback()

//------------------------------------------------------------------------------

double expression(); // declaration so that primary() can call expression()

//------------------------------------------------------------------------------

// deal with numbers and parentheses
double primary()
{
Token t = ts.get();
switch (t.kind) {
case '(': // handle '(' expression ')'
{
double d = expression();
t = ts.get();
if (t.kind != ')') error("')' expected");
return d;
}
case '{':
{
double d = expression();
t = ts.get();
if (t.kind != '}') error("'}' expected");
return d;
}
case '8': // we use '8' to represent a number
return t.value; // return the number's value
default:
error("primary expected");
}
}

//------------------------------------------------------------------------------

// deal with *, /, and %
double term()
{
double left = primary();
Token t = ts.get(); // get the next token from token stream

while (true) {
switch (t.kind) {
case '*':
left *= primary();
t = ts.get();
break;
case '/':
{
double d = primary();
if (d == 0) error("divide by zero");
left /= d;
t = ts.get();
break;
}
default:
ts.putback(t); // put t back into the token stream
return left;
}
}
}

//------------------------------------------------------------------------------

// deal with + and -
double expression()
{
double left = term(); // read and evaluate a Term
Token t = ts.get(); // get the next token from token stream

while (true) {
switch (t.kind) {
case '+':
left += term(); // evaluate Term and add
t = ts.get();
break;
case '-':
left -= term(); // evaluate Term and subtract
t = ts.get();
break;
default:
ts.putback(t); // put t back into the token stream
return left; // finally: no more + or -: return the answer
}
}
}

//------------------------------------------------------------------------------

int main()
try
{
double val = 0;

cout << "Welcome to our simple calculator." << endl
<< "Please, enter an expression with point float numbers." << endl;

cout << "Available operators: '+', '-', '*' and '/'" << endl;
cout << "To see the result, type '='. To exit, type 'x'" << endl;

while (cin) {
Token t = ts.get();

if (t.kind == 'x') break; // 'x' for quit
if (t.kind == '=') // '=' for "print now"
cout << "=" << val << '\n';
else
ts.putback(t);
val = expression();
}
keep_window_open();
}
catch (exception& e) {
cerr << "error: " << e.what() << '\n';
keep_window_open();
return 1;
}
catch (...) {
cerr << "Oops: unknown exception!\n";
keep_window_open();
return 2;
}

//------------------------------------------------------------------------------
14 changes: 14 additions & 0 deletions src/capitulo_06/exercicios/e03/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
cmake_minimum_required(VERSION 3.12.0)

set(thisProject c06e03)

project(${thisProject})

add_executable(${thisProject})

target_include_directories(${thisProject} PRIVATE
${lib_path})

target_sources(${thisProject} PRIVATE
${thisProject}.cpp
${lib_file})
3 changes: 3 additions & 0 deletions src/capitulo_06/exercicios/e03/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Exercícios 3

Acrescente um operador de fatorial: use um operador de sufixo ! para representar "fatorial". Por exemplo, a expressão **7!** significa **7\*6\*5\*4\*3\*2\*1**. Faça com que ! tenha prioridade maior que * e /; isto é **7\*8!** significa **7\*(8!)**, em vez de **(7\*8)!**. Inicie pela modificação da gramática para considerar um operador de mais alto nível. Para concordar com a definição matemática padrão de fatorial, o **0!** é avaliado como **1**. Dica: As funções de calculadora lidam com **doubles**, mas o fatorial é definido somente para **int**s, então somente para **x !** atribua o **x** para um **int** e calcule o fatorial desse **int**.
Loading