0

我正在尝试为 3 个团队生成 3 个随机分数并确定哪个是最大的。我的做法是使用Predfined函数,一个程序员定义的函数,然后声明和定义函数。我对此很陌生,我买的这本书并没有真正帮助我。

以下是代码端的目标概述:

  • 调用预定义函数生成随机数序列

  • 声明并定义一个返回值的函数

  • 调用程序员定义的函数。

最终目标(取自书中):

  • 编写一个名为的函数 max,它接受三个类型int的参数并返回参数中的最大值。您的程序必须同时具有此函数的声明和定义。函数声明必须放在main函数上方。

  • 编写执行main()以下操作的函数:

    一种。生成一个 10 到 40 之间的随机整数作为 Hoosier、Boilermakers 和 Fighting Irish 三支球队的分数,并打印出这些分数。您的程序必须能够在不同时间运行时生成不同的分数序列。

    湾。调用max任务 1 中定义的函数来查找所有团队中的最大分数,并打印出找到的最大分数。

    C。将最大的分数与 Hoosier 的分数进行比较,并打印出“Go Hoosier!!!” 如果 Hoosier 队的得分等于所有队的最大得分。

这是代码

/*

    Author: Dan Wingeart
    Assignment: Lab 9

*/

#include <iostream>

#include <cmath>

#include <cstdlib>

using namespace std;

int max(int Hscore, int Pscore, int Fscore);

int main()
{

    int Fscore, Pscore, Hscore, highestScore;

    Fscore = 10 + rand() % 40;
    Pscore = 10 + rand() % 40;
    Hscore = 10 + rand() % 40;

    cout << "Prediction performance of sport teams:" << endl;
    cout << "Team Hoosier's score is " << Hscore << endl;
    cout << "Team Boilermakers' score is " << Pscore << endl;
    cout << "Team Fighting Irish's score is " << Fscore << endl;

    highestScore = max(Hscore, Pscore, Fscore)

    if (max>Pscore&&max>Fscore){
        cout << "The largest score is " << max << endl;
        cout << "GO HOOSIER!!!" << endl;}
    else
        cout << "The largest score is " << max << endl;


return 0;
}

int max(int Hscore, int Pscore, int Fscore)
{

    if (Hscore>Pscore&&Hscore>Fscore){
        cout << Hscore;}

    else if (Pscore>Hscore&&Pscore>Fscore){
        cout << Pscore;}

    else{
        cout << Fscore;}

return 0;

}

产生的错误:

ClCompile:
1>  Lab9.cpp
1>c:\users\mackiller\documents\visual studio 2010\projects\lab9\lab9\lab9.cpp(34): error C2143: syntax error : missing ';' before 'if'
1>c:\users\mackiller\documents\visual studio 2010\projects\lab9\lab9\lab9.cpp(34): error C2563: mismatch in formal parameter list
1>c:\users\mackiller\documents\visual studio 2010\projects\lab9\lab9\lab9.cpp(34): error C2563: mismatch in formal parameter list
1>c:\users\mackiller\documents\visual studio 2010\projects\lab9\lab9\lab9.cpp(35): error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'overloaded-function' (or there is no acceptable conversion)
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(679): could be 'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)'
1>          with
1>c:\users\mackiller\documents\visual studio 2010\projects\lab9\lab9\lab9.cpp(38): error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'overloaded-function' (or there is no acceptable conversion)
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(679): could be 'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)'
1>          with
1
4

4 回答 4

2
    //don't forget to like if the solution is working
    //ask ban

    #include <iostream>
    #include <cstdlib>
    #include <time.h>
    using namespace std;

    int max(int Hscore, int Pscore, int Fscore);

    int main()
    {
        srand ( time(NULL) );//(1) 
        int Fscore, Pscore, Hscore, highestScore;

        Fscore = 10 + rand() % 40;
        Pscore = 10 + rand() % 40;
        Hscore = 10 + rand() % 40;

        cout << "Prediction performance of sport teams:" << endl;
        cout << "Team Hoosrand ( time(NULL) );sier's score is " << Hscore << endl;
        cout << "Team Boilermakers' score is " << Pscore << endl;
        cout << "Team Fighting Irish's score is " << Fscore << endl;

        highestScore = max(Hscore, Pscore, Fscore);

        cout<<highestScore;


    return 0;
    }

    int max(int a, int b, int c)
    {
        if(a>b && a>c)//we assume that a is the maximum. This means that a > b and a > c
        {
            return a;
        }
        else // if a is not maximum then we assume that b is maximum
        {
            if(b>c)
            {
                return b;
            }
        }
        return c;// if a and b are not maximum then c is maximum
    }

//(1)这个fct会帮助你每次运行程序时都有不同的随机数,更多信息在这里:http ://www.cplusplus.com/reference/cstdlib/srand/

于 2013-10-30T19:45:37.233 回答
2

正如所指出的,您的 max 函数需要返回一个不总是 0 的 int。看起来您正试图在函数中使用 cout 在 main 中打印出来,但它不起作用。这会导致您的一些编译问题。

而且您在分配最大“highestScore”结果的行末尾缺少一个括号。更多编译问题。

此外,您将 max 函数的结果分配给“highestScore”,但从不使用它。实际上,您正在使用函数“max”本身,这可能会导致更多的编译器问题。

这是我的镜头。

 #include <iostream>
 #include <cmath>
 #include <cstdlib>

 using namespace std;

 int max(int Hscore, int Pscore, int Fscore);

 int main()
 {
     int Fscore, Pscore, Hscore, highestScore;

     srand (time(NULL));  // New numbers each time.
     Fscore = 10 + rand() % 40;
     Pscore = 10 + rand() % 40;
     Hscore = 10 + rand() % 40;

     cout << "Prediction performance of sport teams:" << endl;
     cout << "Team Hoosier's score is " << Hscore << endl;
     cout << "Team Boilermakers' score is " << Pscore << endl;
     cout << "Team Fighting Irish's score is " << Fscore << endl;

     highestScore = max(Hscore, Pscore, Fscore);

     if ( (highestScore>Pscore ) && (highestScore>Fscore) ){
       cout << "The largest score is " << highestScore << endl;
       cout << "GO HOOSIER!!!" << endl;
     } else {
       cout << "The largest score is " << highestScore << endl;
     }

 return 0;
 }

 int max(int a, int b, int c) {
   int m = ( a > b ) ? a : b;
   return  ( m > c ) ? m : c;

 }
于 2013-10-30T20:13:22.580 回答
1

您的功能实现存在问题max。首先你总是return 0不管是什么max。但还有另一个问题。

让我们想象一下Hscore = 40,Pscore = 40Fscore = 20看看这个函数。

int max(int Hscore, int Pscore, int Fscore)
{

    if (Hscore>Pscore&&Hscore>Fscore){ // False, Hscore > Pscore is false since Hscore is even but not greater than Pscore
        cout << Hscore;}

    else if (Pscore>Hscore&&Pscore>Fscore){  // False, Pscore > Hscore is false since Pscore is even but not greater than Hscore
        cout << Pscore;}

    else{  // else statement gets executed even tho Fscore was the lowest.
        cout << Fscore;}

    return 0;
}

您可能应该>=在 max 函数中使用而不是>. 它应该是这样的:

int max(int Hscore, int Pscore, int Fscore)
{

    if (Hscore>=Pscore&&Hscore>=Fscore){
        cout << Hscore;
        return Hscore;
    }

    else if (Pscore>=Hscore&&Pscore>=Fscore){
        cout << Pscore;
        return Pscore;
    }

    else{
        cout << Fscore;
        return Fscore;
    }
}
于 2013-10-30T19:31:09.303 回答
1

根据代码示例,我觉得你的书有点旧(它在函数的开头声明了所有变量,这是 1999 年之前的 C 语言所必需的)。我强烈建议你买一本更新的书。就个人而言,我会推荐加速 C++编程:使用 C++ 的原则和实践(由 C++ 的创建者提供)。

您的代码中最大的问题是以下简单事实:

if (max>Pscore&&max>Fscore){

混淆max(函数)和highestScore(变量)。你希望它是:

if (highestScore>Pscore&&highestScore>Fscore){

虽然,就个人而言,我会把它写成

if (highestScore == Hscore) {

除此之外,您可能有兴趣发现标准库在标题中具有函数max(返回两项中较大的一项)和max_element(返回序列中最大的项) 。<algorithm>您可以将您max的电话替换为max

int highestScore = std::max(std::max(Hscore, Pscore), Fscore);

或致电max_element

int scores[] = { Pscore, Fscore, Hscore };
int highestScore = *max_element(scores, scores + 3);
于 2013-10-30T20:19:17.117 回答