Advertisement
penguin88428

random seat

Mar 19th, 2017
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.49 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3. #include <cstdlib>
  4. #include <conio.h>
  5. #include <cstring>
  6. #include <windows.h>
  7. #include <fstream>
  8. using namespace std;
  9. int main()
  10. {
  11.     srand(time(NULL));
  12.     cout<<"===歡迎使用座位隨機分配程式==="<<endl;
  13.     cout<<"請選擇背景顏色:1.黑 2.白"<<endl;//選顏色
  14.     while(1)
  15.     {
  16.         char bg = getch();
  17.         if(bg == '1')
  18.         {
  19.             system("cls");
  20.             cout<<"===歡迎使用座位隨機分配程式==="<<endl;
  21.             cout<<"請選擇字體顏色:1.綠 2.藍 3.紅 4.紫 5.黃 "<<endl;
  22.            
  23.                 char color = getch();
  24.                 switch(color)
  25.                 {
  26.                     case'1':
  27.                     system("color 0a");
  28.                     break;
  29.                     case'2':
  30.                     system("color 0b");
  31.                     break;
  32.                     case'3':
  33.                     system("color 0c");
  34.                     break;
  35.                     case'4':
  36.                     system("color 0d");
  37.                     break;
  38.                     case'5':
  39.                     system("color 0e");
  40.                     break;
  41.                 }
  42.             break;
  43.         }
  44.         else if(bg == '2')
  45.         {
  46.             system("cls");
  47.             cout<<"===歡迎使用座位隨機分配程式==="<<endl;
  48.             cout<<"請選擇字體顏色:1.綠 2.藍 3.紅 4.紫 5.金 "<<endl;
  49.            
  50.                 char color = getch();
  51.                 switch(color)
  52.                 {
  53.                     case'1':
  54.                     system("color f2");
  55.                     break;
  56.                     case'2':
  57.                     system("color f1");
  58.                     break;
  59.                     case'3':
  60.                     system("color fc");
  61.                     break;
  62.                     case'4':
  63.                     system("color f5");
  64.                     break;
  65.                     case'5':
  66.                     system("color f6");
  67.                     break;
  68.                 }
  69.            
  70.             break;
  71.         }
  72.     }
  73.     //讀取相關資料
  74.     system("cls");
  75.     cout<<"請輸入行列數:"<<endl;
  76.     int x,y;
  77.     cin>>x>>y;
  78.     int a[x+1][y+1];
  79.     memset(a,0,sizeof(a));
  80.     system("cls");
  81.     cout<<"請輸入人數:"<<endl;
  82.     int num;
  83.     cin>>num;
  84.     string name[num+1];
  85.     bool check[num] = {0};
  86.     fstream f,ff;
  87.     f.open("班級名條.txt",ios::in);
  88.     ff.open("座位表.txt",ios::out|ios::trunc);//輸出時清除檔案中資料
  89.     int count = 1;
  90.     char ch;
  91.     while(f.get(ch))
  92.     {
  93.         if(ch == '\n')
  94.             count++;
  95.         else
  96.             name[count].push_back(ch);
  97.         if(count>num)
  98.             break;
  99.     }
  100.     //抽選前編排
  101.     system("cls");
  102.     cout<<"請輸入空位座標:"<<endl;
  103.     cout<<"如要結束請輸入0 0"<<endl;
  104.     int n,m;
  105.     while(1)
  106.     {
  107.         cin>>n>>m;
  108.         a[n][m] = -1;//該格將以X表示
  109.         if(n == 0 && m == 0)
  110.             break;
  111.     }
  112.     system("cls");
  113.     cout<<"請輸入固定座位之座標與座號"<<endl;
  114.     cout<<"如要結束請輸入0 0 0"<<endl;
  115.     int nm;
  116.     while(1)
  117.     {
  118.         cin>>n>>m>>nm;
  119.         if(n == 0 && m == 0 && nm == 0)
  120.             break;
  121.         a[n][m] = nm;
  122.         check[nm] = 1;
  123.     }
  124.     system("cls");
  125.     cout<<"按下任意鍵開始抽籤"<<endl;
  126.     system("pause");
  127.     system("cls");
  128.     cout<<"3~~~~"<<endl;
  129.     Sleep(1000);
  130.     system("cls");
  131.     cout<<"2~~~~"<<endl;
  132.     Sleep(1000);
  133.     system("cls");
  134.     cout<<"1~~~~"<<endl;
  135.     Sleep(1000);
  136.     system("cls");
  137.     int w = 0;
  138.     for(int i = 0;i<=x;i++)
  139.     {
  140.         if(w == num)
  141.             break;
  142.         for(int j = 0;j<=y;j++)
  143.         {
  144.             if(i == 0)//排版,數字對齊姓名
  145.             {
  146.                 cout<<j<<"         ";
  147.                 ff<<j<<"         ";    
  148.                 if(j == y)
  149.                 {
  150.                     cout<<endl<<endl;
  151.                     ff<<endl<<endl;
  152.                 }
  153.             }
  154.             else if(j == 0)
  155.             {
  156.                 cout<<i<<"    ";
  157.                 ff<<i<<"    ";
  158.             }
  159.             else
  160.             {
  161.                 if(w == num)
  162.                 {
  163.                     cout<<endl;
  164.                     ff<<endl;
  165.                     break;
  166.                 }
  167.                 if(a[i][j] > 0)
  168.                 {
  169.                     w++;
  170.                     if(a[i][j]<10)
  171.                     {
  172.                         cout<<" ";
  173.                         ff<<" ";
  174.                     }
  175.                     cout<<a[i][j]<<name[a[i][j]]<<"  ";
  176.                     ff<<a[i][j]<<name[a[i][j]]<<"  ";  
  177.                     Sleep(1000);
  178.                 }
  179.                 else if(a[i][j] < 0)
  180.                 {  
  181.                     cout<<"     X    ";
  182.                     ff<<"     X    ";  
  183.                     Sleep(1000);
  184.                 }
  185.                 else
  186.                 {
  187.                     while(1)
  188.                     {
  189.                         int seat;
  190.                         seat = rand()%num +1;
  191.                         if(check[seat] == 0)
  192.                         {
  193.                             w++;
  194.                             check[seat] = 1;
  195.                             a[i][j]  = seat;
  196.                             if(seat<10)
  197.                             {
  198.                                 cout<<" ";
  199.                                 ff<<" ";
  200.                             }
  201.                             cout<<seat<<name[seat]<<"  ";
  202.                             ff<<seat<<name[seat]<<"  ";
  203.                             Sleep(1000);
  204.                             break;
  205.                         }
  206.                     }
  207.                 }
  208.                 if(j == y)
  209.                 {
  210.                     cout<<endl<<endl;
  211.                     ff<<endl<<endl;
  212.                 }
  213.             }
  214.         }
  215.     ff.close();    
  216.     }
  217.     cout<<"是否要交換座位(y/n)?"<<endl;
  218.     char change;
  219.     while(1)
  220.     {
  221.         change = getch();
  222.         if(change == 'y')
  223.         {
  224.             while(1)
  225.             {          
  226.                 cout<<"請輸入兩人座標,結束請輸入0 0 0 0"<<endl;
  227.                 int x1,y1,x2,y2;
  228.                 cin>>x1>>y1>>x2>>y2;
  229.                 if(x1 == 0 && y1 == 0 && x2 == 0&& y2 == 0)
  230.                 {
  231.                     break;
  232.                 }
  233.                 else
  234.                 {
  235.                     fstream fo;
  236.                     fo.open("座位表.txt",ios::out|ios::trunc);
  237.                     swap(a[x1][y1],a[x2][y2]);
  238.                     int ccount = 1;
  239.                     for(int i = 0;i<=x;i++)
  240.                     {
  241.                         if(ccount == num)
  242.                             break;         
  243.                         for(int j = 0;j<=y;j++)
  244.                         {
  245.                             if(i == 0)
  246.                             {
  247.                                 cout<<j<<"         ";
  248.                                 fo<<j<<"         ";
  249.                                 if(j == y)
  250.                                 {
  251.                                     cout<<endl;
  252.                                     fo<<endl;
  253.                                 }
  254.                             }
  255.                             else if(j == 0)
  256.                             {
  257.                                 cout<<i<<"    ";
  258.                                 fo<<i<<"    ";
  259.                             }
  260.                             else
  261.                             {
  262.                                 if(ccount == num)
  263.                                 {
  264.                                     cout<<endl;
  265.                                     fo<<endl;
  266.                                     break;
  267.                                 }
  268.                                 if(a[i][j] < 0)
  269.                                 {
  270.                                 cout<<"     X    ";
  271.                                 fo<<"     X    ";  
  272.                                 }
  273.                                 else
  274.                                 {
  275.                                     if(a[i][j]<10)
  276.                                     {
  277.                                         cout<<" ";
  278.                                         fo<<" ";
  279.                                     }
  280.                                     cout<<a[i][j]<<name[a[i][j]]<<"  ";
  281.                                     fo<<a[i][j]<<name[a[i][j]]<<"  ";
  282.                                     ccount++;
  283.                                 }                  
  284.                                 if(j == y)
  285.                                 {
  286.                                     cout<<endl<<endl;
  287.                                     fo<<endl<<endl;
  288.                                 }
  289.                             }      
  290.                         }  
  291.                     }
  292.                 }      
  293.             }
  294.         break;
  295.         }
  296.         else if(change = 'n')
  297.             break;     
  298.     }
  299.     system("pause");
  300. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement