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


//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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值