C++ 将循环移动到函数

C++ 将循环移动到函数,c++,function,for-loop,C++,Function,For Loop,在下面的代码中,我的主函数中有一个for循环。既然一个函数不能返回2个值,我可以用什么方法创建一个或多个函数,将其从主函数中删除。谢谢 #include <iostream> #include <cmath> using namespace std; int getNumberExercises(); int getScores(int numberOfExercises); int getPoints(int numberOfExercises); double r

在下面的代码中,我的主函数中有一个for循环。既然一个函数不能返回2个值,我可以用什么方法创建一个或多个函数,将其从主函数中删除。谢谢

#include <iostream>
#include <cmath>

using namespace std;

int getNumberExercises();
int getScores(int numberOfExercises);
int getPoints(int numberOfExercises);
double roundToTenth(double number);
double calcPercentage(int totalScore, int totalPoints);
void getTotal(int totalScore, int totalPoints, double scorePercentage);

int main() {
  int numberOfExercises = 0;
  int totalScore = 0;
  int totalPoints = 0;
  double scorePercentage = 0.0;    

  numberOfExercises = getNumberExercises();
  for (int i = 1; i <= numberOfExercises; i++) {
    totalScore += getScores(i);
    totalPoints += getPoints(i);
  }
  scorePercentage = calcPercentage(totalScore, totalPoints);   
  getTotal(totalScore, totalPoints, scorePercentage);

  return 0;
}

int getNumberExercises() {
  int numberOfExercises;
  cout << "How many exercises to input? ";
  cin >> numberOfExercises; 
  cout << endl;
  return numberOfExercises;
}

int getScores(int i) {
  int score = 0;
  cout << "Score received for exercise " << i << ": ";
  cin >> score;
  return score;
}

int getPoints(int i) {
  int points = 0;
  cout << "Total points possible for exercise " << i << ": ";
  cin >> points;
  cout << endl;
  return points;
}

double roundToTenth(double number) {
  return floor(number * 100 + 0.5) / 100;
}

double calcPercentage(int totalScore, int totalPoints) {
  double scorePercentage = (double) totalScore / totalPoints * 100; 
  return scorePercentage;
}

void getTotal(int totalScore, int totalPoints, double scorePercentage) {
  cout << "Your total is " << totalScore << " out of " << totalPoints << ", or " << roundToTenth(scorePercentage) << "%";
}
#包括
#包括
使用名称空间std;
int getnumberxercises();
int getScores(int numberOfExercises);
int getPoints(int numberOfExercises);
双倍整数(双倍数字);
双重计算分数(整数总分,整数总分);
void getTotal(整数总分、整数总分、双倍分数百分比);
int main(){
int numberOfExercises=0;
整数总分=0;
int totalPoints=0;
双倍得分百分比=0.0;
numberOfExercises=getNumberExercises();
对于(int i=1;i);

cout您可以创建一个函数,该函数要么返回大小为2的int类型数组,例如

int* functionName(arg1, arg2)
    array[0] = totalScore;
    array[1] = totalPoints;
return array;
或者您可以通过如下操作通过引用传入值

void functionName(&arg1, &arg2)

引用传递的作用是传递变量的地址,然后直接修改变量,而不是创建副本并将其传递到函数中。

要返回多个值,请使用指针传递:

void modify(int *val1, int *val2)
{
    *val1 = 46;
    *val2 = 100;
}
来电者:

int a, b;

modify(&a, &b);
现在
a
将是46,
b
将是100。这是因为地址是复制的,而不是实际变量,这是传递值中发生的情况

因此,如果要查找文件中出现的制表符和逗号的数量:

void findtabcomma(const char *fn, int *ncomma, int *ntab)
{
    FILE *fp;
    int c;

    if (fp = fopen(fn, "r")) {
        while ((c = getc(fp)) != EOF) {
            if (c == '\t')
                ++(*ntab);
            if (c == ',')
                ++(*ncomma);
        }
        fclose(fp);
    }
}
您可以使用如下功能:

int c, t;

findtabcomma(filenam, &c, &t);

printf("Commas: %d\nTabs: %d", c, t);
int c, t;

findtabcomma(filenam, c, t);

printf("Commas: %d\nTabs: %d", c, t);
<>和C++中,可以使用引用变量,因此可以将< <代码> FIDEBTABCAS> /COD>函数重写为:

void findtabcomma(const char *fn, int &ncomma, int &ntab)
{
    FILE *fp;
    int c;

    if (fp = fopen(fn, "r")) {
        while ((c = getc(fp)) != EOF) {
            if (c == '\t')
                ++ntab;
            if (c == ',')
                ++ncomma;
        }
        fclose(fp);
    }
}
使用方法如下:

int c, t;

findtabcomma(filenam, &c, &t);

printf("Commas: %d\nTabs: %d", c, t);
int c, t;

findtabcomma(filenam, c, t);

printf("Commas: %d\nTabs: %d", c, t);

<>请注意,在C++引用中没有更多的代码< >代码> >和<代码>和>代码> .< /p> ,可以使用参数列表中的引用返回数据


int fun();可以编码为void fun(int&)

或者将
typedef
a
std::pair
转换为描述性名称,或者创建自己的类型来保存要返回的内容:

using ScorePointPair = std::pair<int, int>; // C++11
typedef std::pair<int, int> ScorePointPair; // pre C++11
然后只需从函数中返回(按值):


我建议使用自定义类型(struct/class)方法,因为这往往更易于维护。

@Peter请不要使用第一种方法。我也建议不要使用第二种方法,尽管不太强烈。@Peter返回指针不是自我记录的。它可以指向任意数量的整数,意思是任何东西。调用代码根本不知道,并且依赖于对实现的了解n、 至于引用,调用方必须先声明int,然后再将其传递给函数。在我看来,这导致了messier代码,但它的透明度也较低。返回包含两个命名字段的对象的函数非常简单明了。Downvoter,请详细说明这有什么问题。