系统学习C++(1)

本人没有系统地学习过C++,最近感觉C++对我之后的学习还是很重要的,所以,打算认真地系统地学习!
当然,本博客不会展示很基础的东西,也不会展示知识点,而展示一些具体的例子,待我学习深入之后,可能会展示完整的小项目。注:本博文例子,主要参考自C++ Prime(第5版),有需要可自行网上下载
(注:本人使用的是Visual Stdio 2010,建议使用2012或者更高的版本!!)


练习3.16:编写一段程序,使用vector显示成绩等级(11个等级)的数量,即:如输入:42 5 95 100 39 67 95 76 88 76 83 92 76 93;
则会输出: 0 0 0 1 1 0 2 3 2 4 1

本人实现的代码如下:

/*
author: louishao
date: 2017/09/01
*/
#include<iostream>
#include<vector>

using namespace std;
// 本程序实现的功能,使用vector显示成绩所属等级的数量
// exp: 输入 42 65 95 100 39 67 95 76 88 76 83 92 76 93
// 输出:0 0 0 1 1 0 2 3 2 4 1

// 要正常输出需要空格隔开输入的数字,然后ctrl+z(结束循环)后,回车!
int main()
{
    vector<unsigned> scores(11,0); // 11个分数段,初始化0
    unsigned grade;
    while(cin >> grade)  // 输入分数
    {
        if(grade<=100)
            ++scores[grade/10];  // 该分数所属等级
    }
    for(vector<int>::size_type i=0;i<scores.size();i++)
        cout << scores[i] << " "; 
}

输出结果为:
这里写图片描述


练习3.18:从cin读入一组词并把它们存入一个vector对象,然后设法把所有词都改写成大写形式。输出改变后的结果,每个词占一行!

(简单分析:该题涉及到vector和string的使用)
由于我使用的是vs2010,不能使用C++11新标准的范围for(range for)语句,因此,写起来比较累赘!

/*
author: louishao
date: 2017/09/01
*/
#include <iostream>
#include <vector>
#include <string>
#include <cctype>

using namespace std;

int main()
{
    vector<string> str;
    string words;
    while(cin>>words)
    {
        str.push_back(words);
    }

    for(int i=0;i<str.size();i++)
    {
        string temps = str[i];
        for (int j=0;j<str[i].size();j++)
        {
            temps[j] = toupper(str[i][j]);
        }
        cout << temps <<endl;
    }
    return 0;
}

运行截图:
这里写图片描述


练习3.20:读入一组整数并把它们存入一个vector对象,将每对相邻整数的和输出出来。改写你的程序,这次要求先输出第一个和最后一个元素的和,接着输出第2个和倒数第2个元素的和,以此类推。

程序实现:

#include <iostream>
#include <vector>

using namespace std;

int main()
{
    int i;
    vector<int> nums;
    while (cin >> i)
    {
        nums.push_back(i);
    }

    // 输出原向量
    cout << "Original vector: " << endl;
    for (auto i=0;i<nums.size();++i)
    {
        cout << nums[i] << " ";
    }
    cout << endl;

    // 输出相邻的数的和
    cout << "sum of adjacent element: " << endl;
    for (int i=0;i+1<nums.size();++i)
    {
        cout << (nums[i]+nums[i+1]) << " ";
    }
    cout << endl;

    // 结果3
    cout << "result 3:" << endl;
    for (auto i=0;i<nums.size()/2;++i)
    {
        cout << (nums[i]+nums[(int)nums.size()-1-i]) << " ";
    }
    cout << endl;
    return 0;
}

运行结果:
这里写图片描述


练习3.39:编写一段程序,比较两个string对象。再编写一段程序,比较两个C风格字符串的内容。

代码实现如下:

#include <iostream>
#include <string>  // string对象的头文件
#include <string.h> // C++下C风格的字符串的头文件

using namespace std;

int main()
{
    // 比较两个string对象
    string s1 = "xxxx";
    string s2 = "yyyy";
    if(s1==s2)
    {
        cout << "the two strings are same!"<<endl;
    }
    else if(s1>s2)
    {
        cout << "xxxx > yyyy" << endl;
    }
    else
        cout << "xxxx < yyyy" << endl;

    // 比较两个C风格字符串的内容
    // 可使用strcmp()
    int ans;
    char ss1[] = "aaaa";
    char ss2[] = "bbbb";
    ans = strcmp(ss1,ss2);
    if(ans==0)
    {
        cout << "the same c string!" << endl;
    }
    else if(ans<0)
    {
        cout << "aaaa < bbbb" << endl;
    }
    else
        cout << "aaaa > bbbb" << endl;
    return 0;
}

运行结果如下:
这里写图片描述


练习4.25:如果一台机器上int占32位、char占8位,用的是Latin-1字符集,其中字符'q'的二进制形式是01110001,那么表达式~'q'《6的值是什么?

其实,看这一节的时候,我有点不理解负数的表示。比如:-114,表示为:1111 1111 1111 1111 1000 1110,然后我算了一下,发现1000 1110的十进制并不等于114。然后,我查了一下资料,恍然大悟,原来这个是计算机的基础知识——补码。计算机中,负数是以补码的形式表示的

而正数的补码是其本身,而负数的补码是其反码+1。

使用补码的形式,目的:简化硬件,将减法转为加法,运算更简洁!

明白补码这一点,我们开始正式分析这一题。
首先,执行~’q’操作,该操作将char提升为int,然后需要取反,得到:1111 1111 1111 1111 1111 1111 1000 1110,该二进制对应的十进制为-114,然后左移6位,即进行乘法运算,得-114*2^6=-7296。

程序运行结果:

这里写图片描述


练习5.23、24、25:编写一段程序,从标准输入读取两个整数,输出第一个数除以第二个数的结果。

分析:两数相除,当除数是0的时候,应该抛出异常!实现思路,使用try执行,遇到除数为0时,使用throw抛出异常,用runtime_error定义错误信息,最后使用catch显示出来。

代码实现:

#include <iostream>

using namespace std;
// 两数相除,要考虑除数是0的情况!

int main()
{
    int a,b;
    cout << "Please input two numbers: " <<endl;
    while(cin >> a>>b)
    {
        try
        {
            if(b==0)  // 如果除数为0,
                throw runtime_error("Divisor cannot be zero!"); // 则抛出异常
            cout << static_cast<double>(a)/b << endl;
        }
        catch (runtime_error e)  // e是runtime_error的一个实例
        {
            cout << e.what() << endl;
        }
        cout << "Repeat? Enter y or n: " ;
        char c;
        cin >> c;
        if(!cin || c=='n')
            break;
    }
}

运行截图:

这里写图片描述

未完待续。。。

猜你喜欢

转载自blog.csdn.net/louishao/article/details/77777785