Web前端开发JavaScript html

(1)计算每一个学生的总成绩,以表格形式输出
(2)按总成绩排名,以表格输出

img

img

img

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      table {
        border: 1px solid #000;
        margin-bottom: 20px;
      }
      table td,
      table th {
        border: 1px solid #000;
        text-align: center;
        width: 100px;
      }
    </style>
  </head>
  <body>
    <script>
      const list = [
        {
          stuNo: 1,
          course1: 77,
          course2: 66,
          course3: 88,
          total: "",
        },
        {
          stuNo: 2,
          course1: 78,
          course2: 89,
          course3: 79,
          total: "",
        },
        {
          stuNo: 3,
          course1: 92,
          course2: 84,
          course3: 78,
          total: "",
        },
        {
          stuNo: 4,
          course1: 78,
          course2: 99,
          course3: 83,
          total: "",
        },
        {
          stuNo: 5,
          course1: 85,
          course2: 97,
          course3: 69,
          total: "",
        },
      ];
      const totalList = list.map((item) => {
        const { course1, course2, course3 } = item;
        // 暂时以course1,course2,course3为准
        item.total = course1 + course2 + course3;
        return item;
      });
      const rankList = [...totalList];
      rankList.sort((a, b) => b.total - a.total);

      const str1 = totalList.reduce((pre, cur) => {
        const temp = `<tr>
            <td>${cur.stuNo}</td>
            <td>${cur.course1}</td>
            <td>${cur.course2}</td>
            <td>${cur.course3}</td>
            <td>${cur.total}</td>
          </tr>`;
        return pre + temp;
      }, "");
      const str2 = rankList.reduce((pre, cur) => {
        const temp = `<tr>
            <td>${cur.stuNo}</td>
            <td>${cur.course1}</td>
            <td>${cur.course2}</td>
            <td>${cur.course3}</td>
            <td>${cur.total}</td>
          </tr>`;
        return pre + temp;
      }, "");

      const tableTemplate = `<table><tr><th>stuNo</th><th>course1</th>
          <th>course2</th>
          <th>course3</th>
          <th>total</th></tr>`;
      document.body.innerHTML =
        `${tableTemplate}${str1}</table>` + `${tableTemplate}${str2}</table>`;
    </script>
  </body>
</html>

这是c++还是java啥的?
我写个c++的代码

#include <QCoreApplication>
#include <string>
#include <iostream>
#include <stdio.h>
#include <vector>
using namespace std;

struct Student   //定义一个学生结构体 ,用于存储id号和成绩
{
    int number=0;
    int course1=0;
    int course2=0;
    int course3=0;
    int total=0;
};

int main()
{
    //定义一个vector 用于存储学生信息
    vector<Student> res;
    Student stu1;
    stu1.number=1;
    stu1.course1=77;
    stu1.course2=66;
    stu1.course3=88;
    stu1.total=stu1.course1+stu1.course2+stu1.course3;

    Student stu2;
    stu2.number=2;
    stu2.course1=78;
    stu2.course2=89;
    stu2.course3=79;
    stu2.total=stu2.course1+stu2.course2+stu2.course3;

    Student stu3;
    stu3.number=3;
    stu3.course1=92;
    stu3.course2=84;
    stu3.course3=78;
    stu3.total=stu3.course1+stu3.course2+stu3.course3;

    Student stu4;
    stu4.number=4;
    stu4.course1=78;
    stu4.course2=99;
    stu4.course3=83;
    stu4.total=stu4.course1+stu4.course2+stu4.course3;

    Student stu5;
    stu5.number=5;
    stu5.course1=85;
    stu5.course2=97;
    stu5.course3=69;
    stu5.total=stu5.course1+stu5.course2+stu5.course3;

    res.push_back(stu1);
    res.push_back(stu2);
    res.push_back(stu3);
    res.push_back(stu4);
    res.push_back(stu5);

    cout<<"排序前"<<endl;
    cout<<"StuNo  "<<"course1  "<<"course2  "<<"course3  "<<"total"<<endl;
    for(int i=0;i<res.size();++i)
    {
        cout<<res[i].number<<"       "<<res[i].course1<<"       "<<res[i].course2<<"       "<<res[i].course3<<"       "<<res[i].total<<endl;
    }
    cout<<"排序后"<<endl;
    for(int i=0;i<res.size();++i)
    {
        for(int j=i+1;j<res.size();++j)
        {
            if(res[i].total<res[j].total)
            {
                swap(res[i],res[j]);
            }
        }
    }
    for(int i=0;i<res.size();++i)
    {
        cout<<res[i].number<<"       "<<res[i].course1<<"       "<<res[i].course2<<"       "<<res[i].course3<<"       "<<res[i].total<<endl;
    }


    return 0;
}

效果:

img

1.原题有个小错误

img


2,然后傻瓜式解题:a,

img


定义数组students,i
b,

img


对数组中的total进行汇总
c,

img


新建一个6行5l列
并设置border:1px;cellspacing:0;由于调试问题下面表格并未显示不过不受影响
并对表格的每一个细胞填充数据,这很直接也很麻烦,但是好懂,
d,

img


对数组重新排序,这里有个排序函数sort()
t,

img

对排序后的数组再进行一次输出
f,

img

最后对输出的结果做出对比,这是最简单最直接的方式解题,如果有用点个有用,不枉费说了这么多认认真真的话,