[C++]第一、二、三次作业

// TCPL ; Pages 105 ; 11
// used set class ...
// ...Sort the words before printing them.

#include <iostream>
#include <string>
#include <set>
#include <vector>
using namespace std;

void main()
{
 string word ;
 typedef set<string> StringSet;
 typedef StringSet::iterator SetIter;
 typedef vector<SetIter> SetIterVec;
 typedef SetIterVec::iterator VecSetIter;
 SetIterVec wordlist1;
 StringSet wordlist2;
  while ( cin >> word && word!="Quit" )
 {
  //加入到list2
  pair< SetIter , bool> const &temp = wordlist2.insert( word );
  //加入到list1
  if ( temp.second )
   wordlist1.push_back ( temp.first );
 }
 cout << "/nResult ( Not in order ):" << endl;
 for ( VecSetIter iter = wordlist1.begin(); iter!= wordlist1.end() ; iter++ )
  cout << **iter << endl; // 打印结果1
 
 // 此处,在VS6.0下,将无法通过。

 cout << "/nResult ( In order ):" << endl;
 for ( SetIter iter = wordlist2.begin(); iter!= wordlist2.end() ; iter++ )
  cout << *iter << endl; // 打印结果2
}

// 清翔兔 05/09/20
// 05/10/15修改
// Update URL: http://spaces.msn.com/members/freshare/Blog/cns!1pPhRIUFlbpGjTAyp8hrCt-A!400.entry

// TCPL ; Pages 105 ; 12
#include <iostream>
#include <string>
using namespace std;

int wordStCount (const string &str , const string &word)
{
 int count=0;
 int find = str.find(word);
 while ( find != -1 )
  {
   count++;
   find = str.find(word ,find + word.size());   //从上次找到的位置之后开始寻找下一个关键字
  }
 return count;
}

int wordPstCount (const char *p ,const string &word)
{
 int count=0;
 const char *p1;
 while (*p!='/0')
 {
  p1=p;
  for (int i=0, flag = 0; i< word.size() ;i++,p1++)  //与关键字进行对比
  {
   if( *p1 != word[i]) break;
      if( i == word.size()-1 ) count++;
  }
  p++;
 }
 return count;
}

void main()
{
 const string word = "boy";
 const char * pst = "I'm a very good boy. I'm 22-year-old boy.";
 const string st = pst;
 int stNum = wordStCount( st , word);
 int pstNum = wordPstCount ( pst , word );
 cout << "stNum: " << stNum  << "/n"
   << "pstNum: " << pstNum 
   << endl;
}

// 清翔兔 05/09/20
// Update URL: http://spaces.msn.com/members/freshare/Blog/cns!1pPhRIUFlbpGjTAyp8hrCt-A!400.entry

// 程序仅在VC7.0下通过
// TCPL ; Pages 164 ; 9

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

void main(int argc, char *argv[])
{
 const char *key;
 int len = 1;
 // 以下获取Key
 if ( argc ==1 )
 {
  const char *code1 = "";
  key = code1;
 }
 else
 {
  const char *code1 = argv[1];
  key = code1;
  len = strlen(key);
 }
 cout << "Key:" << key << endl;
 // 从文件输入输出
 string infileName;
 string outfileName;
 cout << "请输入读入文件名:" << endl;
 cin >> infileName;
 ifstream infile (infileName.c_str());
 if ( !infile )
 {
  cerr << "读入文件错误!";
  return;
 }

 cout << "请输入新的文件名:" << endl;
 cin >> outfileName;
 ofstream outfile (outfileName.c_str());

 char inputcode;
 // 编码并输出
 for (int i = 0 ; infile.get(inputcode) ; i = (i+1) % len ) //问题:可能提前终止
  outfile << char(inputcode^key[i]);
      //cout << char(inputcode^key[i]);
}

// 清翔兔 05/10/20
// Update URL: http://spaces.msn.com/members/freshare/Blog/cns!1pPhRIUFlbpGjTAyp8hrCt-A!405.entry

// 程序仅在VC7.0下通过
// TCPL ; Pages 164 ; 11

#include <stdarg.h>
#include <iostream>
using namespace std;

void error(char const *ch, ...) {
   va_list ap;
   va_start( ap, ch );
   for ( ; *ch; ++ch)
    if ( *ch=='%')
    {
     ch++;
     switch ( *ch )
     {
     case 's':
      cerr << va_arg(ap, char const*);
      break;
     case 'c':
      cerr << va_arg(ap, char);
      break;
     case 'd':
      cerr << va_arg(ap, int);
      break;
     default:
      break;
     }
    }
    else
     cerr << *ch;
   va_end(ap);
}


void main()
{
 error("My name is %s %c and I'm %d years old./n" , "Mao",',',21);
}


// 清翔兔 05/10/21
// Update URL: http://spaces.msn.com/members/freshare/Blog/cns!1pPhRIUFlbpGjTAyp8hrCt-A!405.entry

// 程序仅在VC7.0下通过
// TCPL ; Pages 164 ; 15

// 修改为从文件读入 10/27

#include <iostream>
#include <fstream>
#include <map>
#include <string>
using namespace std;


void main()
{
 map<string,string> def;
 string word;
 char p;
 //设定读入文件名称
 string infileName = "code.txt";

 ifstream inoutfile (infileName.c_str());
 cout << "输入代码: " << endl;
 while ( inoutfile.get(p) ) cout << p;

 cout << "/n处理后代码: " << endl;
 ifstream infile (infileName.c_str());
 if ( !infile )
 {
  cerr << "读入文件错误!";
  return;
 }
 while (infile >> word)
 {
  //cout << word;
  if ( word == "#define" )
  {
   string instead;
   infile >> word >> instead;
   def[word] = instead;
   cout << "#define " << word  << " " << instead <<endl;
   continue;
  }
  
  map<string,string>::iterator it = def.find(word);
  if ( it!= def.end() )
   cout << def[word];
  else
   cout << word;
  infile.get(p);
  cout << p;
 }
}


// 清翔兔 05/10/21
// 05/10/27 修改
// Update URL: http://spaces.msn.com/members/freshare/Blog/cns!1pPhRIUFlbpGjTAyp8hrCt-A!405.entry

阅读更多
换一批

没有更多推荐了,返回首页