首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

很疑惑

2013-01-28 
很不解stack.hconst int StackSize100templateclass Tclass Stack{private:int topT data[StackSize]

很不解

stack.h
const int StackSize=100;
template<class T>

class Stack
{
private:
int top;
T data[StackSize];
public:
Stack()
{
top=-1;
}
~Stack()
{
}
void push(T x);

T pop();

bool stkempty()
{
if(top==-1)
return 1;
else return 0;
}
};
template <classT>

void Stack::<T> push(T x)
{
if(top<StackSize)
         data[++top]=x;
else 
break;
}
template <class T>
T Stack::<T> pop()
{
if(top==-1)
break;
else
T x=data[top--];
return x;
}
 

#include<iostream.h>
 #include<fstream.h>
 //#include<cstdlib.h>
 #include<string.h>
#include"stack.h"
 //using namespace std;
 

class txt
 {
 private:
     fstream in;//文件输入输出对象
fstream out;
 char file[30];//存放文件的名字
  public:
  txt();//构造函数原型
 ~txt()//析构函数
 {
  in.close();//关闭文件流操作
     out.close();
  }
  void menu();//菜单
 void create_txt();//创建文本
 void add_txt_content();//添加文本
 void show_content();//展示文本
 void figure_out_lines();//计算行数
 void figure_out_paragraph();//计算段数
void check_sign();//检查匹配符
 };
 
void txt::menu()
 {
 cout<<"                       Main MENU                      "<<endl;
 cout<<"********************************************************"<<endl;
 cout<<"                    1.     create txt                      "<<endl;
 cout<<"                    2.     add txt content                  "<<endl;
 cout<<"                    3.     show content on the screen        "<<endl;
 cout<<"                    4.     figure out the total paragraph      "<<endl;
 cout<<"                    5.     figure out  lines                "<<endl;
 cout<<"                    6.     check the signs                  "<<endl;


 cout<<"                    7.    exit                            "<<endl;
 cout<<"*********************************************************"<<endl;
 }
 
txt::txt()
 {
 cout<<"看清菜单按步骤做哦!:"<<endl;
 cout<<"文件例如:e:\\hui.txt"<<endl;
}
 
void txt::create_txt()
 {
     cout<<"请输入文件名和保存的位置:"<<endl;
 cin>>file;
 out.open(file,ios::out|ios::trunc);
 if(!out)
 {
 cout<<"error!can't open "<<file<<'!';
return ;//exit(1);
 }
 else
     cout<<"the txt file has created!"<<endl;
 out.close();
 out.clear();//清除原来的所有操作,为了进行下次的新操作
 }
 
void txt::add_txt_content()
 {
 char c;
 cout<<"write something on the screen,then the words will be added in txt file(end with #):"<<endl;
 out.open(file,ios::out|ios::app);//以输出文件流打开方式,内容添加在文后
if(!out)
 {
 cout<<"error!can't open "<<file<<'!';
 return ;//exit(1);//相当于return ,退出
}
while((c=cin.get())!='#')//cin不能输入空格,只能用cin.get()
 {
      out<<c;
 }
 out.close();
 out.clear();
 }
 
 void txt::show_content()
 {
 char buffer[10000];
 in.open(file,ios::in);
 if(!in)
 {
 cout<<"error!can't open "<<file<<'!';
 return ;//exit(1);
 }
 cout<<"the content are below:"<<endl;
 while(!in.eof())
 {
        in.getline(buffer,10000,'#');///把一万个字符写入buffer数组,遇见#号就结束
   cout<<buffer<<endl;
 
}
 cout<<endl;
 in.close();
 in.clear();
 }  
 
void txt::figure_out_paragraph()
 { 
char c1;
     int duan=0;
     int kongge=0;
 in.open( file,ios::in); 
 in.get(c1);
 while(!in.eof())
     {
    if(c1==' ') kongge++;//计算段是先计算空格,文本前两个是空格
        else if(kongge==2) 
             {
               duan++;
               kongge=0;
             }
     in.get(c1);
     }
 cout<<"有"<<duan<<"段"<<endl;
 in.close();
 in.clear();
 }
 
void txt::figure_out_lines()
 { 
char c;
 int n=0;
 in.open(file,ios::in);
 if(!in)
 {
 cout<<"error!can't open "<<file<<'!';
//return ; abort();
 }
 while(!in.eof())


 {
   in.get(c);
   if(c=='\n')//判断行是遇见回车键就是一行
    n++;
 } 
    cout<<"这个篇文章,有"<<n<<"行"<<endl;
 in.close();
 in.clear();
 } 
 
 void txt::check_sign()
 {   
   Stack <char> s;
   char c,ch1,ch2,ch;
   int flag=1;
   int i=0;
   in.open(file,ios::in|ios::binary);
   cout<<"请输入你要判断的符号:   "<<endl;
   cout<<"正符号: "<<endl;
   cin>>ch1;
   cout<<"反符号: "<<endl;
   cin>>ch2;
   while(ch=='y'||ch=='Y') 
   { 
      while(!in.eof())
  {
     in.get(c);
 
 
     if(c==ch1||c==ch2)
  {
     if(c==ch1)
                s.push(c);
             else
 { 
    if(s.stkempty())
    { 
       cout<<"第"<<i+1<<"个字符左边缺"<<ch1<<endl;
               flag=0;
           break;
    }
            else
               s.pop();
 }
   if(!s.stkempty())
                cout<<"缺"<<ch2<<endl;
             else if(flag)
            cout<<ch1<<"与"<<ch2<<"匹配!"<<endl;
  }
          i++;   
  }
    cout<<"继续吗?(y/n)";cin>>ch;
  cout<<"请输入你要判断的符号:   "<<endl;
              cout<<"正符号: ";cin>>ch1;
              cout<<"反符号: ";cin>>ch2;
   }
 }
 
int main(void)
 {
 txt t;
 int choice;
 t.menu();
 while(1) 
    { 
   cout<<"请输入你的选择:";
cin>>choice;
       switch(choice) 
   { 
          case 1: 
              t.create_txt();
      break; 
          case 2: 
              t.add_txt_content();  
              break; 
          case 3: 


              t.show_content();  
              break; 
          case 4: 
              t.figure_out_paragraph();
              break; 
          case 5: 
              t.figure_out_lines();
              break; 
          case 6:
 t.check_sign();  
              break; 
          case 7:
  default:
               return 0; 
   } 
    }
 return 0;
 }
各位长辈,我这个程序不知道错哪了,求修正
 
[解决办法]


//stack.h
const int StackSize=100;
using namespace std;
template<class T>
class Stack
{
private:
int top;
T data[StackSize];
public:
Stack()
{
top=-1;
}
~Stack()
{
}
void push(T x);

T pop();

bool stkempty()
{
if(top==-1)
return 1;
else return 0;
}
};

template<class T>
T Stack<T>::pop()
{
T x;
if(top==-1)
return 0;
else
x=data[top--];
return x;
}

template<class T>
void Stack<T>::push( T x )
{
if(top<StackSize)
data[++top]=x;
else 
return ;
}

[解决办法]
void Stack::<T> push(T x)
应该为void Stack<T>::push(T x)

同理 T Stack::<T> pop()
应该为 T Stack<T>::pop()

还有if else里面不能用break
template <classT>
应该为template <class T>

总之要仔细

热点排行