思路分析
我们考虑两个数组
1. 一个字符数组,用于存放表情字符。
2. 一个指针数组,指向每一个表情在字符数组中的位置。
我们的目标是将表情按手,眼,嘴分类,之后构造好指针数组,使得其中每一个元素都指向对应的元素,最后获取输入的序号输出对应的表情
实现方法
首先我们需要一个读取表情并且构造指针的函数,这个函数应该具有如下功能
- 仅读入表情,也即仅读入方括号内的字符,括号及括号外的字符忽略
- 每个表情的结尾应该有分隔符
- 在表情输入完毕之后,对应的指针数组也构造完毕
- 能返回一个值表明该类表情有多少个
为了之后输出的方便,我们这里采用\0
作为表情之间的分割符,这 样我们可以将每个表情看作一个字符串输出。
我们在实现过程中需要注意字符数组的长度以及关于不存在的序号的判定
代码实现
#include <stdio.h>
#define MAXLINE 60
#define ON 1
#define OFF 0
int getin(char *focus[],char save[]); //读入表情,构造指针数组,以及返回表情个数
void printexp(char *first[], int target);
int main(){
int lh, le, m, re, rh; //分别为左手,左眼,嘴,右手,右眼
int eyemax, handmax, monthmax; //最大序号
char *hand[10],*eye[10],*month[10]; //指针数组
char handstore[MAXLINE],eyestore[MAXLINE],monthstore[MAXLINE]; //字符数组
handmax = getin(hand,handstore);
eyemax = getin(eye,eyestore);
monthmax = getin(month,monthstore);
int repeat,ri;
scanf("%d",&repeat);
for(ri=1;ri<=repeat;ri++){
scanf("%d%d%d%d%d",&lh, &le, &m, &re, &rh);
if(lh <= handmax && rh <= handmax &&
le <= eyemax && re <= eyemax &&
m <= monthmax
&& lh >= 1 && rh >= 1 &&
le >= 1 && re >= 1 &&
m >= 1) // 这里注意不仅要考虑给出序号过大,也要考虑过小的情况
{
printexp(hand, lh);
printf("(");
printexp(eye, le);
printexp(month, m);
printexp(eye, re);
printf(")");
printexp(hand, rh);
printf("\n");
} // 按格式输出
else
printf("Are you kidding me? @\\/@\n");
}
}
int getin(char *focus[],char save[]){
int state;
char *p,ch;
p = save;
int i = 0;
focus[0]=p;
while((ch = getchar()) != '\n'){
if(ch == '[')
state = ON; //开始读入字符
else if(ch == ']' && state == ON){
*p++ = 0; //每个表情末尾加上结束符号
focus[++i] = p; //构造指针,使其指向表情开头
state = OFF;
}
else if(state == ON)
*p++ = ch;
}
return i;
}
void printexp(char *first[], int target){
printf("%s", first[target-1]);
}