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

那个高人帮忙找异常

2013-01-28 
那个高人帮忙找错误/*㈠ 题目:文件操作㈡ 设计的主要思路①以本学期的重点,面向对象的类为基础,构建主要框

那个高人帮忙找错误




/*㈠ 题目:文件操作

㈡ 设计的主要思路
①以本学期的重点,面向对象的"类"为基础,构建主要框架;
②用文件流操作为方法进行编码;
③结合面向过程的程序编写技巧进行各函数块得代码编写;
④用C++代码编写思想完善整个程序。
㈢ 主要功能
① 创建自己所想的文本文件;
② 在创建的文件上写自己想要写的内容;
③ 展示内容;
④ 添加内容;
⑤ 计算文本内容的行数和段数;
⑥ 判断内容中的符号是否匹配。

*/

#include<iostream>
#include<fstream>
#include<cstdlib>
#include<string>
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<<'!';
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<<'!';
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<<'!';
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<<'!';
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;
}



[解决办法]

 


void txt::check_sign()
{   
      stack<char> s; // stack在哪?
      ..............
      ..............
}

热点排行