|
查看: 840|回复: 11
|
拜托各位,帮帮我....(programing)
[复制链接]
|
|
|
- #include <stdio.h>
- #include <time.h>
- #include <stdlib.h>
- #include <string.h>
- #include <conio.h>
- #define SIZE 5
- void Pop();
- void Push();
- void View();
- void push(struct stack *,int, int);
- void Push_bin0(struct Bin0 *,int);
- void Push_bin1(struct Bin1 *,int);
- void Push_bin2(struct Bin2 *,int);
- void Push_bin3(struct Bin3 *,int);
- void Push_bin4(struct Bin4 *,int);
- void RemoveBin0(struct Bin0 *);
- void RemoveBin1(struct Bin1 *);
- void RemoveBin2(struct Bin2 *);
- void RemoveBin3(struct Bin3 *);
- void RemoveBin4(struct Bin4 *);
- time_t rawtime;
- struct tm * timeinfo;
- char date[20];
- static struct stack
- {
- int top;
- int items[SIZE];
-
- struct Bin0
- {
- int top;
- int items[SIZE];
- }Bin0;
- struct Bin1
- {
- int top;
- int items[SIZE];
- }Bin1;
- struct Bin2
- {
- int top;
- int items[SIZE];
- }Bin2;
- struct Bin3
- {
- int top;
- int items[SIZE];
- }Bin3;
- struct Bin4
- {
- int top;
- int items[SIZE];
- }Bin4;
- }staker;
- void main()
- {
-
- int choice;
- staker.Bin0.top=-1;
- staker.Bin1.top=-1;
- staker.Bin2.top=-1;
- staker.Bin3.top=-1;
- staker.Bin4.top=-1;
-
- 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: ");
- scanf ("%d", &choice);
- switch (choice)
- {
- case 1:
- Push();
- break;
- case 2:
- Pop();
- break;
- case 3:
- View();
- break;
- case 4:
- printf(" Thanks for using this system , please come again.");
- exit(0);
-
- default: printf (" Invalid value, please key in again \n");
- main();
- break;
- }
- }
- void Push()
- {
-
- int bin_num;
- int i;
- int bin_id;
-
-
-
- system ("cls");
- time(&rawtime);
- timeinfo= localtime(&rawtime);
- strftime(date,20,"Now is %I:%M%p",timeinfo);
- puts(date);
- for (i =0; i< 5 ; i ++)
- {
-
- printf ("\nAttention: Total Input cannot more than 5 \n");
- printf (" To end process, enter (-1) into bin_id\n");
- printf ("Add a new Bin ID to the stack: \t");
- scanf ("%d", &bin_id);
- if (bin_id == 5)
- {
- printf("\t Thank you!!\n\tPress Any Key Back To Main Menu!");
- getch();
- main();
- }
- if (bin_id <0 || bin_id >4)
- {
- printf("\n\tInvalid bin id.\n");
- i--;
- }
- printf("\nAdd a new number for the bin:\t");
- scanf("%d",&bin_num);
- push(&staker,bin_num,bin_id);
- }
- printf("\nThe Stack is Full! \nPress Any key Back To Main Menu!");
- getch();
- main();
- }
-
- void push(struct stack *pq, int x, int y) // int x = bin_num int y = bin_id
-
- {
- if ( y == 0 )
- {
- Push_bin0(&staker.Bin0, x);
- }
- else
- if (y == 1)
- {
- Push_bin1(&staker.Bin1, x);
- }
- else
- if(y == 2 )
- {
- Push_bin2(&staker.Bin2, x);
- }
- else
- if (y == 3 )
- {
- Push_bin3(&staker.Bin3, x);
- }
- else
- if (y == 4)
- {
- Push_bin4(&staker.Bin4, x);
- }
- }
-
-
- void Push_bin0 (struct Bin0 *pq, int x)
- {
- if (pq->top==SIZE -1)
- {
- (pq->items[pq->top--]);
-
- }
- pq->items[++pq->top]=x;
- }
- void Push_bin1 (struct Bin1 *pq, int x)
- {
- if (pq->top==SIZE -1)
- { (pq->items[pq->top--]);
-
- }
- pq->items[++pq->top]=x;
- }
- void Push_bin2 (struct Bin2 *pq, int x)
- {
- if (pq->top==SIZE -1)
- { (pq->items[pq->top--]);
-
- }
- pq->items[++pq->top]=x;
- }
- void Push_bin3 (struct Bin3 *pq, int x)
- {
- if (pq->top==SIZE -1)
- { (pq->items[pq->top--]);
-
- }
- pq->items[++pq->top]=x;
- }
- void Push_bin4 (struct Bin4 *pq, int x)
- {
- if (pq->top==SIZE -1)
- { (pq->items[pq->top--]);
-
- }
- pq->items[++pq->top]=x;
- }
-
- void View()
- {
- int c;
- system("cls");
- time(&rawtime);
- timeinfo= localtime(&rawtime);
- strftime(date,20,"Now is %I:%M%p",timeinfo);
- puts(date);
- printf ("\t\t All Item\n");
- printf ("\nBin 0\n");
-
- for (c=staker.Bin0.top; c>=0; c--)
- {
- printf ("\n Bin0 at stack %d is %5d", c, &staker.Bin0.items[c]);
- }
- printf ("\nBin 1\n");
-
- for (c=staker.Bin1.top; c>=0; c--)
- {
- printf ("\n Bin1 at stack %d is %5d", c, staker.Bin1.items[c]);
- }
-
- printf ("\nBin 2\n");
-
- for (c=staker.Bin2.top; c>=0; c--)
- {
- printf ("\n Bin2 at stack %d is %5d", c, staker.Bin2.items[c]);
- }
- printf ("\nBin 3\n");
-
- for (c=staker.Bin3.top; c>=0; c--)
- {
- printf ("\n Bin3 at stack %d is %5d", c, staker.Bin3.items[c]);
- }
- printf ("\nBin 4\n");
-
- for (c=staker.Bin4.top; c>=0; c--)
- {
- printf ("\n Bin4 at stack %d is %5d", c, staker.Bin4.items[c]);
- }
- printf ("\n Press any key to go back.");
- getch();
- main();
- }
- void Pop()
- {
- int select;
- int z;
- printf ("Remove Top of Bin");
- printf("Please select the Bin which you want to remove its Top of Bin");
- scanf("%d",&select);
-
- {
- switch (select)
- {
- case 0:
- RemoveBin0(&staker.Bin0);
- break;
- case 1:
- RemoveBin1(&staker.Bin1);
- break;
- case 2:
- RemoveBin2(&staker.Bin2);
- break;
- case 3:
- RemoveBin3(&staker.Bin3);
- break;
- case 4:
- RemoveBin4(&staker.Bin4);
- break;
- default:
- printf("Invalid value, pleasekey in again");
- break;
- }
- printf(" To continue press 5, To exit press 6");
- scanf ("%d", &z);
- {
- if (z == 5)
- {
- Pop();
- }
- if (z == 6)
- {
- main();
- }
- }
- }
- }
复制代码
[ 本帖最后由 andrew_pang 于 26-6-2008 03:33 PM 编辑 ] |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 26-6-2008 09:37 AM
|
显示全部楼层
.- void RemoveBin0(struct Bin0 *pq)
- {
- int q;
-
- (pq->items[pq->top--]);
- printf(" The Top of Binis clear.");
- printf(" To continue press 5, To exit press 6");
- scanf ("%d", &q);
- {
- if (q == 5)
- {
- Pop();
- }
- if (q == 6)
- {
- main();
- }
- }
- }
- void RemoveBin1(struct Bin1 *pq)
- {
- int w;
-
- (pq->items[pq->top--]);
- printf(" The Top of Binis clear.");
- printf(" To continue press 5, To exit press 6");
- scanf ("%d", &w);
- {
- if (w == 5)
- {
- Pop();
- }
- if (w == 6)
- {
- main();
- }
- }
- }
- void RemoveBin2(struct Bin2 *pq)
- {
- int e;
-
- (pq->items[pq->top--]);
- printf(" The Top of Binis clear.");
- printf(" To continue press 5, To exit press 6");
- scanf ("%d", &e);
- {
- if (e == 5)
- {
- Pop();
- }
- if (e == 6)
- {
- main();
- }
- }
- }
- void RemoveBin3(struct Bin3 *pq)
- {
- int r;
-
-
- (pq->items[pq->top--]);
- printf(" The Top of Binis clear.");
- printf(" To continue press 5, To exit press 6");
- scanf ("%d", &r);
- {
- if (r == 5)
- {
- Pop();
- }
- if (r == 6)
- {
- main();
- }
- }
- }
- void RemoveBin4(struct Bin4 *pq)
- {
- int u;
-
- (pq->items[pq->top--]);
- printf(" The Top of Binis clear.");
- printf(" To continue press 5, To exit press 6");
- scanf ("%d", &u);
- {
- if (u == 5)
- {
- Pop();
- }
- if (u == 6)
- {
- main();
- }
- }
- }
复制代码
请各位帮帮忙,看哪里出错..
各位的帮忙,小弟感激不尽..
拜托了各位..
谢谢...
[ 本帖最后由 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 里面 例如: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
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 26-6-2008 03:36 PM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|
发表于 26-6-2008 06:05 PM
|
显示全部楼层
ok, 这样看起来就比较整齐,没那么辛苦了。
首先,你的struct...很不符合效率。你并不需要在一个struct里面再declare 另外5个struct,更何况这5个struct都是同样的东西来的。。。- struct bin_struct
- {
- int top;
- int items[SIZE];
- };
- struct
- {
- int top;
- int items[SIZE];
- bin_struct bins[5];
- }staker;
复制代码 而我更不明白的是,你的 stack 和 bin 都是同样的东西,只是 stack 包含了 5个 bin 罢了。。。看来又不像是 recursive data。。。我想你从一开始就构思了错误的 data structure,只是我并不知道你的目的和出发点,所以无从判定如何改造你的构思,只能根据你的原来的source code 做有限的更改。
把5个bin组成array有几个好处,其中一个就是你可以轻易的使用for loop 来设定其内容,例如 main() 里面:- for(int i=0; i<5; i++)
- {
- stacker.bins[i].top = -1;
- }
复制代码 接下来的,更是震撼了我的知识。。。我真的不知道可以这样呼叫 main() 的。。。我现在感到很困惑。你是尝试使用 recursive function 吗?这样的呼叫是十分危险的,一不小心会造成系统记忆体泛滥的。。。- if (bin_id == 5)
- {
- printf("\t Thank you!!\n\tPress Any Key Back To Main Menu!");
- getch();
- main();
- }
复制代码 经过深思熟虑,我感到你的push()里面真的危机重重,如果使用者不断的输入‘5’,你的程式会不断的呼叫main() (main()本身是program的切入点啊啊啊。。。),而main()里面又会再呼叫push()...如此不断循环,迟早会把系统记忆体给吃光的。。。
我想在你解释为何要这么做之前,我真的没有勇气继续下去。 |
|
|
|
|
|
|
|
|
|
|
发表于 26-6-2008 06:21 PM
|
显示全部楼层
我测试过了,使用以下的代码就可以显示出这种recursive的杀伤力:- //---------------------------------------------------------------------------
- #include <stdio.h>
- #include <conio.h>
- #include <stdlib.h>
- #pragma hdrstop
- //---------------------------------------------------------------------------
- #pragma argsused
- void some_function(int);
- static int counter = 0;
- void main(void)
- {
- do
- {
- some_function(counter);
- }while((counter++) < 100);
- printf("\nPress any key to continue...");
- getch();
- }
- //---------------------------------------------------------------------------
- void some_function(int value)
- {
- printf("I'm some function... %d \n", value);
- main();
- }
- //---------------------------------------------------------------------------
复制代码 你会发现,counter 根本不会增加,也就是说这程式会一直不断的循环(main()呼叫some_function() 呼叫main()呼叫some_function().....)直到系统崩溃为止,视乎你有多少记忆体可以使用!
在任何function里面呼叫main()绝对是禁忌中的禁忌啊啊啊。。。 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 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 |
|
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|