c++编程: 输入英文的星期几,再星期表中查找后输出对应的数字.

输入英文的星期几,再星期表中查找后输出对应的数字。具体的算法是:若发现再表中某项相同,则输出该字符串再表中的位置(序号);若查到表尾仍不相同,则输出错误信息。
星期表内容如下:
0 sunday
1 monday
2 tuesday
3 wednesday
4 thursday
5 friday
6 saturday
程序中采用一个二维字符数组来存放星期表。数字定义如下,每个字符串所对应的数字即为该数组所在行的行号:
char week_day[][10]={"sunday","monday","tuesday","wednesday","thursday","friday","saturday"};
请根据上述数组定义完成程序。
注意:不能用strcmp函数!!!

帮你写了一个,主要思想是:
1.先比较字符串的长度是否相等if(strlen(day) == strlen(week_day[i]))
2.若相当,则继续比较每个字符是否相等
#include <iostream>
#include <string>
using namespace std;
char week_day[][10]={"sunday","monday","tuesday","wednesday","thursday","friday","saturday"};
int ChoseDay(char day[])
{
int i=0,j=0,count=-1;
for(i=0;i<7;i++)
{
if(strlen(day) == strlen(week_day[i]))
{
for(j=0;j < strlen(day);j++)
{
if(week_day[i][j] !=day[j])
{
break;
}
}
if(j == strlen(day))
{
count =i;
break;
}
}
}
if(count ==-1)
return -1;
else
return count;
}

int main()
{
char week[10];
int result;
while(1)
{
cout<<"输入英文的星期:"<<endl;
cin>>week;
result = ChoseDay(week);
if(result != -1)
{
cout<<result<<endl;
}
else
{
cout<<"输入信息有错误"<<endl;
}
}
return 0;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2018-04-24
不能用strcmp你就写一个函数一个一个字符去比较啊、、、
相似回答