Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/grails/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ (C+;+;)分解此简单输入/输出代码: 任何能帮我的人,非常感谢!_C++_Optimization - Fatal编程技术网

C++ (C+;+;)分解此简单输入/输出代码: 任何能帮我的人,非常感谢!

C++ (C+;+;)分解此简单输入/输出代码: 任何能帮我的人,非常感谢!,c++,optimization,C++,Optimization,我希望这段代码尽可能地最小化-但是,我希望它显示完全相同的内容 具体来说,关于如何使用其他语句删除代码中的一些重复(如“if,else-if”)等,或者让编译器更快地完成这些语句,因为我觉得我缺乏这样做的经验和知识,而不会弄乱所有事情(如使用switch&case) 例如,当我测试“tf”时,我不想被迫重复“平均值”,这取决于我们是否添加了“TEZA”等级 当然,非常感谢,如果有需要或者我不够清楚,我愿意分享更多细节 我事先道歉,以防我不够清楚 #包括 使用名称空间std; int main(

我希望这段代码尽可能地最小化-但是,我希望它显示完全相同的内容

具体来说,关于如何使用其他语句删除代码中的一些重复(如“if,else-if”)等,或者让编译器更快地完成这些语句,因为我觉得我缺乏这样做的经验和知识,而不会弄乱所有事情(如使用switch&case)

例如,当我测试“tf”时,我不想被迫重复“平均值”,这取决于我们是否添加了“TEZA”等级

当然,非常感谢,如果有需要或者我不够清楚,我愿意分享更多细节

我事先道歉,以防我不够清楚


#包括
使用名称空间std;
int main()
{
字符串sub,tf;
int m1、m2、m3、m4、sum、TEZA;
双平均值,tzm;

cout您可以使用数组和循环来缩短M的部分

#include <iostream>

using namespace std;

int main()

{
    string sub, tf;
    int m[4], TEZA, sum;
    double avg, tzm;

    cout << "SIMPLE AVERAGE CALCULATOR";
    cout << "\n" << "\n" << "Subject at hand?: "; cin >> sub;
    cout << "\n" << "Input the FOUR marks you'd like verified: " << "\n";
    for(int i = 1; i < 5; i++)
    {
        cout<<"\n"<<"M"<<i<<":"; cin>>m[i-1];
    }
    cout << "\n" << "Would you like to include the TEZA grade?(Y/N): "; cin >> tf;
    sum = m[0] + m[1] + m[2] + m[3];
    avg = (double) sum / 4;

    if (tf == "Y" | tf == "y")
    {
        cout << "What is the TEZA grade?: ";  cin >> TEZA;
        int tzm = ((double) avg * 3 + TEZA) / 4;
        cout << "\n" << "Your average grade at " << sub << " is " << tzm << "\n" << "\n";

        cout << "You got the following mark: ";
        if (tzm >= 9 && tzm <= 10)     cout << "A" << "\n";
        else if (tzm >= 8 && tzm <= 9) cout << "B" << "\n";
        else if (tzm >= 7 && tzm <= 8) cout << "C" << "\n";
        else if (tzm >= 6 && tzm <= 7) cout << "D" << "\n";
        else if (tzm >= 5 && tzm <= 6) cout << "E" << "\n";
        else if (tzm < 5)              cout << "F" << "\n";
        cout << "DO YOU PASS: " << "\n";

        if (tzm >= 5) cout << "Yes." << "\n";
        else cout << "No." << "\n";
    }
    else
    {
        cout << "\n" << "Average at " << sub << " is " << avg << "\n" << "\n";
        cout << "You got the following mark: ";
        if (avg >= 9 && avg <= 10) cout << "A" << "\n";
        else if (avg >= 8 && avg <= 9) cout << "B" << "\n";
        else if (avg >= 7 && avg <= 8) cout << "C" << "\n";
        else if (avg >= 6 && avg <= 7) cout << "D" << "\n";
        else if (avg >= 5 && avg <= 6) cout << "E" << "\n";
        else if (avg < 5) cout << "F" << "\n";
        cout << "\n" << "DO YOU PASS?: " << "\n";

        if (avg >= 5) cout << "Yes." << "\n";
        else cout << "No." << "\n";

    }
}
#包括
使用名称空间std;
int main()
{
字符串sub,tf;
int m[4],TEZA,sum;
双平均值,tzm;
库特
  • 不要重复代码,而是使用
    PrintGrade()
    VerifyPassFail()
    等方法,并在每次需要使用该代码块时调用它们
  • 不要在前面声明
    int-TEZA
    ,当
    if条件通过时声明它,因为只有这样你才真正需要使用它
  • 与前面提到的另一个答案一样,您可以使用容器来容纳
    m1、m2、m3和m4
    。我建议使用
    std::vector
    。如果您想扩展到更多主题,您需要做的就是增加循环的范围
  • 附加说明是不使用
    使用名称空间std
    ,而是在
    std
    标记前面加前缀,例如
    std::vector
    std::cout

    #include <iostream>
    #include <string>
    #include <vector>
    
    void PrintGrade( int inInput )
    {
       std::cout << "You got the following mark: ";
    
       if (inInput >=9 && inInput <=10)
         std::cout<<"A"<<"\n";
       else if (inInput >=8 && inInput <=9)
         std::cout<<"B"<<"\n";
       else if (inInput >=7 && inInput <=8)
         std::cout<<"C"<<"\n";
       else if (inInput >=6 && inInput <=7)
         std::cout<<"D"<<"\n";
       else if (inInput >=5 && inInput <=6)
         std::cout<<"E"<<"\n";
       else if(inInput <5)
         std::cout<<"F"<<"\n";
    }
    
    void VerifyPassFail( int inInput )
    {
       std::cout<<"\n"<<"DO YOU PASS?: "<<"\n";
       if (inInput >=5)
         std::cout<<"Yes."<<"\n";
       else
         std::cout<<"No."<<"\n";
    }
    
    int main()
    {
      std::string sub, tf;
      int sum = 0;
      int TEZA;
      std::vector<int> marksList;
      double avg, tzm;
    
      std::cout << "SIMPLE AVERAGE CALCULATOR";
      std::cout << "\n" << "\n" << "Subject at hand?: ";
      std::cin >> sub;
      std::cout << "\n" << "Input the FOUR marks you'd like verified: " << "\n";
    
      for(int i = 1; i < 5; i++)
      {
        int marks;
        std::cout<<"\n"<<"M"<<i<<":";
        std::cin>>marks;
        marksList.push_back(marks);
      }
    
      std::cout << "\n" << "Would you like to include the TEZA grade?(Y/N): ";
      std::cin >> tf;
    
      for( int i : marksList )
      {
        sum += i;
      }
    
      avg = (double) sum / 4;
    
      if (tf == "Y" | tf == "y")
      {
        std::cout << "What is the TEZA grade?: ";
        std::cin >> TEZA;
        int tzm = ((double) avg * 3 + TEZA) / 4;
        std::cout << "\n" << "Your average grade at " << sub << " is " << tzm << "\n"
             << "\n";
    
        std::cout << "You got the following mark: ";
        PrintGrade(tzm);
        VerifyPassFail(tzm);
     }
     else
     {
        std::cout << "\n" << "Average at " << sub << " is " << avg << "\n" << "\n";
        PrintGrade(avg);
        VerifyPassFail(avg);
     }
    }
    
    #包括
    #包括
    #包括
    无效打印等级(输入内部)
    {
    
    std::cout=9&&inInput如果我希望它简短,我可能会这样写:

    #include <iostream>
    #include <string>
    #include <vector>
    #include <cctype>
    #include <numeric>
    
    template <class T> T get(std::string const &prompt) {
        std::cout << prompt;
        T ret;
        std::cin >> ret;
        return ret;
    }
    
    int main() {
        auto sub = get<std::string>("SIMPLE AVERAGE CALCULATOR\n\nSubject at hand?: ");
    
        std::cout << "\nPlease enter the FOUR marks you'd like verified:\n";
        std::vector<int> m;
        for (int i = 0; i < 4; i++) 
            m.push_back(get<int>("M" + std::to_string(i + 1) + ": "));
    
        if (std::toupper(get<char>("\nWould you like to include the TEZA grade?(Y/N): ")) == 'Y')
            m.push_back(get<int>("What is the TEZA grade?: "));
    
        auto tzm = std::accumulate(m.begin(), m.end(), 0.0) / m.size();
    
        char const *yn[] = { "No", "Yes" };
        std::cout << "\nYour average grade at " << sub << " is " << tzm 
                << "\n\nYou got the following mark: " << "FFFFFEDCBAA"[(int)tzm] 
                << "\nDo you pass?\n" << yn[tzm >= 5] << "\n";
    }
    
    auto sub = get<std::string>(
        "SIMPLE AVERAGE CALCULATOR\n"
        "\n"
        "Subject at hand?: ");
    
    在更高的层次上,代码执行了相当多的基本操作序列:向用户打印一个提示,然后读入某种类型的值。将该序列移动到函数中,这样我们就可以避免对同一序列重复几乎相同的代码,这也是一件非常好的事情

    问题中的代码还将可以定义为单个字符串文字的内容分解为多个单独的部分,没有明显的原因。我想有人可能会觉得这样更容易理解,但我不确定是谁会这样做,或者为什么会这样做——对我来说,这看起来并不是一种改进

    将事情逐行分解是有意义的,比如:

    #include <iostream>
    #include <string>
    #include <vector>
    #include <cctype>
    #include <numeric>
    
    template <class T> T get(std::string const &prompt) {
        std::cout << prompt;
        T ret;
        std::cin >> ret;
        return ret;
    }
    
    int main() {
        auto sub = get<std::string>("SIMPLE AVERAGE CALCULATOR\n\nSubject at hand?: ");
    
        std::cout << "\nPlease enter the FOUR marks you'd like verified:\n";
        std::vector<int> m;
        for (int i = 0; i < 4; i++) 
            m.push_back(get<int>("M" + std::to_string(i + 1) + ": "));
    
        if (std::toupper(get<char>("\nWould you like to include the TEZA grade?(Y/N): ")) == 'Y')
            m.push_back(get<int>("What is the TEZA grade?: "));
    
        auto tzm = std::accumulate(m.begin(), m.end(), 0.0) / m.size();
    
        char const *yn[] = { "No", "Yes" };
        std::cout << "\nYour average grade at " << sub << " is " << tzm 
                << "\n\nYou got the following mark: " << "FFFFFEDCBAA"[(int)tzm] 
                << "\nDo you pass?\n" << yn[tzm >= 5] << "\n";
    }
    
    auto sub = get<std::string>(
        "SIMPLE AVERAGE CALCULATOR\n"
        "\n"
        "Subject at hand?: ");
    
    auto-sub=get(
    “简单平均计算器\n”
    “\n”
    “当前主题:”;
    
    编译器会将类似这样的相邻字符串文本合并为单个文本,因此最终的结果与上面所示的代码完全相同——但有些编译器发现它更具可读性。另一种可能是使用原始字符串:

    auto sub = get<std::string>(
    R"("SIMPLE AVERAGE CALCULATOR
    
    Subject at hand?: )"); 
    
    auto-sub=get(
    R”(“简单平均计算器
    手头的主题?:)”;
    
    原始字符串文字完全按照原样处理所有内容,包括新行字符,因此我们可以将新行表示为实际新行,而不是将其编码为
    \n
    。在这种特殊情况下,我不觉得这有多大好处,但我可以看出有些人可能更喜欢它

    归根结底,代码的长度很少是非常相关的——但是(例如)代码中有重复确实很重要——很多。消除重复绝对是一件好事


    同样,在某种程度上,我们可以用数据而不是控制流来表示任务,这几乎总是一个胜利。

    如果代码可以工作,并且您只想优化它,那么这个问题对于StackOverflow来说是离题的,您应该继续问instead@RemyLebeau那个代码审查站点指向了其他地方。它应该是poin丁托:谢谢,我不知道。我也会把它贴在那里,对不起!你可以用数组和loop@Asesh我的链接指向正确的位置。再看一看,其中任何一个都可能会改变编译器的输出。没有基准测试的优化是徒劳的。我知道这不会对编译器的输出产生太大影响OP还问他是否可以“删除代码中的一些重复(例如“if,else if”)".抱歉。请放心。我想OP想知道他如何最小化代码,因为性能改进的余地不大,因为它非常简单,而且正如您前面提到的那样,IO限制非常大。您已经对其进行了基准测试?或者比较了compiler.asm输出?这很糟糕。请原谅我,我怀疑任何更改都可能是错误的这段代码可以做任何一个优秀的优化编译器做不到或做得更好的事情。您好,我花了几十年时间试图运行这段代码,以完全理解它的真正含义。它似乎不起作用,在修复后总是显示某种错误(使用GNU编译器)@Johnny:它需要C++11或更高版本,所以编译它时需要通过
    std=C++11
    ,如:“g++-std=C++11 foo.cpp”。不过它不需要任何“修复”——它已经用gcc 5.4进行了测试。