Dmitry лет назад: 7
Сommit
3be850f0b9
6 измененных файлов с 576 добавлено и 0 удалено
  1. 33 0
      const_gurd.h
  2. 117 0
      main.cpp
  3. 202 0
      sCPU.cpp
  4. 30 0
      sCPU.h
  5. 156 0
      sStack.cpp
  6. 38 0
      sStack.h

+ 33 - 0
const_gurd.h

@@ -0,0 +1,33 @@
+#ifndef __CONST_GURD_H__
+#define __CONST_GURD_H__
+	
+//=============================================		
+	#ifdef _MSC_VER
+		#define _FUNC_  __FUNCTION__
+	#elif __GNUC__
+		#define _FUNC_  __PRETTY_FUNCTION__
+	#endif	
+
+	#define _DEBUG_ASSERT_(msg) cout << _FUNC_ << ":" << msg; assert(0);
+	#define _ASSERT_OK_(strct, ptr) if (!strct##_Ok (ptr)) { \
+						strct##_show (ptr); \
+						_DEBUG_ASSERT_("Not Ok!") \
+					} 
+
+	#define USE_SUPER_PUPER_GUARDS
+//============================================
+
+
+	unsigned	const int NUMBER_OF_COMAND = 9;
+
+//const double  PI_CONST 	= 3.14159265;
+	const int guard_first = -100800;
+	
+	const int guard_second = -100500;
+	
+	const int guard_third = 100500;
+	
+	const int not_enouth_number = -333;
+	const int no_element = -444;
+	
+#endif

+ 117 - 0
main.cpp

@@ -0,0 +1,117 @@
+#include <iostream>
+#include <string>
+#include <cstdio>
+#include <cmath>
+#include <cstring>
+#include <assert.h>
+#include <cstdlib>
+#include <vector>
+
+#include "sStack.h"
+#include "sCPU.h"
+#include "const_gurd.h"
+
+
+using std::cout;
+using std::endl;
+
+void abstract ();
+
+int main () {
+	
+	bool end_of_while = true;
+	sCPU cpu1;
+	sStack stack;
+	
+	Stack_constructor (&stack);
+	//sStack_show (&stack);
+	cpu_constructor (&cpu1, &stack);
+	
+	abstract ();
+	
+	while (end_of_while) {
+
+		char comand [12];
+		double number = 0;
+		
+		//std::vector< std::vector<const char> >  analogue;
+		
+		//analogue.push_back(analogue1);
+		const char analogue [][44] = 
+						{"push", "sin", "cos", "add", "sub", "mul", "end","last", "show"};
+		
+
+		
+		scanf ("%s", comand);
+		
+		if (0 == strcmp(analogue [0], comand)) {
+			scanf ("%le", &number);
+			//cout << number << endl;
+		}
+	
+		for (int i = 0; i < NUMBER_OF_COMAND; i++) {
+			
+			if (0 == strcmp(analogue [i], comand)) {
+				switch (i) {
+					case 0:
+								sStack_Push (&stack, number);
+					break;
+					
+					case 1:
+								sCPU_sinus (&cpu1);
+					break;
+					
+					case 2:
+								sCPU_cosine (&cpu1);
+					break;
+					
+					case 3:
+								sCPU_addition (&cpu1);
+					break;
+					
+					case 4:
+								 sCPU_subtraction (&cpu1);
+					break;
+					
+					case 5:
+								sCPU_multiplication (&cpu1);
+					break;
+					
+					case 6:
+								cout << "The calculation result: " << sStack_show_last_element (&stack) << endl;
+								end_of_while = false;
+					break;
+					
+					case 7:
+								cout << "The last element: " <<	
+								sStack_show_last_element (&stack) << endl;
+					break;
+					
+					case 8:
+								sStack_show (&stack);
+					break;
+				} // end of switch
+				
+			} // end of if
+			
+		} // end of for
+		
+	} // end of while
+
+return 0;
+}
+
+void abstract () {
+	cout << "Hellow ! \n"
+			 << "Comand Enter like \n"
+			 << "push 232 \n"
+			 << "sin \n"
+			 << "cos \n"
+			 << "add --- +\n"
+			 << "sub --- -\n"
+			 << "mul --- * \n"
+			 << "show \n"
+			 << "last \n"
+			 << "end " << endl;
+}
+

+ 202 - 0
sCPU.cpp

@@ -0,0 +1,202 @@
+#include <iostream>
+#include <string>
+#include <cstdio>
+#include <cmath>
+#include <cstring>
+#include <assert.h>
+#include <cstdlib>
+
+#include "sCPU.h"
+#include "const_gurd.h"
+
+
+using std::cout;
+using std::endl;
+
+
+
+//=======================================
+void cpu_constructor (sCPU *cpu1, sStack *stack) {
+	
+	cpu1->st = stack;
+	
+}
+
+//========================================
+bool sCPU_Ok (sCPU *cpu1) {
+	
+	if (!sStack_Ok (cpu1->st)) {
+		return false;
+	}
+	
+	return true;
+}
+
+//========================================
+double sCPU_sinus (sCPU *cpu) {
+
+#ifdef USE_SUPER_PUPER_GUARDS	
+	_ASSERT_OK_(sCPU, cpu)
+#endif
+  
+ 	if (sStack_size (cpu->st) < 1 ) {
+		processing_error (no_element);
+		return 0;
+	}
+	
+  double rezalt = my_sinus (sStack_Pop (cpu->st));
+	sStack_Push (cpu->st, rezalt);
+
+#ifdef USE_SUPER_PUPER_GUARDS	
+	_ASSERT_OK_(sCPU, cpu)
+#endif
+
+	return rezalt;
+}
+
+//=======================================
+double sCPU_cosine (sCPU *cpu) {
+
+#ifdef USE_SUPER_PUPER_GUARDS	
+	_ASSERT_OK_(sCPU, cpu)
+#endif
+	
+	if (sStack_size (cpu->st) < 1 ) {
+		processing_error (no_element);
+		return 0;
+	}
+	
+	
+	double result  = my_cosine (sStack_Pop (cpu->st));
+	sStack_Push (cpu->st, result);
+	
+#ifdef USE_SUPER_PUPER_GUARDS	
+	_ASSERT_OK_(sCPU, cpu)
+#endif
+	
+	return result;	
+}
+
+//=======================================
+double sCPU_addition (sCPU *cpu) {
+	
+#ifdef USE_SUPER_PUPER_GUARDS	
+	_ASSERT_OK_(sCPU, cpu)
+#endif
+	
+	if (sStack_size (cpu->st) < 2 ) {
+		processing_error (not_enouth_number);
+		return 0;
+	}
+	
+	double element1 = sStack_Pop (cpu->st);
+	double element2 = sStack_Pop (cpu->st);
+	double result = (element1 + element2);
+	
+	sStack_Push (cpu->st, result);
+
+#ifdef USE_SUPER_PUPER_GUARDS	
+	_ASSERT_OK_(sCPU, cpu)
+#endif
+	
+	return result;
+}
+
+//=======================================
+double sCPU_subtraction (sCPU *cpu) {
+
+#ifdef USE_SUPER_PUPER_GUARDS	
+	_ASSERT_OK_(sCPU, cpu)
+#endif
+	
+	if (sStack_size (cpu->st) < 2 ) {
+		processing_error (not_enouth_number);
+		return 0;
+	}
+	
+	double element1 = sStack_Pop (cpu->st);
+	double element2 = sStack_Pop (cpu->st);
+	double result = (element2 - element1);
+	
+	sStack_Push (cpu->st, result);
+
+#ifdef USE_SUPER_PUPER_GUARDS	
+	_ASSERT_OK_(sCPU, cpu)
+#endif
+
+	return result;
+
+}
+
+//=======================================
+double sCPU_multiplication (sCPU *cpu) {
+		
+#ifdef USE_SUPER_PUPER_GUARDS	
+	_ASSERT_OK_(sCPU, cpu)
+#endif
+
+	if (sStack_size (cpu->st) < 2 ) {
+		processing_error (not_enouth_number);
+		return 0;
+	}
+	
+	double element1 = sStack_Pop (cpu->st);
+	double element2 = sStack_Pop (cpu->st);
+	double result = (element2 * element1);
+	
+	sStack_Push (cpu->st, result);
+
+#ifdef USE_SUPER_PUPER_GUARDS	
+	_ASSERT_OK_(sCPU, cpu)
+#endif
+	
+	return result;
+
+}
+
+//======================================
+void 	 sCPU_show (const sCPU *cpu) {
+		
+	sStack_show (cpu->st);
+
+}
+
+//=====================================
+bool sCPU_Ok (const sCPU *cpu) {
+		
+	if (!sStack_Ok (cpu->st)) {
+			return true;
+	} 
+	return false;
+}
+
+//=======================================
+double my_sinus (double _number) {
+	
+	return sin (_number*M_PI/180);
+
+}
+
+//=======================================
+double my_cosine (double _number) {
+
+	return cos (_number*M_PI/180);
+
+}
+
+//======================================
+bool 	processing_error (const double namb_0f_error) {
+	
+	if (namb_0f_error == not_enouth_number) {
+		cout << "Not enough numbers in the stack \n";
+		return true;
+	}
+	
+	if (namb_0f_error == no_element) {
+		cout << "No element in the stack \n";
+		return true;
+	}
+	
+	return false;
+}
+

+ 30 - 0
sCPU.h

@@ -0,0 +1,30 @@
+#ifndef __sCPU_H__
+#define __sCPU_H__
+
+#include "sStack.h"
+//--------------------------
+struct sCPU {
+	
+	sStack *st;
+	
+};
+
+
+//--------------------------
+void cpu_constructor (sCPU *cpu1, sStack *stack);
+void 	 sCPU_show (const sCPU *cpu);
+bool   sCPU_Ok (const sCPU *cpu);
+
+double sCPU_cosine (sCPU *cpu);
+double sCPU_sinus (sCPU *cpu);
+double sCPU_addition (sCPU *cpu);
+double sCPU_subtraction (sCPU *cpu);
+double sCPU_multiplication (sCPU *cpu);
+
+
+double my_cosine (double _number);
+double my_sinus (double _number);
+
+bool 	processing_error (const double namb_0f_error);
+
+#endif

+ 156 - 0
sStack.cpp

@@ -0,0 +1,156 @@
+#include <iostream>
+#include <string>
+#include <cstdio>
+#include <cmath>
+#include <cstring>
+#include <cassert>
+#include <cstdlib>
+
+#include "sStack.h"
+#include "sCPU.h"
+#include "const_gurd.h"
+
+
+
+
+
+using std::cout;
+using std::endl;
+
+//========================================
+void Stack_constructor (sStack *stack) {
+	 
+	stack->memory_guard_first = guard_first;
+	stack->memory_guard_second = guard_second;
+	stack->memory_guard_third = guard_third;
+	stack->counter_change_number = 0;
+	
+	stack->top = 0;
+	
+	for (int i = 0; i < stack->MAX_STACK_SIZE; i++) {
+		stack->data [i] = -999; 
+	}
+
+}
+
+//========================================
+void Stack_destructoin (sStack *stack) {
+
+	stack->top = -100;
+	stack->counter_change_number = 0;
+	
+	for (int i = 0; i < stack->MAX_STACK_SIZE; i++) {
+		stack->data [i] = -100500; 
+	}
+
+}
+
+//========================================
+void sStack_show (const sStack *stack) {
+
+	cout << "top = " << stack->top << endl;
+	cout << "counter_change_number = " << stack->counter_change_number << endl;
+
+	for (int i = 0; i < stack->MAX_STACK_SIZE; i++ ) {
+		
+		cout << "data " << i << " " << 	stack->data [i] << "\n";
+	}
+	
+	cout << endl;
+	
+}
+
+//========================================
+double sStack_show_last_element (const sStack *stack) {
+	
+	if (sStack_size (stack) < 1 ) {
+		processing_error (no_element);
+		return 0;
+	}
+	
+	double last_element = stack->data [(stack->top) - 1];
+
+#ifdef USE_SUPER_PUPER_GUARDS	
+	_ASSERT_OK_(sStack, stack)
+#endif
+
+	return last_element;
+}
+
+//========================================
+int  sStack_size (const sStack *stack) {
+
+	//cout << "Stack size: " << stack->top << endl; 
+
+#ifdef USE_SUPER_PUPER_GUARDS	
+	_ASSERT_OK_(sStack, stack)
+#endif
+	
+	return stack->top;
+}
+
+//========================================
+void sStack_Push (sStack *stack, double vell) {
+
+#ifdef USE_SUPER_PUPER_GUARDS	
+	_ASSERT_OK_(sStack, stack)
+#endif
+
+	if (stack->data [stack->top] == -999 ) {
+		stack->counter_change_number++;
+	}		
+	
+	stack->data [stack->top++] = vell;
+
+#ifdef USE_SUPER_PUPER_GUARDS	
+	_ASSERT_OK_(sStack, stack)
+#endif
+	/*	
+	if (!sStack_Ok (stack)) {
+		sStack_show (stack);
+		_DEBUG_ASSERT_("Stack overflow!");
+	}
+*/
+}
+
+//========================================
+double sStack_Pop 	(sStack *stack) {
+	
+	double last_element = stack->data [(stack->top) - 1];
+  //cout << "last_element = " << last_element << endl;
+	stack->data [(stack->top) - 1] = 0;
+	stack->top--;
+	
+#ifdef USE_SUPER_PUPER_GUARDS	
+	_ASSERT_OK_(sStack, stack)
+#endif
+	
+	return last_element;
+}
+
+//========================================
+bool sStack_Ok (const sStack *stack) {
+	
+	if ((stack->top < 0) || (stack->top > stack->MAX_STACK_SIZE)) {
+		return false;
+	}
+	if (stack->memory_guard_first != guard_first ||	
+										stack->memory_guard_second != guard_second ||	stack->memory_guard_third != guard_third) {
+		cout << "!!! boundary constants are change !!! \n";
+		return false;			
+	}
+
+	int counter = 0;
+	for (int i = 0; i < stack->MAX_STACK_SIZE; i++) {
+		if (stack->data [i] == -999) {
+			counter++;
+		}
+	}
+	
+	if ((stack->MAX_STACK_SIZE - stack->counter_change_number) != counter)	{
+		cout << "!!!WARNING!!!  counter_change_number != counter /n";
+	}
+	
+	return true;
+}
+

+ 38 - 0
sStack.h

@@ -0,0 +1,38 @@
+#ifndef __sSTUCT_H__
+#define __sSTUCT_H__
+
+
+
+
+// --------------------------
+struct sStack {
+	
+	static unsigned	const int MAX_STACK_SIZE = 2;
+	
+	int memory_guard_first;
+	double data [MAX_STACK_SIZE];
+	int memory_guard_second;
+	
+	unsigned int counter_change_number;
+	
+	unsigned int top;
+	
+	int memory_guard_third;
+	
+		
+};
+
+//--------------------------
+void Stack_constructor (sStack *stack); 
+void Stack_destructoin (sStack *stack);
+
+void   sStack_show (const sStack *stack);
+int    sStack_size (const sStack *stack);		
+double sStack_Pop  (sStack *stack);
+void   sStack_Push  (sStack *stack, double vell);
+
+bool   sStack_Ok (const sStack *stack);
+double sStack_show_last_element (const sStack *stack);
+
+
+#endif