Browse Source

the second version

Dmitry 3 years ago
parent
commit
f4f5849bb2
3 changed files with 82 additions and 47 deletions
  1. 59 33
      allStack.cpp
  2. 4 3
      allStack.h
  3. 19 11
      main.cpp

+ 59 - 33
allStack.cpp

@@ -42,7 +42,7 @@ void Stack_destructoin (sStack *stack) {
 }
 
 //========================================
-void show_stack (sStack *stack) {
+void sStack_show (sStack *stack) {
 
 	cout << "top = " << stack->top << endl;
 	cout << "counter_change_number = " << stack->counter_change_number << endl;
@@ -55,12 +55,30 @@ void show_stack (sStack *stack) {
 }
 
 //========================================
-int  stack_size (sStack *stack) {
+double sStack_show_last_element (sStack *stack) {
+	
+	if (sStack_size (stack) < 1 ) {
+		processing_error (-444);
+		return 0;
+	}
+	
+	double last_element = stack->st [(stack->top) - 1];
+	
+	if (!sStack_Ok (stack)) {
+		sStack_show (stack);
+		assert (0);
+	}
+
+	return last_element;
+}
+
+//========================================
+int  sStack_size (sStack *stack) {
 
 	//cout << "Stack size: " << stack->top << endl; 
 	
 	if (!sStack_Ok (stack)) {
-		show_stack (stack);
+		sStack_show (stack);
 		assert (0);
 	}
 	
@@ -71,7 +89,7 @@ int  stack_size (sStack *stack) {
 void sStack_Push (sStack *stack, double vell) {
 	
 	if (!sStack_Ok (stack)) {
-		show_stack (stack);
+		sStack_show (stack);
 		assert (0);
 	}
 	
@@ -82,8 +100,8 @@ void sStack_Push (sStack *stack, double vell) {
 	stack->st [stack->top++] = vell;
 	
 	if (!sStack_Ok (stack)) {
-		show_stack (stack);
-		assert (("Push",0));
+		sStack_show (stack);
+		assert (("Push", 0));
 	}
 
 }
@@ -92,13 +110,13 @@ void sStack_Push (sStack *stack, double vell) {
 double sStack_Pop 	(sStack *stack) {
 	
 	double last_element = stack->st [(stack->top) - 1];
-//	cout << "last_element = " << last_element << endl;
+  //cout << "last_element = " << last_element << endl;
 	stack->st [(stack->top) - 1] = 0;
 	stack->top--;
 	
 	if (!sStack_Ok (stack)) {
-		show_stack (stack);
-		assert (("Pop",0));
+		sStack_show (stack);
+		assert (("Pop", 0));
 	}
 	
 	return last_element;
@@ -114,8 +132,8 @@ bool sStack_Ok (sStack *stack) {
 										stack->second_point != 0 ||	stack->third_point != 100500) {
 		cout << "!!! boundary constants are change !!! \n";
 		return false;			
-
 	}
+
 	int counter = 0;
 	for (int i = 0; i < MAX_STACK_SIZE; i++) {
 		if (stack->st [i] == -999) {
@@ -126,6 +144,7 @@ bool sStack_Ok (sStack *stack) {
 	if ((MAX_STACK_SIZE - stack->counter_change_number) != counter)	{
 		cout << "!!!WARNING!!!  counter_change_number != counter /n";
 	}
+	
 	return true;
 }
 //=======================================
@@ -138,17 +157,18 @@ void cpu_constructor (sCPU *cpu1, sStack *stack) {
 //========================================
 bool sCPU_Ok (sCPU *cpu1) {
 	
-	 if (!sStack_Ok (cpu1->s1)) {
-	 		return false;
-	 }
+	if (!sStack_Ok (cpu1->s1)) {
+		return false;
+	}
+	
 	return true;
 }
 
 //========================================
 double sCPU_sinus (sCPU *cpu) {
   
- 	if (stack_size (cpu->s1) < 1 ) {
-		processing_error ( -333);
+ 	if (sStack_size (cpu->s1) < 1 ) {
+		processing_error (-333);
 		return 0;
 	}
 	
@@ -156,8 +176,8 @@ double sCPU_sinus (sCPU *cpu) {
 	sStack_Push (cpu->s1, rezalt);
 	
 	if (!sCPU_Ok (cpu)) {
-		show_stack (cpu->s1);
-		assert (("sin",0));
+		sStack_show (cpu->s1);
+		assert (("sin", 0));
 	}
 	
 	return rezalt;
@@ -166,8 +186,8 @@ double sCPU_sinus (sCPU *cpu) {
 //=======================================
 double sCPU_cosine (sCPU *cpu) {
 	
-	if (stack_size (cpu->s1) < 1 ) {
-		processing_error ( -333);
+	if (sStack_size (cpu->s1) < 1 ) {
+		processing_error (-333);
 		return 0;
 	}
 	
@@ -176,8 +196,8 @@ double sCPU_cosine (sCPU *cpu) {
 	sStack_Push (cpu->s1, result);
 	
 	if (!sCPU_Ok (cpu)) {
-		show_stack (cpu->s1);
-		assert (("cos",0));
+		sStack_show (cpu->s1);
+		assert (("cos", 0));
 	}
 	
 	
@@ -187,8 +207,8 @@ double sCPU_cosine (sCPU *cpu) {
 //=======================================
 double sCPU_addition (sCPU *cpu) {
 	
-	if (stack_size (cpu->s1) < 2 ) {
-		processing_error ( -333);
+	if (sStack_size (cpu->s1) < 2 ) {
+		processing_error (-333);
 		return 0;
 	}
 	
@@ -199,8 +219,8 @@ double sCPU_addition (sCPU *cpu) {
 	sStack_Push (cpu->s1, result);
 	
 	if (!sCPU_Ok (cpu)) {
-		show_stack (cpu->s1);
-		assert (("addition",0));
+		sStack_show (cpu->s1);
+		assert (("addition", 0));
 	}
 	
 	return result;
@@ -209,8 +229,8 @@ double sCPU_addition (sCPU *cpu) {
 //=======================================
 double sCPU_subtraction (sCPU *cpu) {
 	
-	if (stack_size (cpu->s1) < 2 ) {
-		processing_error ( -333);
+	if (sStack_size (cpu->s1) < 2 ) {
+		processing_error (-333);
 		return 0;
 	}
 	
@@ -221,8 +241,8 @@ double sCPU_subtraction (sCPU *cpu) {
 	sStack_Push (cpu->s1, result);
 	
 	if (!sCPU_Ok (cpu)) {
-		show_stack (cpu->s1);
-		assert (("subtraction",0));
+		sStack_show (cpu->s1);
+		assert (("subtraction", 0));
 	}
 	
 	return result;
@@ -232,8 +252,8 @@ double sCPU_subtraction (sCPU *cpu) {
 //=======================================
 double sCPU_multiplication (sCPU *cpu) {
 	
-	if (stack_size (cpu->s1) < 2 ) {
-		processing_error ( -333);
+	if (sStack_size (cpu->s1) < 2 ) {
+		processing_error (-333);
 		return 0;
 	}
 	
@@ -244,8 +264,8 @@ double sCPU_multiplication (sCPU *cpu) {
 	sStack_Push (cpu->s1, result);
 	
 	if (!sCPU_Ok (cpu)) {
-		show_stack (cpu->s1);
-		assert (("multiplication",0));
+		sStack_show (cpu->s1);
+		assert (("multiplication", 0));
 	}
 	
 	return result;
@@ -273,6 +293,12 @@ bool 	processing_error (const double namb_0f_error) {
 		cout << "Not enough numbers in the stack \n";
 		return true;
 	}
+	
+	if (namb_0f_error == -444) {
+		cout << "No element in the stack \n";
+		return true;
+	}
+	
 	return false;
 }
 

+ 4 - 3
allStack.h

@@ -2,7 +2,7 @@
 #define __CSTACK_H__
 
 unsigned	const int MAX_STACK_SIZE = 20;
-unsigned	const int NUMBER_0F_COMAND = 8;
+unsigned	const int NUMBER_0F_COMAND = 9;
 const double  PI_CONST = 3.14159265;
 
 // --------------------------
@@ -28,11 +28,12 @@ struct sCPU {
 //--------------------------
 void Stack_constructor (sStack *stack); 
 void Stack_destructoin (sStack *stack);
-void show_stack (sStack *stack);
-int  stack_size (sStack *stack);		
+void sStack_show (sStack *stack);
+int  sStack_size (sStack *stack);		
 double sStack_Pop  (sStack *stack);
 void sStack_Push  (sStack *stack, double vell);
 bool sStack_Ok (sStack *stack);
+double sStack_show_last_element (sStack *stack);
 
 void cpu_constructor (sCPU *cpu1, sStack *stack);
 double sCPU_cosine (sCPU *cpu);

+ 19 - 11
main.cpp

@@ -21,17 +21,17 @@ int main () {
 	sStack stack;
 	
 	Stack_constructor (&stack);
-	//show_stack (&stack);
+	//sStack_show (&stack);
 	cpu_constructor (&cpu1, &stack);
 	
 	abstract ();
 	
 	while (end_of_while) {
 
-		char comand[12];
+		char comand [12];
 		double number = 0;
 		const char analogue [NUMBER_0F_COMAND][55] = 
-									{"push", "sin", "cos", "add", "sub", "mul", "end", "show"};
+									{"push", "sin", "cos", "add", "sub", "mul", "end","last", "show"};
 		
 		
 		scanf ("%s", comand);
@@ -70,18 +70,25 @@ int main () {
 					break;
 					
 					case 6:
+								cout << "The calculation result: " << sStack_show_last_element (&stack) << endl;
 								end_of_while = false;
 					break;
 					
 					case 7:
-								show_stack (&stack);
+								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;
 }
@@ -92,10 +99,11 @@ void abstract () {
 			 << "push 232 \n"
 			 << "sin \n"
 			 << "cos \n"
-			 << "add \n"
-			 << "sub \n"
-			 << "mul \n"
+			 << "add --- +\n"
+			 << "sub --- -\n"
+			 << "mul --- * \n"
 			 << "show \n"
+			 << "last \n"
 			 << "end " << endl;
 }