评论

收藏

[C++] 厦大小学期C语言程序设计实践(二)

编程语言 编程语言 发布于:2021-07-11 11:02 | 阅读数:354 | 评论:0

  ​
  所有实践项目的源程序可到我的Github上下载:https://github.com/liulizhi1996/C-Programming-Practice-in-short-semester
  任务一:颠倒的世界
  小明最近突然喜欢倒着写字,写出来的句子全是颠倒的,也就是把一句话里的字符全都逆序写,譬如“I Love This Game!”,他就偏偏要写成“!emaG sihT evoL I”弄的小华同他Email交流都很难受。请你帮助小华解决这个问题,将输入的字符串全都还原成本来的面目。
  任务1要求:
  利用main函数带参数的功能,编写程序,将命令行中的字符串全部逆序显示出来。例如编写的可执行文件为go.exe 那么运行 go !emaG sihT evoL I 时,显示出I Love This Game!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char **argv)
{
  if (argc <= 1)
  {
    perror("usage: ./reverse.out [string]\n");
  }
  int len;
  for (int i = argc-1; i > 0; --i)
  {
  len = (int)strlen(argv[i]);
  for (int j = len-1; j >= 0; --j)
  printf("%c", argv[i][j]);
  printf(" ");
  }
  printf("\n");
  return 0;
}
  任务二:时间转换
  由于工作原因,小明常常需要签署文件日期,但他比较偷懒,都用阿拉伯数字签署,譬如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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int days[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
char english_months[][15] = {"", "January", "February", "March", "April", "May", "June", 
               "July", "August", "September", "October", "November", "December"};
char english_days[][15] = {"", "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"};
int main(int argc, char **argv)
{
  if (argc <= 1)
  {
    perror("usage: ./date.out [date]\n");
  }
  int month, day, is_legal, j;
  for (int i = 1; i < argc; ++i)
  {
    is_legal = 1;
  month = 0;
    for (j = 0; argv[i][j] && argv[i][j] != '.'; ++j)
    {
      if ('0' <= argv[i][j] && argv[i][j] <= '9')
        month = month * 10 + (argv[i][j] - '0');
      else
      {
        is_legal = 0;
        break;
      }
    }
    if (!is_legal)
    {
      printf("Error\n");
      continue;
    }
    day = 0;
    for (j++; argv[i][j]; ++j)
    {
      if ('0' <= argv[i][j] && argv[i][j] <= '9')
        day = day * 10 + (argv[i][j] - '0');
      else
      {
        is_legal = 0;
        break;
      }
    }
    if (!is_legal)
    {
      printf("Error\n");
      continue;
    }
    if (1 <= month && month <= 12)
    {
      if (1 <= day && day <= days[month])
        printf("%s the %s\n", english_months[month], english_days[day]);
      else
      {
        printf("Error\n");
        continue;
      }
    }
    else
    {
      printf("Error\n");
      continue;
    }
  }
  return 0;
}
  任务三:时间转换(续)
  小明觉得这么一点点的手工输入还是有点麻烦,他把需要翻译的日期都整理到文本文件in.txt中,请你帮助小明,将其中的内容翻译完毕后保存到文本文件out.txt中,便于他统一处理。
  任务3要求:翻译过程同任务2,将结果保存至文件out.txt中
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int days[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
char english_months[][15] = {"", "January", "February", "March", "April", "May", "June", 
               "July", "August", "September", "October", "November", "December"};
char english_days[][15] = {"", "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"};
int main(int argc, char **argv)
{
  if (argc <= 1)
  {
    perror("usage: ./datefile.out [date]\n");
  }
  FILE *fp_in = fopen("in.txt", "w");
  FILE *fp_out = fopen("out.txt", "w");
  int month, day, is_legal, j;
  for (int i = 1; i < argc; ++i)
  {
    fprintf(fp_in, "%s\n", argv[i]);
    is_legal = 1;
  month = 0;
    for (j = 0; argv[i][j] && argv[i][j] != '.'; ++j)
    {
      if ('0' <= argv[i][j] && argv[i][j] <= '9')
        month = month * 10 + (argv[i][j] - '0');
      else
      {
        is_legal = 0;
        break;
      }
    }
    if (!is_legal)
    {
      fprintf(fp_out, "Error\n");
      continue;
    }
    day = 0;
    for (j++; argv[i][j]; ++j)
    {
      if ('0' <= argv[i][j] && argv[i][j] <= '9')
        day = day * 10 + (argv[i][j] - '0');
      else
      {
        is_legal = 0;
        break;
      }
    }
    if (!is_legal)
    {
      fprintf(fp_out, "Error\n");
      continue;
    }
    if (1 <= month && month <= 12)
    {
      if (1 <= day && day <= days[month])
        fprintf(fp_out, "%s the %s\n", english_months[month], english_days[day]);
      else
      {
        fprintf(fp_out, "Error\n");
        continue;
      }
    }
    else
    {
      fprintf(fp_out, "Error\n");
      continue;
    }
  }
  fclose(fp_in);
  fclose(fp_out);
  return 0;
}
  ​

  
关注下面的标签,发现更多相似文章