test.cpp

// test.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <string>
using std::string;
using std::cin;
using std::cout;
//using std::map;
#include "memento.h"
//#include "clist.h"
#include "visitor.h"
#include "RoleStateMemento.h"
#include "observer.h"
//#include "cstring.h"
#include "textquery.h"
#include "sale_item.h"
#include "exception.h"
#include "virt.h"
#include "resquest.h"
#include "decorator.h"
#include "CheckedPtr.h"
#include "ScrPtr.h"
#include "assert.h"
#include <fstream> //文件输出流
#include <map>
#include "query.h"
#define MAXLINE 112
#define PAGEMAX 20
int highlight=0;
#if 0
int alldateshow[MAXLINE];
//上下翻页效果
void init()
{
 int i=0;
 for(i=0;i<MAXLINE;i++)
 {
  alldateshow[i]=i;
 }
 for(i=0;i<PAGEMAX;i++)
  printf("%d\n",alldateshow[i]);
}
void page(int pageup,int pagedown,int up,int down)
{
 int i=0;
   if(pageup==1) //pageup
   {
    if(highlight<PAGEMAX)
    {
          highlight=0;
   
      for(i=0;i<PAGEMAX;i++)
    {
     printf("%d\n",alldateshow[highlight+i]);
    }
    }
    else
    {
          highlight-=PAGEMAX;
    for(i=highlight-highlight%PAGEMAX;i<highlight-highlight%PAGEMAX+PAGEMAX;i++)
    {
     printf("%d\n",alldateshow[i]);
    }
    }
   }
   else if(pagedown==1)
   {
       if(highlight>=MAXLINE-PAGEMAX)
    {
     for(i=highlight;i<MAXLINE;i++)
     {
      printf("%d\n",alldateshow[i]);
     }
    }
    else
    {
     highlight+=PAGEMAX;
       for(i=highlight-highlight%PAGEMAX;i<highlight-highlight%PAGEMAX+PAGEMAX;i++)
    {
     printf("%d\n",alldateshow[i]);
    }
    }
   }
   else if(up==1)
   {
    if(highlight>0)
     highlight--;
          for(i=highlight;i<PAGEMAX;i++)
     {
      printf("%d\n",alldateshow[i]);
     }
   }
   else if(down==1)
   {
    if(highlight<MAXLINE-1)
    {
          highlight++;
    }
   }
   else
   {
   
   }
}

//最长递增子序列
 void longestarray()
  {int j=0;
  int a[20]={1,1,2,3,3,3,3,4,5,5,3,5,6,1,5,7,8,9,1,4};
  int b[20]={1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
  int index=0;
  int max=1;
  int count=1;
  b[index]=1;
 while(index<20)
 { 
   int i=0;
  for(;i<index;i++)// 将这个改为二分法查找
  {
    if(a[index]==a[i])  
  {
    b[i]++;
   b[index]=b[i];
  }
  }
   index++;
 }
 
  for(;j<20;j++)
  {  if(max<b[j])
    max=b[j];
     printf("%d ,",b[j]);
   }
  printf("%d",max);
  }
 //最长递增子序列
 int find(int *a ,int len, int value)
 {
   int left=0;
   int right=len-1;
   int mid=(left+right)/2;
   while(left<=right)
    {
    if(value>a[mid])
  left=mid+1;
 else if(value<a[mid])
  right=mid-1;
 else
  return mid;
     mid=(left+right)/2;
    }
   return -1;//没有找到
 }
  void longestarray2()
   {
   int a[10]={1,1,2,3,3,3,3,4,5,5};
   int b[10]={1,1,1,1,1,1,1,1,1,1};
   int index=0;
   int max=1;
   int count=1;
   b[index]=1;
   index=1;
  while(index<10)
  {  
    int i=0;
          int pos=0;
  if((pos=find(a,index,a[index]))!=-1)   
   {
     b[pos]++;
    b[index]=b[pos];
   }
  
    index++;
  }
  int j=0;
   for(;j<10;j++)
   {   if(max<b[j])
     max=b[j];
      printf("%d ,",b[j]);
    }
   printf("%d",max);
   }
  void f1(int **num)
  {
      printf("%ld\n",*num);
  
   printf("%d",**num);
  }
  void test(int *num)
  {
   f1(&num);
  }

  void func1(item_base obj)//复制形参
  {

  }
  void func2(item_base &obj)//不复制形参
  {

  }
  item_base func3()
  {
   item_base obj;
   return obj;
  }
  item_base & func4() //错误,不能返回局部函数的引用
  {
   item_base obj;
   return obj;
  }


  class Foo {
  public:
   Foo():date(100)
   {
    for (int i = 0; i != 100; ++i)
     date[i] = i;
   }
   int & operator[](const size_t size);
   const int & operator[](const size_t size) const;
  private:
   vector<int> date;
  };

  class Account {
public:
    // interface functions here
    void applyint()
 {
  amount += amount * interestRate;
 }
friend int main();
    double balance()
 {
  return amount;
 }
public:
    static double rate() // staitc成员变量只能由static函数处理
 {
  return interestRate;
 }
    static void rate(double);  // sets a new rate
private:
    std::string owner;
    double amount;
    static double interestRate;
    static double initRate();
private:
    static const std::string accountType;
private:
    static const int period = 30;  // interest posted every 30 days
    double daily_tbl[period]; // ok: period is constant expression
};
using std::string;
const string Account::accountType("Savings Account");

void Account::rate(double newRate)
{
    interestRate = newRate;
}

int& Foo::operator[](const size_t index)
{
 return date[index];   // no range checking on index
}

const int& Foo::operator[](const size_t index) const
{
 return date[index];   // no range checking on index
}

  struct absint
  {
   int operat(int a){
    return a<0 ? -a:a;
   }
  };

  ifstream &get(ifstream &in)  //流类型不能作为函数的返回值或者形参
{
 int ival;
 while(in>>ival,!in.eof())
 {
  if(in.bad())//流进入错误的状态
  {
   cerr<<""<<endl;
  }
 
 if(in.fail())
 {
  in.clear();
  continue;
 }
 }
 return in;
}
    #endif
int _tmain(int argc, _TCHAR* argv[])
{
 //longestarray();
#if 0
 int state=1;
 //一开始的状态
    originator *ori=new originator(state);
 //建 备忘录保存
    Memento *mem=ori->CreateMemento();
 //管理备忘录
    adminmem * admin=new adminmem(mem);
    ori->print();
 state=0;
 
 //ori->setstate(state);
    //ori->print();

   //恢复开始状态
 mem->setstate(state);
    ori->SetMemento(mem);
 ori->print();

    init();
 printf("pagedown---------\n");
 page(0,1,0,0);
 printf("up--------------\n");
 page(0,0,1,0);
 printf("down----------\n");
 page(0,0,0,1);

 GameRole * game=new GameRole();
 RoleStateCaretaker * role=new RoleStateCaretaker();
    role->pMemento =game->CreateRole();
 game->ChangeValue();
    //这里就恢复原来的啦
 game->RecoveryState(role->pMemento);
 delete game;
 delete role;
 return 0;

 int pos=0;
 LinkedList<int> *list= new LinkedList<int>();
 //Node<int> *node=new Node(10,NULL);
 list->InsertFront(10);
 list->InsertRear(100);
    pos=list->CurrentPosition();
 std::cout<<pos;//<<endl;
 
 
 concreatesubject * con=new concreatesubject();
 //设置好观察者
 concreateobserver * observer=new concreateobserver(con);

 con->setstate(11); //状态改变,自动通知观察者

 observer->draw();
 string s;
 while(cin>>s)
 {
  std::cout<<s<<std::endl;
 }

 int num=10;
 int *p=&num;
 test(p);
 
ObjectStruct *o=new ObjectStruct();
ObjectStruct obj;
obj.Attach(new EleA());
o->Attach(new EleA());
o->Attach(new EleB());
concreatevisitor1 *con1=new concreatevisitor1();
concreatevisitor2 *con2=new concreatevisitor2();
con1->visitorEleA(new EleA());
con1->visitorEleB(new EleB());
con2->visitorEleA(new EleA());
con2->visitorEleB(new EleB());
delete o;

item_base obj;
//func1(obj);
//func2(obj);
//obj=func3();
//obj=func4();

item_base *pbase=new item_base();

  
    Boss pm = new PM("pm");
    Boss hr = new HR("hr");
    Boss manager = new Manager("manager");
    pm.Successor = hr;
    hr.Successor = manager;
    bool pass = pm.PassRequest(request);
    Console.Write(pass);

 Request request(3, "非正当理由");
 Boss *pm =new PM("pm");
 Boss *hr =new HR("hr");
 pm->SetBosssuccessor(hr);
 hr->SetBosssuccessor(NULL);
 bool pass =pm->PassRequest(request);
 printf("%d",pass);
 

BorderDecorator *border =new BorderDecorator(new ScrollDecorator(new TextView(),11),12);

 int a=-10;
// absint absobj;
 int arr[10]={0,1,2,3,4,5,6,7,8,9};
 CheckedPtr ptr(arr,arr+sizeof(arr)/sizeof(int),arr);
 ptr.operator ++();
 ptr.operator ++(0);
 //a=absobj.operat(a);
Foo f;
try {
 ptr++;
    }
   catch (exception &e)
   {
    cout << e.what() << endl;
   }
std::cout<<f[10]<<endl;

ScreenPtr *ptrscreen =new ScreenPtr(new Screen());
ptrscreen->operator ->();

int ia[3][4]={0,1,2,3,
4,5,6,7,
8,9,10,11
};
int (*ip)[4]=ia;// 这样的话,ip[0]指向ia的第0行,ip[1]指向ia的第1行,ip是一个指向含有4个int 型数组元素的指针
printf("%d,%d,%d,%d",*ip[0],*ip[1],*ip[2],*ip[3]);
ip=&ia[1];
printf("%d,%d,%d,%d",*ip[0],*ip[1],*ip[2],*ip[3]);
ip=&ia[2];
printf("%d,%d,%d,%d",*ip[0],*ip[1],*ip[2],*ip[3]);
std::string next_word;
 while (std::cin >> next_word) {
  std::cout<<next_word;
 }

  string item1, item2;

    while (cin >> item1 >> item2) {
        try {
            std::cout << item1 + item2 << std::endl;
        } catch (runtime_error err) {
            // remind the user that ISBN must match and prompt for another pair
            cout << err.what()
                 << "\nTry Again?  Enter y or n" << endl;
            char c;
            cin >> c;
            if (cin && c == 'n')
                break;      // break out of the while loop
        }
    }

 #ifndef NDEBUG
cerr << "starting main" << endl;
#endif

    string word = "foo";
    const string::size_type threshold = 5;
    if (word.size() < threshold)
        cerr << "Error: " << __FILE__
             << " : line " << __LINE__ << endl
             << "       Compiled on " << __DATE__
             << " at " << __TIME__ << endl
             << "       Word read was \"" << word
             << "\":  Length too short" << endl;
    word = "something longer than five chars";
 assert(word.size() > threshold);


 char ch;
 while((ch=cin.get())!=EOF)
 {
  cout.put(ch);
 }
 
 string s;
 cin>>s;
 ifstream infile;
 infile.open(s.c_str());
     if(!infile)
  {
   std::cerr<<"error:can not open file"<<endl;
  }
  get(infile);
 

  vector<string> files;
  string filename,s;
  while(cin>>filename)
  {
   files.push_back(filename);
  }
  for(vector<string>::const_iterator it=files.begin();it!=files.end();it++)
  {
   ifstream infile;
   infile.open(it->c_str());
   if(!infile) //打开文件失败
   {
    infile.clear();
   }
   else
   {
    string s;
    while(getline(infile,s)) //获得一行
    {
            s+=" ";//行末加上空格,避免最后一个数据无法输出
    }
    //处理完毕关闭文件
    infile.close();
    //清除流
    infile.clear();
   }
  }

  fstream inOut("copyOut", fstream::binary | fstream::ate | fstream::in | fstream::out);
  string numerics("0123456789");
  string name("r2d2");
  string::size_type pos1 = name.find("Anna");
  string::size_type pos = name.find_first_of(numerics);

  cout<<pos<<endl;
  if (pos1 != string::npos)
        cout << " " << pos1 << endl;
  else
        cout << "not found!" << endl;
string line1="we asa sas hhh";
string::size_type maxlen,minlen,count=0;
string::size_type startpos =0;

map<string ,vector<string>> children;
string surname,child;
do{
   cout<<"enter surname"<<endl;
   cin>>surname;
    if(!cin)
      break;
   vector<string> chd;
   //插入
   pair<map<string,vector<string> >::iterator,bool > ret=children.insert(make_pair(surname,chd)); //注意返回值
   if(!ret.second)
   {
    cout<<"already exist in children"<<endl;
    continue;
   }
   while(cin>>child)
   {
    ret.first->second.push_back(child);//ret.first->second 既是vector
   }
   cin.clear();//重置输出流
}while(cin);
//查找

    map<string, int> word_count;  // empty map from string to int
    string word;
    while (cin >> word)
      ++word_count[word];
map< string, vector<string> >::iterator iter =children.find(surname);
   if(iter == children.end())
   cout<<"can't find it!"<<endl;
  else
   {
    vector<string>::iterator it=iter->second.begin();
    while(it!=iter->second.end())
    {
     cout<<*it++<<endl;
    }
   }
   map<string,int> word_count;
   map<string,int>::iterator whe;
    whe = word_count.find(removal_word);
 if(whe==word_count.end())
 {
 }
 else
 {
  word_count.erase(whe);
 }
#endif
return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值