q2110809471 2023-04-29 22:20 采纳率: 38.5%
浏览 10

怎么才能简化代码方法


void MainWindow::surveyData()
{
    int math=0, chinese=0, english=0;
    for (int i = 0; i < iniDataRowCount; i++)
    {
        QStandardItem *aItem= theModel->item(i, 1);
        int text = aItem->text().toInt();
        if (text < 60)
            math++;
    }
    QString Math = QString::number(math);
    math = 0;
    for (int i = 0; i < iniDataRowCount; i++)
    {
        QStandardItem* aItem = theModel->item(i, 2);
        int text = aItem->text().toInt();
        if (text < 60)
            chinese++;
    }
    QString Chinese = QString::number(chinese);
    chinese = 0;

    for (int i = 0; i < iniDataRowCount; i++)
    {
        QStandardItem* aItem = theModel->item(i, 3);
        int text = aItem->text().toInt();
        if (text < 60)
            english++;
    }
    QString English = QString::number(english);
    english = 0;


    QStringList headerList;
    headerList << "     分数段" << "数学" <<"语文"<< "英语";
    ui.treeWidget->setHeaderLabels(headerList);
    QTreeWidgetItem* item;
    item = new QTreeWidgetItem(1001);
    item->setText(0, "60分以下");
    item->setText(1, Math);
    item->setText(2, Chinese);
    item->setText(3, English);
    ui.treeWidget->addTopLevelItem (item);

    for (int i = 0; i < iniDataRowCount; i++)
    {
        QStandardItem* aItem = theModel->item(i, 1);
        int text = aItem->text().toInt();
        if (text > 60&&text<69)
            math++;
    }
    QString Math1 = QString::number(math);
    math = 0;
    for (int i = 0; i < iniDataRowCount; i++)
    {
        QStandardItem* aItem = theModel->item(i, 2);
        int text = aItem->text().toInt();
        if (text > 60&&text<69)
            chinese++;
    }
    QString Chinese1 = QString::number(chinese);
    chinese = 0;

    for (int i = 0; i < iniDataRowCount; i++)
    {
        QStandardItem* aItem = theModel->item(i, 3);
        int text = aItem->text().toInt();
        if (text > 60&&text<69)
            english++;
    }
    QString English1 = QString::number(english);
    english = 0;
    item = new QTreeWidgetItem(1002);
    item->setText(0, "60-69分");
    item->setText(1, Math1);
    item->setText(2, Chinese1);
    item->setText(3, English1);
    ui.treeWidget->addTopLevelItem(item);

    for (int i = 0; i < iniDataRowCount; i++)
    {
        QStandardItem* aItem = theModel->item(i, 1);
        int text = aItem->text().toInt();
        if (text > 70 && text < 79)
            math++;
    }
    QString Math2 = QString::number(math);
    math = 0;
    for (int i = 0; i < iniDataRowCount; i++)
    {
        QStandardItem* aItem = theModel->item(i, 2);
        int text = aItem->text().toInt();
        if (text > 70 && text < 79)
            chinese++;
    }
    QString Chinese2 = QString::number(chinese);
    chinese = 0;

    for (int i = 0; i < iniDataRowCount; i++)
    {
        QStandardItem* aItem = theModel->item(i, 3);
        int text = aItem->text().toInt();
        if (text > 70 && text < 79)
            english++;
    }
    QString English2 = QString::number(english);
    english = 0;
    item = new QTreeWidgetItem(1003);
    item->setText(0, "70-79分");
    item->setText(1, Math2);
    item->setText(2, Chinese2);
    item->setText(3, English2);
    ui.treeWidget->addTopLevelItem(item);

    for (int i = 0; i < iniDataRowCount; i++)
    {
        QStandardItem* aItem = theModel->item(i, 1);
        int text = aItem->text().toInt();
        if (text > 80 && text < 89)
            math++;
    }
    QString Math3 = QString::number(math);
    math = 0;
    for (int i = 0; i < iniDataRowCount; i++)
    {
        QStandardItem* aItem = theModel->item(i, 2);
        int text = aItem->text().toInt();
        if (text > 80 && text < 89)
            chinese++;
    }
    QString Chinese3 = QString::number(chinese);
    chinese = 0;

    for (int i = 0; i < iniDataRowCount; i++)
    {
        QStandardItem* aItem = theModel->item(i, 3);
        int text = aItem->text().toInt();
        if (text > 80 && text < 89)
            english++;
    }
    QString English3 = QString::number(english);
    english = 0;
    item = new QTreeWidgetItem(1004);
    item->setText(0, "80-89分");
    item->setText(1, Math3);
    item->setText(2, Chinese3);
    item->setText(3, English3);
    ui.treeWidget->addTopLevelItem(item);

    for (int i = 0; i < iniDataRowCount; i++)
    {
        QStandardItem* aItem = theModel->item(i, 1);
        int text = aItem->text().toInt();
        if (text > 90)
            math++;
    }
    QString Math4 = QString::number(math);
    math = 0;
    for (int i = 0; i < iniDataRowCount; i++)
    {
        QStandardItem* aItem = theModel->item(i, 2);
        int text = aItem->text().toInt();
        if (text > 90)
            chinese++;
    }
    QString Chinese4 = QString::number(chinese);
    chinese = 0;

    for (int i = 0; i < iniDataRowCount; i++)
    {
        QStandardItem* aItem = theModel->item(i, 3);
        int text = aItem->text().toInt();
        if (text > 90)
            english++;
    }
    QString English4 = QString::number(english);
    english = 0;
    item = new QTreeWidgetItem(1005);
    item->setText(0, "90分以上");
    item->setText(1, Math4);
    item->setText(2, Chinese4);
    item->setText(3, English4);
    ui.treeWidget->addTopLevelItem(item);
}
大佬们,这个代码有办法简化吗?
  • 写回答

3条回答 默认 最新

  • threenewbee 2023-04-29 22:55
    关注

    目测可以提炼出函数,避免重复编写。

    评论

报告相同问题?

问题事件

  • 创建了问题 4月29日

悬赏问题

  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证