您的位置:首页 > 编程语言 > C语言/C++

PAT考试乙级1044(C语言实现) (用到了strstr())

2017-11-25 12:47 375 查看
#include<stdio.h>
#include<string.h>
typedef struct tempString
{
char *mars;
}tempString;
int main(){
int n,i,j,num=0,len;
char temp[10];
tempString low[13]={"tret", "jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"};
tempString high[13]={0,"tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"};
scanf("%d",&n);
gets(temp);
for(i=0;i<n;i++){
gets(temp);
num=0;
len=strlen(temp);
if(temp[0]>='0'&&temp[0]<='9'){//数字
for(j=0;j<len;j++){
num=num*10+temp[j]-'0';
}
if(num<13)
printf("%s\n",low[num]);
else{
if(num%13==0) printf("%s\n",high[num/13]);
else printf("%s %s\n",high[num/13],low[num%13]);
}
}else{//火星数字
for(j=1;j<13;j++){
if(strstr(temp,high[j].mars)!=NULL){
num=num+j*13;
break;
}
}
for(j=0;j<13;j++){
if(strstr(temp,low[j].mars)!=NULL){
num=num+j;
break;
}
}
printf("%d\n",num);
}
}
return 0;
}


总结

1、出现格式错误,发现是少了一个换行。

2、出现段错误,即数组越界,后来把高位第一个个改为0,在判断过程中简化很多,段错误也没有再出现了。

3、第一个亮点:由于C语言中没有string类型,所以创建一个struct当作string类型,这样就和c++一样了。

4、第二个亮点:在把火星数字转化成地球数字时,读取火星数字字符串时使用了strstr()函数,来判断输入字符是否包含火星字符串。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c语言 pat