|
我的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了- void badtable(){
- printf("|---------------------------------------------------------------|\n");
- printf("| court | 7am-9am | 9am-11am | 11am-1pm | 1pm-3pm | 3pm-5pm |\n");
- printf("|---------------------------------------------------------------|\n");
- printf("| A | 1. %s 2. %s 3. %s 4. %s 5. %s |\n", q1.bad1, q1.bad2, q1.bad3, q1.bad4, q1.bad5);
- printf("|---------------------------------------------------------------|\n");
- printf("| A | 6. %s 7. %s 8. %s 9. %s 10. %s |\n", q1.bad1, q1.bad2, q1.bad3, q1.bad4, q1.bad5);
- printf("|---------------------------------------------------------------|\n");
- printf("| A | 11. %s 12. %s 13. %s 14. %s 15. %s |\n", q1.bad1, q1.bad2, q1.bad3, q1.bad4, q1.bad5);
- printf("|---------------------------------------------------------------|\n");
- printf("| A | 16. %s 17. %s 18. %s 19. %s 20. %s |\n", q1.bad1, q1.bad2, q1.bad3, q1.bad4, q1.bad5);
- printf("|---------------------------------------------------------------|\n");
复制代码 本帖最后由 Chaisoo333 于 20-1-2014 07:25 PM 编辑
|
|
|
|
|
|
|
|
发表于 21-1-2014 09:33 AM
|
显示全部楼层
用for loop啊。。。基本中的基本。
- int num;
- for(int i=0; i<badminton.x; i++)
- {
- num = (i * 5) + 1;
- 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);
- }
复制代码 |
|
|
|
|
|
|
|
楼主 |
发表于 22-1-2014 02:20 AM
|
显示全部楼层
geekman 发表于 21-1-2014 09:33 AM
用for loop啊。。。基本中的基本。
不好意思我写错了
table的code是这个- printf("|---------------------------------------------------------------|\n");
- printf("| court | 7am-9am | 9am-11am | 11am-1pm | 1pm-3pm | 3pm-5pm |\n");
- printf("|---------------------------------------------------------------|\n");
- printf("| A | 1. %s 2. %s 3. %s 4. %s 5. %s |\n", q1.bad1, q1.bad2, q1.bad3, q1.bad4, q1.bad5);
- printf("|---------------------------------------------------------------|\n");
- printf("| B | 6. %s 7. %s 8. %s 9. %s 10. %s |\n", q1.bad6, q1.bad7, q1.bad8, q1.bad9, q1.bad10);
- printf("|---------------------------------------------------------------|\n");
- printf("| C | 11. %s 12. %s 13. %s 14. %s 15. %s |\n", q1.bad11, q1.bad12, q1.bad13, q1.bad14, q1.bad15);
- printf("|---------------------------------------------------------------|\n");
- printf("| D | 16. %s 17. %s 18. %s 19. %s 20. %s |\n", q1.bad16, q1.bad17, q1.bad18, q1.bad19, q1.bad20);
- 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- #include <conio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- #include <stdio.h>
- char pasword[10], usrname[10], ch;
- char day[10];
- int i, menu1, badminton, qq;
- struct badminton_none{
- char *bad1, *bad2, *bad3, *bad4, *bad5, *bad6, *bad7, *bad8, *bad9, *bad10;
- char *bad11, *bad12, *bad13, *bad14, *bad15, *bad16, *bad17, *bad18, *bad19, *bad20;
- }q1 = { "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O" };
- void chooseday(){
- printf("Which day?");
- scanf("%s", &day);
- while (!strcmp(day, "monday"))
- {
- printf("Monday is off day !\nPlease choose another day!");
- scanf("%s", &day);
- }
- system("cls");
- {
- printf("You have choose %s\nPlease pick a time\n", day);
- printf("1.None Peak time\n");
- printf("2.Peak time\n");
- scanf("%d", &badminton);
- }
- }
- void badtable(int num){
- printf("|---------------------------------------------------------------|\n");
- printf("| court | 7am-9am | 9am-11am | 11am-1pm | 1pm-3pm | 3pm-5pm |\n");
- printf("|---------------------------------------------------------------|\n");
- printf("| A | 1. %s 2. %s 3. %s 4. %s 5. %s |\n", q1.bad1, q1.bad2, q1.bad3, q1.bad4, q1.bad5);
- printf("|---------------------------------------------------------------|\n");
- printf("| B | 6. %s 7. %s 8. %s 9. %s 10. %s |\n", q1.bad6, q1.bad7, q1.bad8, q1.bad9, q1.bad10);
- printf("|---------------------------------------------------------------|\n");
- printf("| C | 11. %s 12. %s 13. %s 14. %s 15. %s |\n", q1.bad11, q1.bad12, q1.bad13, q1.bad14, q1.bad15);
- printf("|---------------------------------------------------------------|\n");
- printf("| D | 16. %s 17. %s 18. %s 19. %s 20. %s |\n", q1.bad16, q1.bad17, q1.bad18, q1.bad19, q1.bad20);
- printf("|---------------------------------------------------------------|\n");
- }
- void checkcourt(){
- printf("1.Badminton\n");
- printf("2.Squash\n");
- printf("3.Tennis\n");
- printf("4.Futsal\n");
- scanf("%d", &menu1);
- system("cls");
- if (menu1 == 1){
- chooseday();
- switch (badminton){
- case 1:{
- badtable(1);
- printf("Please insert the court");
- scanf("%d", &qq);
- system("cls");
- switch (qq)
- {
- case 1: {
- printf("You successsfully have booked a court.\n");
- q1.bad1 = "X";
- badtable(1);
- mainmenu();
- }break;
- case 2: {
- printf("You successsfully have booked a court.");
- q1.bad2 = "X";
- badtable(1);
- mainmenu();
- }break;
- }
- }break;
- case 2:{
- printf("|------------------------------------------|\n");
- printf("| court | 5pm-7pm | 7pm-9pm | 9pm-10pm |\n");
- printf("|------------------------------------------|\n");
- printf("| A | |\n");
- printf("|------------------------------------------|\n");
- printf("| B | |\n");
- printf("|------------------------------------------|\n");
- printf("| C | |\n");
- printf("|------------------------------------------|\n");
- printf("| D | |\n");
- printf("|------------------------------------------|\n");
- }break;
- }
- }
- else if (menu1 == 2)
- {
- chooseday();
- switch (badminton){
- case 1:{
- printf("|---------------------------------------------------------------|\n");
- printf("| court | 10am-12am | 12pm-2pm | 2pm-4pm | 4pm-5pm |\n");
- printf("|---------------------------------------------------------------|\n");
- printf("| A | |\n");
- printf("|---------------------------------------------------------------|\n");
- printf("| B | |\n");
- printf("|---------------------------------------------------------------|\n");
- }
- case 2:{
- printf("|---------------------------------------------------------------|\n");
- printf("| court | 5pm-7pm | 7pm-9pm | 9pm-10pm |\n");
- printf("|---------------------------------------------------------------|\n");
- printf("| A | |\n");
- printf("|---------------------------------------------------------------|\n");
- printf("| B | |\n");
- printf("|---------------------------------------------------------------|\n");
- }
- }
- }
- else if (menu1 == 3)
- {
- chooseday();
- switch (badminton){
- case 1:{
- printf("|---------------------------------------------------------------------------|\n");
- printf("| court | 8am-10am | 10am-12pm | 12pm-2pm | 2pm-4pm | 4pm-5pm |\n");
- printf("|---------------------------------------------------------------------------|\n");
- printf("| A | |\n");
- printf("|---------------------------------------------------------------------------|\n");
- printf("| B | |\n");
- printf("|---------------------------------------------------------------------------|\n");
- printf("| c | |\n");
- printf("|---------------------------------------------------------------------------|\n");
- }
- case 2:{
- printf("|---------------------------------------------------------------|\n");
- printf("| court | 5pm-7pm | 7pm-9pm | 9pm-10pm |\n");
- printf("|---------------------------------------------------------------|\n");
- printf("| A | |\n");
- printf("|---------------------------------------------------------------|\n");
- printf("| B | |\n");
- printf("|---------------------------------------------------------------|\n");
- printf("| c | |\n");
- printf("|---------------------------------------------------------------|\n");
- }
- }
- }
- else if (menu1 == 4){
- chooseday();
- switch (badminton){
- case 1:{
- printf("|---------------------------------------------------------------------------|\n");
- printf("| court | 7am-9am | 9am-11am | 11am-1pm | 1pm-3pm | 3pm-5pm |\n");
- printf("|---------------------------------------------------------------------------|\n");
- printf("| A | |\n");
- printf("|---------------------------------------------------------------------------|\n");
- printf("| B | |\n");
- printf("|---------------------------------------------------------------------------|\n");
- printf("| c | |\n");
- printf("|---------------------------------------------------------------------------|\n");
- }
- case 2:{
- printf("|---------------------------------------------------------------|\n");
- printf("| court | 5pm-7pm | 7pm-9pm | 9pm-10pm |\n");
- printf("|---------------------------------------------------------------|\n");
- printf("| A | |\n");
- printf("|---------------------------------------------------------------|\n");
- printf("| B | |\n");
- printf("|---------------------------------------------------------------|\n");
- printf("| c | |\n");
- printf("|---------------------------------------------------------------|\n");
- }
- }
- }
- else{}
- }
- int mainmenu(){
- system("cls");
- printf("1.Check court availability\n");
- printf("2.Booking\n");
- printf("3.Generate bill\n");
- printf("4.Exit\n");
- scanf("%d", &menu1);
- system("cls");
- switch (menu1){
- case 1:{
- checkcourt();
- }
-
- }
- }
- int main()
- {
- printf("Enter User name: ");
- gets(usrname);
- printf("Enter the password : ");
- for (i = 0; i < 10; i++)
- {
- ch = getch();
- if (isprint(ch))
- {
- pasword[i] = ch;
- ch = '*';
- printf("%c", ch);
- }
- else
- {
- pasword[i] = '\0';
- break;
- }
- }
- if (!strcmp(pasword, "class"))
- {
- printf("\n\naccess\n");
- mainmenu();
- }
- else
- {
- printf("\n\nno access\n");
- }
- }
复制代码 本帖最后由 Chaisoo333 于 22-1-2014 04:10 PM 编辑
|
|
|
|
|
|
|
|
发表于 22-1-2014 04:53 PM
|
显示全部楼层
穿上画着标靶的衣服然后向前辈们大喊:“朝我开炮吧!!” 是最好的学习方式。福建话有句话说:“歹势吃自己”,不经历风雨,怎么见彩虹?- struct badminton_none{
- char *bad1, *bad2, *bad3, *bad4, *bad5, *bad6, *bad7, *bad8, *bad9, *bad10;
- char *bad11, *bad12, *bad13, *bad14, *bad15, *bad16, *bad17, *bad18, *bad19, *bad20;
- }q1 = { "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O" };
复制代码 如果是我的话,我会用Array:
- char q1[20];
- /// initialize
- for(int i=0; i<20; i++)
- {
- q1[i] = 'O';
- }
复制代码 你的做法有以下的危险:
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分成几个元素:
- void print_line(int time)
- {
- switch(time)
- {
- /// non-peak
- case 1: printf("|---------------------------------------------------------------|\n");
- break;
- /// peak
- case 2: printf("|------------------------------------------|\n");
- break;
- }
- }
- void print_header(int time)
- {
- switch(time)
- {
- /// peak
- case 1: printf("| court | 7am-9am | 9am-11am | 11am-1pm | 1pm-3pm | 3pm-5pm |\n");
- break;
- /// non peak
- case 2: printf("| court | 5pm-7pm | 7pm-9pm | 9pm-10pm |\n");
- break;
- }
- }
- void print_content(char *court, int time, int start_index)
- {
- switch(time)
- {
- /// common element
- printf("| %s |", court);
- ///peak
- case 1:
- for(int i=0; i<5; i++)
- {
- printf(" %d. %s ", start_index + i, q1[start_index + i - 1];
- }
- printf("|\n");
- break;
- /// non peak 这部分有点不确定,视乎你自己如何Handle non-peak的情形。
- case 2:
- for(int i=0; i<3; i++)
- {
- printf(" %d. %s ", start_index + i, q1[start_index + i - 1];
- }
- break;
-
- }
- }
复制代码 当你要print table时:
- void print_badminton_table(int time)
- {
- print_line(time);
- print_header(time);
- print_line(time);
- print_content("A", time, 1);
- print_line(time);
- print_content("B", time, 6);
- print_line(time);
- print_content("C", time, 11);
- print_line(time);
- print_content("D", time, 16);
- print_line(time);
- }
复制代码 你甚至可以把Game Type也套进上面的Functions里面,然每个Functions都可以Handle每一种球类的Peak time和non-peak time,又或者你可以“勤劳”一点,把以上的Functions复制成不同的球类的版本。。。 本帖最后由 geekman 于 22-1-2014 05:01 PM 编辑
|
评分
-
查看全部评分
|
|
|
|
|
|
|
发表于 22-1-2014 06:31 PM
|
显示全部楼层
刚刚才发现,我把Peak和Non-peak给弄反了,你自己看着办吧,我懒得改了。 |
|
|
|
|
|
|
|
楼主 |
发表于 22-1-2014 07:29 PM
|
显示全部楼层
我用了你的方法applied下去
可是run到后面就不可以了
好像是printcontent()有问题,可是我找不出,请你帮我看下- #include <conio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- #include <stdio.h>
- char pasword[10], usrname[10], ch,day[10];
- int i, menu1, badminton, qq;
- char bad[20];
- void print_line(int time)
- {
- switch (time)
- {
-
- case 1: {
- printf("|---------------------------------------------------------------|\n");
- }break;
-
- case 2: {
- printf("|------------------------------------------|\n");
- }break;
- }
- }
- void print_header(int time)
- {
- switch (time)
- {
-
- case 1: {
- printf("| court | 7am-9am | 9am-11am | 11am-1pm | 1pm-3pm | 3pm-5pm |\n");
- }break;
-
- case 2: {
- printf("| court | 5pm-7pm | 7pm-9pm | 9pm-10pm |\n");
- }break;
- }
- }
- void print_content(char *court, int time, int start_index){
- switch (time){
- printf("| %s |", court);
- case 1:{
- for (int i = 0; i < 5; i++)
- {
- printf(" %d. %s ", start_index + i, bad[start_index + i - 1]);
- }
- printf("|\n");
- }break;
- case 2:{
- for (int i = 0; i < 3; i++)
- {
- printf(" %d. %s ", start_index + i, bad[start_index + i - 1]);
- }
- }break;
- }
- }
- void print_badminton_table(int time)
- {
- print_line(time);
- print_header(time);
- print_line(time);
- print_content("A", time, 1);
- print_line(time);
- print_content("B", time, 6);
- print_line(time);
- print_content("C", time, 11);
- print_line(time);
- print_content("D", time, 16);
- print_line(time);
- }
- void chooseday(){
- printf("Which day?");
- scanf("%s", &day);
- while (!strcmp(day, "monday"))
- {
- printf("Monday is off day !\nPlease choose another day!");
- scanf("%s", &day);
- }
- system("cls");
- {
- printf("You have choose %s\nPlease pick a time\n", day);
- printf("1.None Peak time\n");
- printf("2.Peak time\n");
- scanf("%d", &badminton);
- }
- }
- void checkcourt(){
- printf("1.Badminton\n");
- printf("2.Squash\n");
- printf("3.Tennis\n");
- printf("4.Futsal\n");
- scanf("%d", &menu1);
- system("cls");
- if (menu1 == 1){
- chooseday();
- switch (badminton){
- case 1:{
- print_badminton_table(badminton);
- printf("Please insert the court");
- scanf("%d", &qq);
- system("cls");
- printf("You successsfully have booked a court.\n");
- bad[qq] = "X";
- print_badminton_table(badminton);
- mainmenu();
- case 2:{
- printf("|------------------------------------------|\n");
- printf("| court | 5pm-7pm | 7pm-9pm | 9pm-10pm |\n");
- printf("|------------------------------------------|\n");
- printf("| A | |\n");
- printf("|------------------------------------------|\n");
- printf("| B | |\n");
- printf("|------------------------------------------|\n");
- printf("| C | |\n");
- printf("|------------------------------------------|\n");
- printf("| D | |\n");
- printf("|------------------------------------------|\n");
- }break;
- }
- }
- }
- else if (menu1 == 2)
- {
- chooseday();
- switch (badminton){
- case 1:{
- printf("|---------------------------------------------------------------|\n");
- printf("| court | 10am-12am | 12pm-2pm | 2pm-4pm | 4pm-5pm |\n");
- printf("|---------------------------------------------------------------|\n");
- printf("| A | |\n");
- printf("|---------------------------------------------------------------|\n");
- printf("| B | |\n");
- printf("|---------------------------------------------------------------|\n");
- }
- case 2:{
- printf("|---------------------------------------------------------------|\n");
- printf("| court | 5pm-7pm | 7pm-9pm | 9pm-10pm |\n");
- printf("|---------------------------------------------------------------|\n");
- printf("| A | |\n");
- printf("|---------------------------------------------------------------|\n");
- printf("| B | |\n");
- printf("|---------------------------------------------------------------|\n");
- }
- }
- }
- else if (menu1 == 3)
- {
- chooseday();
- switch (badminton){
- case 1:{
- printf("|---------------------------------------------------------------------------|\n");
- printf("| court | 8am-10am | 10am-12pm | 12pm-2pm | 2pm-4pm | 4pm-5pm |\n");
- printf("|---------------------------------------------------------------------------|\n");
- printf("| A | |\n");
- printf("|---------------------------------------------------------------------------|\n");
- printf("| B | |\n");
- printf("|---------------------------------------------------------------------------|\n");
- printf("| c | |\n");
- printf("|---------------------------------------------------------------------------|\n");
- }
- case 2:{
- printf("|---------------------------------------------------------------|\n");
- printf("| court | 5pm-7pm | 7pm-9pm | 9pm-10pm |\n");
- printf("|---------------------------------------------------------------|\n");
- printf("| A | |\n");
- printf("|---------------------------------------------------------------|\n");
- printf("| B | |\n");
- printf("|---------------------------------------------------------------|\n");
- printf("| c | |\n");
- printf("|---------------------------------------------------------------|\n");
- }
- }
- }
- else if (menu1 == 4){
- chooseday();
- switch (badminton){
- case 1:{
- printf("|---------------------------------------------------------------------------|\n");
- printf("| court | 7am-9am | 9am-11am | 11am-1pm | 1pm-3pm | 3pm-5pm |\n");
- printf("|---------------------------------------------------------------------------|\n");
- printf("| A | |\n");
- printf("|---------------------------------------------------------------------------|\n");
- printf("| B | |\n");
- printf("|---------------------------------------------------------------------------|\n");
- printf("| c | |\n");
- printf("|---------------------------------------------------------------------------|\n");
- }
- case 2:{
- printf("|---------------------------------------------------------------|\n");
- printf("| court | 5pm-7pm | 7pm-9pm | 9pm-10pm |\n");
- printf("|---------------------------------------------------------------|\n");
- printf("| A | |\n");
- printf("|---------------------------------------------------------------|\n");
- printf("| B | |\n");
- printf("|---------------------------------------------------------------|\n");
- printf("| c | |\n");
- printf("|---------------------------------------------------------------|\n");
- }
- }
- }
- else{}
- }
- int mainmenu(){
- system("cls");
- printf("1.Check court availability\n");
- printf("2.Booking\n");
- printf("3.Generate bill\n");
- printf("4.Exit\n");
- scanf("%d", &menu1);
- system("cls");
- switch (menu1){
- case 1:{
- checkcourt();
- }
- }
- }
- int main(){
- for (int i = 0; i < 20; i++){
- bad[i] = "O";
- }
- printf("Enter User name: ");
- gets(usrname);
- printf("Enter the password : ");
- for (i = 0; i < 10; i++)
- {
- ch = getch();
- if (isprint(ch))
- {
- pasword[i] = ch;
- ch = '*';
- printf("%c", ch);
- }
- else
- {
- pasword[i] = '\0';
- break;
- }
- }
- if (!strcmp(pasword, "class"))
- {
- printf("\n\naccess\n");
- mainmenu();
- }
- else
- {
- printf("\n\nno access\n");
- }
- }
复制代码 |
|
|
|
|
|
|
|
楼主 |
发表于 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。 |
评分
-
查看全部评分
|
|
|
|
|
|
|
楼主 |
发表于 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- #include <conio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- #include <stdio.h>
- char pasword[10], usrname[10], ch, day[10];
- int i, menu1, picktime, qq, select;
- struct badminton{
- char *bad[20];
- }p, np;
- struct squash{
- char *squ[20];
- }a1, a2;
- struct tennis{
- char *ten[20];
- }b1, b2;
- struct futsal{
- char *fut[20];
- }c1, c2;
- enum day{tuesday,wednesday,thursday,friday,saturday,sunday};
- void print_line(int time)
- {
- switch (time)
- {
- case 1: {
- printf("|-------------------------------------------------------------------|\n");
- }break;
- case 2: {
- printf("|---------------------------------------------|\n");
- }break;
- }
- }
- void print_header(int time)
- {
- switch (time)
- {
- case 1: {
- printf("| court | 7am-9am | 9am-11am | 11am-1pm | 1pm-3pm | 3pm-5pm |\n");
- }break;
- case 2: {
- printf("| court | 5pm-7pm | 7pm-9pm | 9pm-10pm |\n");
- }break;
- }
- }
- void print_header2(int time)
- {
- switch (time)
- {
- case 1: {
- printf("| court | 10am-12am | 12pm-2pm | 2pm-4pm | 4pm-5pm |\n");
- }break;
- case 2: {
- printf("| court | 5pm-7pm | 7pm-9pm | 9pm-10pm |\n");
- }break;
- }
- }
- void print_header3(int time)
- {
- switch (time)
- {
- case 1: {
- printf("| court | 8am-10am | 10am-12pm | 12pm-2pm | 2pm-4pm | 4pm-5pm |\n");
- }break;
- case 2: {
- printf("| court | 5pm-7pm | 7pm-9pm | 9pm-10pm |\n");
- }break;
- }
- }
- void print_header4(int time)
- {
- switch (time)
- {
- case 1: {
- printf("| court | 7am-9am | 9am-11am | 11am-1pm | 1pm-3pm | 3pm-5pm |\n");
- }break;
- case 2: {
- printf("| court | 5pm-7pm | 7pm-9pm | 9pm-10pm |\n");
- }break;
- }
- }
- void print_content(char *court, int time, int start_index){
- char *space;
- printf("| %s |", court);
- switch (time){
- case 1:{
- for (int i = 0; i < 5; i++)
- {
- if (start_index + i < 10)
- {
- space = " ";
- printf(" %d. %s%s ", start_index + i, np.bad[start_index + i - 1], space);
- }
- else
- {
- printf(" %d. %s ", start_index + i, np.bad[start_index + i - 1]);
- }
- }
- printf("|\n");
- }break;
- case 2:{
- for (int i = 0; i < 3; i++)
- {
- if (start_index + i < 10)
- {
- space = " ";
- printf(" %d. %s%s ", start_index + i, p.bad[start_index + i - 1], space);
- }
- else
- {
- printf(" %d. %s ", start_index + i, p.bad[start_index + i - 1]);
- }
- }
- printf("|\n");
- }break;
- }
- }
- void print_content2(char *court, int time, int start_index){
- char *space;
- printf("| %s |", court);
- switch (time){
- case 1:{
- for (int i = 0; i < 4; i++)
- {
- if (start_index + i < 10)
- {
- space = " ";
- printf(" %d. %s%s ", start_index + i, a1.squ[start_index + i - 1], space);
- }
- else
- {
- printf(" %d. %s ", start_index + i, a1.squ[start_index + i - 1]);
- }
- }
- printf("|\n");
- }break;
- case 2:{
- for (int i = 0; i < 3; i++)
- {
- if (start_index + i < 10)
- {
- space = " ";
- printf(" %d. %s%s ", start_index + i, a2.squ[start_index + i - 1], space);
- }
- else
- {
- printf(" %d. %s ", start_index + i, a2.squ[start_index + i - 1]);
- }
- }
- printf("|\n");
- }break;
- }
- }
- void print_content3(char *court, int time, int start_index){
- char *space;
- printf("| %s |", court);
- switch (time){
- case 1:{
- for (int i = 0; i < 5; i++)
- {
- if (start_index + i < 10)
- {
- space = " ";
- printf(" %d. %s%s ", start_index + i, b1.ten[start_index + i - 1], space);
- }
- else
- {
- printf(" %d. %s ", start_index + i, b1.ten[start_index + i - 1]);
- }
- }
- printf("|\n");
- }break;
- case 2:{
- for (int i = 0; i < 3; i++)
- {
- if (start_index + i < 10)
- {
- space = " ";
- printf(" %d. %s%s ", start_index + i, b2.ten[start_index + i - 1], space);
- }
- else
- {
- printf(" %d. %s ", start_index + i, b2.ten[start_index + i - 1]);
- }
- }
- printf("|\n");
- }break;
- }
- }
- void print_content4(char *court, int time, int start_index){
- char *space;
- printf("| %s |", court);
- switch (time){
- case 1:{
- for (int i = 0; i < 5; i++)
- {
- if (start_index + i < 10)
- {
- space = " ";
- printf(" %d. %s%s ", start_index + i, c1.fut[start_index + i - 1], space);
- }
- else
- {
- printf(" %d. %s ", start_index + i, c1.fut[start_index + i - 1]);
- }
- }
- printf("|\n");
- }break;
- case 2:{
- for (int i = 0; i < 3; i++)
- {
- if (start_index + i < 10)
- {
- space = " ";
- printf(" %d. %s%s ", start_index + i, c2.fut[start_index + i - 1], space);
- }
- else
- {
- printf(" %d. %s ", start_index + i, c2.fut[start_index + i - 1]);
- }
- }
- printf("|\n");
- }break;
- }
- }
- void print_badminton_table(int time)
- {
- switch (menu1){
- case 1:{
- print_line(time);
- print_header(time);
- print_line(time);
- print_content("A", time, 1);
- print_line(time);
- print_content("B", time, 6);
- print_line(time);
- print_content("C", time, 11);
- print_line(time);
- print_content("D", time, 16);
- print_line(time);
- }break;
- case 2:{
- print_line(time);
- print_header2(time);
- print_line(time);
- print_content2("A", time, 1);
- print_line(time);
- print_content2("B", time, 4);
- print_line(time);
- }break;
- case 3:{
- print_line(time);
- print_header3(time);
- print_line(time);
- print_content3("A", time, 1);
- print_line(time);
- print_content3("B", time, 6);
- print_line(time);
- print_content3("C", time, 11);
- print_line(time);
- }break;
- case 4:{
- print_line(time);
- print_header(time);
- print_line(time);
- print_content4("A", time, 1);
- print_line(time);
- print_content4("B", time, 6);
- print_line(time);
- print_content4("C", time, 11);
- print_line(time);
- }break;
- }
- }
- void chooseday(){
- printf("Which day?");
- scanf("%s", &day);
- while (!strcmp(day, "monday"))
- {
- printf("Monday is off day !\nPlease choose another day!");
- scanf("%s", &day);
- }
- system("cls");
- {
- printf("You have choose %s\nPlease pick a time\n", day);
- printf("1.None Peak time\n");
- printf("2.Peak time\n");
- scanf("%d", &picktime);
- }
- }
- void checkcourt(){
- printf("1.Badminton\n");
- printf("2.Squash\n");
- printf("3.Tennis\n");
- printf("4.Futsal\n");
- scanf("%d", &menu1);
- system("cls");
- if (menu1 == 1){
- chooseday();
- print_badminton_table(picktime);
- printf("Please insert the court");
- scanf("%d", &qq);
- system("cls");
- printf("You successsfully have booked a court.\n");
- switch (picktime){
- case 1:{
- np.bad[qq - 1] = "X";
- print_badminton_table(picktime);
- getch();
- mainmenu();
- }break;
- case 2:{
- p.bad[qq - 1] = "X";
- print_badminton_table(picktime);
- mainmenu();
- }break;
- }
- }
- else if (menu1 == 2){
- chooseday();
- print_badminton_table(picktime);
- printf("Please insert the court");
- scanf("%d", &qq);
- system("cls");
- printf("You successsfully have booked a court.\n");
- switch (picktime){
- case 1:{
- a1.squ[qq - 1] = "X";
- print_badminton_table(picktime);
- mainmenu();
- }
- case 2:{
- a2.squ[qq - 1] = "X";
- print_badminton_table(picktime);
- mainmenu();
- }
- }
- }
- else if (menu1 == 3){
- chooseday();
- print_badminton_table(picktime);
- printf("Please insert the court");
- scanf("%d", &qq);
- system("cls");
- printf("You successsfully have booked a court.\n");
- switch (picktime){
- case 1:{
- b1.ten[qq - 1] = "X";
- print_badminton_table(picktime);
- mainmenu();
- }
- case 2:{
- b2.ten[qq - 1] = "X";
- print_badminton_table(picktime);
- mainmenu();
- }
- }
- }
- else if (menu1 == 4){
- chooseday();
- print_badminton_table(picktime);
- printf("Please insert the court");
- scanf("%d", &qq);
- system("cls");
- printf("You successsfully have booked a court.\n");
- switch (picktime){
- case 1:{
- c1.fut[qq - 1] = "X";
- print_badminton_table(picktime);
- mainmenu();
- }
- case 2:{
- c2.fut[qq - 1] = "X";
- print_badminton_table(picktime);
- mainmenu();
- }
- }
- }
- else{}
- }
- int mainmenu(){
- system("cls");
- printf("1.Check court availability\n");
- printf("2.Booking\n");
- printf("3.Generate bill\n");
- printf("4.Exit\n");
- scanf("%d", &menu1);
- system("cls");
- switch (menu1){
- case 1:{
- checkcourt();
- }
- }
- }
- int main(){
- int a;
- for (a = 0; a < 20; a++){
- np.bad[a] = "O";
- p.bad[a] = "O";
- a1.squ[a] = "O";
- a2.squ[a] = "O";
- b1.ten[a] = "O";
- b2.ten[a] = "O";
- c1.fut[a] = "O";
- c2.fut[a] = "O";
- }
- printf("Enter User name: ");
- gets(usrname);
- printf("Enter the password : ");
- for (i = 0; i < 10; i++)
- {
- ch = getch();
- if (isprint(ch))
- {
- pasword[i] = ch;
- ch = '*';
- printf("%c", ch);
- }
- else
- {
- pasword[i] = '\0';
- break;
- }
- }
- if (!strcmp(pasword, "class"))
- {
- printf("\n\naccess\n");
- mainmenu();
- }
- else
- {
- printf("\n\nno access\n");
- }
- }
复制代码 |
|
|
|
|
|
|
|
楼主 |
发表于 23-1-2014 12:12 AM
|
显示全部楼层
这样子可以吗?- enum nameday{ tuesday=0, wednesday, thursday, friday, saturday, sunday }dayy;
复制代码- struct badminton{
- char *bad[20];
- }p[10], np[10];
复制代码- void print_content(char *court, int time, int start_index){
- char *space;
- printf("| %s |", court);
- switch (time){
- case 1:{
- for (int i = 0; i < 5; i++)
- {
- if (start_index + i < 10)
- {
- space = " ";
- printf(" %d. %s%s ", start_index + i, np[dayy].bad[start_index + i - 1], space);
- }
- else
- {
- printf(" %d. %s ", start_index + i, np[dayy].bad[start_index + i - 1]);
- }
- }
- printf("|\n");
- }break;
- case 2:{
- for (int i = 0; i < 3; i++)
- {
- if (start_index + i < 10)
- {
- space = " ";
- printf(" %d. %s%s ", start_index + i, p[dayy].bad[start_index + i - 1], space); //改成p[dayy]
- }
- else
- {
- printf(" %d. %s ", start_index + i, p[dayy].bad[start_index + i - 1]);
- }
- }
- printf("|\n");
- }break;
- }
- }
复制代码 因为我declare enum 里面放tuesday
所以tuesday是0
我这边要怎样写才可以让user写tuesday时把 0这个号码存进dayy里面?- void chooseday(){
- printf("Which day?");
- scanf("%s", &day);
- if (!strcmp(day, "monday"))
- {
- printf("Monday is off day !\nPlease choose another day!");
- scanf("%s", &day);
- }
- dayy = day;
- system("cls");
- {
- printf("You have choose %s\nPlease pick a time\n", day);
- printf("1.None Peak time\n");
- printf("2.Peak time\n");
- scanf("%d", &picktime);
- }
- }
复制代码 |
|
|
|
|
|
|
|
楼主 |
发表于 23-1-2014 12:21 AM
|
显示全部楼层
ok其实我改成用if -else 了,弄到了,不过上面那个其实是可以的吗? |
|
|
|
|
|
|
|
发表于 23-1-2014 12:56 PM
|
显示全部楼层
呃,每一天的Table都不一样的吗?
关于Day的问题,其实一直以来(在DOS时代)当有Multiple Choice的时候,大家都习惯用Int来作为选项:
我会把Menu设定成这样- Please choose a day:
- 1. Tuesday
- 2. Wednesday
- 3. Thursday
- 4. Friday
- 5. Saturday
- 6. Sunday
- (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小时
以下是整个程式的流程:- /// program flow pseudo code
- get_day();
- int day = user day choice
- get_game();
- int game = user game choice
- get_time();
- int time = user time choice
- get_booking(day, game, time);
- int book_time = user booking choice
复制代码- int day = 0, game = 0, time = 0;
- bool done;
- void get_day(void)
- {
- done = false;
- do
- {
- clrscr();
- printf("Please select a day:\n");
- printf("1. Tuesday\n");
- printf("2. Wednesday\n");
- printf("3. Thursday\n");
- printf("4. Friday\n");
- printf("5. Saturday\n");
- printf("6. Sunday\n");
- printf("(Note: Monday is off day\n");
- scanf("%d", &day);
- if(day >= 1 && day <= 6)
- {
- done = true;
- }
- else
- {
- printf("Invalid choice. Please choose 1 to 6.");
- done = false;
- }
- }while(!done);
- }
- void get_game(void)
- {
- done = false;
- do
- {
- /// 大致上和上面差不多,让用户选择游戏/球类
- }while(!done);
- }
- void get_time(void)
- {
- /// 以上雷同,选择时间段
- }
- /// 现在你已经有了day, game 和 time,接下来就是要显示booking table
- void get_booking(day, game, time)
- {
- print_table(day, game, time);
- /// 读取用户选项,以上雷同,每个时间段都有一个数字了嘛,
- /// 自然就是读取该数字来代表 时间段/球场 啰。。。
- }
- /// 到此,一次booking完成。是否要loop,自己看着办吧
复制代码 |
|
|
|
|
|
|
|
发表于 23-1-2014 04:22 PM
|
显示全部楼层
这个是我自己做的,应该已经Cover你大多数的需要了:- //---------------------------------------------------------------------------
- #include <stdio.h>
- #include <tchar.h>
- #include <conio.h>
- #include <string.h>
- #pragma hdrstop
- //---------------------------------------------------------------------------
- #pragma argsused
- /// global
- /// 6 days (Monday not included), 4 Games, 2 times, 4 Courts, 5 Columns
- char booking[6][4][2][4][5];
- int quit = 0;
- int col, row;
- int book_court(int day, int game, int time);
- int get_day(void);
- int get_game(void);
- int get_time(void);
- void print_table(int day, int game, int time);
- //---------------------------------------------------------------------------
- int _tmain(int argc, _TCHAR* argv[])
- {
- int i, j, k, l, m, day = 1, game = 1, time = 1, result, booked = 0;
- /// init booking table
- for(i=0; i<6; i++)
- {
- for(j=0; j<4; j++)
- {
- for(k=0; k<2; k++)
- {
- for(l=0; l<4; l++)
- {
- for(m=0; m<5; m++)
- {
- booking[i][j][k][l][m] = 'O';
- }
- }
- }
- }
- }
- while (!quit)
- {
- day = get_day();
- if(day == 0)
- break;
- game = get_game();
- if(game == 0)
- break;
- time = get_time();
- if(time == 0)
- break;
- booked = 0;
- while(!booked)
- {
- result = book_court(day, game, time);
- if(result > 0)
- {
- /// display updated booking info
- print_table(day, game, time);
- printf("You have successfully booked a court. \nPress any key to continue.");
- getch();
- booked = 0;
- }
- else if(result == -1)
- { /// block is booked, retry
- booked = 0;
- }
- else
- { /// quit
- booked = 1;
- break;
- }
- }
- }
- return 0;
- }
- //---------------------------------------------------------------------------
- int get_day(void)
- {
- int day;
- int done = 0;
- do
- {
- clrscr();
- printf("Please select a day:\n");
- printf("1 Tuesday\n");
- printf("2 Wednesday\n");
- printf("3 Thursday\n");
- printf("4 Friday\n");
- printf("5 Saturday\n");
- printf("6 Sunday\n");
- printf("0 to quit\n");
- printf("(Note: Monday is off day\n\n");
- scanf("%d", &day);
- if(day >= 1 && day <= 6)
- {
- done = 1;
- }
- else if(day == 0)
- {
- quit = 1;
- done = 1;
- }
- else
- {
- done = 0;
- }
- }while(!done);
- return day;
- }
- //---------------------------------------------------------------------------
- int get_game(void)
- {
- int game;
- int done = 0;
- do
- {
- clrscr();
- printf("Please select a game:\n");
- printf("1 Badminton\n");
- printf("2 Squash\n");
- printf("3 Tennis\n");
- printf("4 Footsal\n");
- printf("0 to quit\n\n");
- scanf("%d", &game);
- if(game >= 1 && game <= 4)
- {
- done = 1;
- }
- else if(game == 0)
- {
- quit = 1;
- done = 1;
- }
- else
- {
- done = 0;
- }
- }while(!done);
- return game;
- }
- //---------------------------------------------------------------------------
- int get_time(void)
- {
- int time;
- int done = 0;
- do
- {
- clrscr();
- printf("Please select a time slot:\n");
- printf("1 Non Peak Time\n");
- printf("2 Peak Time\n");
- printf("0 to quit\n\n");
- scanf("%d", &time);
- if(time >= 1 && time <= 2)
- {
- done = 1;
- }
- else if(time == 0)
- {
- quit = 1;
- done = 1;
- }
- else
- {
- done = 0;
- }
- }while(!done);
- return time;
- }
- //---------------------------------------------------------------------------
- int book_court(int day, int game, int time)
- {
- int book, c, r;
- print_table(day, game, time);
- printf("Please type in the block number you want to book: \n");
- printf("Key in 0 to go back to DAY selection.\n\n");
- scanf("%d", &book);
- if(book == 0)
- {
- quit = 0;
- }
- else
- {
- c = (book - 1) / col;
- r = (book - 1) % col;
- /// if already booked
- if(booking[day-1][game-1][time-1][c][r] == 'X')
- {
- printf("Sorry, that block already booked\n");
- getch();
- book = -1;
- }
- else
- {
- booking[day-1][game-1][time-1][c][r] = 'X';
- }
- }
- return book;
- }
- //---------------------------------------------------------------------------
- void print_table(int day, int game, int time)
- {
- int head, length, i, j, k;
- /// text mode 的宽度是 80 个字吧?记不清楚了,就当它是80吧。
- char line[80];
- /// 每个格子最宽(根据Header的字数)是 9 个字,+ 左右各一个 space, + 1 个最右边的 | = 12
- char cell[13]; /// + NULL
- char *days[6] = {"Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
- char *games[4] = {"Badminton", "Squash", "Tennis", "Footsal"};
- char *times[2] = {"Non Peak Time", "Peak Time"};
- char *courts[4] = {"| A |",
- "| B |",
- "| C |",
- "| D |"};
- char *hdrs[4] = {"| Court | 7am-9am | 9am-11am | 11am-1pm | 1pm-3pm | 3pm-5pm |",
- "| Court | 8am-10am | 10am-12pm | 12pm-2pm | 2pm-4pm | 4pm-5pm |",
- "| Court | 10am-12pm | 12pm-2pm | 2pm-4pm | 4pm-5pm |",
- "| Court | 5pm-7pm | 7pm-9pm | 9pm-10pm |"};
- /// non peak
- if(time == 1)
- {
- switch(game)
- {
- /// badminton
- case 1: col = 5;
- row = 4;
- head = 0;
- break;
- /// Squash
- case 2: col = 4;
- row = 2;
- head = 1;
- break;
- /// Tennis
- case 3: col = 5;
- row = 3;
- head = 2;
- break;
- case 4: col = 5;
- row = 3;
- head = 0;
- break;
- }
- }
- else if(time == 2)
- {
- /// peak time col is same for all games
- col = 3;
- head = 3;
- switch(game)
- {
- case 1: row = 4;
- break;
- case 2: row = 2;
- break;
- case 3:
- case 4: row = 3;
- break;
- }
- }
- /// col# + 1 court column = (col + 1) * 12 chars
- length = (col + 1) * 12;
- memset(line, '-', length);
- /// put the | at left and right, and the NULL terminator
- line[0] = '|';
- line[length-1] = '|';
- line[length] = NULL;
- /// print the table
- clrscr();
- printf("Day [%s] Game [%s] Time [%s]\n", days[day-1], games[game-1], times[time-1]);
- printf("%s\n", line);
- printf("%s\n", hdrs[head]);
- printf("%s\n", line);
- for(j=0; j<row; j++)
- {
- printf("%s", courts[j]);
- for(k=0; k<col; k++)
- {
- printf(" %2d. %c |", ((j * col) + k + 1), booking[day-1][game-1][time-1][j][k]);
- }
- printf("\n");
- printf("%s\n", line);
- }
- }
- //---------------------------------------------------------------------------
复制代码 要注意得是我用的是 C++ Builder,要在Visual Studio跑的话,你得修改或去除掉某些Header。 |
|
|
|
|
|
|
|
楼主 |
发表于 23-1-2014 05:31 PM
|
显示全部楼层
Wa thank you 你帮我写酱多
我applied你的一点,因为我不想全部一样等下变成抄功课 Lol
帮我看下可以吗?void chooseday()我改成酱,run到选peaktime那边就error了。。。- void chooseday(){
- int done = 0;
- do{
-
- printf("Please select a day\n");
- printf("Note that Monday is off day !\n\n");
- printf("1 Tuesday\n");
- printf("2 Wednesday\n");
- printf("3 Thursday\n");
- printf("4 Friday\n");
- printf("5 Saturday\n");
- printf("6 Sunday\n");
- scanf("%d", &day);
-
- if (day==1)
- {
- dayy = "0";
- done = 1;
- }
- else if (day ==2){
- dayy = "1";
- done = 1;
- }
- else if (day==3){
- dayy = "2";
- done = 1;
- }
- else if (day== 4){
- dayy = "3";
- done = 1;
- }
- else if (day==5){
- dayy = "4";
- done = 1;
- }
- else if (day==6){
- dayy = "5";
- done = 1;
- }
- else
- {
- done = 0;
- }
- } while (done == 0);
- system("cls");
- {
- printf("You have choose %s\nPlease pick a time\n", day);
- printf("1.None Peak time\n");
- printf("2.Peak time\n");
- scanf("%d", &picktime);
- }
- }
复制代码 |
|
|
|
|
|
|
|
发表于 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不对吗?- #include <stdio.h>
- #include<stdlib.h>
- void insertion();
- void deletion();
- void createlist();
- void display();
- int option,element,placefound;
- void menu();
- struct node
- {
- int data;
- struct node *next;
- }*newnode,*list,*prev,*temp,*tmpdisplay;
- void main()
- {
- createlist();
-
- do
- {
- menu();
- switch (option)
- {
- case 1: insertion();break;
- case 2: deletion();break;
- case 3: display();break;
- case 4: break;
- }
- }while (option !=4);
- }
- void createlist()
- {
- list=NULL;
- }
- void menu()
- {
- printf(" linked list\n");
- printf("1-- Insertion\n");
- printf("2-- Deletion\n");
- printf("3-- Display\n");
- printf("select your option\n");
- scanf("%d",&option);
- }
- void insertion()
- {
- newnode=(struct node*) malloc (sizeof(struct node));
-
-
- printf("enter the element:");
- scanf("%d",&element);
- newnode->data=element;
- newnode->next=NULL;
-
- if (list==NULL)
- list=newnode;
- else if(element<list->data)
- {
- newnode->next=list;
- list=newnode;
- }
- else
- {
- <font color="#ffff00" style="background-color: rgb(139, 0, 0);"> temp=list;
- while(temp!=NULL )
- {
- if(element>temp->data)
- {
- prev=temp;
- temp=temp->next;
- }
- else
- break;
- }
- newnode->next=prev->next;
- prev->next=newnode;
- }}
- </font>
- void deletion()
- {
- printf("element to delete?");
- scanf("%d", &element);
- if (list==NULL)
- printf("list is empty");
- else if (element==list->data)
-
- list=list->next;
- else
- {
- temp=list;
- while(temp->data!=element)
- {
- prev=temp;
- temp=temp->next;
- }
- prev->next=prev->next->next;
- }
- }
- void display()
- {
- if (list==NULL)
- printf("\n\nList is empty");
- else
- {
- tmpdisplay=list;
- while(tmpdisplay!=NULL)
- {
- printf("%d\n",tmpdisplay->data);
- tmpdisplay=tmpdisplay->next;
- }
- }
- }
复制代码 |
|
|
|
|
|
|
|
发表于 17-3-2014 07:53 AM
|
显示全部楼层
Chaisoo333 发表于 16-3-2014 11:24 PM
老大,又有东西问你了
insertion 我highlight 起来那段我不明白勒。。。
while(temp!=NULL )[/backcolo ...
因为这个:那段代码的含义就是:walk through the list till it hit the end of the list. 当来到最后一个Node的时候,next是指向NULL的。
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|