前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C代写编程辅导:CS101 Binary Arithmetic

C代写编程辅导:CS101 Binary Arithmetic

原创
作者头像
拓端
发布2022-10-24 21:53:26
2940
发布2022-10-24 21:53:26
举报
文章被收录于专栏:拓端tecdat拓端tecdat

原文链接:tecdat.cn/?p=29620

Requirement

In this Assignment, you should write a program that allows the user to perform simple arithmetic in binary. Upon starting, the program should tell the user that it is a binary math program, along with brief instructions on how to use the program. The program should then enter a loop, where it gives a prompt, such as “input>”. Upon receiving input from the user, the program should process it, report the output result (or error), and loop back to the prompt. This should continue until the user gives the keyphrase to exit the program (keyphrase is your choice, good choices are “quit”, “end”, “exit”, etc.). For example:

代码语言:javascript
复制
Input> 101+1100
10001
Input> 111001-1010
101111
复制代码

Analysis

Binary arithmetic, 也就是二进制算法,是程序设计的基础。本题需要实现一个可交互的程序,根据用户输入,实现二进制算法,如二进制加法、二进制减法等。 本题难度不大,注意输入的数据类型是char,需要拆分后转换为int,以及keyphrase关键响应符号的处理逻辑即可。

Tips

下面是处理用户交互部分的实现

代码语言:javascript
复制
int main(int argc, char *argv[]) {
  char input[100];
  char *keyphrase = "quit";
  char *add = "+";
  char *sub = "-";
  char *mul = "*";
  char *div = "/";
  while (1) {
    printf("input> ");
    scanf("%s", input);
    if (strncmp(input, keyphrase, strlen(keyphrase)) == 0) {
      return 0;
    }
    if (strstr(input, add) != NULL) {
      binary_add(input);
    }
    if (strstr(input, sub) != NULL) {
      binary_sub(input);
    }
    if (strstr(input, mul) != NULL) {
      binary_mul(input);
    }
    if (strstr(input, div) != NULL) {
      binary_div(input);
    }
  }
  return 0;
}

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 原文链接:tecdat.cn/?p=29620
    • Requirement
      • Analysis
        • Tips
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
        http://www.vxiaotou.com