11.获取页面代码和题号|完成发起和获取结果请求|显示结果到网页|上线与综合测试(C++)

获取页面代码和题号

用jQuery

function submit(){
	// alert("嘿嘿!");
	// 1. 收集当前页面的有关数据, 1. 题号 2.代码
	// 2. 构建json,并通过ajax向后台发起基于http的json请求
	// 3. 得到结果,解析并显示到 result中
}
var code = editor.getSession().getValue();
console.log(code);
var number = $(".container .part1 .left_desc h3 #number").text();
// console.log(number);
var judge_url = "/judge/" + number;
console.log(judge_url);

完成发起和获取结果请求

$.ajax({
	method: 'Post',   // 向后端发起请求的方式
	url: judge_url,   // 向后端指定的url发起请求
	dataType: 'json', // 告知server,我需要什么格式
	contentType: 'application/json;charset=utf-8',  // 告知server,我给你的是什么格式
	data: JSON.stringify({
		'code':code,
		'input': ''
	}),
	success: function(data){
		//成功得到结果
		// console.log(data);
		show_result(data);
	}
});

在oj_control.hpp的Judge函数里添加一个LOG

        void Judge(const std::string &number, const std::string in_json, std::string *out_json)
        {
            LOG(DEBUG) << in_json << " \nnumber:" << number << "\n";
            // 0. 根据题目编号,直接拿到对应的题目细节
            struct Question q;
            model_.GetOneQuestion(number, &q);

显示结果到网页

function submit(){
	// alert("嘿嘿!");
	// 1. 收集当前页面的有关数据, 1. 题号 2.代码
	var code = editor.getSession().getValue();
	// console.log(code);
	var number = $(".container .part1 .left_desc h3 #number").text();
	// console.log(number);
	var judge_url = "/judge/" + number;
	// console.log(judge_url);
	// 2. 构建json,并通过ajax向后台发起基于http的json请求
	$.ajax({
		method: 'Post',   // 向后端发起请求的方式
		url: judge_url,   // 向后端指定的url发起请求
		dataType: 'json', // 告知server,我需要什么格式
		contentType: 'application/json;charset=utf-8',  // 告知server,我给你的是什么格式
		data: JSON.stringify({
			'code':code,
			'input': ''
		}),
		success: function(data){
			//成功得到结果
			// console.log(data);
			show_result(data);
		}
	});
	// 3. 得到结果,解析并显示到 result中
	function show_result(data)
	{
		// console.log(data.status);
		// console.log(data.reason);
		// 拿到result结果标签
		var result_div = $(".container .part2 .result");
		// 清空上一次的运行结果
		result_div.empty();

		// 首先拿到结果的状态码和原因结果
		var _status = data.status;
		var _reason = data.reason;

		var reason_lable = $( "<p>",{
			   text: _reason
		});
		reason_lable.appendTo(result_div);

		if(status == 0){
			// 请求是成功的,编译运行过程没出问题,但是结果是否通过看测试用例的结果
			var _stdout = data.stdout;
			var _stderr = data.stderr;

			var stdout_lable = $("<pre>", {
				text: _stdout
			});

			var stderr_lable = $("<pre>", {
				text: _stderr
			})

			stdout_lable.appendTo(result_div);
			stderr_lable.appendTo(result_div);
		}
		else{
			// 编译运行出错,do nothing
		}
	}
}

![[Pasted image 20250226143947.png]]

![[Pasted image 20250226143934.png]]

![[Pasted image 20250226144052.png]]

![[Pasted image 20250226144110.png]]

![[Pasted image 20250226144255.png]]

![[Pasted image 20250226144216.png]]

![[Pasted image 20250226144229.png]]

![[Pasted image 20250226144307.png]]

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

class Solution
{
public:
    int Max(const vector<int> &v)
    {
        //将你的代码写在下面
        int max = v[0];
        for(int i = 1; i < v.size(); i++)
        {
            if(max < v[i]){
                max = v[i];
            }
        }
        return max;
    }
};

![[Pasted image 20250226150107.png]]

![[Pasted image 20250226150119.png]]

![[Pasted image 20250226150132.png]]

上线与综合测试

负载测试

![[Pasted image 20250226151420.png]]

![[Pasted image 20250226151543.png]]

![[Pasted image 20250226151730.png]]

上线处理

oj_control.hpp

class Machine
{
public:
	void ResetLoad()
	{
		if(mtx) mtx->lock();
		load = 0;
		if(mtx) mtx->unlock();
	}
};

void OnlineMachine()
{
	//我们统一上线,后面统一解决
	mtx.lock();
	online.insert(online.end(), offline.begin(), offline.end());
	offline.erase(offline.begin(), offline.end());
	mtx.unlock();

	LOG(INFO) << "所有的主机有上线啦!" << "\n";
}

void OfflineMachine(int which)
{
	mtx.lock();
	for(auto iter = online.begin(); iter != online.end(); iter++)
	{
		if(*iter == which)
		{
			machines[which].ResetLoad();
			//要离线的主机已经找到啦
			online.erase(iter);
			offline.push_back(which);
			break; //因为break的存在,所有我们暂时不考虑迭代器失效的问题
		}
	}
	mtx.unlock();
}
public:
	void RecoveryMachine()
	{
		load_blance_.OnlineMachine();
	}

oj_server.cc

static Control *ctrl_ptr = nullptr;

void Recovery(int signo)
{
    ctrl_ptr->RecoveryMachine();
}

int main()
{
    signal(SIGQUIT, Recovery);
    ctrl_ptr = &ctrl;
}

使用ctrl+\,一键上线
![[Pasted image 20250226163910.png]]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值