佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 1939|回复: 27

C语言求救

[复制链接]
发表于 20-1-2014 07:08 PM | 显示全部楼层 |阅读模式
我的code有很多用printf做的table
请问如果我1个table我要用在7天,怎样才可以让它分成7次?
就好像
struct badminton(){
         int x;
}
then 我可以
struct badminton 1;
int x = 5;
stuct badminton 2;
int x= 6;
可以把我的table做的类似酱嘛?酱我就不用酱麻烦写到酱多table了
  1. void badtable(){
  2.         printf("|---------------------------------------------------------------|\n");
  3.         printf("|    court  | 7am-9am | 9am-11am | 11am-1pm | 1pm-3pm | 3pm-5pm |\n");
  4.         printf("|---------------------------------------------------------------|\n");
  5.         printf("|      A    |   1. %s     2. %s      3. %s        4. %s     5. %s   |\n", q1.bad1, q1.bad2, q1.bad3, q1.bad4, q1.bad5);
  6.         printf("|---------------------------------------------------------------|\n");
  7.         printf("|      A    |   6. %s     7. %s      8. %s        9. %s     10. %s   |\n", q1.bad1, q1.bad2, q1.bad3, q1.bad4, q1.bad5);
  8.         printf("|---------------------------------------------------------------|\n");
  9.         printf("|      A    |   11. %s    12. %s     13. %s       14. %s    15. %s   |\n", q1.bad1, q1.bad2, q1.bad3, q1.bad4, q1.bad5);
  10.         printf("|---------------------------------------------------------------|\n");
  11.         printf("|      A    |   16. %s    17. %s     18. %s       19. %s    20. %s   |\n", q1.bad1, q1.bad2, q1.bad3, q1.bad4, q1.bad5);
  12.         printf("|---------------------------------------------------------------|\n");
复制代码
本帖最后由 Chaisoo333 于 20-1-2014 07:25 PM 编辑

回复

使用道具 举报


ADVERTISEMENT

发表于 21-1-2014 09:33 AM | 显示全部楼层
用for loop啊。。。基本中的基本。

  1. int num;

  2. for(int i=0; i<badminton.x; i++)
  3. {
  4.     num = (i * 5) + 1;
  5.     printf("|      A    |   %d. %s     %d. %s      %d. %s        %d. %s     %d. %s   |\n", num, q1.bad1, (num+1), q1.bad2, (num + 2), q1.bad3, (num + 3), q1.bad4, (num + 4), q1.bad5);
  6. }
复制代码
回复

使用道具 举报

 楼主| 发表于 22-1-2014 02:20 AM | 显示全部楼层
geekman 发表于 21-1-2014 09:33 AM
用for loop啊。。。基本中的基本。

不好意思我写错了
table的code是这个
  1. printf("|---------------------------------------------------------------|\n");
  2.         printf("|    court  | 7am-9am | 9am-11am | 11am-1pm | 1pm-3pm | 3pm-5pm |\n");
  3.         printf("|---------------------------------------------------------------|\n");
  4.         printf("|      A    |   1. %s     2. %s      3. %s        4. %s     5. %s   |\n", q1.bad1, q1.bad2, q1.bad3, q1.bad4, q1.bad5);
  5.         printf("|---------------------------------------------------------------|\n");
  6.         printf("|      B    |   6. %s     7. %s      8. %s        9. %s     10. %s   |\n", q1.bad6, q1.bad7, q1.bad8, q1.bad9, q1.bad10);
  7.         printf("|---------------------------------------------------------------|\n");
  8.         printf("|      C    |   11. %s    12. %s     13. %s       14. %s    15. %s   |\n", q1.bad11, q1.bad12, q1.bad13, q1.bad14, q1.bad15);
  9.         printf("|---------------------------------------------------------------|\n");
  10.         printf("|      D    |   16. %s    17. %s     18. %s       19. %s    20. %s   |\n", q1.bad16, q1.bad17, q1.bad18, q1.bad19, q1.bad20);
  11.         printf("|---------------------------------------------------------------|\n");
复制代码
因为我要用到太多variable (q1.bad1....),看到很乱,请问我要怎样写才可以给他自动打q1.bad1,的1这个号码
这个table是放进一个switch case的
case1: q1.bad1 =X;
case2: q1.bad2 =x;
case3.......
case20:q1.bad20=x;
变成我有20个case@@,怎样子好
回复

使用道具 举报

发表于 22-1-2014 10:15 AM | 显示全部楼层
你的描述还是很模糊,我不知道你的q1是什么样的,也不知道你的q1.bad1代表的是什么,也不知道你的case 1, case 2...代表的是什么,因为你没说你的switch()是什么,所以无法给你很好的答案。如果你很珍惜你的Source Code,我只能说,论坛不是适合你发问的地方。

现在我只能对你说,用Array吧。
回复

使用道具 举报

 楼主| 发表于 22-1-2014 02:34 PM | 显示全部楼层
我不是不要放
只是害羞,我的code很noob
这个是半成品,你试试看run了选1for全部selection
密码是class
  1. #include <conio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. #include <stdio.h>
  6. char pasword[10], usrname[10], ch;
  7. char day[10];
  8. int i, menu1, badminton, qq;

  9. struct badminton_none{
  10.         char *bad1, *bad2, *bad3, *bad4, *bad5, *bad6, *bad7, *bad8, *bad9, *bad10;
  11.         char *bad11, *bad12, *bad13, *bad14, *bad15, *bad16, *bad17, *bad18, *bad19, *bad20;
  12. }q1 = { "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O" };
  13. void chooseday(){
  14.         printf("Which day?");
  15.         scanf("%s", &day);
  16.         while (!strcmp(day, "monday"))
  17.         {
  18.                 printf("Monday is off day !\nPlease choose another day!");
  19.                 scanf("%s", &day);
  20.         }

  21.         system("cls");

  22.         {
  23.                 printf("You have choose %s\nPlease pick a time\n", day);
  24.                 printf("1.None Peak time\n");
  25.                 printf("2.Peak time\n");
  26.                 scanf("%d", &badminton);

  27.         }
  28. }
  29. void badtable(int num){
  30.         printf("|---------------------------------------------------------------|\n");
  31.         printf("|    court  | 7am-9am | 9am-11am | 11am-1pm | 1pm-3pm | 3pm-5pm |\n");
  32.         printf("|---------------------------------------------------------------|\n");
  33.         printf("|      A    |   1. %s     2. %s      3. %s        4. %s     5. %s   |\n", q1.bad1, q1.bad2, q1.bad3, q1.bad4, q1.bad5);
  34.         printf("|---------------------------------------------------------------|\n");
  35.         printf("|      B    |   6. %s     7. %s      8. %s        9. %s     10. %s   |\n", q1.bad6, q1.bad7, q1.bad8, q1.bad9, q1.bad10);
  36.         printf("|---------------------------------------------------------------|\n");
  37.         printf("|      C    |   11. %s    12. %s     13. %s       14. %s    15. %s   |\n", q1.bad11, q1.bad12, q1.bad13, q1.bad14, q1.bad15);
  38.         printf("|---------------------------------------------------------------|\n");
  39.         printf("|      D    |   16. %s    17. %s     18. %s       19. %s    20. %s   |\n", q1.bad16, q1.bad17, q1.bad18, q1.bad19, q1.bad20);
  40.         printf("|---------------------------------------------------------------|\n");
  41. }
  42. void checkcourt(){
  43.         printf("1.Badminton\n");
  44.         printf("2.Squash\n");
  45.         printf("3.Tennis\n");
  46.         printf("4.Futsal\n");
  47.         scanf("%d", &menu1);
  48.         system("cls");

  49.         if (menu1 == 1){
  50.                 chooseday();
  51.                 switch (badminton){
  52.                 case 1:{
  53.                                    badtable(1);
  54.                                    printf("Please insert the court");
  55.                                    scanf("%d", &qq);
  56.                                    system("cls");
  57.                                    switch (qq)
  58.                                    {
  59.                                    case 1: {
  60.                                                            printf("You successsfully have booked a court.\n");
  61.                                                            q1.bad1  = "X";
  62.                                                            badtable(1);
  63.                                                            mainmenu();
  64.                                    }break;

  65.                                    case 2: {
  66.                                                            printf("You successsfully have booked a court.");
  67.                                                            q1.bad2 = "X";
  68.                                                            badtable(1);
  69.                                                            mainmenu();
  70.                                    }break;
  71.                                    }

  72.                 }break;

  73.                 case 2:{
  74.                                    printf("|------------------------------------------|\n");
  75.                                    printf("|    court  | 5pm-7pm | 7pm-9pm | 9pm-10pm |\n");
  76.                                    printf("|------------------------------------------|\n");
  77.                                    printf("|      A    |                              |\n");
  78.                                    printf("|------------------------------------------|\n");
  79.                                    printf("|      B    |                              |\n");
  80.                                    printf("|------------------------------------------|\n");
  81.                                    printf("|      C    |                              |\n");
  82.                                    printf("|------------------------------------------|\n");
  83.                                    printf("|      D    |                              |\n");
  84.                                    printf("|------------------------------------------|\n");

  85.                 }break;
  86.                 }

  87.         }
  88.         else if (menu1 == 2)
  89.         {
  90.                 chooseday();
  91.                 switch (badminton){

  92.                 case 1:{
  93.                                    printf("|---------------------------------------------------------------|\n");
  94.                                    printf("|    court  |  10am-12am  |  12pm-2pm  |  2pm-4pm  |  4pm-5pm   |\n");
  95.                                    printf("|---------------------------------------------------------------|\n");
  96.                                    printf("|      A    |                                                   |\n");
  97.                                    printf("|---------------------------------------------------------------|\n");
  98.                                    printf("|      B    |                                                   |\n");
  99.                                    printf("|---------------------------------------------------------------|\n");
  100.                 }
  101.                 case 2:{
  102.                                    printf("|---------------------------------------------------------------|\n");
  103.                                    printf("|    court  |     5pm-7pm    |     7pm-9pm    |     9pm-10pm    |\n");
  104.                                    printf("|---------------------------------------------------------------|\n");
  105.                                    printf("|      A    |                                                   |\n");
  106.                                    printf("|---------------------------------------------------------------|\n");
  107.                                    printf("|      B    |                                                   |\n");
  108.                                    printf("|---------------------------------------------------------------|\n");
  109.                 }
  110.                 }

  111.         }
  112.         else if (menu1 == 3)
  113.         {
  114.                 chooseday();
  115.                 switch (badminton){

  116.                 case 1:{
  117.                                    printf("|---------------------------------------------------------------------------|\n");
  118.                                    printf("|    court  |  8am-10am  |  10am-12pm  |  12pm-2pm  |  2pm-4pm  |  4pm-5pm  |\n");
  119.                                    printf("|---------------------------------------------------------------------------|\n");
  120.                                    printf("|      A    |                                                               |\n");
  121.                                    printf("|---------------------------------------------------------------------------|\n");
  122.                                    printf("|      B    |                                                               |\n");
  123.                                    printf("|---------------------------------------------------------------------------|\n");
  124.                                    printf("|      c    |                                                               |\n");
  125.                                    printf("|---------------------------------------------------------------------------|\n");
  126.                 }
  127.                 case 2:{
  128.                                    printf("|---------------------------------------------------------------|\n");
  129.                                    printf("|    court  |     5pm-7pm    |     7pm-9pm    |     9pm-10pm    |\n");
  130.                                    printf("|---------------------------------------------------------------|\n");
  131.                                    printf("|      A    |                                                   |\n");
  132.                                    printf("|---------------------------------------------------------------|\n");
  133.                                    printf("|      B    |                                                   |\n");
  134.                                    printf("|---------------------------------------------------------------|\n");
  135.                                    printf("|      c    |                                                   |\n");
  136.                                    printf("|---------------------------------------------------------------|\n");
  137.                 }
  138.                 }
  139.         }
  140.         else if (menu1 == 4){

  141.                 chooseday();
  142.                 switch (badminton){

  143.                 case 1:{
  144.                                    printf("|---------------------------------------------------------------------------|\n");
  145.                                    printf("|    court  |  7am-9am  |  9am-11am  |  11am-1pm  |  1pm-3pm  |  3pm-5pm    |\n");
  146.                                    printf("|---------------------------------------------------------------------------|\n");
  147.                                    printf("|      A    |                                                               |\n");
  148.                                    printf("|---------------------------------------------------------------------------|\n");
  149.                                    printf("|      B    |                                                               |\n");
  150.                                    printf("|---------------------------------------------------------------------------|\n");
  151.                                    printf("|      c    |                                                               |\n");
  152.                                    printf("|---------------------------------------------------------------------------|\n");
  153.                 }
  154.                 case 2:{
  155.                                    printf("|---------------------------------------------------------------|\n");
  156.                                    printf("|    court  |     5pm-7pm    |     7pm-9pm    |     9pm-10pm    |\n");
  157.                                    printf("|---------------------------------------------------------------|\n");
  158.                                    printf("|      A    |                                                   |\n");
  159.                                    printf("|---------------------------------------------------------------|\n");
  160.                                    printf("|      B    |                                                   |\n");
  161.                                    printf("|---------------------------------------------------------------|\n");
  162.                                    printf("|      c    |                                                   |\n");
  163.                                    printf("|---------------------------------------------------------------|\n");
  164.                 }
  165.                 }
  166.         }
  167.         else{}
  168. }
  169. int mainmenu(){
  170.         system("cls");
  171.         printf("1.Check court availability\n");
  172.         printf("2.Booking\n");
  173.         printf("3.Generate bill\n");
  174.         printf("4.Exit\n");
  175.         scanf("%d", &menu1);
  176.         system("cls");
  177.         switch (menu1){
  178.         case 1:{
  179.                            checkcourt();
  180.         }
  181.                            
  182.         }
  183.         }
  184. int main()
  185. {

  186.         printf("Enter User name: ");
  187.         gets(usrname);
  188.         printf("Enter the password : ");

  189.         for (i = 0; i < 10; i++)
  190.         {
  191.                 ch = getch();

  192.                 if (isprint(ch))
  193.                 {
  194.                         pasword[i] = ch;
  195.                         ch = '*';
  196.                         printf("%c", ch);
  197.                 }
  198.                 else
  199.                 {
  200.                         pasword[i] = '\0';
  201.                         break;
  202.                 }
  203.         }
  204.         if (!strcmp(pasword, "class"))
  205.         {
  206.                 printf("\n\naccess\n");
  207.                 mainmenu();
  208.         }
  209.         else
  210.         {
  211.                 printf("\n\nno access\n");
  212.         }
  213. }
复制代码
本帖最后由 Chaisoo333 于 22-1-2014 04:10 PM 编辑

回复

使用道具 举报

发表于 22-1-2014 04:53 PM | 显示全部楼层
穿上画着标靶的衣服然后向前辈们大喊:“朝我开炮吧!!” 是最好的学习方式。福建话有句话说:“歹势吃自己”,不经历风雨,怎么见彩虹?
  1. struct badminton_none{
  2.         char *bad1, *bad2, *bad3, *bad4, *bad5, *bad6, *bad7, *bad8, *bad9, *bad10;
  3.         char *bad11, *bad12, *bad13, *bad14, *bad15, *bad16, *bad17, *bad18, *bad19, *bad20;
  4. }q1 = { "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O" };
复制代码
如果是我的话,我会用Array:

  1. char q1[20];
  2. /// initialize
  3. for(int i=0; i<20; i++)
  4. {
  5.     q1[i] = 'O';
  6. }
复制代码
你的做法有以下的危险:
1)依照你的做法,如果你Assign去q1.bad#的data有超过一个字母,那会造成Memory Corruption,你的数据会写入不属于它的空间里。
2)消耗多余的记忆体。Console program基本上是DOS program,依然受640k记忆体的限制。
3)也就是你面对的问题,太多Variable要记住。

根据我从你的源代码例分析,你只是要在bad里面存入O或者X,所以使用一个20元素(elements)的Char Array才是正确的。

然后再分析你的Table里的重复的元素,再根据这个分析你的Table Printing的方法,你应该将你的Table分成几个元素:

  1. void print_line(int time)
  2. {
  3.     switch(time)
  4.     {
  5.          /// non-peak
  6.         case 1: printf("|---------------------------------------------------------------|\n");
  7.                 break;
  8.         /// peak
  9.         case 2: printf("|------------------------------------------|\n");
  10.                 break;
  11.     }
  12. }

  13. void print_header(int time)
  14. {
  15.     switch(time)
  16.     {
  17.         /// peak
  18.         case 1: printf("|    court  | 7am-9am | 9am-11am | 11am-1pm | 1pm-3pm | 3pm-5pm |\n");
  19.                 break;
  20.         /// non peak
  21.         case 2: printf("|    court  | 5pm-7pm | 7pm-9pm | 9pm-10pm |\n");
  22.                 break;
  23.     }
  24. }

  25. void print_content(char *court, int time, int start_index)
  26. {
  27.     switch(time)
  28.     {
  29.         /// common element
  30.         printf("|      %s    |", court);
  31.         ///peak
  32.         case 1:
  33.                 for(int i=0; i<5; i++)
  34.                 {
  35.                     printf("    %d. %s  ", start_index + i, q1[start_index + i - 1];
  36.                 }
  37.                 printf("|\n");
  38.                 break;
  39.         /// non peak 这部分有点不确定,视乎你自己如何Handle non-peak的情形。
  40.         case 2:
  41.                 for(int i=0; i<3; i++)
  42.                 {
  43.                     printf("    %d. %s  ", start_index + i, q1[start_index + i - 1];
  44.                 }
  45.                 break;
  46.          
  47.     }
  48. }
复制代码
当你要print table时:

  1. void print_badminton_table(int time)
  2. {
  3.     print_line(time);
  4.     print_header(time);
  5.     print_line(time);
  6.     print_content("A", time, 1);
  7.     print_line(time);
  8.     print_content("B", time, 6);
  9.     print_line(time);
  10.     print_content("C", time, 11);
  11.     print_line(time);
  12.     print_content("D", time, 16);
  13.     print_line(time);
  14. }
复制代码
你甚至可以把Game Type也套进上面的Functions里面,然每个Functions都可以Handle每一种球类的Peak time和non-peak time,又或者你可以“勤劳”一点,把以上的Functions复制成不同的球类的版本。。。 本帖最后由 geekman 于 22-1-2014 05:01 PM 编辑

评分

参与人数 1人气 +5 收起 理由
Chaisoo333 + 5 给我&amp;quot;消化”一下

查看全部评分

回复

使用道具 举报

Follow Us
发表于 22-1-2014 06:31 PM | 显示全部楼层
刚刚才发现,我把Peak和Non-peak给弄反了,你自己看着办吧,我懒得改了。
回复

使用道具 举报

 楼主| 发表于 22-1-2014 07:29 PM | 显示全部楼层
我用了你的方法applied下去
可是run到后面就不可以了
好像是printcontent()有问题,可是我找不出,请你帮我看下
  1. #include <conio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. #include <stdio.h>

  6. char pasword[10], usrname[10], ch,day[10];
  7. int i, menu1, badminton, qq;
  8. char bad[20];


  9. void print_line(int time)
  10. {
  11.         switch (time)
  12.         {
  13.                
  14.         case 1: {
  15.                                 printf("|---------------------------------------------------------------|\n");
  16.         }break;
  17.                
  18.         case 2: {
  19.                                 printf("|------------------------------------------|\n");
  20.         }break;
  21.         }
  22. }

  23. void print_header(int time)
  24. {
  25.         switch (time)
  26.         {
  27.        
  28.         case 1: {
  29.                                 printf("|    court  | 7am-9am | 9am-11am | 11am-1pm | 1pm-3pm | 3pm-5pm |\n");

  30.         }break;
  31.                
  32.         case 2: {
  33.                                 printf("|    court  | 5pm-7pm | 7pm-9pm | 9pm-10pm |\n");
  34.         }break;
  35.         }
  36. }

  37. void print_content(char *court, int time, int start_index){
  38.         switch (time){

  39.                 printf("|      %s    |", court);

  40.         case 1:{
  41.                            for (int i = 0; i < 5; i++)
  42.                            {
  43.                                    printf("    %d. %s  ", start_index + i, bad[start_index + i - 1]);
  44.                            }
  45.                            printf("|\n");
  46.         }break;

  47.         case 2:{
  48.                 for (int i = 0; i < 3; i++)
  49.                 {
  50.                         printf("    %d. %s  ", start_index + i, bad[start_index + i - 1]);
  51.                 }
  52.         }break;

  53.         }
  54. }

  55. void print_badminton_table(int time)
  56. {
  57.         print_line(time);
  58.         print_header(time);
  59.         print_line(time);
  60.         print_content("A", time, 1);
  61.         print_line(time);
  62.         print_content("B", time, 6);
  63.         print_line(time);
  64.         print_content("C", time, 11);
  65.         print_line(time);
  66.         print_content("D", time, 16);
  67.         print_line(time);
  68. }

  69. void chooseday(){
  70.         printf("Which day?");
  71.         scanf("%s", &day);
  72.         while (!strcmp(day, "monday"))
  73.         {
  74.                 printf("Monday is off day !\nPlease choose another day!");
  75.                 scanf("%s", &day);
  76.         }

  77.         system("cls");

  78.         {
  79.                 printf("You have choose %s\nPlease pick a time\n", day);
  80.                 printf("1.None Peak time\n");
  81.                 printf("2.Peak time\n");
  82.                 scanf("%d", &badminton);

  83.         }
  84. }

  85. void checkcourt(){
  86.         printf("1.Badminton\n");
  87.         printf("2.Squash\n");
  88.         printf("3.Tennis\n");
  89.         printf("4.Futsal\n");
  90.         scanf("%d", &menu1);
  91.         system("cls");

  92.         if (menu1 == 1){
  93.                 chooseday();
  94.                 switch (badminton){
  95.                 case 1:{
  96.                                    print_badminton_table(badminton);
  97.                                    printf("Please insert the court");
  98.                                    scanf("%d", &qq);
  99.                                    system("cls");

  100.                                    printf("You successsfully have booked a court.\n");
  101.                                    bad[qq] = "X";
  102.                                    print_badminton_table(badminton);
  103.                                    mainmenu();





  104.                 case 2:{
  105.                                    printf("|------------------------------------------|\n");
  106.                                    printf("|    court  | 5pm-7pm | 7pm-9pm | 9pm-10pm |\n");
  107.                                    printf("|------------------------------------------|\n");
  108.                                    printf("|      A    |                              |\n");
  109.                                    printf("|------------------------------------------|\n");
  110.                                    printf("|      B    |                              |\n");
  111.                                    printf("|------------------------------------------|\n");
  112.                                    printf("|      C    |                              |\n");
  113.                                    printf("|------------------------------------------|\n");
  114.                                    printf("|      D    |                              |\n");
  115.                                    printf("|------------------------------------------|\n");

  116.                 }break;


  117.                 }
  118.                 }
  119.         }
  120.         else if (menu1 == 2)
  121.         {
  122.                 chooseday();
  123.                 switch (badminton){

  124.                 case 1:{
  125.                                    printf("|---------------------------------------------------------------|\n");
  126.                                    printf("|    court  |  10am-12am  |  12pm-2pm  |  2pm-4pm  |  4pm-5pm   |\n");
  127.                                    printf("|---------------------------------------------------------------|\n");
  128.                                    printf("|      A    |                                                   |\n");
  129.                                    printf("|---------------------------------------------------------------|\n");
  130.                                    printf("|      B    |                                                   |\n");
  131.                                    printf("|---------------------------------------------------------------|\n");
  132.                 }
  133.                 case 2:{
  134.                                    printf("|---------------------------------------------------------------|\n");
  135.                                    printf("|    court  |     5pm-7pm    |     7pm-9pm    |     9pm-10pm    |\n");
  136.                                    printf("|---------------------------------------------------------------|\n");
  137.                                    printf("|      A    |                                                   |\n");
  138.                                    printf("|---------------------------------------------------------------|\n");
  139.                                    printf("|      B    |                                                   |\n");
  140.                                    printf("|---------------------------------------------------------------|\n");
  141.                 }
  142.                 }

  143.         }
  144.         else if (menu1 == 3)
  145.         {
  146.                 chooseday();
  147.                 switch (badminton){

  148.                 case 1:{
  149.                                    printf("|---------------------------------------------------------------------------|\n");
  150.                                    printf("|    court  |  8am-10am  |  10am-12pm  |  12pm-2pm  |  2pm-4pm  |  4pm-5pm  |\n");
  151.                                    printf("|---------------------------------------------------------------------------|\n");
  152.                                    printf("|      A    |                                                               |\n");
  153.                                    printf("|---------------------------------------------------------------------------|\n");
  154.                                    printf("|      B    |                                                               |\n");
  155.                                    printf("|---------------------------------------------------------------------------|\n");
  156.                                    printf("|      c    |                                                               |\n");
  157.                                    printf("|---------------------------------------------------------------------------|\n");
  158.                 }
  159.                 case 2:{
  160.                                    printf("|---------------------------------------------------------------|\n");
  161.                                    printf("|    court  |     5pm-7pm    |     7pm-9pm    |     9pm-10pm    |\n");
  162.                                    printf("|---------------------------------------------------------------|\n");
  163.                                    printf("|      A    |                                                   |\n");
  164.                                    printf("|---------------------------------------------------------------|\n");
  165.                                    printf("|      B    |                                                   |\n");
  166.                                    printf("|---------------------------------------------------------------|\n");
  167.                                    printf("|      c    |                                                   |\n");
  168.                                    printf("|---------------------------------------------------------------|\n");
  169.                 }
  170.                 }
  171.         }
  172.         else if (menu1 == 4){

  173.                 chooseday();
  174.                 switch (badminton){

  175.                 case 1:{
  176.                                    printf("|---------------------------------------------------------------------------|\n");
  177.                                    printf("|    court  |  7am-9am  |  9am-11am  |  11am-1pm  |  1pm-3pm  |  3pm-5pm    |\n");
  178.                                    printf("|---------------------------------------------------------------------------|\n");
  179.                                    printf("|      A    |                                                               |\n");
  180.                                    printf("|---------------------------------------------------------------------------|\n");
  181.                                    printf("|      B    |                                                               |\n");
  182.                                    printf("|---------------------------------------------------------------------------|\n");
  183.                                    printf("|      c    |                                                               |\n");
  184.                                    printf("|---------------------------------------------------------------------------|\n");
  185.                 }
  186.                 case 2:{
  187.                                    printf("|---------------------------------------------------------------|\n");
  188.                                    printf("|    court  |     5pm-7pm    |     7pm-9pm    |     9pm-10pm    |\n");
  189.                                    printf("|---------------------------------------------------------------|\n");
  190.                                    printf("|      A    |                                                   |\n");
  191.                                    printf("|---------------------------------------------------------------|\n");
  192.                                    printf("|      B    |                                                   |\n");
  193.                                    printf("|---------------------------------------------------------------|\n");
  194.                                    printf("|      c    |                                                   |\n");
  195.                                    printf("|---------------------------------------------------------------|\n");
  196.                 }
  197.                 }
  198.         }
  199.         else{}
  200. }

  201. int mainmenu(){
  202.         system("cls");
  203.         printf("1.Check court availability\n");
  204.         printf("2.Booking\n");
  205.         printf("3.Generate bill\n");
  206.         printf("4.Exit\n");
  207.         scanf("%d", &menu1);
  208.         system("cls");
  209.         switch (menu1){
  210.         case 1:{
  211.                            checkcourt();
  212.         }

  213.         }
  214. }

  215. int main(){
  216.         for (int i = 0; i < 20; i++){
  217.                 bad[i] = "O";
  218.         }
  219.         printf("Enter User name: ");
  220.         gets(usrname);
  221.         printf("Enter the password : ");

  222.         for (i = 0; i < 10; i++)
  223.         {
  224.                 ch = getch();

  225.                 if (isprint(ch))
  226.                 {
  227.                         pasword[i] = ch;
  228.                         ch = '*';
  229.                         printf("%c", ch);
  230.                 }
  231.                 else
  232.                 {
  233.                         pasword[i] = '\0';
  234.                         break;
  235.                 }
  236.         }
  237.         if (!strcmp(pasword, "class"))
  238.         {
  239.                 printf("\n\naccess\n");
  240.                 mainmenu();
  241.         }
  242.         else
  243.         {
  244.                 printf("\n\nno access\n");
  245.         }
  246. }
复制代码
回复

使用道具 举报


ADVERTISEMENT

 楼主| 发表于 22-1-2014 07:36 PM | 显示全部楼层
我找出来了,我改declare成
char *bad[20];
but 为什么要用*勒?
酱不是变成pointer聊??
回复

使用道具 举报

发表于 22-1-2014 11:06 PM | 显示全部楼层
String 本身就是 access by pointer 的。char *bad[20]就是20个string (pointers)。char bad[20]是20个char。

评分

参与人数 1人气 +5 收起 理由
Chaisoo333 + 5 原来

查看全部评分

回复

使用道具 举报

 楼主| 发表于 22-1-2014 11:34 PM | 显示全部楼层
geekman 发表于 22-1-2014 11:06 PM
String 本身就是 access by pointer 的。char *bad[20]就是20个string (pointers)。char bad[20]是20个ch ...

Geekman ,你终于上来了
我又有一个problem了,我要怎样把我的table分成7天?7天都有自己的table
这个是目前我modify过的code。。。。
是不是用enum?上网找的可是还是不懂怎样用
难道要写多7个table
  1. #include <conio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. #include <stdio.h>

  6. char pasword[10], usrname[10], ch, day[10];
  7. int i, menu1, picktime, qq, select;

  8. struct badminton{
  9.         char *bad[20];
  10. }p, np;
  11. struct squash{
  12.         char *squ[20];
  13. }a1, a2;
  14. struct tennis{
  15.         char *ten[20];
  16. }b1, b2;
  17. struct futsal{
  18.         char *fut[20];
  19. }c1, c2;
  20. enum day{tuesday,wednesday,thursday,friday,saturday,sunday};
  21. void print_line(int time)
  22. {
  23.         switch (time)
  24.         {

  25.         case 1: {
  26.                                 printf("|-------------------------------------------------------------------|\n");
  27.         }break;

  28.         case 2: {
  29.                                 printf("|---------------------------------------------|\n");
  30.         }break;
  31.         }
  32. }

  33. void print_header(int time)
  34. {
  35.         switch (time)
  36.         {

  37.         case 1: {
  38.                                 printf("|   court  |  7am-9am |  9am-11am |  11am-1pm |  1pm-3pm |  3pm-5pm |\n");

  39.         }break;

  40.         case 2: {
  41.                                 printf("|    court  | 5pm-7pm  | 7pm-9pm  | 9pm-10pm  |\n");
  42.         }break;
  43.         }
  44. }
  45. void print_header2(int time)
  46. {
  47.         switch (time)
  48.         {

  49.         case 1: {
  50.                                 printf("|    court  |  10am-12am  |  12pm-2pm  |  2pm-4pm  |  4pm-5pm    |\n");

  51.         }break;

  52.         case 2: {
  53.                                 printf("|    court  | 5pm-7pm  | 7pm-9pm  | 9pm-10pm  |\n");
  54.         }break;
  55.         }
  56. }
  57. void print_header3(int time)
  58. {
  59.         switch (time)
  60.         {

  61.         case 1: {
  62.                                 printf("|   court  |   8am-10am  |  10am-12pm  |  12pm-2pm  |  2pm-4pm  |  4pm-5pm  |\n");

  63.         }break;

  64.         case 2: {
  65.                                 printf("|    court  | 5pm-7pm  | 7pm-9pm  | 9pm-10pm  |\n");
  66.         }break;
  67.         }
  68. }
  69. void print_header4(int time)
  70. {
  71.         switch (time)
  72.         {

  73.         case 1: {
  74.                                 printf("|   court  |  7am-9am  |  9am-11am  |  11am-1pm  |  1pm-3pm  |  3pm-5pm |\n");

  75.         }break;

  76.         case 2: {
  77.                                 printf("|    court  | 5pm-7pm  | 7pm-9pm  | 9pm-10pm  |\n");
  78.         }break;
  79.         }
  80. }

  81. void print_content(char *court, int time, int start_index){
  82.         char *space;
  83.         printf("|      %s    |", court);
  84.         switch (time){
  85.         case 1:{
  86.                            for (int i = 0; i < 5; i++)
  87.                            {
  88.                                    if (start_index + i < 10)
  89.                                    {
  90.                                            space = " ";
  91.                                            printf("    %d. %s%s  ", start_index + i, np.bad[start_index + i - 1], space);
  92.                                    }

  93.                                    else
  94.                                    {
  95.                                            printf("    %d. %s  ", start_index + i, np.bad[start_index + i - 1]);
  96.                                    }

  97.                            }
  98.                            printf("|\n");
  99.         }break;

  100.         case 2:{
  101.                            for (int i = 0; i < 3; i++)
  102.                            {
  103.                                    if (start_index + i < 10)
  104.                                    {
  105.                                            space = " ";
  106.                                            printf("    %d. %s%s  ", start_index + i, p.bad[start_index + i - 1], space);
  107.                                    }

  108.                                    else
  109.                                    {
  110.                                            printf("    %d. %s  ", start_index + i, p.bad[start_index + i - 1]);
  111.                                    }
  112.                            }
  113.                            printf("|\n");
  114.         }break;

  115.         }
  116. }
  117. void print_content2(char *court, int time, int start_index){
  118.         char *space;
  119.         printf("|      %s    |", court);
  120.         switch (time){
  121.         case 1:{
  122.                            for (int i = 0; i < 4; i++)
  123.                            {
  124.                                    if (start_index + i < 10)
  125.                                    {
  126.                                            space = " ";
  127.                                            printf("    %d. %s%s  ", start_index + i, a1.squ[start_index + i - 1], space);
  128.                                    }

  129.                                    else
  130.                                    {
  131.                                            printf("    %d. %s  ", start_index + i, a1.squ[start_index + i - 1]);
  132.                                    }

  133.                            }
  134.                            printf("|\n");
  135.         }break;

  136.         case 2:{
  137.                            for (int i = 0; i < 3; i++)
  138.                            {
  139.                                    if (start_index + i < 10)
  140.                                    {
  141.                                            space = " ";
  142.                                            printf("    %d. %s%s  ", start_index + i, a2.squ[start_index + i - 1], space);
  143.                                    }

  144.                                    else
  145.                                    {
  146.                                            printf("    %d. %s  ", start_index + i, a2.squ[start_index + i - 1]);
  147.                                    }
  148.                            }
  149.                            printf("|\n");
  150.         }break;

  151.         }
  152. }
  153. void print_content3(char *court, int time, int start_index){
  154.         char *space;
  155.         printf("|      %s    |", court);
  156.         switch (time){
  157.         case 1:{
  158.                            for (int i = 0; i < 5; i++)
  159.                            {
  160.                                    if (start_index + i < 10)
  161.                                    {
  162.                                            space = " ";
  163.                                            printf("    %d. %s%s  ", start_index + i, b1.ten[start_index + i - 1], space);
  164.                                    }

  165.                                    else
  166.                                    {
  167.                                            printf("    %d. %s  ", start_index + i, b1.ten[start_index + i - 1]);
  168.                                    }

  169.                            }
  170.                            printf("|\n");
  171.         }break;

  172.         case 2:{
  173.                            for (int i = 0; i < 3; i++)
  174.                            {
  175.                                    if (start_index + i < 10)
  176.                                    {
  177.                                            space = " ";
  178.                                            printf("    %d. %s%s  ", start_index + i, b2.ten[start_index + i - 1], space);
  179.                                    }

  180.                                    else
  181.                                    {
  182.                                            printf("    %d. %s  ", start_index + i, b2.ten[start_index + i - 1]);
  183.                                    }
  184.                            }
  185.                            printf("|\n");
  186.         }break;

  187.         }
  188. }
  189. void print_content4(char *court, int time, int start_index){
  190.         char *space;
  191.         printf("|      %s    |", court);
  192.         switch (time){
  193.         case 1:{
  194.                            for (int i = 0; i < 5; i++)
  195.                            {
  196.                                    if (start_index + i < 10)
  197.                                    {
  198.                                            space = " ";
  199.                                            printf("    %d. %s%s  ", start_index + i, c1.fut[start_index + i - 1], space);
  200.                                    }

  201.                                    else
  202.                                    {
  203.                                            printf("    %d. %s  ", start_index + i, c1.fut[start_index + i - 1]);
  204.                                    }

  205.                            }
  206.                            printf("|\n");
  207.         }break;

  208.         case 2:{
  209.                            for (int i = 0; i < 3; i++)
  210.                            {
  211.                                    if (start_index + i < 10)
  212.                                    {
  213.                                            space = " ";
  214.                                            printf("    %d. %s%s  ", start_index + i, c2.fut[start_index + i - 1], space);
  215.                                    }

  216.                                    else
  217.                                    {
  218.                                            printf("    %d. %s  ", start_index + i, c2.fut[start_index + i - 1]);
  219.                                    }
  220.                            }
  221.                            printf("|\n");
  222.         }break;

  223.         }
  224. }

  225. void print_badminton_table(int time)
  226. {
  227.         switch (menu1){
  228.         case 1:{
  229.                            print_line(time);
  230.                            print_header(time);
  231.                            print_line(time);
  232.                            print_content("A", time, 1);
  233.                            print_line(time);
  234.                            print_content("B", time, 6);
  235.                            print_line(time);
  236.                            print_content("C", time, 11);
  237.                            print_line(time);
  238.                            print_content("D", time, 16);
  239.                            print_line(time);
  240.         }break;
  241.         case 2:{
  242.                            print_line(time);
  243.                            print_header2(time);
  244.                            print_line(time);
  245.                            print_content2("A", time, 1);
  246.                            print_line(time);
  247.                            print_content2("B", time, 4);
  248.                            print_line(time);
  249.         }break;
  250.         case 3:{
  251.                            print_line(time);
  252.                            print_header3(time);
  253.                            print_line(time);
  254.                            print_content3("A", time, 1);
  255.                            print_line(time);
  256.                            print_content3("B", time, 6);
  257.                            print_line(time);
  258.                            print_content3("C", time, 11);
  259.                            print_line(time);
  260.         }break;
  261.         case 4:{
  262.                            print_line(time);
  263.                            print_header(time);
  264.                            print_line(time);
  265.                            print_content4("A", time, 1);
  266.                            print_line(time);
  267.                            print_content4("B", time, 6);
  268.                            print_line(time);
  269.                            print_content4("C", time, 11);
  270.                            print_line(time);
  271.         }break;
  272.         }
  273. }

  274. void chooseday(){
  275.         printf("Which day?");
  276.         scanf("%s", &day);
  277.         while (!strcmp(day, "monday"))
  278.         {
  279.                 printf("Monday is off day !\nPlease choose another day!");
  280.                 scanf("%s", &day);
  281.         }

  282.         system("cls");

  283.         {
  284.                 printf("You have choose %s\nPlease pick a time\n", day);
  285.                 printf("1.None Peak time\n");
  286.                 printf("2.Peak time\n");
  287.                 scanf("%d", &picktime);

  288.         }
  289. }

  290. void checkcourt(){
  291.         printf("1.Badminton\n");
  292.         printf("2.Squash\n");
  293.         printf("3.Tennis\n");
  294.         printf("4.Futsal\n");
  295.         scanf("%d", &menu1);
  296.         system("cls");

  297.         if (menu1 == 1){
  298.                 chooseday();
  299.                 print_badminton_table(picktime);
  300.                 printf("Please insert the court");
  301.                 scanf("%d", &qq);
  302.                 system("cls");
  303.                 printf("You successsfully have booked a court.\n");
  304.                 switch (picktime){


  305.                 case 1:{
  306.                                    np.bad[qq - 1] = "X";
  307.                                    print_badminton_table(picktime);
  308.                                    getch();
  309.                                    mainmenu();
  310.                 }break;
  311.                 case 2:{
  312.                                    p.bad[qq - 1] = "X";
  313.                                    print_badminton_table(picktime);
  314.                                    mainmenu();
  315.                 }break;

  316.                 }
  317.         }
  318.         else if (menu1 == 2){
  319.                 chooseday();
  320.                 print_badminton_table(picktime);
  321.                 printf("Please insert the court");
  322.                 scanf("%d", &qq);
  323.                 system("cls");
  324.                 printf("You successsfully have booked a court.\n");
  325.                 switch (picktime){

  326.                 case 1:{
  327.                                    a1.squ[qq - 1] = "X";
  328.                                    print_badminton_table(picktime);
  329.                                    mainmenu();
  330.                 }
  331.                 case 2:{
  332.                                    a2.squ[qq - 1] = "X";
  333.                                    print_badminton_table(picktime);
  334.                                    mainmenu();
  335.                 }
  336.                 }

  337.         }
  338.         else if (menu1 == 3){
  339.                 chooseday();
  340.                 print_badminton_table(picktime);
  341.                 printf("Please insert the court");
  342.                 scanf("%d", &qq);
  343.                 system("cls");
  344.                 printf("You successsfully have booked a court.\n");
  345.                 switch (picktime){

  346.                 case 1:{
  347.                                    b1.ten[qq - 1] = "X";
  348.                                    print_badminton_table(picktime);
  349.                                    mainmenu();
  350.                 }
  351.                 case 2:{
  352.                                    b2.ten[qq - 1] = "X";
  353.                                    print_badminton_table(picktime);
  354.                                    mainmenu();
  355.                 }
  356.                 }
  357.         }
  358.         else if (menu1 == 4){
  359.                 chooseday();
  360.                 print_badminton_table(picktime);
  361.                 printf("Please insert the court");
  362.                 scanf("%d", &qq);
  363.                 system("cls");
  364.                 printf("You successsfully have booked a court.\n");
  365.                 switch (picktime){

  366.                 case 1:{
  367.                                    c1.fut[qq - 1] = "X";
  368.                                    print_badminton_table(picktime);
  369.                                    mainmenu();
  370.                 }
  371.                 case 2:{
  372.                                    c2.fut[qq - 1] = "X";
  373.                                    print_badminton_table(picktime);
  374.                                    mainmenu();
  375.                 }
  376.                 }
  377.         }
  378.         else{}
  379. }

  380. int mainmenu(){

  381.         system("cls");
  382.         printf("1.Check court availability\n");
  383.         printf("2.Booking\n");
  384.         printf("3.Generate bill\n");
  385.         printf("4.Exit\n");
  386.         scanf("%d", &menu1);
  387.         system("cls");
  388.         switch (menu1){
  389.         case 1:{
  390.                            checkcourt();
  391.         }

  392.         }
  393. }

  394. int main(){

  395.         int a;
  396.         for (a = 0; a < 20; a++){
  397.                 np.bad[a] = "O";
  398.                 p.bad[a] = "O";
  399.                 a1.squ[a] = "O";
  400.                 a2.squ[a] = "O";
  401.                 b1.ten[a] = "O";
  402.                 b2.ten[a] = "O";
  403.                 c1.fut[a] = "O";
  404.                 c2.fut[a] = "O";
  405.         }

  406.         printf("Enter User name: ");
  407.         gets(usrname);
  408.         printf("Enter the password : ");

  409.         for (i = 0; i < 10; i++)
  410.         {
  411.                 ch = getch();

  412.                 if (isprint(ch))
  413.                 {
  414.                         pasword[i] = ch;
  415.                         ch = '*';
  416.                         printf("%c", ch);
  417.                 }
  418.                 else
  419.                 {
  420.                         pasword[i] = '\0';
  421.                         break;
  422.                 }
  423.         }
  424.         if (!strcmp(pasword, "class"))
  425.         {
  426.                 printf("\n\naccess\n");
  427.                 mainmenu();
  428.         }
  429.         else
  430.         {
  431.                 printf("\n\nno access\n");
  432.         }
  433. }
复制代码
回复

使用道具 举报

 楼主| 发表于 23-1-2014 12:12 AM | 显示全部楼层
这样子可以吗?
  1. enum nameday{ tuesday=0, wednesday, thursday, friday, saturday, sunday }dayy;
复制代码
  1. struct badminton{
  2.         char *bad[20];
  3. }p[10], np[10];
复制代码
  1. void print_content(char *court, int time, int start_index){
  2.         char *space;
  3.         printf("|      %s    |", court);
  4.         switch (time){
  5.         case 1:{
  6.                            for (int i = 0; i < 5; i++)
  7.                            {
  8.                                    if (start_index + i < 10)
  9.                                    {
  10.                                            space = " ";
  11.                                            printf("    %d. %s%s  ", start_index + i, np[dayy].bad[start_index + i - 1], space);
  12.                                    }

  13.                                    else
  14.                                    {
  15.                                            printf("    %d. %s  ", start_index + i, np[dayy].bad[start_index + i - 1]);
  16.                                    }

  17.                            }
  18.                            printf("|\n");
  19.         }break;

  20.         case 2:{
  21.                            for (int i = 0; i < 3; i++)
  22.                            {
  23.                                    if (start_index + i < 10)
  24.                                    {
  25.                                            space = " ";
  26.                                            printf("    %d. %s%s  ", start_index + i, p[dayy].bad[start_index + i - 1], space); //改成p[dayy]
  27.                                    }

  28.                                    else
  29.                                    {
  30.                                            printf("    %d. %s  ", start_index + i, p[dayy].bad[start_index + i - 1]);
  31.                                    }
  32.                            }
  33.                            printf("|\n");
  34.         }break;

  35.         }
  36. }
复制代码
因为我declare enum 里面放tuesday
所以tuesday是0
我这边要怎样写才可以让user写tuesday时把 0这个号码存进dayy里面?
  1. void chooseday(){
  2.         printf("Which day?");
  3.         scanf("%s", &day);
  4.         if (!strcmp(day, "monday"))
  5.         {
  6.                 printf("Monday is off day !\nPlease choose another day!");
  7.                 scanf("%s", &day);
  8.         }
  9.         dayy = day;
  10.         system("cls");

  11.         {
  12.                 printf("You have choose %s\nPlease pick a time\n", day);
  13.                 printf("1.None Peak time\n");
  14.                 printf("2.Peak time\n");
  15.                 scanf("%d", &picktime);

  16.         }
  17. }
复制代码
回复

使用道具 举报

 楼主| 发表于 23-1-2014 12:21 AM | 显示全部楼层
ok其实我改成用if -else 了,弄到了,不过上面那个其实是可以的吗?
回复

使用道具 举报

发表于 23-1-2014 12:56 PM | 显示全部楼层
呃,每一天的Table都不一样的吗?

关于Day的问题,其实一直以来(在DOS时代)当有Multiple Choice的时候,大家都习惯用Int来作为选项:

我会把Menu设定成这样
  1. Please choose a day:
  2. 1. Tuesday
  3. 2. Wednesday
  4. 3. Thursday
  5. 4. Friday
  6. 5. Saturday
  7. 6. Sunday
  8. (Note: Monday is off day)
复制代码
然后让用户输入相关的数目字作为选择。当然,如果你要做GUI那就是另一回事了。

这样做的好处是:
1)数字不必担心大小写的问题,也不必担心用户输入错误的文字,例如用户懒惰,所以输入 Sun 来选择 Sunday。
2)在检测Input validity时很简单, 只要 if(user_input >= 1 && user_input <= 6) { // User Input is Valid }
3)得到的用户选择可以直接用在switch()里面。
4)直接就免除了用户输入Monday的可能性,少了一个烦恼。

至于Table,你并不用编写7个那么多,你只要分析和归纳你的Table的格式,找出共同的地方,就可以编写出一个可以用在所有选项的Table Printing Function。
1)你的Table最大会有5个时间段,4个Court,自然的,你最好的选择就是设计一个最大能够Print 5 column x 4 row 的Table。
2)Badminton non-peak time 有 5 个 Column 4 个 Court,起始时间是 7am,每节两个小时
3)Squash non-peak time 有 4 个 Column,2 个 Court,起始时间是 10am,每节两个小时,最后一节1个小时
4)Tennis non-peak time 有 5 个 Column,3 个 Court,起始时间是 8am,每节两个小时,最后一节1小时
5)Footsal non-peak time 有 5 个 Column,3 个 Court,起始时间是 7am,每节两个小时
6)所有项目的 peak time 都只有 3 个 Column,相同时间段,Court 数量和 non-peak time 相同。时间都是 5pm 开始,每节两个小时,最后一节1小时

以下是整个程式的流程:
  1. /// program flow pseudo code
  2. get_day();
  3. int day = user day choice
  4. get_game();
  5. int game = user game choice
  6. get_time();
  7. int time = user time choice
  8. get_booking(day, game, time);
  9. int book_time = user booking choice
复制代码
  1. int day = 0, game = 0, time = 0;
  2. bool done;

  3. void get_day(void)
  4. {
  5.     done = false;
  6.     do
  7.     {
  8.         clrscr();
  9.         printf("Please select a day:\n");
  10.         printf("1. Tuesday\n");
  11.         printf("2. Wednesday\n");
  12.         printf("3. Thursday\n");
  13.         printf("4. Friday\n");
  14.         printf("5. Saturday\n");
  15.         printf("6. Sunday\n");
  16.         printf("(Note: Monday is off day\n");
  17.         scanf("%d", &day);
  18.         if(day >= 1 && day <= 6)
  19.         {
  20.             done = true;
  21.         }
  22.         else
  23.         {
  24.             printf("Invalid choice. Please choose 1 to 6.");
  25.             done = false;
  26.         }
  27.     }while(!done);
  28. }

  29. void get_game(void)
  30. {
  31.     done = false;
  32.     do
  33.     {
  34.         /// 大致上和上面差不多,让用户选择游戏/球类
  35.     }while(!done);
  36. }

  37. void get_time(void)
  38. {
  39.     /// 以上雷同,选择时间段
  40. }

  41. /// 现在你已经有了day, game 和 time,接下来就是要显示booking table
  42. void get_booking(day, game, time)
  43. {
  44.     print_table(day, game, time);
  45.     /// 读取用户选项,以上雷同,每个时间段都有一个数字了嘛,
  46.     /// 自然就是读取该数字来代表 时间段/球场 啰。。。
  47. }
  48. /// 到此,一次booking完成。是否要loop,自己看着办吧
复制代码
回复

使用道具 举报

发表于 23-1-2014 04:22 PM | 显示全部楼层
这个是我自己做的,应该已经Cover你大多数的需要了:
  1. //---------------------------------------------------------------------------
  2. #include <stdio.h>
  3. #include <tchar.h>
  4. #include <conio.h>
  5. #include <string.h>
  6. #pragma hdrstop
  7. //---------------------------------------------------------------------------
  8. #pragma argsused
  9. /// global
  10. /// 6 days (Monday not included), 4 Games, 2 times, 4 Courts, 5 Columns
  11. char booking[6][4][2][4][5];
  12. int quit = 0;
  13. int col, row;

  14. int book_court(int day, int game, int time);
  15. int get_day(void);
  16. int get_game(void);
  17. int get_time(void);
  18. void print_table(int day, int game, int time);
  19. //---------------------------------------------------------------------------
  20. int _tmain(int argc, _TCHAR* argv[])
  21. {
  22.         int i, j, k, l, m, day = 1, game = 1, time = 1, result, booked = 0;
  23.         /// init booking table
  24.         for(i=0; i<6; i++)
  25.         {
  26.                 for(j=0; j<4; j++)
  27.                 {
  28.                         for(k=0; k<2; k++)
  29.                         {
  30.                                 for(l=0; l<4; l++)
  31.                                 {
  32.                                         for(m=0; m<5; m++)
  33.                                         {
  34.                                                 booking[i][j][k][l][m] = 'O';
  35.                                         }
  36.                                 }
  37.                         }
  38.                 }
  39.         }

  40.         while (!quit)
  41.         {
  42.                 day  = get_day();
  43.                 if(day == 0)
  44.                         break;
  45.                 game = get_game();
  46.                 if(game == 0)
  47.                         break;
  48.                 time = get_time();
  49.                 if(time == 0)
  50.                         break;

  51.                 booked = 0;
  52.                 while(!booked)
  53.                 {
  54.                         result = book_court(day, game, time);
  55.                         if(result > 0)
  56.                         {
  57.                                 /// display updated booking info
  58.                                 print_table(day, game, time);
  59.                                 printf("You have successfully booked a court. \nPress any key to continue.");
  60.                                 getch();
  61.                                 booked = 0;
  62.                         }
  63.                         else if(result == -1)
  64.                         {   /// block is booked, retry
  65.                                 booked = 0;
  66.                         }
  67.                         else
  68.                         {   /// quit
  69.                                 booked = 1;
  70.                                 break;
  71.                         }
  72.                 }
  73.         }
  74.         return 0;
  75. }
  76. //---------------------------------------------------------------------------
  77. int get_day(void)
  78. {
  79.         int day;
  80.         int done = 0;

  81.         do
  82.         {
  83.                 clrscr();
  84.                 printf("Please select a day:\n");
  85.                 printf("1 Tuesday\n");
  86.                 printf("2 Wednesday\n");
  87.                 printf("3 Thursday\n");
  88.                 printf("4 Friday\n");
  89.                 printf("5 Saturday\n");
  90.                 printf("6 Sunday\n");
  91.                 printf("0 to quit\n");
  92.                 printf("(Note: Monday is off day\n\n");
  93.                 scanf("%d", &day);
  94.                 if(day >= 1 && day <= 6)
  95.                 {
  96.                         done = 1;
  97.                 }
  98.                 else if(day == 0)
  99.                 {
  100.                         quit = 1;
  101.                         done = 1;
  102.         }
  103.                 else
  104.                 {
  105.                         done = 0;
  106.                 }
  107.         }while(!done);
  108.         return day;
  109. }
  110. //---------------------------------------------------------------------------
  111. int get_game(void)
  112. {
  113.         int game;
  114.         int done = 0;

  115.         do
  116.         {
  117.                 clrscr();
  118.                 printf("Please select a game:\n");
  119.                 printf("1 Badminton\n");
  120.                 printf("2 Squash\n");
  121.                 printf("3 Tennis\n");
  122.                 printf("4 Footsal\n");
  123.                 printf("0 to quit\n\n");
  124.                 scanf("%d", &game);
  125.                 if(game >= 1 && game <= 4)
  126.                 {
  127.                         done = 1;
  128.                 }
  129.                 else if(game == 0)
  130.                 {
  131.                         quit = 1;
  132.                         done = 1;
  133.                 }
  134.                 else
  135.                 {
  136.                         done = 0;
  137.                 }
  138.         }while(!done);
  139.         return game;
  140. }
  141. //---------------------------------------------------------------------------
  142. int get_time(void)
  143. {
  144.         int time;
  145.         int done = 0;

  146.         do
  147.         {
  148.                 clrscr();
  149.                 printf("Please select a time slot:\n");
  150.                 printf("1 Non Peak Time\n");
  151.                 printf("2 Peak Time\n");
  152.                 printf("0 to quit\n\n");
  153.                 scanf("%d", &time);
  154.                 if(time >= 1 && time <= 2)
  155.                 {
  156.                         done = 1;
  157.                 }
  158.                 else if(time == 0)
  159.                 {
  160.                         quit = 1;
  161.                         done = 1;
  162.         }
  163.                 else
  164.                 {
  165.                         done = 0;
  166.                 }
  167.         }while(!done);
  168.         return time;
  169. }
  170. //---------------------------------------------------------------------------
  171. int book_court(int day, int game, int time)
  172. {
  173.         int book, c, r;
  174.         print_table(day, game, time);

  175.         printf("Please type in the block number you want to book: \n");
  176.         printf("Key in 0 to go back to DAY selection.\n\n");
  177.         scanf("%d", &book);
  178.         if(book == 0)
  179.         {
  180.                 quit = 0;
  181.         }
  182.         else
  183.         {
  184.                 c = (book - 1) / col;
  185.                 r = (book - 1) % col;
  186.                 /// if already booked
  187.                 if(booking[day-1][game-1][time-1][c][r] == 'X')
  188.                 {
  189.                         printf("Sorry, that block already booked\n");
  190.                         getch();
  191.                         book = -1;
  192.                 }
  193.                 else
  194.                 {
  195.                         booking[day-1][game-1][time-1][c][r] = 'X';
  196.                 }
  197.         }
  198.         return book;
  199. }
  200. //---------------------------------------------------------------------------
  201. void print_table(int day, int game, int time)
  202. {
  203.         int head, length, i, j, k;
  204.         /// text mode 的宽度是 80 个字吧?记不清楚了,就当它是80吧。
  205.         char line[80];
  206.         /// 每个格子最宽(根据Header的字数)是 9 个字,+ 左右各一个 space, + 1 个最右边的 | = 12
  207.         char cell[13]; /// + NULL
  208.         char *days[6]   = {"Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
  209.         char *games[4]  = {"Badminton", "Squash", "Tennis", "Footsal"};
  210.         char *times[2]  = {"Non Peak Time", "Peak Time"};
  211.         char *courts[4] = {"|    A     |",
  212.                                            "|    B     |",
  213.                                            "|    C     |",
  214.                                            "|    D     |"};
  215.         char *hdrs[4]   = {"|  Court   |  7am-9am  |  9am-11am | 11am-1pm  |  1pm-3pm  |  3pm-5pm  |",
  216.                                            "|  Court   |  8am-10am | 10am-12pm | 12pm-2pm  |  2pm-4pm  |  4pm-5pm  |",
  217.                                            "|  Court   | 10am-12pm | 12pm-2pm  |  2pm-4pm  |  4pm-5pm  |",
  218.                                            "|  Court   |  5pm-7pm  |  7pm-9pm  |  9pm-10pm |"};
  219.         /// non peak
  220.         if(time == 1)
  221.         {
  222.                 switch(game)
  223.                 {
  224.                         /// badminton
  225.                         case 1: col = 5;
  226.                                         row = 4;
  227.                                         head = 0;
  228.                                         break;
  229.                         /// Squash
  230.                         case 2: col = 4;
  231.                                         row = 2;
  232.                                         head = 1;
  233.                                         break;
  234.                         /// Tennis
  235.                         case 3: col = 5;
  236.                                         row = 3;
  237.                                         head = 2;
  238.                                         break;
  239.                         case 4: col = 5;
  240.                                         row = 3;
  241.                                         head = 0;
  242.                                         break;
  243.                 }
  244.         }
  245.         else if(time == 2)
  246.         {
  247.                 /// peak time col is same for all games
  248.                 col = 3;
  249.                 head = 3;
  250.                 switch(game)
  251.                 {
  252.                         case 1: row = 4;
  253.                                         break;
  254.                         case 2: row = 2;
  255.                                         break;
  256.                         case 3:
  257.                         case 4: row = 3;
  258.                                         break;
  259.                 }
  260.         }
  261.         /// col# + 1 court column = (col + 1) * 12 chars
  262.         length = (col + 1) * 12;
  263.         memset(line, '-', length);
  264.         /// put the | at left and right, and the NULL terminator
  265.         line[0] = '|';
  266.         line[length-1] = '|';
  267.         line[length] = NULL;

  268.         /// print the table
  269.         clrscr();
  270.         printf("Day [%s] Game [%s] Time [%s]\n", days[day-1], games[game-1], times[time-1]);
  271.         printf("%s\n", line);
  272.         printf("%s\n", hdrs[head]);
  273.         printf("%s\n", line);
  274.         for(j=0; j<row; j++)
  275.         {
  276.                 printf("%s", courts[j]);
  277.                 for(k=0; k<col; k++)
  278.                 {
  279.                         printf("   %2d. %c   |", ((j * col) + k + 1), booking[day-1][game-1][time-1][j][k]);
  280.                 }
  281.                 printf("\n");
  282.                 printf("%s\n", line);
  283.         }
  284. }
  285. //---------------------------------------------------------------------------
复制代码
要注意得是我用的是 C++ Builder,要在Visual Studio跑的话,你得修改或去除掉某些Header。
回复

使用道具 举报

 楼主| 发表于 23-1-2014 05:31 PM | 显示全部楼层
Wa thank you 你帮我写酱多
我applied你的一点,因为我不想全部一样等下变成抄功课 Lol

帮我看下可以吗?void chooseday()我改成酱,run到选peaktime那边就error了。。。
  1. void chooseday(){
  2.         int done = 0;
  3.         do{
  4.                
  5.         printf("Please select a day\n");
  6.         printf("Note that Monday is off day !\n\n");
  7.         printf("1 Tuesday\n");
  8.         printf("2 Wednesday\n");
  9.         printf("3 Thursday\n");
  10.         printf("4 Friday\n");
  11.         printf("5 Saturday\n");
  12.         printf("6 Sunday\n");

  13.         scanf("%d", &day);       
  14.        
  15.                 if (day==1)
  16.                 {                       
  17.                         dayy = "0";
  18.                         done = 1;
  19.                 }
  20.                 else if (day ==2){
  21.                         dayy = "1";
  22.                         done = 1;
  23.                 }
  24.                 else if (day==3){
  25.                         dayy = "2";
  26.                         done = 1;
  27.                 }
  28.                 else if (day== 4){
  29.                         dayy = "3";
  30.                         done = 1;
  31.                 }
  32.                 else if (day==5){
  33.                         dayy = "4";
  34.                         done = 1;
  35.                 }
  36.                 else if (day==6){
  37.                         dayy = "5";
  38.                         done = 1;
  39.                 }
  40.                 else
  41.                 {
  42.                         done = 0;
  43.                 }
  44.         } while (done == 0);
  45.         system("cls");

  46.         {
  47.                 printf("You have choose %s\nPlease pick a time\n", day);
  48.                 printf("1.None Peak time\n");
  49.                 printf("2.Peak time\n");
  50.                 scanf("%d", &picktime);

  51.         }
  52. }
复制代码
回复

使用道具 举报


ADVERTISEMENT

发表于 23-1-2014 07:19 PM | 显示全部楼层
不知道出现的是什么Error,也不知道你怎样declare picktime,帮不了你。
回复

使用道具 举报

 楼主| 发表于 23-1-2014 11:08 PM | 显示全部楼层
geekman 发表于 23-1-2014 07:19 PM
不知道出现的是什么Error,也不知道你怎样declare picktime,帮不了你。

大致上已经弄好了,谢谢你

回复

使用道具 举报

 楼主| 发表于 16-3-2014 11:24 PM | 显示全部楼层
geekman 发表于 23-1-2014 07:19 PM
不知道出现的是什么Error,也不知道你怎样declare picktime,帮不了你。

老大,又有东西问你了
insertion 我highlight 起来那段我不明白勒。。。
while(temp!=NULL ) 有什么作用?
因为我以为temp本来就不可能会是null,因为有放if (list==NULL)了,如果list==null then就会走list=newnode;
那么temp应该就不可能会是null不对吗?
  1. #include <stdio.h>
  2. #include<stdlib.h>
  3. void insertion();
  4. void deletion();
  5. void createlist();
  6. void display();
  7. int option,element,placefound;
  8. void menu();

  9. struct node
  10. {
  11.         int data;
  12.         struct node *next;
  13. }*newnode,*list,*prev,*temp,*tmpdisplay;

  14. void main()
  15. {       
  16.         createlist();
  17.        
  18.         do
  19.         {
  20.                 menu();
  21.                 switch (option)
  22.                 {
  23.                 case 1: insertion();break;
  24.                 case 2: deletion();break;
  25.                 case 3: display();break;
  26.                 case 4: break;
  27.                 }
  28.         }while (option !=4);
  29. }

  30. void createlist()
  31. {
  32.         list=NULL;
  33. }


  34. void menu()
  35. {
  36.         printf(" linked list\n");
  37.         printf("1-- Insertion\n");
  38.         printf("2-- Deletion\n");
  39.         printf("3-- Display\n");
  40.         printf("select your option\n");
  41.         scanf("%d",&option);
  42. }

  43. void insertion()
  44. {
  45.         newnode=(struct node*) malloc (sizeof(struct node));
  46.        
  47.        
  48.         printf("enter the element:");
  49.         scanf("%d",&element);
  50.         newnode->data=element;
  51.         newnode->next=NULL;
  52.        

  53.         if (list==NULL)
  54.                 list=newnode;
  55.         else if(element<list->data)
  56.                 {
  57.                         newnode->next=list;
  58.                         list=newnode;
  59.                 }       
  60.         else
  61.                         {
  62. <font color="#ffff00" style="background-color: rgb(139, 0, 0);">                                temp=list;
  63.                                 while(temp!=NULL )
  64.                                 {
  65.                                         if(element>temp->data)
  66.                                         {
  67.                                                 prev=temp;
  68.                                                 temp=temp->next;
  69.                                         }
  70.                                         else       
  71.                                                 break;
  72.                                 }
  73.                                 newnode->next=prev->next;
  74.                                 prev->next=newnode;
  75.                         }}
  76. </font>
  77. void deletion()
  78. {
  79.         printf("element to delete?");
  80.         scanf("%d", &element);

  81.         if (list==NULL)
  82.         printf("list is empty");
  83.                 else if (element==list->data)
  84.                        
  85.                         list=list->next;
  86.                 else
  87.                 {
  88.                         temp=list;
  89.                         while(temp->data!=element)

  90.                         {
  91.                                 prev=temp;
  92.                                 temp=temp->next;
  93.                         }
  94.                         prev->next=prev->next->next;
  95.                 }
  96. }

  97. void display()
  98. {

  99.         if (list==NULL)
  100.                 printf("\n\nList is empty");
  101.         else
  102.         {
  103.                 tmpdisplay=list;
  104.                 while(tmpdisplay!=NULL)
  105.                 {
  106.                         printf("%d\n",tmpdisplay->data);
  107.                         tmpdisplay=tmpdisplay->next;
  108.                 }
  109.         }
  110. }

复制代码
回复

使用道具 举报

发表于 17-3-2014 07:53 AM | 显示全部楼层
Chaisoo333 发表于 16-3-2014 11:24 PM
老大,又有东西问你了
insertion 我highlight 起来那段我不明白勒。。。
while(temp!=NULL )[/backcolo ...

因为这个:
  1. temp=temp->next;
复制代码
那段代码的含义就是:walk through the list till it hit the end of the list. 当来到最后一个Node的时候,next是指向NULL的。

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 14-6-2024 10:30 PM , Processed in 0.077792 second(s), 29 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表