佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 839|回复: 11

拜托各位,帮帮我....(programing)

[复制链接]
发表于 26-6-2008 09:37 AM | 显示全部楼层 |阅读模式
  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <conio.h>
  6. #define SIZE 5


  7. void Pop();
  8. void Push();
  9. void View();
  10. void push(struct stack *,int, int);
  11. void Push_bin0(struct Bin0 *,int);
  12. void Push_bin1(struct Bin1 *,int);
  13. void Push_bin2(struct Bin2 *,int);
  14. void Push_bin3(struct Bin3 *,int);
  15. void Push_bin4(struct Bin4 *,int);
  16. void RemoveBin0(struct Bin0 *);
  17. void RemoveBin1(struct Bin1 *);
  18. void RemoveBin2(struct Bin2 *);
  19. void RemoveBin3(struct Bin3 *);
  20. void RemoveBin4(struct Bin4 *);





  21. time_t rawtime;
  22. struct tm * timeinfo;
  23. char date[20];


  24. static struct stack
  25. {
  26.     int top;
  27.     int items[SIZE];
  28.    

  29.     struct Bin0
  30.     {
  31.         int top;
  32.         int items[SIZE];
  33.     }Bin0;

  34.     struct Bin1
  35.     {
  36.         int top;
  37.         int items[SIZE];
  38.     }Bin1;

  39.     struct Bin2
  40.     {
  41.         int top;
  42.         int items[SIZE];
  43.     }Bin2;

  44.     struct Bin3
  45.     {
  46.         int top;
  47.         int items[SIZE];
  48.     }Bin3;

  49.     struct Bin4
  50.     {
  51.         int top;
  52.         int items[SIZE];
  53.     }Bin4;

  54. }staker;



  55. void main()

  56. {
  57.    
  58.     int choice;
  59.     staker.Bin0.top=-1;
  60.     staker.Bin1.top=-1;
  61.     staker.Bin2.top=-1;
  62.     staker.Bin3.top=-1;
  63.     staker.Bin4.top=-1;
  64.    
  65.     system ("cls");
  66.     time(&rawtime);
  67.     timeinfo= localtime(&rawtime);

  68.     strftime(date,20,"Now is %I:%M%p",timeinfo);
  69.     puts(date);
  70.         
  71.     printf (" \n\n\t\t\t ||======================||");
  72.     printf (" \n\t\t\t ||  ^Wellcome, my guest^||");
  73.     printf ("\n\t\t\t ||======================||");
  74.     printf ("\n\t\t\t ||    Stacke Operation  ||");
  75.     printf ("\n\t\t\t ||======================||");
  76.     printf ("\n\n\n\t\t||========================================||");
  77.     printf ("\n\t\t||                                        ||");
  78.     printf ("\n\t\t||        1. Insert Item (Push)           ||");
  79.     printf ("\n\t\t||        2. Remove Item (Pop)            ||");
  80.     printf ("\n\t\t||        3. View Item (view)             ||");
  81.     printf ("\n\t\t||        4. Exit                         ||");
  82.     printf ("\n\t\t||                                        ||");
  83.     printf ("\n\t\t||========================================||\n\n");
  84.     printf ("\n\t       Please key in a number that you want to process\n");
  85.     printf ("\t\t\t      Choice: ");
  86.     scanf ("%d", &choice);

  87.     switch (choice)
  88.     {

  89.         case 1:
  90.                 Push();
  91.                 break;

  92.         case 2:
  93.                 Pop();
  94.                 break;

  95.         case 3:
  96.                 View();
  97.                 break;

  98.         case 4:
  99.                 printf(" Thanks for using this system , please come again.");
  100.                 exit(0);
  101.                
  102.         default: printf (" Invalid value, please key in again \n");
  103.                  main();
  104.                  break;
  105.     }
  106. }


  107. void Push()
  108. {
  109.    
  110.     int bin_num;
  111.     int i;
  112.     int bin_id;
  113.    

  114.    
  115.    
  116.     system ("cls");
  117.     time(&rawtime);
  118.     timeinfo= localtime(&rawtime);

  119.     strftime(date,20,"Now is %I:%M%p",timeinfo);
  120.     puts(date);

  121.     for (i =0; i< 5 ; i ++)
  122.     {
  123.    
  124.         printf ("\nAttention: Total Input cannot more than 5 \n");
  125.         printf (" To end process, enter (-1) into bin_id\n");
  126.         printf ("Add a new Bin ID to the stack: \t");
  127.         scanf  ("%d", &bin_id);

  128.         if (bin_id == 5)
  129.         {
  130.             printf("\t Thank you!!\n\tPress Any Key Back To Main Menu!");
  131.             getch();
  132.             main();
  133.         }

  134.         if (bin_id <0 || bin_id >4)
  135.         {
  136.             printf("\n\tInvalid bin id.\n");
  137.             i--;
  138.         }

  139.             printf("\nAdd a new number for the bin:\t");
  140.             scanf("%d",&bin_num);
  141.             push(&staker,bin_num,bin_id);
  142.     }
  143.     printf("\nThe Stack is Full! \nPress Any key Back To Main Menu!");
  144.     getch();
  145.     main();
  146. }
  147.         


  148. void push(struct stack *pq, int x, int y) // int x = bin_num  int y = bin_id
  149.       
  150. {
  151.         if ( y == 0 )
  152.         {
  153.             Push_bin0(&staker.Bin0, x);
  154.         }

  155.             else
  156.                 if (y == 1)
  157.                 {
  158.                     Push_bin1(&staker.Bin1, x);
  159.                 }

  160.                     else
  161.                         if(y == 2 )
  162.                     {
  163.                         Push_bin2(&staker.Bin2, x);
  164.                     }
  165.                         else
  166.                             if (y == 3 )
  167.                             {
  168.                                 Push_bin3(&staker.Bin3, x);
  169.                             }
  170.                             else
  171.                                 if (y == 4)
  172.                                 {
  173.                                     Push_bin4(&staker.Bin4, x);
  174.                                 }
  175. }
  176.                     
  177.                            
  178. void Push_bin0 (struct Bin0 *pq, int x)

  179. {
  180.     if (pq->top==SIZE -1)
  181.         {   
  182.             (pq->items[pq->top--]);
  183.             
  184.         }
  185.     pq->items[++pq->top]=x;
  186. }

  187. void Push_bin1 (struct Bin1 *pq, int x)
  188. {
  189.     if (pq->top==SIZE -1)
  190.         {    (pq->items[pq->top--]);
  191.          
  192.         }
  193.     pq->items[++pq->top]=x;
  194. }

  195. void Push_bin2 (struct Bin2 *pq, int x)
  196. {
  197.     if (pq->top==SIZE -1)
  198.         {    (pq->items[pq->top--]);
  199.         
  200.         }
  201.     pq->items[++pq->top]=x;
  202. }

  203. void Push_bin3 (struct Bin3 *pq, int x)
  204. {
  205.     if (pq->top==SIZE -1)
  206.         {    (pq->items[pq->top--]);
  207.         
  208.         }
  209.     pq->items[++pq->top]=x;
  210. }            

  211. void Push_bin4 (struct Bin4 *pq, int x)
  212. {
  213.     if (pq->top==SIZE -1)
  214.         {    (pq->items[pq->top--]);
  215.         
  216.         }
  217.     pq->items[++pq->top]=x;
  218. }               


  219.             


  220. void View()
  221. {
  222.     int c;
  223.     system("cls");
  224.     time(&rawtime);
  225.     timeinfo= localtime(&rawtime);

  226.     strftime(date,20,"Now is %I:%M%p",timeinfo);
  227.     puts(date);
  228.     printf ("\t\t All Item\n");
  229.     printf ("\nBin 0\n");
  230.    
  231.     for (c=staker.Bin0.top; c>=0; c--)
  232.     {
  233.         printf ("\n Bin0 at stack %d is %5d", c, &staker.Bin0.items[c]);

  234.     }
  235.         printf ("\nBin 1\n");
  236.    
  237.         for (c=staker.Bin1.top; c>=0; c--)
  238.         {
  239.             printf ("\n Bin1 at stack %d is %5d", c, staker.Bin1.items[c]);

  240.         }
  241.             
  242.             printf ("\nBin 2\n");
  243.    
  244.             for (c=staker.Bin2.top; c>=0; c--)
  245.             {
  246.                 printf ("\n Bin2 at stack %d is %5d", c, staker.Bin2.items[c]);

  247.             }

  248.                 printf ("\nBin 3\n");
  249.    
  250.                     for (c=staker.Bin3.top; c>=0; c--)
  251.                 {
  252.                     printf ("\n Bin3 at stack %d is %5d", c, staker.Bin3.items[c]);

  253.                 }

  254.                         printf ("\nBin 4\n");
  255.    
  256.                         for (c=staker.Bin4.top; c>=0; c--)
  257.                     {
  258.                         printf ("\n Bin4 at stack %d is %5d", c, staker.Bin4.items[c]);

  259.                     }
  260.                         printf ("\n Press any key to go back.");
  261.                         getch();
  262.                         main();

  263. }

  264. void Pop()
  265. {

  266.     int select;
  267.     int z;



  268.     printf ("Remove Top of Bin");
  269.     printf("Please select the Bin which you want to remove its Top of Bin");
  270.     scanf("%d",&select);
  271.    
  272.     {
  273.         switch (select)
  274.         {

  275.         case 0:
  276.                 RemoveBin0(&staker.Bin0);
  277.                 break;

  278.         case 1:
  279.                 RemoveBin1(&staker.Bin1);
  280.                 break;

  281.         case 2:
  282.                 RemoveBin2(&staker.Bin2);
  283.                 break;

  284.         case 3:
  285.                 RemoveBin3(&staker.Bin3);
  286.                 break;

  287.         case 4:
  288.                 RemoveBin4(&staker.Bin4);
  289.                 break;

  290.         default:
  291.                 printf("Invalid value, pleasekey in again");
  292.                 break;
  293.         }

  294.         printf(" To continue press 5, To exit press 6");
  295.         scanf ("%d", &z);

  296.         {
  297.             if (z == 5)
  298.             {
  299.                 Pop();
  300.             }

  301.             if (z == 6)

  302.             {
  303.                 main();
  304.             }
  305.         }
  306.     }
  307. }
复制代码

[ 本帖最后由 andrew_pang 于 26-6-2008 03:33 PM 编辑 ]
回复

使用道具 举报


ADVERTISEMENT

 楼主| 发表于 26-6-2008 09:37 AM | 显示全部楼层
.
  1.     void RemoveBin0(struct Bin0 *pq)
  2.     {
  3.         int q;
  4.         
  5.         (pq->items[pq->top--]);

  6.         printf(" The Top of Binis clear.");
  7.         printf(" To continue press 5, To exit press 6");
  8.         scanf ("%d", &q);

  9.         {
  10.             if (q == 5)
  11.             {
  12.                 Pop();
  13.             }

  14.             if (q == 6)

  15.             {
  16.                 main();
  17.             }
  18.         }
  19.     }

  20.     void RemoveBin1(struct Bin1 *pq)
  21.     {
  22.         int w;
  23.         
  24.         (pq->items[pq->top--]);

  25.         printf(" The Top of Binis clear.");
  26.         printf(" To continue press 5, To exit press 6");
  27.         scanf ("%d", &w);

  28.         {
  29.             if (w == 5)
  30.             {
  31.                 Pop();
  32.             }

  33.             if (w == 6)

  34.             {
  35.                 main();
  36.             }
  37.         }
  38.     }

  39.     void RemoveBin2(struct Bin2 *pq)
  40.     {
  41.         int e;
  42.         
  43.         (pq->items[pq->top--]);

  44.         printf(" The Top of Binis clear.");
  45.         printf(" To continue press 5, To exit press 6");
  46.         scanf ("%d", &e);

  47.         {
  48.             if (e == 5)
  49.             {
  50.                 Pop();
  51.             }

  52.             if (e == 6)

  53.             {
  54.                 main();
  55.             }
  56.         }
  57.     }

  58.     void RemoveBin3(struct Bin3 *pq)
  59.     {
  60.         int r;
  61.         
  62.         
  63.         (pq->items[pq->top--]);

  64.         printf(" The Top of Binis clear.");
  65.         printf(" To continue press 5, To exit press 6");
  66.         scanf ("%d", &r);

  67.         {
  68.             if (r == 5)
  69.             {
  70.                 Pop();
  71.             }

  72.             if (r == 6)

  73.             {
  74.                 main();
  75.             }
  76.         }
  77.     }
  78.     void RemoveBin4(struct Bin4 *pq)
  79.     {
  80.         int u;
  81.         
  82.         (pq->items[pq->top--]);

  83.         printf(" The Top of Binis clear.");
  84.         printf(" To continue press 5, To exit press 6");
  85.         scanf ("%d", &u);

  86.         {
  87.             if (u == 5)
  88.             {
  89.                 Pop();
  90.             }

  91.             if (u == 6)

  92.             {
  93.                 main();
  94.             }
  95.         }
  96.     }
复制代码



请各位帮帮忙,看哪里出错..
各位的帮忙,小弟感激不尽..
拜托了各位..
谢谢...

[ 本帖最后由 andrew_pang 于 26-6-2008 03:53 PM 编辑 ]
回复

使用道具 举报

发表于 26-6-2008 11:44 AM | 显示全部楼层
为什么你的 view() 里面,不论是哪一个bin,都是用 bin0.top 当作 for loop 的 condition???

你的code 有点乱,我看得很辛苦,所以没仔细去研究。。。下次用 code tag 吧,把你的source code 从你的 IDE copy & paste 在 code tag 里面 例如:
  1. source code goes here!!
复制代码
code tag 的 discuz!代码: http://cforum.cari.com.my/faq.php?action=message&id=18

[ 本帖最后由 geekman 于 26-6-2008 11:51 AM 编辑 ]
回复

使用道具 举报

 楼主| 发表于 26-6-2008 03:28 PM | 显示全部楼层

那个是我copy n paste时,没改到..
你说的那个什么code tag , 我不明白..
回复

使用道具 举报

 楼主| 发表于 26-6-2008 03:30 PM | 显示全部楼层
我试下..
  1. source code goes here!!
复制代码
回复

使用道具 举报

 楼主| 发表于 26-6-2008 03:36 PM | 显示全部楼层
我弄了,请帮我在看看....
pls...
回复

使用道具 举报

Follow Us
发表于 26-6-2008 06:05 PM | 显示全部楼层
ok, 这样看起来就比较整齐,没那么辛苦了。

首先,你的struct...很不符合效率。你并不需要在一个struct里面再declare 另外5个struct,更何况这5个struct都是同样的东西来的。。。
  1. struct bin_struct
  2. {
  3.    int top;
  4.    int items[SIZE];
  5. };

  6. struct
  7. {
  8.     int top;
  9.     int items[SIZE];
  10.     bin_struct bins[5];
  11. }staker;
复制代码
而我更不明白的是,你的 stack 和 bin 都是同样的东西,只是 stack 包含了 5个 bin 罢了。。。看来又不像是 recursive data。。。我想你从一开始就构思了错误的 data structure,只是我并不知道你的目的和出发点,所以无从判定如何改造你的构思,只能根据你的原来的source code 做有限的更改。

把5个bin组成array有几个好处,其中一个就是你可以轻易的使用for loop 来设定其内容,例如 main() 里面:
  1. for(int i=0; i<5; i++)
  2. {
  3.     stacker.bins[i].top = -1;
  4. }
复制代码
接下来的,更是震撼了我的知识。。。我真的不知道可以这样呼叫 main() 的。。。我现在感到很困惑。你是尝试使用 recursive function 吗?这样的呼叫是十分危险的,一不小心会造成系统记忆体泛滥的。。。
  1. if (bin_id == 5)
  2. {
  3.             printf("\t Thank you!!\n\tPress Any Key Back To Main Menu!");
  4.             getch();
  5.             main();
  6. }
复制代码
经过深思熟虑,我感到你的push()里面真的危机重重,如果使用者不断的输入‘5’,你的程式会不断的呼叫main() (main()本身是program的切入点啊啊啊。。。),而main()里面又会再呼叫push()...如此不断循环,迟早会把系统记忆体给吃光的。。。

我想在你解释为何要这么做之前,我真的没有勇气继续下去。
回复

使用道具 举报

发表于 26-6-2008 06:21 PM | 显示全部楼层
我测试过了,使用以下的代码就可以显示出这种recursive的杀伤力:
  1. //---------------------------------------------------------------------------
  2. #include <stdio.h>
  3. #include <conio.h>
  4. #include <stdlib.h>
  5. #pragma hdrstop
  6. //---------------------------------------------------------------------------
  7. #pragma argsused
  8. void some_function(int);
  9. static int counter = 0;
  10. void main(void)
  11. {
  12.     do
  13.     {
  14.         some_function(counter);
  15.     }while((counter++) < 100);
  16.     printf("\nPress any key to continue...");
  17.     getch();
  18. }
  19. //---------------------------------------------------------------------------
  20. void some_function(int value)
  21. {
  22.     printf("I'm some function... %d \n", value);
  23.     main();
  24. }
  25. //---------------------------------------------------------------------------
复制代码
你会发现,counter 根本不会增加,也就是说这程式会一直不断的循环(main()呼叫some_function() 呼叫main()呼叫some_function().....)直到系统崩溃为止,视乎你有多少记忆体可以使用!

在任何function里面呼叫main()绝对是禁忌中的禁忌啊啊啊。。。
回复

使用道具 举报


ADVERTISEMENT

 楼主| 发表于 26-6-2008 09:57 PM | 显示全部楼层
可是还是view不到呀?
在main那边有个view。。
你打了就去那边view..
view那边是空的...
回复

使用道具 举报

发表于 27-6-2008 12:51 AM | 显示全部楼层
你当然无法得到正确的结果,因为我看到你在push()里面呼叫main()就没有继续分析下去,有问题的部分都还没分析到。

我相信你的program真的需要彻头彻尾的大改造,给我一点时间吧。我家里的电脑并没有安装C/C++(我现在已经改去用C#了,只有工作时才有使用C++),也不可能放下工作来跟你修改program,只能等我有空闲的时间才来仔细研究了。
回复

使用道具 举报

发表于 29-6-2008 07:13 PM | 显示全部楼层
一般上,大家都把main当作主要的routine,而不是function

所以都这样做,

void main()
{
      while(1)
      {
          做爱做的事...

          if ( 特别情况 )
                 return;
      }
}
回复

使用道具 举报

发表于 29-6-2008 07:21 PM | 显示全部楼层
把你的push , pop function 里面的main()去掉

在void main()里头,稍稍修改,

void main()
{
   int choice;
    staker.Bin0.top=-1;
    staker.Bin1.top=-1;
    staker.Bin2.top=-1;
    staker.Bin3.top=-1;
    staker.Bin4.top=-1;

     while ( 1 )
     {
          display_menu();    /* 用这个function取代一堆printf */
          scanf ("%d", &choice);, 其实我觉得你用getch就可以了

          switch( choice )
          {
              ....
          }
    }

    return;
}

void display_menu()
{
     system ("cls");
    time(&rawtime);
    timeinfo= localtime(&rawtime);

    strftime(date,20,"Now is %I:%M%p",timeinfo);
    puts(date);
        
    printf (" \n\n\t\t\t ||======================||");
    printf (" \n\t\t\t ||  ^Wellcome, my guest^||");
    printf ("\n\t\t\t ||======================||");
    printf ("\n\t\t\t ||    Stacke Operation  ||");
    printf ("\n\t\t\t ||======================||");
    printf ("\n\n\n\t\t||========================================||");
    printf ("\n\t\t||                                        ||");
    printf ("\n\t\t||        1. Insert Item (Push)           ||");
    printf ("\n\t\t||        2. Remove Item (Pop)            ||");
    printf ("\n\t\t||        3. View Item (view)             ||");
    printf ("\n\t\t||        4. Exit                         ||");
    printf ("\n\t\t||                                        ||");
    printf ("\n\t\t||========================================||\n\n");
    printf ("\n\t       Please key in a number that you want to process\n");
    printf ("\t\t\t      Choice: ");
}

这样就避免了不恰当的recursive call而造成的memory leak
回复

使用道具 举报

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

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


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

GMT+8, 24-12-2025 07:19 AM , Processed in 0.108932 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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