且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C++primer习题--第3章

更新时间:2022-06-23 22:33:57

【习题 2.11】
编写程序,要求用户输入两个数——底数( base)和指数( exponent),输出底数的指数次方的结果。

#include <iostream>
#include <math.h>
#include <string>
using namespace std;
int main( )
{
    int base, exp;
    long result=1;
    cout<<"请输入底数和指数:"<<endl;
    cin>>base>>exp;
    if(exp<0) {
        cout<<"指数不能为负数!"<<endl;
        return -1;
    }
    for(int i=1; i <= exp; i++)
        result *= base;
    cout<<base<<""<<exp<<"次方为"<<result<<endl;
    system("PAUSE");
    return 0;
}

【习题 3.7】
编一个程序读入两个 string 对象,测试它们是否相等。若不相等,则指出两个中哪个较大。接着,改写程序测试它们的长度是否相等,若不相等,则指出两个中哪个较长。

#include <iostream>
#include <string>
using namespace std;
int main( )
{
    string str1, str2;
    cin>>str1>>str2;
    if(str1 == str2)
        cout<<"str1与str2相等"<<endl;
    else
        cout<<"str1与str2不相等"<<endl;
    system("PAUSE");
    return 0;
}

【习题 3.8】

编一个程序,从标准输入读取多个 string 对象,把它们连接起来存放到一个更大的 string 对象中。并输出连接后的 string 对象。接着,改写程序,将连接后相邻 string 对象以空格隔开。

#include <iostream>
#include <string>
using namespace std;
int main( )
{
    string str, ss;
    cout<<"请输入字符串:\n";
    while(cin>>str)
        ss = ss + str;
    cout<<"连接后的字符串为:"<<ss<<endl;
    system("PAUSE");
    return 0;
}

改写后的程序:

#include <iostream>
#include <string>
using namespace std;
int main( )
{
    string str, ss;
    cout<<"请输入字符串:\n";
    while(cin>>str)
        ss= ss + ' ' + str;
    cout<<"连接后的字符串为:"<<ss<<endl;
    system("PAUSE");
    return 0;
}

【习题 3.10】

编一个程序,从 string 对象中去掉标点符号。要求输入到程序的字符串必须含 有标点符号,输出结果则是去掉标点符号后的 string 对象。

#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main( )
{
    string str, ss;
    cout<<"请输入字符串:\n";
    getline(cin, str);
    for(string::size_type i=0; i!=str.size(); ++i) {
        if(!ispunct(str[i]))
            ss+=str[i];
    }
    cout<<"连接后的字符串为:"<<ss<<endl;
    system("PAUSE");
    return 0;
}

【习题 3.13】

读一组整数到 vector 对象,计算并输出每对相邻元素的和。如果读入元素个数为奇数,则提示用户最后一个元素没有求和,并输出其值。

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

int main( )
{
    vector<int> vec;
    int n;
    while(cin>>n)
        vec.push_back(n);
    if(!vec.size()) {
        cout<<"没有数字!"<<endl;
        return -1;
    }
    for(vector<int>::size_type i=0; i<vec.size()-1; i+=2) {
        cout<<vec[i]+vec[i+1]<<"\t";
        if((i+1)%6==0) cout<<endl;
    }
    if(vec.size()%2!=0)
        cout<<endl<<"最后一个数是:"<<vec[vec.size()-1]<<endl;
    system("PAUSE");
    return 0;
}

【习题 3.14】
读入一段文本到 vector 对象,每个单词存储为 vector 中的一个元素。把 vector 对象中每个单词转化为大写字母。输出 vector 对象中转化后的元素, 每八个单词为一行输出。

#include <iostream>
#include <cctype>
#include <string>
#include <vector>
using namespace std;
void replace(string &s)  //将字符串中的所有的小写字符全部转化为大写
{
    for(int i=0; i<s.length(); ++i) {
        if(islower(s[i]))
            s[i]=toupper(s[i]);
    }
}
int main( )
{
    int n;
    string str;
    vector<string> vec;
    n=1;
    cout<<"请输入一段文本:\n";
    while(cin>>str)
        vec.push_back(str);
    for(vector<string>::iterator i=vec.begin(); i!=vec.end(); ++i) {
        replace(*i);
        cout<<(*i);
        if(n%8==0)
            cout<<endl;
        else
            cout<<" ";
        n++;
    }
    system("PAUSE");
    return 0;
}

【习题 3.18】

编写程序来创建有 10 个元素的 vector 对象。用迭代器把每个元素值改为当前 值的 2 倍,输出 vector 的所有元素。

#include <iostream>
#include <vector>
using namespace std;
int main( )
{
    vector<int> vec(10,2);
    for(vector<int>::iterator it=vec.begin(); it!=vec.end(); it++) {
        *it=(*it)*2;
        cout<<(*it)<<" ";
    }
    cout<<endl;
    system("PAUSE");
    return 0;
}