鱼C论坛

 找回密码
 立即注册
查看: 3754|回复: 12

[已解决]C语言

[复制链接]
发表于 2022-12-21 17:09:22 | 显示全部楼层    本楼为最佳答案   
  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. int num;
  4. char fuhao;

  5. void getnum_fuhao() {
  6.         num = 0;
  7.         char temp;
  8.         while(true) {
  9.                 temp = getchar();
  10.                 if('0' <= temp && temp <= '9') {
  11.                         num *= 10;
  12.                         num += (temp - '0');
  13.                 }
  14.                 else if(temp == ' ' || temp == '\n') continue;
  15.                 else break;
  16.         }
  17.         fuhao = temp;
  18. }

  19. double input;
  20. void getinput_fuhao() {
  21.         getnum_fuhao();
  22.         if(fuhao != '.') input = num;
  23.         else {
  24.                 int num_before_point = num;
  25.                 getnum_fuhao();
  26.                 input = num;
  27.                 while(input >= 1) {
  28.                         input /= 10;
  29.                 }
  30.                 input += num_before_point;
  31.         }
  32.         //printf("input:%lf\n", input);
  33. }

  34. int main()
  35. {
  36.         double result;
  37.         char op;
  38.         while(true) {
  39.                 while(true) {
  40.                         getinput_fuhao();
  41.                         //printf("result:%lf\n", result);
  42.                         //printf("op:%c\n", op);
  43.                         switch(op) {
  44.                                 case '+': result += input; break;
  45.                                 case '-': result -= input; break;
  46.                                 case '*': result *= input; break;
  47.                                 case '/':
  48.                                         if(input == 0) printf("除数不能为零!");
  49.                                         else  result /= input;
  50.                         }
  51.                         if(fuhao == '=') {
  52.                                 printf("%lf\n", result);
  53.                                 break;
  54.                         }
  55.                         if(fuhao == 'a' || fuhao == 'A') {
  56.                                 result = 0;
  57.                                 op = '+';
  58.                                 break;
  59.                         }
  60.                         if(fuhao == 'c' || fuhao == 'C') {
  61.                                 system("cls");
  62.                                 result = 0;
  63.                                 op = '+';
  64.                                 break;
  65.                         }
  66.                         op = fuhao;
  67.                 }
  68.         }
  69.        
  70.         return 0;
  71. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-9-24 02:43

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表