C++ Primer 学习笔记 ——语句

来源:互联网 发布:离线整个知乎 编辑:程序博客网 时间:2024/05/21 04:41
//p164#include<iostream>#include<string>using std::string;using std::cin;using std::cout;int main(){    char i;    int count_vowel=0;    int count_blank = 0, count_t = 0, count_n = 0, count_ff = 0;    while ((i = getchar() )!= EOF)//用cin读入时候,i的类型是什么、??????????    {        switch(i)        {        case 'a':case 'A':        case 'e':case 'i':        case 'o':case 'u':            count_vowel++; break;        case '\n':count_n++; break;        case '\t':count_t++; break;        case' ':count_blank++; break;        case'ff':count_ff++;//无法统计2个字符的。????????        default:break;        }    }    cout << count_vowel << " " << count_blank << " " << count_n << " " << count_t << " " << count_ff;    return 0;}
//5.4.1#include<iostream>#include<string>#include<vector>using std::string;using std::vector;using std::cout;using std::cin;using std::endl;int main(){    vector<string> vstring;    string word, next;    cin >>  word;    vstring.push_back(word);    next = word;    int count = 1;    while (cin >> word)    {        vstring.push_back(word);        if (word == next)            count++;        else        {            cout<<next<<":" << count<<endl;             count = 1;        }        next = word;    }    return 0;}//#include<iostream>//#include<string>//#include<vector>//using std::string;//using std::cin;//using std::vector;//using std::cout;//int main()//{//  vector<string> v;//  string i;//  int count;//  while (cin >> i)//      v.push_back(i);//  for (auto c : v)//      cout << c << std::endl;//  for (int index = 1; index != v.size()-1; index++)//  {//      while (v[index - 1] == v[index] && index != v.size()-1 )//v[index - 1]为何不行???????//      {//          count++;//          index++;//      }//      cout << count;//      count = 0;//  }//  int index = 0;//  /*if (v[index] == v[index - 1])//      cout << v[index];*///  cout << v[index+1];//  return 0;//}
//5.17#include<iostream>#include<vector>using std::vector;using std::cout;using std::endl;int main(){    vector<int> v1{ 0, 1, 1, 2 }, v2{ 0, 1, 1, 2, 3, 5, 8 };    for (int index = 0; index < v1.size() && index < v2.size(); index++)    {        if (v1[index] != v2[index])            cout << false;    }    cout << true;    return 0;}
//5.19#include<iostream>#include<string>using std::cout;using std::cin;int main(){    std::string st1, st2;    do    {        std::cin >> st1 >> st2;        cout<<(st1 < st2 ? st1 : st2);    } while (cin);    return 0;}

break
负责终止离他最近的while,do while ,for ,swith;

//5.5.1#include<iostream>#include<string>using std::string;using std::cout;using std::cin;int main(){    string st,last;    cin >> st;    last = st;    int flag = 0;    while (cin >> st)    {        if (st == last)        {            cout << st;            flag = 1;            break;        }        last = st;    }    if (!flag)        cout << "none";    return 0;}

goto
goto lable;

try和异常处理

//5.6.3#include<iostream>#include<stdexcept>using std::cin;using std::cout;int main(){    int a, b;    /*cin >> a >> b;*/    //if (b == 0)    //  throw "error";    while (cin >> a >> b)    {        try{            if (b == 0)                throw "error";        }        catch (const char*)        {            cout << "error and try again";            continue;        }        cout << a / b;    }    cout << a / b;    return 0;}
0 0