Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <ctime>
- #include <cstdlib>
- #include <conio.h>
- #include <cstring>
- #include <windows.h>
- #include <fstream>
- using namespace std;
- int main()
- {
- srand(time(NULL));
- cout<<"===歡迎使用座位隨機分配程式==="<<endl;
- cout<<"請選擇背景顏色:1.黑 2.白"<<endl;//選顏色
- while(1)
- {
- char bg = getch();
- if(bg == '1')
- {
- system("cls");
- cout<<"===歡迎使用座位隨機分配程式==="<<endl;
- cout<<"請選擇字體顏色:1.綠 2.藍 3.紅 4.紫 5.黃 "<<endl;
- char color = getch();
- switch(color)
- {
- case'1':
- system("color 0a");
- break;
- case'2':
- system("color 0b");
- break;
- case'3':
- system("color 0c");
- break;
- case'4':
- system("color 0d");
- break;
- case'5':
- system("color 0e");
- break;
- }
- break;
- }
- else if(bg == '2')
- {
- system("cls");
- cout<<"===歡迎使用座位隨機分配程式==="<<endl;
- cout<<"請選擇字體顏色:1.綠 2.藍 3.紅 4.紫 5.金 "<<endl;
- char color = getch();
- switch(color)
- {
- case'1':
- system("color f2");
- break;
- case'2':
- system("color f1");
- break;
- case'3':
- system("color fc");
- break;
- case'4':
- system("color f5");
- break;
- case'5':
- system("color f6");
- break;
- }
- break;
- }
- }
- //讀取相關資料
- system("cls");
- cout<<"請輸入行列數:"<<endl;
- int x,y;
- cin>>x>>y;
- int a[x+1][y+1];
- memset(a,0,sizeof(a));
- system("cls");
- cout<<"請輸入人數:"<<endl;
- int num;
- cin>>num;
- string name[num+1];
- bool check[num] = {0};
- fstream f,ff;
- f.open("班級名條.txt",ios::in);
- ff.open("座位表.txt",ios::out|ios::trunc);//輸出時清除檔案中資料
- int count = 1;
- char ch;
- while(f.get(ch))
- {
- if(ch == '\n')
- count++;
- else
- name[count].push_back(ch);
- if(count>num)
- break;
- }
- //抽選前編排
- system("cls");
- cout<<"請輸入空位座標:"<<endl;
- cout<<"如要結束請輸入0 0"<<endl;
- int n,m;
- while(1)
- {
- cin>>n>>m;
- a[n][m] = -1;//該格將以X表示
- if(n == 0 && m == 0)
- break;
- }
- system("cls");
- cout<<"請輸入固定座位之座標與座號"<<endl;
- cout<<"如要結束請輸入0 0 0"<<endl;
- int nm;
- while(1)
- {
- cin>>n>>m>>nm;
- if(n == 0 && m == 0 && nm == 0)
- break;
- a[n][m] = nm;
- check[nm] = 1;
- }
- system("cls");
- cout<<"按下任意鍵開始抽籤"<<endl;
- system("pause");
- system("cls");
- cout<<"3~~~~"<<endl;
- Sleep(1000);
- system("cls");
- cout<<"2~~~~"<<endl;
- Sleep(1000);
- system("cls");
- cout<<"1~~~~"<<endl;
- Sleep(1000);
- system("cls");
- int w = 0;
- for(int i = 0;i<=x;i++)
- {
- if(w == num)
- break;
- for(int j = 0;j<=y;j++)
- {
- if(i == 0)//排版,數字對齊姓名
- {
- cout<<j<<" ";
- ff<<j<<" ";
- if(j == y)
- {
- cout<<endl<<endl;
- ff<<endl<<endl;
- }
- }
- else if(j == 0)
- {
- cout<<i<<" ";
- ff<<i<<" ";
- }
- else
- {
- if(w == num)
- {
- cout<<endl;
- ff<<endl;
- break;
- }
- if(a[i][j] > 0)
- {
- w++;
- if(a[i][j]<10)
- {
- cout<<" ";
- ff<<" ";
- }
- cout<<a[i][j]<<name[a[i][j]]<<" ";
- ff<<a[i][j]<<name[a[i][j]]<<" ";
- Sleep(1000);
- }
- else if(a[i][j] < 0)
- {
- cout<<" X ";
- ff<<" X ";
- Sleep(1000);
- }
- else
- {
- while(1)
- {
- int seat;
- seat = rand()%num +1;
- if(check[seat] == 0)
- {
- w++;
- check[seat] = 1;
- a[i][j] = seat;
- if(seat<10)
- {
- cout<<" ";
- ff<<" ";
- }
- cout<<seat<<name[seat]<<" ";
- ff<<seat<<name[seat]<<" ";
- Sleep(1000);
- break;
- }
- }
- }
- if(j == y)
- {
- cout<<endl<<endl;
- ff<<endl<<endl;
- }
- }
- }
- ff.close();
- }
- cout<<"是否要交換座位(y/n)?"<<endl;
- char change;
- while(1)
- {
- change = getch();
- if(change == 'y')
- {
- while(1)
- {
- cout<<"請輸入兩人座標,結束請輸入0 0 0 0"<<endl;
- int x1,y1,x2,y2;
- cin>>x1>>y1>>x2>>y2;
- if(x1 == 0 && y1 == 0 && x2 == 0&& y2 == 0)
- {
- break;
- }
- else
- {
- fstream fo;
- fo.open("座位表.txt",ios::out|ios::trunc);
- swap(a[x1][y1],a[x2][y2]);
- int ccount = 1;
- for(int i = 0;i<=x;i++)
- {
- if(ccount == num)
- break;
- for(int j = 0;j<=y;j++)
- {
- if(i == 0)
- {
- cout<<j<<" ";
- fo<<j<<" ";
- if(j == y)
- {
- cout<<endl;
- fo<<endl;
- }
- }
- else if(j == 0)
- {
- cout<<i<<" ";
- fo<<i<<" ";
- }
- else
- {
- if(ccount == num)
- {
- cout<<endl;
- fo<<endl;
- break;
- }
- if(a[i][j] < 0)
- {
- cout<<" X ";
- fo<<" X ";
- }
- else
- {
- if(a[i][j]<10)
- {
- cout<<" ";
- fo<<" ";
- }
- cout<<a[i][j]<<name[a[i][j]]<<" ";
- fo<<a[i][j]<<name[a[i][j]]<<" ";
- ccount++;
- }
- if(j == y)
- {
- cout<<endl<<endl;
- fo<<endl<<endl;
- }
- }
- }
- }
- }
- }
- break;
- }
- else if(change = 'n')
- break;
- }
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement