XMU C语言程序设计实践(2)

任务一:颠倒的世界

 

小明最近突然喜欢倒着写字,写出来的句子全是颠倒的,也就是把一句话里的字符全都逆序写,譬如“I Love This Game!”,他就偏偏要写成“!emaG sihT evoL I”弄的小华同他Email交流都很难受。请你帮助小华解决这个问题,将输入的字符串全都还原成本来的面目。

 

任务1要求:

利用main函数带参数的功能,编写程序,将命令行中的字符串全部逆序显示出来。例如编写的可执行文件为go.exe 那么运行 go !emaG sihT evoL I 时,显示出I Love This Game!

 

任务二:时间转换

由于工作原因,小明常常需要签署文件日期,但他比较偷懒,都用阿拉伯数字签署,譬如3.8表示三月八日,7.12表示七月十二日。最近公司改组成外资企业,主管要求将所有的日期全部更改为英文表示,一来同国际接轨,二来也不容易篡改,譬如将3.8表示为March the eighth 面对这么多的日期,小明犯愁了,他英文不好,那么多的英文单词他记不住。而且小明还是个马大哈,有时候居然会签出3.32(三月三十二日)这样的日期,为了避免被上司责备,趁着这次机会也赶快一并修改。请你帮助小明完成这个任务。

 

任务2要求:

利用main函数带参数的功能,编写程序,将命令行中的字符串翻译成英文表示,每行显示一个日期,同时需检查日期表示是否有误,如有误,请给出提示。例如:编写的可执行文件为go.exe 那么运行go  3.8  5.5  4.38 时显示

March the eighth

May the fifth

Error

 

任务三:时间转换(续)

小明觉得这么一点点的手工输入还是有点麻烦,他把需要翻译的日期都整理到文本文件in.txt中,请你帮助小明,将其中的内容翻译完毕后保存到文本文件out.txt中,便于他统一处理。

任务3要求:翻译过程同任务2,将结果保存至文件out.txt中

 

第一题:

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<string.h>
 4 #define max(a,b) ((a)>(b)?(a):(b))
 5 #define min(a,b) ((a)<(b)?(a):(b))
 6 #define abs(a) ((a)>0?(a):(-(a)))
 7 #define sqr(a) ((a)*(a))
 8 #define swap(a,b) (a)^=(b),(b)^=(a),(a)^=(b)
 9 #define eps 1e-8
10 #define MAX 0x7f7f7f7f
11 #define N 104
12 int a[N];
13 int main(int argc,char* argv[])
14 {
15     int i,j,k;
16     for(i=argc-1;i>0;i--)
17     {
18         for(j=strlen(argv[i])-1;j>=0;j--)
19             putchar(argv[i][j]);
20         printf(" ");
21     }
22     puts("");
23     return 0;
24 }
25 /*
26 Q:\xxx\111\222\bin\Debug\222 !emaG sihT evoL I
27 */
View Code

第二题:

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<string.h>
 4 #define max(a,b) ((a)>(b)?(a):(b))
 5 #define min(a,b) ((a)<(b)?(a):(b))
 6 #define abs(a) ((a)>0?(a):(-(a)))
 7 #define sqr(a) ((a)*(a))
 8 #define swap(a,b) (a)^=(b),(b)^=(a),(a)^=(b)
 9 #define eps 1e-8
10 #define MAX 0x7f7f7f7f
11 #define N 104
12 int m,d;
13 int mon[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
14 char *M[]={"","January","February","March","April","May","June","July","August","September","October","November","December"};
15 char *D[]={"","first","second","third","fourth","fifth","sixth","seventh","eighth","ninth","tenth","eleventh","twelfth","thirteenth","fourteenth","fifteenth","sixteenth","seventeenth","eighteenth","nineteenth","twentieth","twenty-first","twenty-second","twenty-third","twenty-fourth","twenty-fifth","twenty-sixth","twenty-seventh","twenty-eighth","twenty-ninth","thirtieth","thirty-first"};
16 void work(char s[])
17 {
18     int i;
19     m=d=0;
20     if(s[2]=='.')
21     {
22         m=(s[0]-'0')*10+s[1]-'0';
23         i=3;
24     }
25     else if(s[1]=='.')
26     {
27         m=s[0]-'0';
28         i=2;
29     }
30     for(;i<strlen(s);i++)
31         d=d*10+s[i]-'0';
32 }
33 int main(int argc,char * argv[])
34 {
35     int i,j,k;
36     for(i=1;i<argc;i++)
37     {
38         work(argv[i]);
39         if(d>mon[m] || d<1 || m>12 ||m<1)puts("Error");
40         else printf("%s the %s",*(M+m),*(D+d));
41         puts("");
42     }
43     return 0;
44 }
45 /*
46 Q:\xxx\111\222\bin\Debug\222 3.8 5.5 4.38
47 */
View Code

第三题:

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<string.h>
 4 #define max(a,b) ((a)>(b)?(a):(b))
 5 #define min(a,b) ((a)<(b)?(a):(b))
 6 #define abs(a) ((a)>0?(a):(-(a)))
 7 #define sqr(a) ((a)*(a))
 8 #define swap(a,b) (a)^=(b),(b)^=(a),(a)^=(b)
 9 #define eps 1e-8
10 #define MAX 0x7f7f7f7f
11 #define N 1004
12 int m,d;
13 int mon[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
14 char argv[N];
15 char *M[]={"","January","February","March","April","May","June","July","August","September","October","November","December"};
16 char *D[]={"","first","second","third","fourth","fifth","sixth","seventh","eighth","ninth","tenth","eleventh","twelfth","thirteenth","fourteenth","fifteenth","sixteenth","seventeenth","eighteenth","nineteenth","twentieth","twenty-first","twenty-second","twenty-third","twenty-fourth","twenty-fifth","twenty-sixth","twenty-seventh","twenty-eighth","twenty-ninth","thirtieth","thirty-first"};
17 void work(char s[])
18 {
19     int i;
20     m=d=0;
21     if(s[2]=='.')
22     {
23         m=(s[0]-'0')*10+s[1]-'0';
24         i=3;
25     }
26     else if(s[1]=='.')
27     {
28         m=s[0]-'0';
29         i=2;
30     }
31     for(;i<strlen(s);i++)
32         d=d*10+s[i]-'0';
33 }
34 int main()
35 {
36     FILE *fp1=fopen("Q:\\xxx\\111\\111\\in.txt","r"),*fp=fopen("Q:\\xxx\\111\\111\\out.txt","w");
37     int i,j,k;
38     while(~fscanf(fp1,"%s",argv))
39     {
40         work(argv);
41         if(d>mon[m] || d<1 || m>12 ||m<1)fputs("Error\n",fp);
42         else fprintf(fp,"%s the %s\n",*(M+m),*(D+d));
43     }
44     fclose(fp1);
45     fclose(fp);
46     return 0;
47 }
48 /*
49 Q:\xxx\111\111\bin\Debug\111 3.8 5.5 4.38 13.1 1.32 1.0 2.29 115123 3.31
50 */
View Code

 

posted @ 2017-06-28 17:24  Cool639zhu  阅读(536)  评论(0编辑  收藏  举报