按顺序显示字符串 - 练习题

时间:2011-04-12 21:52:22

标签: c++ visual-studio string

所以,我正在阅读Bjarne Stroustrup的“编程:使用c ++进行校长和练习”,我在第3章。书中有一个问题是“编写一个程序,提示用户输入3个字符串然后,如果用户输入值“Steinbeck”,“Hemingway”,“Fitzgerald”,则输出应为“Fitzgerald,Hemingway,Steinbeck”。

我目前仍然坚持这个问题,我似乎无法使该计划有效。我知道如何以数字顺序输出整数,就像使用此代码http://www.stroustrup.com/Programming/Solutions/Ch3/e3-6.cpp但我似乎无法修改它以使其与字符串值一起工作(我得到错误“致命错误LNK1120:1未解析的外部”,我希望了解如何这样做。

感谢任何帮助。

ps:这是修改后的链接内容的代码(这是我对回答问题的蹩脚尝试)。而且我知道有更简单的方法可以这样做,但我还没有,它将在下一章(向量):

#include "D:\Mes Documents\Visual Studio 2010\std_lib_facilities.h"

int main()
{
    string name1 = " ";
    string name2 = " ";
    string name3 = " ";

    cout << "Please enter three names seperated by spaces: " << endl;
    cin >> name1 >> name2 >> name3;
    if (!cin) error("Something went wrong with the read");

    string smallest = " ";
    string middle = " ";
    string biggest = " ";

    if (name1<=name2 && name1<=name3)
    {
        smallest = name1;
        if (name2<=name3)
        {
            middle = name2;
            biggest = name3;
        }
        else
        {
            middle = name3;
            biggest = name2;
        }
    }
    else if (name2<=name1 && name2<=name3)
    {
        smallest = name2;
        if (name1<=name3)
        {
            middle = name1;
            biggest = name3;
        }
        else
        {
            middle = name3;
            biggest = name1;
        }
    }
    else
    {
        smallest = name3;
        if (name1<=name2)
        {
            middle = name1;
            biggest = name2;
        }
        else
        {
            middle = name2;
            biggest =name1;
        }
    }

    cout << "The names in order are: " << smallest << " " << middle << " " << biggest << endl;

    system("pause");
    return 0;
}

3 个答案:

答案 0 :(得分:0)

以这种方式解决#6 - 与任务所要求的略有不同。稍后会纠正并发布3.7的解决方案。

#include "../../std_lib_facilities.h"
int main()
{
    cout << "Enter three integers:\n";
    int a = 0;
    int b = 0;
    int c = 0;
    cin >> a >> b >> c;
    if (a == b)
    {
        if (b == c)
        {
            cout << '\n' << a << " = " << b << " = " << c;
        }
        else if (a > c)
        {
            cout << '\n' << a << " = " << b << " > " << c;
        }
        else if (a < c)
        {
            cout << '\n' << c << " > " << a << " = " << b;
        }
    }
    else if (a == c)
    {
        if (a > b)
        {
            cout << '\n' << a << " = " << c << " > " << b;
        }
        else if (a < b)
        {
            cout << '\n' << b << " > " << a << " = " << c;
        }
    }
    else if (b == c)
    {
        if (b > a)
        {
            cout << '\n' << b << " = " << c << " > " << a;
        }
        else if (b < a)
        {
            cout << '\n' << a << " > " << b << " = " << c;
        }
    }
    else if (a > b)
    {
        if (a > c)
        {
            if (b > c)
            {
                cout << '\n' << a << " > " << b << " > " << c;
            }
            else if (c > b)
            {
                cout << '\n' << a << " > " << c << " > " << b;
            }
        }
        else if (a < c)
        {
            cout << '\n' << c << " > " << a << " > " << b;
        }
    }
    else if (b > a)
    {
        if (b > c)
        {
            if (a > c)
            {
                cout << '\n' << b << " > " << a << " > " << c;
            }
            else if (c > a)
            {
                cout << '\n' << b << " > " << c << " > " << a;
            }
        }
        else if (b < c)
        {
            cout << '\n' << c << " > " << b << " > " << a;
        }
    }
}

答案 1 :(得分:0)

我已经使用VS 2013 Express for Windows Desktop。设置与B.3.1相同。此外,我正在为所有任务使用相同的项目。我只是排除旧的.cpp并创建一个新的.cpp。确保您已经排除了所有旧的cpp文件!他们似乎互相干扰

所以,“正确”的解决方案。对于3.6,只需将字符串更改为int,将“输入三个名称”更改为“输入三个整数”:

3.7

#include "../../std_lib_facilities.h"
int main()
{
    cout << "Enter three names:\n";
    string a = " ";
    string b = " ";
    string c = " ";
    cin >> a >> b >> c;
    if (a == b)
    {
        if (b == c)
        {
            cout << '\n' << a << ", " << b << ", " << c; 
        }
        else if (a > c)
        {
            cout << '\n' << c << ", " << a << ", " << b;
        }
        else if (a < c)
        {
            cout << '\n' << a << ", " << b << ", " << c;
        }
    }
    else if (a == c)
    {
        if (a > b)
        {
            cout << '\n' << b << ", " << a << ", " << c;
        }
        else if (a < b)
        {
            cout << '\n' << a << ", " << c << ", " << b;
        }
    }
    else if (b == c)
    {
        if (b > a)
        {
            cout << '\n' << a << ", " << b << ", " << c;
        }
        else if (b < a)
        {
            cout << '\n' << b << ", " << c << ", " << a;
        }
    }
    else if (a > b)
    {
        if (a > c)
        {
            if (b > c)
            {
                cout << '\n' << c << ", " << b << ", " << a;
            }
            else if (c > b)
            {
                cout << '\n' << b << ", " << c << ", " << a;
            }
        }
        else if (a < c)
        {
            cout << '\n' << b << ", " << a << ", " << c;
        }
    }
    else if (b > a)
    {
        if (b > c)
        {
            if (a > c)
            {
                cout << '\n' << c << ", " << a << ", " << b;
            }
            else if (c > a)
            {
                cout << '\n' << a << ", " << c << ", " << b;
            }
        }
        else if (b < c)
        {
            cout << '\n' << a << ", " << b << ", " << c;
        }
    }
}

答案 2 :(得分:0)

我使用了相同的链接,并进行了修改以获取所需的输出: http://www.stroustrup.com/Programming/Solutions/Ch3/e3-6.cpp

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;
int main()
{

    cout << "Please enter three string values separated by a space:\n";
    string val1;
    string val2;
    string val3;
    cin >> val1 >> val2 >> val3;

    string smallest ="";
    string middle ="";
    string largest ="";
    if (val1<=val2 && val1<=val3) {
        smallest = val1;
        if (val2<=val3) {
            middle = val2;
            largest = val3;
        }
        else {
            middle = val3;
            largest = val2;
        }
    }
    else if (val2<=val1 && val2<=val3) {
        smallest = val2;
        if (val1<=val3)  {
            middle = val1;
            largest = val3;
        }
        else {
            middle = val3;
            largest = val1;
        }
    }
    else {  
        smallest = val3;
        if (val1<=val2) {
            middle = val1;
            largest = val2;
        }
        else {
            middle = val2;
            largest = val1;
        }
    }

    cout << "string values sorted : " << smallest << ", " << middle << ", " << largest <<'\n';
}

输出(希望有帮助)

Please enter three integer values separated by a space:
Steinbeck Hemingway Fitzgerald
values sorted : Fitzgerald, Hemingway, Steinbeck