如何编写函数并从main调用它们

How to write functions and call them from main

本文关键字:调用 main 何编写 函数      更新时间:2023-10-16
#include<iostream> 
#include<iomanip> 
#include<string> 
#include<fstream> 
using namespace std;

struct studentType 
{
    string studentFName[20];
    string studentLName[20];
    string studentData[20][3];
    int testscore[20][5];
    char grade[20];
};
studentType s;

int main() 
{
    int index;
    int maxindex;
    int maxindex1;
    int coldex;
    int coldex1;
    int totalscore[20];
    double avscore[20];
    double highscore;
    double highindivscore;
    maxindex = 0;
    maxindex1 = 0;
    coldex1 = 0;
    ifstream infile;
    infile.open("test.txt");    

    for(index = 0; index < 20; index++)
    {
        infile >> s.studentFName[index] >> s.studentLName[index] >> s.testscore[index][0] >> s.testscore[index][1] >> s.testscore[index][2] >> s.testscore[index][3] >> s.testscore[index][4];
        totalscore[index] = ((s.testscore[index][0]) + (s.testscore[index][1]) + (s.testscore[index][2]) + (s.testscore[index][3]) + (s.testscore[index][4]));
        avscore[index] = (static_cast<double>(totalscore[index])/5);

        if(avscore[index]<= 100)
        {
            s.grade[index] = 'A';
        }
        if(avscore[index]<= 89.9)
        {
            s.grade[index] = 'B';
        }
        if(avscore[index]<= 79.9)
        {
            s.grade[index] = 'C';
        }
        if(avscore[index] <= 69.9)
        {
            s.grade[index] = 'D';
        }
        if(avscore[index] <= 59.9)
        {
            s.grade[index] = 'F';
        }
    }
    for (index = 0; index < 20; index++)
    {
        cout << s.studentLName[index] << "," << " " << s.studentFName[index] << " " << s.testscore[index][0] << " " << s.testscore[index][1] << " " << s.testscore[index][2] << " " << s.testscore[index][3] << " " << s.testscore[index][4] << " " << s.grade[index] << endl;
    }
    cout << endl;
    for (index = 1; index < 20; index++)
        for (coldex = 0; coldex < 5; coldex++)
        {
            if (s.testscore[maxindex1][coldex1] < s.testscore[index][coldex])
            {
                maxindex1 = index;
                coldex1 = coldex;
            }
        }
    highindivscore = s.testscore[maxindex1][coldex1];
    for (index = 1; index < 20; index++)
    {
        if (avscore[maxindex] < avscore[index])
        {
            maxindex = index;
        }
    }
    highscore = avscore[maxindex];

    cout << s.studentFName[maxindex] << " " << s.studentLName[maxindex] << " Achieved The Highest Average Test Score Of A: " <<highscore <<endl;
    cout << s.studentFName[maxindex1] << " " << s.studentLName[maxindex1] << " " << s.testscore[maxindex1][coldex1] << endl;
    infile.close();
    system("pause");
    return 0;
}
这是作业问题,我知道我做错了,这就是为什么我需要帮助。我如何使主函数调用。我是新手。这个实验室把我难住了。作业如下(强调我的):

编写下列程序并测试以确保它们正常工作。请遵循书中的指导方针或我的风格指导方针来编写代码。编写一个程序,从给定的输入文件中读取学生的姓名和考试成绩。程序应该输出到一个文件,output.txt,每个学生的名字后面跟着考试成绩和相关的等级。学生数据应该存储在StudentType类型的结构变量中,它有四个组件:字符串类型的studentFName和studentLName, int类型的testScore和char类型的grade。假设这个班级有20个学生。使用一个包含20个组件的数组,类型为StudentType。您的程序必须至少包含以下函数:

  1. 一个将学生数据读入数组的函数。
  2. 为每个学生分配相应分数的函数。

你的程序应该以这种形式输出每个学生的名字:姓氏后面跟着一个逗号,后面跟着一个空格,后面跟着名字;名称必须左对齐。此外,除了声明变量和打开输入和输出文件之外,函数main应该只是函数调用的集合。

这叫做重构。我猜对于这个作业,你想把main中的所有东西都移到单独的函数中。你可以剪切粘贴main的整个主体并将其移动到一个函数中,然后在main中调用它。我不认为这是作业的重点。

相反,读取主过程和分离/提取过程。你应该把main函数分成做一件事的函数,比如处理输入/输出、循环、检查变量和返回一个字母等。这类更改不仅使程序更容易理解,以后也更容易使用,而且使程序中重复的过程更容易重用。

你的代码在逻辑上看起来很好,应该可以工作,然而,你的作业要求你的本质是用良好的风格编写代码,并将程序的各个部分分离成函数。

本质上,作为一个简单的修复,只需将代码中做类似事情的不同部分放入void函数中。例如,这个cpp段:
void function1();
void function2();
void function3();
void main(){
    function1();
    function2();
    function3();
    return 0;
}
void function1(){
    // Your code. Ex: Some large algorithm.
}
void function2(){
    //More code, for a different algorithm. Maybe some input or output.
}
void function3(){
    //The final code you want the program to execute.
}

然后传递参数并根据需要调整返回类型。它使代码更容易遵循,特别是如果您的函数以它们的功能命名,例如gradingLogic()studentOutput()