用C例如將日期格式轉換成英語可讀的格式:14/08/1994應該打印爲14個月1900和90只four.I已經寫代碼一天,一個月一年,如果以同樣的方式寫的代碼會變得冗長我怎麼可以把日期爲可讀格式英語用C
#include<stdio.h>
#include<conio.h>
int main()
{
char *str,case_ch;
int lv=0;
str=(char *)malloc(11*sizeof(char));
printf("enter the date in this format dd/mm/yyyy");
scanf("%s",str);
for(lv=0;str[lv];lv++)
{
if(str[lv]=='0')
{
switch(str[lv+1])
{
case '1':
printf("first");
break;
case '2':
printf("second ");
break;
case '3':
printf("third ");
break;
case '4':
printf("fourth ");
break;
case '5':
printf("fifth ");
break;
case '6':
printf("sixth ");
break;
case '7':
printf("seventh ");
break;
case '8':
printf("eigth ");
break;
case '9':
printf("nineth ");
break;
default:
printf("input wrong");
break;
}
break;
}
else if(str[lv]=='1')
{
switch(str[lv+1])
{
case '0':
printf("tength ");
break;
case '1':
printf("eleventh ");
break;
case '2':
printf("twelevth ");
break;
case '3':
printf("thiteenth ");
break;
case '4':
printf("fourteenth ");
break;
case '5':
printf("fifteenth ");
break;
case '6':
printf("sixteenth ");
break;
case '7':
printf("seventeenth ");
break;
case '8':
printf("eigteenth ");
break;
case '9':
printf("nineteenth ");
break;
default:
printf("input wrong");
break;
}
break;
}
else if(str[lv]=='2')
{
switch(str[lv+1])
{
case '0':
printf("twenteeth ");
break;
case '1':
printf("twenty first");
break;
case '2':
printf("twenty second ");
break;
case '3':
printf("twenty thrid ");
break;
case '4':
printf("twenty fourth ");
break;
case '5':
printf("twenty fifth ");
break;
case '6':
printf("twenty sixth ");
break;
case '7':
printf("twenty seventh ");
break;
case '8':
printf("twenty eighth");
break;
case '9':
printf("twety ningth");
break;
default:
printf("input wrong");
break;
}
break;
}
else if(str[lv]=='3')
{
switch(str[lv+1])
{
case '0':
printf("thrity ");
break;
case '1':
printf("thirty first");
break;
default:
printf("invalid input");
break;
}
break;
}
else
{
printf("invalid input");
break;
}
}
for(lv=3;str[lv];lv++)
{
if(str[lv]=='0')
{
switch(str[lv+1])
{
case '1':
printf("jan");
break;
case '2':
printf("Feb");
break;
case '3':
printf("March ");
break;
case '4':
printf("April");
break;
case '5':
printf("May ");
break;
case '6':
printf("june ");
break;
case '7':
printf("july ");
break;
case '8':
printf("august");
break;
case '9':
printf("september ");
break;
default:
printf("input wrong");
break;
}
break;
}
else if(str[lv]=='1')
{
switch(str[lv+1])
{
case '0':
printf("october");
break;
case '1':
printf("november");
break;
case '2':
printf("december");
break;
default:
printf("invalid input");
break;
}
break;
}
else
{
printf("invalid input");
break;
}
}
/*how to write code for year if i write in the above manner the code will be tooo lenghty*/
}
如果我一直在設置這個功課,我可能會通過看'的sscanf()開始'讀取日期字符串成三個'int's。 – TripeHound
你需要嘗試一些東西,而不是尋求幫助沒有人會爲你寫代碼! – Engine
我試過了,但它變得太冗長了代碼 – user3165666