# 2013级测试赛 - F 播放器

Description

Input

PRE 表示按下了“上一首”按钮。

PLAY x 其中x为一个整数( 0 < x <= n )，表示按下了播放列表中的第x首歌。

NEXT 表示按下了“下一首”按钮。

Output

Sample Input

1
5 10
PRE
NEXT
PLAY 5
NEXT
PLAY 5
PLAY 3
NEXT
PRE
PRE
PRE


Sample Output

1
2
5
5
5
3
4
3
5
2


Hint

这个题也算我对链表的第一次应用，首先摆出开始回馈编译错误的代码

#include <stdio.h>
#include <stdlib.h>

typedef struct
{
int play;
struct lis *next;
struct lis *last;
} lis;

int main()
{
int ii = 0,nn = 0;
int n = 0,m = 0;
int len = 0,mp3 = 0,quan = 0;
char c[10],*p;
lis *head = NULL;
lis *ne,*t;  //NEW

freopen ("1.txt","r",stdin);

scanf ("%d",&nn);
for (ii = 0; ii < nn; ii++)
{
head = malloc (sizeof (lis) * 1);
ne->play = 1;
ne->next = NULL;
ne->last = NULL;

scanf ("%d %d",&n,&m);
getchar ();
while(m--)
{
gets (c);
p = c;
len = 0;
while (*p != '\0')
{
len++;
p++;
}

if(len >= 6)
{
p--;
quan = 1;
mp3 = 0;
while (*p != ' ')
{
mp3 += ((int)*p - 48) * quan;
quan *= 10;
p--;
}

if (mp3 != t->play)
{
t = malloc (sizeof (lis) * 1);
ne->next = t;
t->last = ne;
ne = ne->next;
ne->play = mp3;
ne->next = NULL;
}
}
else if (len == 4)
{
if (ne->play < n)
{
t = malloc (sizeof (lis) * 1);
t->play = ne->play + 1;
ne->next = t;
t->last = ne;
ne = ne->next;
ne->next = NULL;
}
}
else if (len == 3)
{
if (ne != head)
{
free (ne);
ne = ne->last;
ne->next = NULL;
}
}
printf ("%d\n",ne->play);
}
}

return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct lis
{
int play;
struct lis *next;
struct lis *last;
};

int main()
{
int ii = 0,nn = 0;
int n = 0,m = 0;
int len = 0,mp3 = 0,quan = 0;
char c[10],*p;
struct lis *head = NULL;
struct lis *ne,*t;  //NEW

//freopen ("1.txt","r",stdin);

scanf ("%d",&nn);
for (ii = 0; ii < nn; ii++)
{
head = (struct lis *)malloc (sizeof (struct lis) * 1);
ne->play = 1;
ne->next = NULL;
ne->last = NULL;

t = NULL;//将t指空

scanf("%d %d%*c",&n,&m);
//getchar ();
while(m--)
{
gets (c);
p = c;
len = 0;
//这里可以用 strlen()
//或者换一个思路 用strcmp()
//在课本上貌似都有 可以去看一下
while (*p != '\0')
{
len++;
p++;
}
//if(strcmp("PLAY",c) == 0)
if(len >= 6)
{
p--;
quan = 1;
mp3 = 0;
while (*p != ' ')
{
mp3 += ((int)*p - 48) * quan;
quan *= 10;
p--;
}
//scanf("%d",&mp3);
if (mp3 > n)
mp3 = n;

//printf("mp3 = %d\n",mp3);

//此处t有可能没有分配内存
//比如第一组输入即为 PLAY 5
//if (mp3 != t->play)
if(mp3 != ne->play)//逻辑或与 的 短路原则
{
t = (struct lis *)malloc (sizeof (struct lis) * 1);
ne->next = t;
t->last = ne;
ne = ne->next;
ne->play = mp3;
ne->next = NULL;
}
}
else if (len == 4 || c[0] == 'N')
//else if(strcmp("NEXT",c) == 0)
{
if (ne->play < n)
{
t = (struct lis *)malloc (sizeof (struct lis) * 1);
t->play = ne->play + 1;
ne->next = t;
t->last = ne;
ne = ne->next;
ne->next = NULL;
}
}
else if (len == 3 || c[0] == 'P')
// else if(strcmp("PRE",c) == 0)
{
if (ne != head)
{
t = ne;
ne = ne->last;
free (t);
ne->next = NULL;
}
}
printf ("%d\n",ne->play);
// printf ("\t%#x\n",&ne);
}
}
return 0;
}


#include <stdio.h>
#include <stdlib.h>

struct lis
{
int play;
struct lis *next;
struct lis *last;
};

int main()
{
int ii = 0,nn = 0;
int n = 0,m = 0;
int len = 0,mp3 = 0,quan = 0;
char c[10],*p;
struct lis *head = NULL;
struct lis *ne,*t;  //NEW

//freopen ("1.txt","r",stdin);

scanf ("%d",&nn);
for (ii = 0; ii < nn; ii++)
{
head = (struct lis *)malloc (sizeof (struct lis) * 1);
ne->play = 1;
ne->next = NULL;
ne->last = NULL;
//第一个错误没有把t初始化

scanf ("%d %d",&n,&m);
getchar ();
while(m--)
{
gets (c);
p = c;
len = 0;
while (*p != '\0')
{
len++;
p++;
}

if(len >= 6)
{
p--;
quan = 1;
mp3 = 0;
while (*p != ' ')
{
mp3 += ((int)*p - 48) * quan;
quan *= 10;
p--;
}
if (mp3 > n)
mp3 = n;

if (mp3 != ne->play)
{
t = (struct lis *)malloc (sizeof (struct lis) * 1);
ne->next = t;
t->last = ne;
ne = ne->next;
ne->play = mp3;
ne->next = NULL;
}
}
else if (len == 4)
{
if (ne->play < n)
{
t = (struct lis *)malloc (sizeof (struct lis) * 1);
t->play = ne->play + 1;
ne->next = t;
t->last = ne;
ne = ne->next;
ne->next = NULL;
}
}
else if (len == 3)
{
if (ne != head)
{
t = ne;
ne = ne->last;
free (t);
ne->next = NULL;
}
}
printf ("%d\n",ne->play);
}
}

return 0;
}


• 本文已收录于以下专栏：

## 嵌入式39.300.300F.800系列-MP4播放器

• 2013年12月11日 09:55
• 1.95MB
• 下载

## 基于STC11F04E的蜂鸣器音乐播放器

• 2017年06月25日 02:18
• 44.43MB
• 下载

## STM32F1 做的MP3播放器

• 2017年06月09日 08:10
• 8.76MB
• 下载

## s w f播放器

• 2013年10月07日 16:24
• 1.84MB
• 下载

## SD卡MP3播放器源代码-加入所有控制按键STM32F130ZET6+znFAT

• 2015年11月24日 09:26
• 1.47MB
• 下载

## 基于msp430f149做的mp3播放器

• 2010年10月24日 13:05
• 184KB
• 下载

## Android 播放器外挂字幕支持测试

举报原因： 您举报文章：2013级测试赛 - F 播放器 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)