⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 templat2.cpp

📁 Since the field of object oriented programming is probably new to you, you will find that there is a
💻 CPP
字号:
                             // Chapter 9 - Program 7 - TEMPLAT2.CPP
#include <stdio.h>

const int MAXSIZE = 128;

template<class ANY_TYPE>
class stack
{
   ANY_TYPE array[MAXSIZE];
   int stack_pointer;
public:
   stack(void) { stack_pointer = 0; };
   void push(ANY_TYPE in_dat) { array[stack_pointer++] = in_dat; };
   ANY_TYPE pop(void)    { return array[--stack_pointer]; };
   int empty(void)       { return (stack_pointer == 0); };
}

char name[] = "John Herkimer Doe";

int main(void)
{
int x = 12, y = -7;
float real = 3.1415;

stack<int> int_stack;
stack<float> float_stack;
stack<char *> string_stack;

   int_stack.push(x);
   int_stack.push(y);
   int_stack.push(77);
   float_stack.push(real);
   float_stack.push(-12.345);
   float_stack.push(100.01);
   string_stack.push("This is line 1");
   string_stack.push("This is the second line");
   string_stack.push("This is the third line");
   string_stack.push(name);

   printf("Integer stack ---> ");
   printf("%8d ", int_stack.pop());
   printf("%8d ", int_stack.pop());
   printf("%8d\n", int_stack.pop());

   printf("  Float stack ---> ");
   printf("%8.3f ", float_stack.pop());
   printf("%8.3f ", float_stack.pop());
   printf("%8.3f\n", float_stack.pop());

   printf("\n     Strings\n");
   do 
   {
      printf("%s\n", string_stack.pop());
   } while (!string_stack.empty());

   return 0;
}


// Result of execution

// Integer stack --->       12       -7       77
//   Float stack --->    3.141  -12.345  100.010
//
//      Strings
// John Herkimer Doe
// This is the third line
// This is the second line
// This is line 1

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -