可运行代码 Demo

2026-10-02 更新 50 次浏览

下面三段代码是真的在服务器沙箱里跑的:点「▶ 运行」就能看到输出 —— 一次性容器、没有网络、限时 12 秒;只能跑预先登记的代码,访客不能改源码。

① C++ 通讯录管理系统(菜单式交互)

下面那块「预设输入」就是我在替你按键:添加一位联系人 → 显示全部 → 退出。点运行即可。

C++ 通讯录管理系统 CPP 在服务器沙箱里跑:无网络 · 限时 12 秒 · 只跑登记过的代码

#include<iostream>
using namespace std;
#include<string>

#define MAX 1000

typedef struct Person//联系人结构体
{
	string name;
	int sex;//1男 2女
	int age;
	string phone;
	string address;
}P;
typedef struct Addressbooks//通讯录结构体
{
	P personArray[MAX];
	int number;
}A;
void menu()
{
	cout << "**************************" << endl;
	cout << "****** 1.添加联系人 ******" << endl;
	cout << "****** 2.显示联系人 ******" << endl;
	cout << "****** 3.删除联系人 ******" << endl;
	cout << "****** 4.查找联系人 ******" << endl;
	cout << "****** 5.修改联系人 ******" << endl;
	cout << "****** 6.清空联系人 ******" << endl;
	cout << "****** 0.退出通讯录 ******" << endl;
	cout << "**************************" << endl;

}
void AddPerson(A*abs)
{
	//姓名
	string name;
	cout << "请输入姓名:"<<endl;
	cin >> name;
	abs->personArray[abs->number].name = name;
	//性别
	int sex = 0;
	cout << "请输入性别:"<<endl;
	cout << "1--男 2--女" << endl;
	while (1)
	{
		cin >> sex;
		if (sex == 1 || sex == 2)
		{
			abs->personArray[abs->number].sex = sex;
			break;
		}
		else
		{
			cout << "输入有误,请重新输入:>" << endl;
		}
	}
	//年龄
	int age = 0;
	cout << "请输入年龄:" << endl;
	cin >> age;
	abs->personArray[abs->number].age = age;
	//电话
	string phone;
	cout << "请输入电话:" << endl;
	cin >> phone;
	abs->personArray[abs->number].phone = phone;
	//住址
	string address;
	cout << "请输入住址:" << endl;
	cin >> address;
	abs->personArray[abs->number].address = address;

	abs->number++;	//人数加1
	cout << "添加成功!" << endl; 
	system("pause");
	system("cls");
}
void ShowPerson(A*abs)
{
	if (abs->number == 0)
	{
		cout << "当前记录为空!" << endl;
		system("pause");
	}
	else
	{
		for (int i = 0; i < abs->number; i++)
		{
			cout << "姓名: " << abs->personArray[i].name <<"    ";
			cout << "性别: " << (abs->personArray[i].sex == 1 ? "男" : "女") << "    ";
			cout << "年龄: " << abs->personArray[i].age << "    ";
			cout << "电话: " << abs->personArray[i].phone << "    ";
			cout << "住址: " << abs->personArray[i].address << endl;
		}
	}
	system("pause");
	system("cls");
}
int IsExist(A* abs, string name)
{
	for (int i = 0; i < abs->number; i++)
	{
		if (abs->personArray[i].name == name)
		{
			return i;
		}
	}
	return -1;
}
void DeletePerson(A* abs)
{
	string name;
	cout << "请输入要删除的联系人姓名:"<<endl; 
	cin >> name;
	int ret = IsExist(abs, name);
	if (ret != -1)
	{
		if (ret == (abs->number-1))
		{
			abs->number--;
		}
		else
		{
			for (int i = ret; i < abs->number; i++)
			{
				abs->personArray[i] = abs->personArray[i + 1];
				abs->number--;
			}
		}
		cout << "删除成功!" << endl;
		system("pause");
		system("cls");
	}
	else
	{
		cout << "查无此人!" << endl;
		system("pause");
		system("cls");
	}
}
void FindPerson(A* abs)
{
	string name;
	cout << "请输入要查找的联系人姓名:" << endl;
	cin >> name;
	int ret = IsExist(abs, name);
	if (ret != -1)
	{
		cout << "姓名: " << abs->personArray[ret].name << "    ";
		cout << "性别: " << (abs->personArray[ret].sex == 1 ? "男" : "女") << "    ";
		cout << "年龄: " << abs->personArray[ret].age << "    ";
		cout << "电话: " << abs->personArray[ret].phone << "    ";
		cout << "住址: " << abs->personArray[ret].address << endl;
	}
	else
	{
		cout << "查无此人!" << endl;
	}
	system("pause");
	system("cls");
}
void ModifyPerson(A*abs)
{
	cout << "请输入要修改的联系人姓名:" << endl;
	string name;
	cin >> name;
	int ret = IsExist(abs, name);
	if (ret != -1)
	{
		cout << "请输入新的姓名:" << endl;
		cin >> abs->personArray[ret].name;
		cout << "请输入新的性别:" << endl;
		cout << "1--男 2--女" << endl;
		cin >> abs->personArray[ret].sex;
		cout << "请输入新的年龄:" << endl;
		cin >> abs->personArray[ret].age;
		cout << "请输入新的电话:" << endl;
		cin >> abs->personArray[ret].phone;;
		cout << "请输入新的住址:" << endl;
		cin >> abs->personArray[ret].address;
		cout << "更改成功!" << endl;
	}
	else
	{
		cout << "查无此人!" << endl;
	}
	system("pause");
	system("cls");
}
void CleanPerson(A* abs)
{
	int input = 0;
	cout << "是否确认清空通讯录?" << endl;
	cout << "1--确认 其他--取消" << endl;
	cin >> input;
	if (input == 1)
	{
		abs->number = 0;
		cout << "通讯录已清空!" << endl;
	}
	else
	{
		cout << "已取成功取消!" << endl;
	}
	system("pause");
	system("cls");
}
int main()
{
	A books;
	books.number = 0;//创建通讯录并初始化人数为0
	int input = 0;
	do
	{
		menu();
		cout << "请选择功能:>";
		cin >> input;
		switch (input)
		{
		case 0://出通讯录
			cout << "欢迎下次使用!" << endl; 
			system("pause");
			return 0;
		case 1://添加联系人
			AddPerson(&books);
			break;
		case 2://显示联系人
			ShowPerson(&books);
			break;
		case 3://删除联系人
			DeletePerson(&books);
			break;
		case 4://查找联系人
			FindPerson(&books);
			break;
		case 5://修改联系人
			ModifyPerson(&books);
			break;
		case 6://清空联系人
			CleanPerson(&books);
			break;
		default:
			cout << "输入有误,请重新输入!" << endl; 
			break;
		}

	} while (input);
	return 0;
}
预设输入(stdin)
输出
点「▶ 运行」看结果;想边跑边输就点「⌨ 交互运行」
代码取自文章《c++通讯录管理系统》的展示页

② C++ STL 容器示例(无需输入)

C++ STL 容器示例(vector / map) CPP 在服务器沙箱里跑:无网络 · 限时 12 秒 · 只跑登记过的代码

#include<iostream>
using namespace std;
#include<map>
#include<string>
#include<cstdlib>
#include<vector>

class Person
{
public:
	Person(string name,int salary)
	{
		this->m_name = name;
		this->m_salary= salary;
	}
	string m_name;
	int m_salary;
};
void makeperson(vector<Person>& p)//创建员工
{
	string nameseed = "ABCDEF";
	for (int i = 0; i < 6; i++)
	{
		string name = "职工";
		name += nameseed[i];
		int salary = rand()%(8000 - 5000 + 1) + 5000;
		Person worker(name,salary);
		p.push_back(worker);
	}	
}
void setdep(vector<Person>& p, multimap<int, Person> &m)//分部门
{
	for (int i = 0; i < 6; i++)
	{
		int d = rand() % (3 - 1 + 1) + 1;
		m.insert(make_pair(d, p[i]));
	}
}
void show(multimap<int, Person>& m)//展示
{
	cout << "策划部门:" << endl;
	multimap<int,Person>::iterator pos1 = m.find(1);
	int num1 = m.count(1);
	int index1 = 0;
	for (; pos1 != m.end() && index1 < num1; pos1++, index1++)
	{
		cout << "姓名:" << pos1->second.m_name
			<< "  工资:" << pos1->second.m_salary << endl;
	}
	cout << "美术部门:" << endl;
	multimap<int, Person>::iterator pos2 = m.find(2);
	int num2 = m.count(2);
	int index2 = 0;
	for (; pos2 != m.end() && index2 < num2; pos2++, index2++)
	{
		cout << "姓名:" << pos2->second.m_name
			<< "  工资:" << pos2->second.m_salary << endl;
	}
	cout << "研发部门:" << endl;
	multimap<int, Person>::iterator pos3= m.find(3);
	int num3 = m.count(3);
	int index3 = 0;
	for (; pos3 != m.end() && index3 < num3; pos3++, index3++)
	{
		cout << "姓名:" << pos3->second.m_name
			<< "  工资:" << pos3->second.m_salary << endl;
	}
}
void test()
{
	vector<Person> p;
	makeperson(p);
	multimap<int, Person> m;
	setdep(p, m);
	show(m);
}



int main()
{
	test();
	return 0;
}
预设输入(stdin)
输出
点「▶ 运行」看结果;想边跑边输就点「⌨ 交互运行」
代码取自《cpp容器》展示页

③ Python 斐波那契(无需输入)

Python 斐波那契(生成器) PYTHON 在服务器沙箱里跑:无网络 · 限时 12 秒 · 只跑登记过的代码

from typing import Iterator


def fib(n: int) -> Iterator[int]:
    """生成前 n 个斐波那契数。"""
    a, b = 0, 1
    for _ in range(n):
        yield a
        a, b = b, a + b


def main() -> None:
    nums = [x for x in fib(10) if x % 2 == 0]
    print(f"偶数项:{nums},共 {len(nums)} 个")

    # 比较与位运算示例(< 与 & 都需转义)
    if len(nums) <= 10 and (len(nums) & 1) == 0:
        print("条件成立")


if __name__ == "__main__":
    main()
预设输入(stdin)
输出
点「▶ 运行」看结果;想边跑边输就点「⌨ 交互运行」
代码取自 md/python 展示模板的内置示例

(这一页是给你验收用的测试页,随时可以删。)

🔑