随机分位系统,适用于班级中的座位分配。
Version 4.1 功能如下
–分配后二次调换
–座位表导出(exe)
–定点分位
注:行、列设置在代码第 :12 行,默认5行8列,共40人
请按照string数组定义格式规范输入学生姓名信息!!!
《Created By WTC ,未经许可请勿转载》
#include<windows.h>
#include<bits/stdc++.h>
#include<cmath>
#include<ctime>
#include<fstream>
#define This_program_is_writing_by_WTC
#define Sunshine_Positioning_System
//定义所需头文件
using namespace std;
int h=5,l=8;//定义座位表布局
bool seat[10][10];//判断此座位是否被占用
bool stu[100];//判断该学生是否已经分配座位
string name[100]={//定义学生名单
" error ",//不存在编号为0的学生
" aaa ",
" bbb ",
//" ccc ",
};
string s[10][10];//定义座位表数组
void slowout(string nr)//慢速输出文字
{
int sz=nr.length();
for(int i=0;i<sz;i++)
{
cout<<nr[i];
Sleep(1);
}
}
string setname(string a)//输入处理
{
string chulia;
for(int i=0;i<a.size()-1;i++)
{
if(a[i]!=' ')//去除空格
{
chulia+=a[i];
}
}
return chulia;
}
string nameadd(string adds)//添加空格
{
int padding=0;
if(adds.length()==4) padding=2;
else if(adds.length()==6) padding=1;
adds.insert(0, padding, ' ');
adds.append(padding, ' ');
return adds;
}
int findxy(string what)//寻找学生下标
{
int leng=50;
for(int i=1;i<=leng;i++)
{
if(name[i]==what)//如果找到即返回下标
{
return i;
}
}
return 0;//没有找到即返回0
}
// 定义导出到文件的函数
void exportToFile(string seatTable[10][10], int rows, int cols) {
ofstream outFile("zwb.txt"); // 创建并打开文件 zwb.txt
if (outFile.is_open()) {
// 写入表头
outFile << "行\\\\列| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |" << endl << endl;
// 循环写入每一行的内容
for (int i = 1; i <= rows; i++) {
outFile << "| " << i << " |";
for (int j = 1; j <= cols; j++) {
outFile << seatTable[i][j] << "|";
}
outFile << endl << endl;
}
outFile.close(); // 关闭文件
}
}
struct Students_with_reserved_seats
{
int posx=0,posy=0;
int pos_stu=0;
}t[15];
int main()
{
system("cls");//清空屏幕
system("title Sunshine Positioning System—By WTC");//设置窗口标题
srand(time(0));//防止生成伪随机数
stu[99]=true;//使第一次生成时进入循环
//生成前内容部分
slowout("阳光分位系统(Sunshine Positioning System) ——By WTC");
Sleep(345);
cout<<endl;
slowout("请按任意键继续. . .");
system("cls");
cout<<"阳光分位系统(Sunshine Positioning System) ——By WTC";
cout<<endl;
system("pause");
system("cls");
int num=0,count=0;
slowout("请输入定点位置的学生个数:");
cin>>num;
while(count<num)
{
count++;
cout<<"\\n\\n";
slowout(" Case #");
Sleep(1);
cout<<count;
Sleep(1);
cout<<endl;
string posnam,posname;
slowout("请输入需要固定位置的学生名:");
cin>>posnam;
posname=nameadd(posnam);
t[count].pos_stu=findxy(posname);
if(findxy(posname)==0)
{
slowout("没有找到该学生!");
Sleep(567);
count–;
continue;
}
slowout("请输入该学生的位置:(行 列)");
cin>>t[count].posx>>t[count].posy;
if(t[count].posx<1||t[count].posx>h||t[count].posy<1||t[count].posy>l)
{
slowout("没有该位置!");
Sleep(567);
count–;
continue;
}
seat[t[count].posx][t[count].posy]=true;
stu[t[count].pos_stu]=true;
}
//开始随机分配座位
slowout("座位表已生成:");
cout<<endl;
slowout("行\\\\列| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |");
Sleep(1);
cout<<endl<<endl;
for(int i=1;i<=h;i++)
{
cout<<"|";
Sleep(1);
cout<<" ";
Sleep(1);
cout<<" ";
Sleep(1);
cout<<i;
Sleep(1);
cout<<" ";
Sleep(1);
cout<<"|";
Sleep(1);
for(int j=1;j<=l;j++)
{
Sleep(1);
bool isReserved = false;
for(int k=1;k<=num;k++)
{
if(i==t[k].posx&&j==t[k].posy)
{
slowout(name[t[k].pos_stu]);
s[i][j] = name[t[k].pos_stu]; // 记录固定座位的学生
Sleep(1);
cout<<"|";
isReserved = true;
break; // 找到后退出内层循环
}
}
if (isReserved) {
continue; // 如果是固定座位,跳过随机分配
}
// 处理随机分配的座位
int st=0;
while(stu[st]==true||name[st]==""||name[st]==" error ")
{//如果生成了不在区间内的随机数或该学生已经分配座位,则重新生成
st=rand()%50+1;//生成随机数
}
slowout(name[st]);//输出学生姓名
stu[st]=true;//标记该学生已分配到座位
s[i][j]=name[st];//记录该座位学生
Sleep(1);
cout<<"|";
}
Sleep(99);
cout<<endl<<endl;
}
//调换座位部分
slowout("生成完毕,输入坐标(行 列)调换座位:");
cout<<endl;
int x,y,a,b;
while(true)
{
//输出提示内容
cout<<endl;
slowout("请输入需要调换的两个座位: ");
Sleep(1);
cout<<endl;
slowout("示例:");
Sleep(1);
cout<<endl;
slowout("________________________________________");
Sleep(1);
cout<<endl;
slowout("如1行1列为");
cout<<s[1][1];
Sleep(1);
cout<<endl;
slowout("2排2列为");
cout<<s[2][2];
Sleep(1);
cout<<endl;
slowout("如需调换二人座位,则在S1输入:1 1 ,S2输入 2 2 。");
Sleep(1);
cout<<endl;
slowout("如需终止程序,请在S1第一个数输入0。");
Sleep(1);
cout<<endl;
slowout("—————————————-");
Sleep(1);
cout<<endl;
slowout(" S1 = ");
cin>>x>>y;//请求S1坐标
if(x==0) break;//x为0,则结束程序
else if(x<1||x>h||y<1||y>l)//判断x,y是否为合理值
{
slowout("无效输入!");
Sleep(123);
system("cls");//如果是,则要求重新输入
}
else
{
cout<<endl;
slowout(" S2 = ");
cin>>a>>b;//请求S2坐标
if(a<1||a>h||b<1||b>l) //判断x,y是否为合理值
{
slowout("无效输入!");
Sleep(123);
system("cls");//如果是,则要求重新输入
}
else
{
swap(s[x][y],s[a][b]);//交换S1与S2
slowout("已完成:");
cout<<s[a][b]<<"与"<<s[x][y];
slowout("的调换!");
Sleep(2345);
system("cls");
slowout("调换后座位表:");
}
}
//输出调换后座位表
cout<<"\\n行\\\\列| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |\\n";
for(int i=1;i<=h;i++)
{
cout<<"\\n| "<<i<<" |";
for(int j=1;j<=l;j++)
{
cout<<s[i][j];
cout<<"|";
}
cout<<endl;
}
}
//调用函数将生成的座位表导出到文件
exportToFile(s, h, l);
slowout("座位表已成功导出到 zwb.txt 文件!");
cout<<endl;
Sleep(1);
cout<<endl;
slowout("程序已结束。");//结束语
return 10086;
}
// Created by WTC
/*
Version 04.01
-8.633 KiB
编译器:TDM-GCC 4.9.2 64-bit Release
CD : May 19 2025 16:07:08
RT :Jul 25 2026 09:04:24
更新日志:
————————————
①2025/9/23 V1.0完成随机分位主体内容
②2025/9/24 V2.0优化内容显示、增加调换位置功能
③2025/9/25 V2.1优化内容显示、增加代码注释
④2025/9/26 V3.0增加学生绑定功能、优化内容显示、增加座位表导出到文件功能
⑤2025/10/26 V4.0去除学生绑定功能、简化代码、增加定点位置功能
⑥2025/11/3 V4.1修复定点位置功能的已知BUG
*/
想偷偷和自己想坐的人绑定在一起?#【戳我】#
网硕互联帮助中心






评论前必须登录!
注册