美文网首页
《双语版C++程序设计》《C++》第六章部分习题解

《双语版C++程序设计》《C++》第六章部分习题解

作者: 凌丶星护 | 来源:发表于2018-10-18 17:23 被阅读0次

    Exercise 7:

    Write a program to read in a line of text from the keyboard and calculate the average length of the worth in that line. Assume each word in the line is separated from the next by at least one space. Allow for punctuation marks. Use C++ strings.

    翻译

    写一个从键盘读取一行文本,然后计算这一行中单词的平均长度的一个程序。假设这一行中每一个单词和下一个单词之间至少有一个空格。允许使用标点符号。使用C++字符串

    分析

    从键盘读取一行文本,可以使用getline(),读取到的结果保存到一个string类型的变量input中。
    关于单词的平均长度,需要声明一个float类型的变量average,而它的计算,则需要两个整形变量wordsNumlettersNum
    在统计单词个数和字母个数之前,需要先将字符串中所有的标点符号处理掉,可以通过新创建一个字符串变量check,里面存储所有的单词与一个空格,然后通过find_first_not_of()找出标点符号,再使用erase()删除掉标点,重复直到所有的标点符号都被删除完,即查找函数返回值为-1。
    接下来便需要统计一下单词个数和字母个数,通过以空格为界,使用find()找到空格,将前面的单词存储到另一个字符串中,在input中通过查找得到的下标删除这个单词和这个空格,单词个数+1,字母个数+=另一个字符串的长度,查找下一个空格,需要注意的是,有可能中间间隔多个空格,可以通过判断查找出的空格下标是否为0进行判断,之后重复之前的内容,直至没有空格,此时,原字符串要么没有任何内容,要么存在最后一个单词,此时通过它是否为空判断是否对单词数与字母数进行加的操作。
    最后,通过除法运算便可以求出平均数。

    流程

    1. 清除掉所有的标点符号
    2. 按照空格的存在将单词放入新的变量中,将单词数和字母数增加相应值。
    3. 字母数除以单词数得到平均数

    代码

    //  Programe Name    : Exercise 6-07
    //  Written by       : by_sknight
    //  Date             : 18/10/2018
    //  Version          : 0.01
    
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main(void)
    {
        string input, word;
        float average;
        int wordsNum = 0, lettersNum = 0;
        string check = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ";
        // getline(cin, input);
        input = "One Word, Two Words, Three Words! Four Words?";
        // 清除出现的标点符号
        while (input.find_first_not_of(check) != -1)
            input.erase(input.find_first_not_of(check), 1);
        while (input.find(" ") != -1)
        {
            if (input.find(" ") != 0)
            {
                word = input.substr(0, input.find(" "));
                wordsNum++;
                lettersNum += word.length();
            }
            input.erase(0, input.find(" ") + 1);
        }
        
        if (input.empty() == false)
        {
            wordsNum++;
            lettersNum += input.length();
        }
        
        average = (float)lettersNum / wordsNum;
        
        cout << average << endl;
        
        return 0;
    }
    

    Exercise 9:

    Write a program to input a user's first and last name and generate a password for the user. The password is made up from the first middle and last character from the first and last name.
    Example:
    Enter your first name: Yang
    Enter your second name: Liwei
    Your password is YngLwi

    翻译

    写一个输入用户第一个和最后一个名字然后为用户生成一个密码的程序。这个密码由第一个名字和最后一个名字的第一个、中间、最后一个字符组成。
    示例:
    Yang Liwei -> YngLwi

    分析

    通过下标可以看出这个中间字母的选取很简单,只需要字符串长度除以2就可以了。

    流程

    1. 读取输入firstNamesecondName
    2. 将其对应位置的字母组合为一个新的变量password

    代码

    //  Program name    : Exercise 6-09
    //  Written by      : by_sknight
    //  Date            : 19/10/2018
    //  Version number  : 0.01
    
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main(void)
    {
        string firstName, secondName;
        string password;
        cout << "Enter your first name: " << endl;
        // cin >> firstName;
        firstName = "Yang";
        cout << "Enter your second name:" << endl;
        // cin >> secondName;
        secondName = "Liwei";
        password.assign(6, '*');
        password.at(0) = firstName.at(0);
        password.at(1) = firstName.at(firstName.length() / 2);
        password.at(2) = firstName.at(firstName.length() - 1);
        password.at(3) = secondName.at(0);
        password.at(4) = secondName.at(secondName.length() / 2);
        password.at(5) = secondName.at(secondName.length() - 1);
        
        cout << password << endl;
        return 0;
    }
    

    Exercise 10

    Write a program to ask a user for their name. The user's name is then compared with a list of names held in an array in memory. If the user's name is in this list, display a suitable greeting; otherwise display the message "Name not found".

    翻译

    写一个请求用户姓名的程序。将用户名与内存中的一个名字列表进行比较。如果用户输入的名字在这个列表中,显示一个合适的欢迎语,否则显示“名字未找到”。

    分析

    涉及到字符串的比较,C++可以直接用==运算符去比较两个字符串对象。需要的只是一个存储字符串的数组names,和用户输入的名字input,此外,需要设置一个标记,如果名字在列表中,标记赋值为1。等跳出循环后就可以通过标记判断名字是否存在于列表中。

    流程

    1. 创建一个用户名列表names并赋值
    2. 提示用户输入他自己的名字,并将其存储在input中。
    3. 循环比较列表中的每一个名字与用户名,如果找到了就可以跳出,或者查找完了所有列表才可以跳出
    4. 判断标记的值是否为1,如果是1,就输出欢迎语句,否则输入未找到语句

    代码

    //  Program name    : Exercise 6-10
    //  Written by      : by_sknight
    //  Date            : 19/10/2018
    //  Version number  : 0.01
    
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main(void)
    {
        string input;
        string names[10] = { "One", "Two", "Three",
                            "Four", "Five", "Six",
                            "Seven", "Eight", "Nine", "Ten"};
        cout << "Please input your name : ";
        // getline(cin, input);
        input = "Ten";
        
        int i, flag = 0;
        for (i = 0; i < 10; i++)
        {
            if (names[i] == input)
            {
                flag = 1;
                break;
            }
        }
        
        if (flag == 1)
        {
            cout << "Hello, " << input << endl;
        }
        else
        {
            cout << "Name not found.";
        }
        
    }
    

    Exercise 11

    Write a program to input a string. if every character in the string is a digit('0' to '9'), then convent the string to an integer, add 1 to it, and display the result. If any one of the characters in the string is not a digit, display an error message.

    翻译

    写一个输入一个字符串的程序,如果字符串中的每一个字符都是一个数字字符,那就讲这个字符串转化为一个整数,并加1,然后显示这个结果,如果字符串中存在一个不是数字字符的字符,显示一个错误的信息。

    分析

    判断是否全部是数字字符,通过查找是否存在不是数字字符的字符可以得到。find_first_not_of()
    转化成为数字,可以通过与最后一位的下标差值判断应该乘以10的多少次方,需要用到次方函数pow(),此函数存在于math.h头文件中。比如说,最后一个数字为个位,他与最后一位下标相差为0,所以它乘以10的0次方,就是乘以1,倒数第三位,为百位,他与最后一位下标相差2,所以它乘以10的2次方,即乘100。以此类推。
    注意:数字字符不能直接运算,需要与数字字符'0'相减得到可以运算的值

    流程

    1. 读取输入input
    2. 查找是否存在不是数字字符的字符,如果存在,输出错误信息,结束,不存在就进入下面的流程
    3. 将最后一位的下标提取出来为end,并新建一个用于存储整数的变量output
    4. 循环整个字符串,按照它所在的位加到output中。
    5. 加1输出

    代码

    //  Program name    : Exercise 6-11
    //  Written by      : by_sknight
    //  Date            : 19/10/2018
    //  Version number  : 0.01
    
    #include <iostream>
    #include <string>
    #include <math.h>
    
    using namespace std;
    
    int main(void)
    {
        string input;
        // getline(cin, input);
        input = "99999999";
        
        if (input.find_first_not_of("0123456789") == -1)
        {
            int output, end, i;
            output = 0;
            end = input.length() - 1;
            for (i = 0; i < end + 1; i++)
            {
                output += (input.at(i) - '0') * pow(10, end - i);
            }
            output += 1;
            cout << output << endl;
        }
        else
        {
            cout << "Error!" << endl;
        }
    }
    

    Exercise 13

    Initialise an array of strings with the following quotations:
    "Threr is no reason for any individual to have a computer in their home."
    "Computers are useless. They can only give you answers."
    "To erris human, but to really foul things up requires a computer."
    "The electronic computer is to individual privacy what the machine gun was to the horse cavalry."
    Input a word from the keyboard and display all quotations, if any, containing that word.

    翻译

    用下列引文初始化字符串数组: “任何个人都没有理由在家里拥有一台电脑。” “电脑是无用的。他们只能给你答案。 “犯人类的错误,但真正把事情搞糟需要一台电脑。” 电子计算机对于个人隐私的重要性就像机关枪对于骑兵的重要性一样。 从键盘输入一个单词,并显示所有包含该单词的引语(如果有的话)。
    以上翻译来源为有道翻译。

    分析

    只需要一个find()便可以搞定,如果找到,输出引语,没找到则不用输出

    流程

    1. 初始化一个引语的字符串数组。
    2. 获取用户输入的单词
    3. 循环整个数组,进行查找,如果存在,输出该条引语,不存在,则在下一条引语中查找,知道循环完整个数组

    代码

    //  Program name    : Exercise 6-13
    //  Written by      : by_sknight
    //  Date            : 19/10/2018
    //  Version number  : 0.01
    
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main(void)
    {
        string quotations[4] = {
            "There is no reason for any individual to have a computer in their home.",
            "Computers are useless. They can only give you answers.",
            "To err is human, but to really foul things up requires a computer.",
            "The electronic computer is to individual privacy what the machine gun was to the horse cavalry"
            };
    
        string word;
        cout << "Please input a word: ";
        word = "computer";
        int i;
        for (i = 0; i < 4; i++)
        {
            if (quotations[i].find(word) != -1)
                cout << quotations[i] << endl;
        }
        
        return 0;
        
    }
    

    相关文章

      网友评论

          本文标题:《双语版C++程序设计》《C++》第六章部分习题解

          本文链接:https://www.haomeiwen.com/subject/vnyszftx.html