[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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章