2016年10月4日 星期二

(綠色皮) 最新C++程式語言, 施威銘研究室 著, 旗標 第十一章 字串 '參考' 答案

==============================================
/* 程式檔名:Test_C11-38-1.cpp */
/* 程式目的:請寫一程式可將字串中的'a'都換成'A'*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20151027 */

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

int main()
{
  string s,target="a";
  cout << "請輸入一個字串:";
  cin >> s;
;
  unsigned int i, count = 0, pos = 0;
  cout << endl << "在[" << s << "]中" << endl;
  while ((i = s.find(target,pos))!=string::npos) {
    count++;       // 計數器加 1
    cout << "第 " << count << " 次出現[" << target
         << "]的位置是在第 " << i+1 << " 個字" << endl; 
    pos = i + 1;   // 從前次找到的位置之後繼續搜尋
    s[i] = toupper(s[i]);
  }
  
  if (count == 0)   // 若傳回 npos
    cout << "沒有符合[" << target << "]的字串!"<< endl;
  else
    cout << "總共找到 " << count << " 次" << endl;
  cout << endl << "修改後字串:" << s << endl;
  system("PAUSE");
  return 0; 
}
==============================================
/* 程式檔名:Test_C11-38-2.cpp */
/* 程式目的:將字串中的每個英文單字(word)的字首變成大寫,例如"hello word" 會變成"Hello World"*/
/* 程設計:蘇彥儒*/
/* 完成日期:20151027 */

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

int main()
{
  string s, target=" \n\t";
  cout << "請輸入一個字串:";
  getline(cin, s);
  s[0] = toupper(s[0]);
  unsigned int i, count = 0, pos = 0;
  while ((i = s.find_first_of(target,pos))!=string::npos) {
    count++;       // 計數器加 1
    pos = i + 1;   // 從前次找到的位置之後繼續搜尋
    s[pos] = toupper(s[pos]);
  }
  cout << endl << "修改後字串:" << s << endl;
  system("PAUSE");
  return 0; 
}
==============================================
/* 程式檔名:Test_C11-38-3.cpp */
/* 程式目的:讓使用者輸入10個字串,並將此10個字串排序*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20151027 */

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

int main()
{
  cout << "本程式會將輸入的字串排序。\n";

  string str[10];
  for (int i=0;i<10;i++) {
    cout << "請輸入第 " << i+1 << " 個字串:";
    getline(cin,str[i]);
  }

  for(int i=0;i<9;i++)      // 排序的迴圈
    for(int j =i+1;j<10;j++)
      if(str[i]>str[j]) {
        string tmp = str[i];
        str[i] = str[j];
        str[j] = tmp;
      }

  cout << "排序後:\n";
  for(int i=0;i<10;i++)      // 輸出所有字串
    cout << str[i] << '\n';

}

==============================================
/* 程式檔名:Test_C11-38-4.cpp */
/* 程式目的:可檢查使用者輸入的是否為合法的電話號碼格式*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20151027 */

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

int main()
{
  string idStr;      // 記錄使用者輸入資料
  bool isID = false; // 使用者輸入的格式是否正確
  
  do {
    cout << "請輸入電話號碼(xxxx-xxxxxx):";
    getline(cin,idStr);
    
    // 將所有的空白去除
    string::size_type pos=0, i;
    while((i=idStr.find_first_of(" \t",pos)) != string::npos) {
      idStr.erase(i,1);
      pos = i;
    }
    
    if ((idStr.size()==11) && (idStr.find("-")!=string::npos) && (idStr.substr(0,3).find_first_not_of("0123456789")==string::npos) && (idStr.substr(5,10).find_first_not_of("0123456789")==string::npos) )
    {
     cout << "輸入正確" << endl;
     isID = true;
    }
    else
    {
     cout << "請輸入正確的格式" << endl;    
    } 
  } while (!isID);
  
  system("PAUSE");
  return 0; 
}
==============================================
/* 程式檔名:Test_C11-38-5.cpp */
/* 程式目的:要求使用者輸入正確格式的電子郵件信箱*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20161004 */

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

string specials = "()<>@,;:\\\"[]";  // 特殊字元


bool checkname(string str)    // 檢查名稱的函式
{
//判斷是否有特殊字元 
  if(str.find_first_of(specials)!=string::npos)
    return false;

//判斷是否在可顯示字元內 
  for(int i=0;i<(int)str.size();i++)
    if(str[i] < 32 || str[i] >=127)
       return false;
    
  return true;
}

bool checkserver(string str)  // 檢查伺服器位址的函式
{
  //判斷是否有特殊字元 
  if(str.find_first_of(specials)!=string::npos)
    return false;

  int dot=0;    // 計算有幾個 '.'
  for(int i=0;i<(int)str.size();i++) {
    if(str[i] == '.')
      if((i==0) || (str[i+i] == '.'))  // 如果第 1 個字是 '.'
        return false;                  // 或連續出現兩個 '.'
      else
        dot++;
    //判斷是否有特殊字元 
    else if(str[i] < 32 || str[i] >=127)
       return false;
  }

  if(dot>0)    // 位址中至少要有一個 '.'
    return true;
  else
    return false;
}

int main()
{
  string email = "test";

  while (email != "0" ) {
    cout << "請輸入電子郵件地址(輸入0結束):";
    getline(cin,email);

    // 將所有的空白去除
    string::size_type pos=0, i;
    while((i=email.find_first_of(" \t",pos)) != string::npos) {
      email.erase(i,1);
      pos = i;
    }

    // 將字串從 @ 分成前後兩部份 name@server
    pos = email.find_first_of("@");

    if(pos == string::npos ||              // 如果沒有 @
       pos == 0 || pos == email.size()) {  // 或 @ 是第 1 個字或最後 1 字
      cout << "格式錯誤\n";
      continue;
    }

//將輸入的Email 分為帳號部分及網域部分做檢查 
    if(checkname(email.substr(0,pos)) &&
       checkserver(email.substr(pos+1)))
      cout << "格式正確\n";
    else
      cout << "格式錯誤\n";
  }
}
==============================================
/* 程式檔名:Test_C11-38-6.cpp */
/* 程式目的:請撰寫一個程式,讓使用者可以YYYY/MM/DD的格式輸入日期,並以"YYYY年MM月DD日"的
格式顯示使用者所輸入的日期*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20161004 */

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

bool check(string str)    // 檢查日期格式的函式
{                         // 檢查是否含數字以外的字元
  string legal = "0123456789";
  if(str.find_first_not_of(legal) == string::npos && (str.size() != 0))
    return true;
  else
    return false;
}


int main()
{
  string dates = "test";
  string yy,mm,dd;

  while (dates != "0" ) {
    cout << "請輸入日期字串YYYY/MM/DD(輸入0結束):";
    getline(cin,dates);

if(dates=="0")
break;

    // 分別取得年、月、日三個子字串
    string::size_type pos1, pos2;
    if ((pos1 = dates.find_first_of("/"))== string::npos) {
      break;                            // 如果找不到 /
      cout << "輸入的日期格式錯誤\n";
      }
    else
      yy = dates.substr(0,pos1);
    pos2 = dates.find_first_of("/",pos1+1);
    mm = dates.substr(pos1+1,pos2-pos1-1);
    pos1 = dates.find_first_of("/",pos2+1);
    dd = dates.substr(pos2+1);

    if(check(yy) && check(mm) && check (dd))
      cout << yy << "年"
           << mm << "月"
           << dd << "日" << '\n';
    else
      cout << "輸入的日期格式錯誤\n";
  }
}
==============================================
/* 程式檔名:Test_C11-38-7.cpp */
/* 程式目的:請設計一函式可去掉字串物件中的空白字元*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20161004 */

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

string& trim(string& str)    // 去除字串中空白的函式
{
// 將所有的空白去除
    string::size_type pos=0, i;
    while((i=str.find_first_of(" \t",pos)) != string::npos) {
      str.erase(i,1);
      pos = i;
    }
    return str;
}

int main()
{
  string strerase = "test";

  while (strerase != "0" ) {
    cout << "請輸入資料(輸入0結束):";
    getline(cin,strerase);
    cout << "刪除資料中的空格:" << trim(strerase) << endl;
  }

}
==============================================
/* 程式檔名:Test_C11-38-8.cpp */
/* 程式目的:請設計一函式可將String字串物件中的英文字母大小寫互換*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20161004 */

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

string& change(string& str)
{
for(string::size_type i=0;i<str.size();i++)
    if(isupper(str[i]))
      str[i] = tolower(str[i]);
    else if(islower(str[i]))
      str[i] = toupper(str[i]);
  return str;
}

int main()
{
  string stra = "test";

  while (stra != "0" ) {
    cout << "請輸入資料(輸入0結束):";
    getline(cin,stra);
    cout << "刪除資料中的空格:" << change(stra) << endl;
  }
  

}

==============================================
/* 程式檔名:Test_C11-38-9.cpp */
/* 程式目的:請撰寫一個程式,可將使用者輸入的檔案名稱分成主檔名及副檔名兩部分輸出*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20161004 */

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

int main()
{
  string filename;

  cout << "請輸入一檔案名稱:";
  cin >> filename;

  // 長檔名中可能有多個 .
  // 以最後一個點之後的子字串為副檔名
  string::size_type dot = filename.find_last_of(".");
  string mainfilename = filename.substr(0,dot);
  string subfilename  = filename.substr(dot+1);

  if(mainfilename.size() != 0) {
    cout << "主檔名為:" << mainfilename << '\n';
    if(subfilename.size() != 0)
      cout << "副檔名為:" << subfilename << '\n';
    else
      cout << "沒有副檔名";
  }
  else  // 主檔名為空字串
    cout << "無主檔名,格式錯誤";

}

==============================================
/* 程式檔名:Test_C11-38-10.cpp */
/* 程式目的:請撰寫一string物件比對程式,程式會顯示兩字串從第幾個字開始是不同的*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20161004 */
#include<iostream>
#include<string>
#include<cctype>
using namespace std;

string::size_type diff(string str1, string str2)
{
  string::size_type offset;
  for (offset = 1;offset<str1.size() || offset<str2.size();offset++)
    if(str1[offset] != str2[offset])
      break;
  return offset+1;
}

int main()
{
  string str1,str2;

  cout << "請輸入兩個字串以進行比對。\n";
  cout << "字串1:";
  getline(cin,str1);
  cout << "字串2:";
  getline(cin,str2);

  if(str1 == str2)
    cout << "兩字串內容相同。\n";
  else
    cout << "兩字串從第 " << diff(str1,str2) << " 個字開始不同。\n";

}

==============================================

沒有留言:

張貼留言