Skip to content

Commit 1170a5e

Browse files
committed
Added old code
Homework1, which was done about 3 weeks ago before commit
1 parent b3e0f01 commit 1170a5e

21 files changed

+1153
-0
lines changed

Homework1/1/Node.h

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include <iostream>
2+
3+
template <class T>
4+
struct Node{
5+
Node<T>* next;
6+
T inf;
7+
};

Homework1/1/NotSTL.cpp

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#include "NotSTL.h"
2+
3+
int getLength(char* string){
4+
if(string == nullptr){
5+
std::cerr<<"Trying to get length of null pointer\n";
6+
return 0;
7+
}
8+
int len = 0;
9+
while(string[len] != 0)
10+
len++;
11+
12+
return len;
13+
}
14+
15+
void copyString(char* destination, char* source){
16+
if(destination == nullptr){
17+
std::cerr<<"Trying to copy into null pointer\n";
18+
return;
19+
}
20+
21+
if(source == nullptr){
22+
std::cerr<<"Trying to copy from null pointer\n";
23+
return;
24+
}
25+
26+
int i=0;
27+
28+
do{
29+
destination[i] = source[i];
30+
i++;
31+
}while(source[i] != 0);
32+
destination[i] = '\0';
33+
}
34+
35+
void printString(char* string){
36+
if(string == nullptr){
37+
std::cerr<<"Trying to print null pointer\n";
38+
return;
39+
}
40+
41+
for(int i=0; i<getLength(string); i++){
42+
std::cout<<string[i];
43+
}
44+
}
45+
46+
bool areStringsEqual(char* first, char* second){
47+
int lengthFirst = getLength(first), lengthSecond = getLength(second);
48+
if(lengthFirst != lengthSecond)
49+
return false;
50+
51+
for(int i=0; i<lengthFirst; i++){
52+
if(first[i]!=second[i])
53+
return false;
54+
}
55+
56+
return true;
57+
}
58+
59+
char* inputUnspecifiedLengthString(){
60+
char *buffer = new char[10];
61+
int cap = 9;
62+
int curr = 0;
63+
char c;
64+
c=std::cin.peek();
65+
do{
66+
std::cin.get();
67+
buffer[curr++] = c;
68+
if(curr == cap)
69+
{
70+
char *temp = new char[cap+1];
71+
copyString(temp,buffer);
72+
buffer = new char[cap*2];
73+
cap *= 2;
74+
copyString(buffer,temp);
75+
}
76+
}while( (c=std::cin.peek())!= '\n');
77+
buffer[curr] = '\0';
78+
79+
return buffer;
80+
}

Homework1/1/NotSTL.h

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#pragma once
2+
#include<iostream>
3+
4+
int getLength(char* string);
5+
void copyString(char* destination, char* source);
6+
void printString(char* string);
7+
bool areStringsEqual(char* first, char* second);
8+
char* inputUnspecifiedLengthString();

Homework1/1/Stack.cpp

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#include "Stack.h"
2+
3+
template <class T>
4+
LStack<T>::LStack(){
5+
topEl = nullptr;
6+
}
7+
8+
template <class T>
9+
LStack<T>::LStack(LStack<T> const& other){
10+
copyStack(other);
11+
}
12+
13+
template <class T>
14+
LStack<T>::~LStack(){
15+
deleteStack();
16+
}
17+
18+
template <class T>
19+
LStack<T>& LStack<T>::operator=(LStack<T> const& other){
20+
if(this!=other){
21+
deleteStack();
22+
copyStack(other);
23+
}
24+
return *this;
25+
}
26+
27+
template <class T>
28+
void LStack<T>::copyStack(LStack<T> const& other){
29+
copy(other.top);
30+
}
31+
32+
template <class T>
33+
void LStack<T>::copy(Node<T>* toCopy){
34+
if(toCopy){
35+
push(toCopy->inf);
36+
copy(toCopy->next);
37+
}
38+
}
39+
40+
template <class T>
41+
void LStack<T>::push(T inf){
42+
Node<T>* newNode = new Node<T>;
43+
newNode->inf = inf;
44+
newNode->next = topEl;
45+
topEl = newNode;
46+
}
47+
48+
template <class T>
49+
void LStack<T>::deleteStack(){
50+
while(!isEmpty())
51+
pop();
52+
}
53+
54+
template <class T>
55+
T LStack<T>::pop(){
56+
if(!isEmpty()){
57+
Node<T>* toDelete = topEl;
58+
T inf = topEl->inf;
59+
topEl = topEl->next;
60+
delete toDelete;
61+
return inf;
62+
}
63+
return 0;
64+
}
65+
66+
template <class T>
67+
T LStack<T>::top() const{
68+
return topEl->inf;
69+
}
70+
71+
template <class T>
72+
bool LStack<T>::isEmpty() const{
73+
return topEl == nullptr;
74+
}
75+
76+

Homework1/1/Stack.h

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <iostream>
2+
#include "Node.h"
3+
4+
template <class T>
5+
class LStack{
6+
Node<T>* topEl;
7+
8+
public:
9+
LStack();
10+
LStack(LStack<T> const&);
11+
~LStack();
12+
LStack& operator=(LStack<T> const&);
13+
14+
T top() const;
15+
T pop();
16+
void push(T inf);
17+
18+
bool isEmpty() const;
19+
20+
private:
21+
void copy(Node<T>* toCopy);
22+
void erase();
23+
void copyStack(LStack<T> const&);
24+
void deleteStack();
25+
};

Homework1/1/main.cpp

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#include <iostream>
2+
#include "Stack.cpp"
3+
#include "NotSTL.cpp"
4+
5+
double pow(double number, int times){
6+
double result = number;
7+
for(int i=1; i<times; i++)
8+
result *= number;
9+
return result;
10+
}
11+
12+
double getElement(LStack<double>& st){
13+
if(st.isEmpty()){
14+
std::cerr<<"Invalid input format!\n";
15+
return 0;
16+
}
17+
return st.pop();
18+
}
19+
20+
double calculate(char* expression){
21+
LStack<double> st;
22+
double sum = 0;
23+
24+
int n = getLength(expression);
25+
26+
for(int i=0; i<n; i++){
27+
if(expression[i] >= '0' && expression[i] <= '9'){
28+
double toPush = expression[i] - '0';
29+
while(expression[i+1] >= '0' && expression[i+1]<= '9')
30+
{
31+
toPush*=10;
32+
toPush += expression[i+1] - '0';
33+
i++;
34+
}
35+
if(expression[i+1] == '.'){
36+
i++;
37+
int placeAfterDecimal=1;
38+
while(expression[i+1] >='0' && expression[i+1] <= '9'){
39+
toPush += (expression[i+1] - '0') * pow(0.1, placeAfterDecimal);
40+
i++;
41+
}
42+
}
43+
st.push(toPush);
44+
} else {
45+
if(expression[i]!=' '){
46+
47+
double second = getElement(st);
48+
double first = getElement(st);
49+
if(expression[i] == '+')
50+
st.push(first + second);
51+
else if(expression[i] == '-')
52+
st.push(first - second);
53+
else if(expression[i] == '*')
54+
st.push(first * second);
55+
else if(expression[i] == '/')
56+
st.push(first/second);
57+
else if(expression[i] == '%'){
58+
if(second!=(int)second || first!=(int)first){
59+
std::cerr<<"Trying to use % operation on double arguments!\n";
60+
return 0;
61+
}
62+
st.push((int)first%(int)second);
63+
}
64+
}
65+
}
66+
}
67+
double result = st.pop();
68+
if(!st.isEmpty()){
69+
std::cerr<<"Invalid input format!\n";
70+
return 0;
71+
}
72+
return result;
73+
}
74+
75+
int main(){
76+
char *expression = new char[100];
77+
std::cin.getline(expression, 100);
78+
79+
std::cout<<calculate(expression)<<std::endl;
80+
81+
return 0;
82+
}

Homework1/1/main.h

Whitespace-only changes.

0 commit comments

Comments
 (0)