第五个点WA,求助

B2052 简单计算器

YeZhetia @ 2022-05-13 23:14:37

#include<bits/stdc++.h>
#define ll long long
using namespace std;

int read() {
  int x = 0, w = 1;
  char ch = 0;
  while (ch < '0' || ch > '9') {
    if (ch == '-') w = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + (ch - '0');
    ch = getchar();
  }
  return x * w;
}
inline void write(int x) {
  static int sta[35];
  int top = 0;
  do {
    sta[top++] = x % 10, x /= 10;
  } while (x);
  while (top) putchar(sta[--top] + 48);
}

int main(){
    ll x,y;
    char qwq;
    x=read(),y=read();
    scanf("%c",&qwq);
    if(qwq=='+') write(x+y);
    else if(qwq=='-') write(x-y);
    else if(qwq=='*' ) write(x*y);
    else if(qwq=='/'&&y!=0) write(x/y);
    else if(qwq=='/'&&y==0) cout<<"Divided by zero!";
    else cout<<"Invalid operator!";
    return 0;
}

by YeZhetia @ 2022-05-13 23:22:40

把快读改成cin后第六个点报错??


by YeZhetia @ 2022-05-13 23:23:27

开了O2第一个点报错


by rzh123 @ 2022-05-13 23:25:44

这个快输是无符号的,负数就会乱码


by rzh123 @ 2022-05-13 23:32:16

@YeZhetia 这样就过了:

#include<bits/stdc++.h>
#define ll long long
using namespace std;

int read() {
  int x = 0, w = 1;
  char ch = 0;
  while (ch < '0' || ch > '9') {
    if (ch == '-') w = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + (ch - '0');
    ch = getchar();
  }
  return x * w;
}
inline void write(int x) {
  if(x<0){
    putchar('-');
    write(-x);
    return;
  } 
  static int sta[35];
  int top = 0;
  do {
    sta[top++] = x % 10, x /= 10;
  } while (x);
  while (top) putchar(sta[--top] + 48);
}

int main(){
    ll x,y;
    char qwq;
    x=read(),y=read();
    scanf("%c",&qwq);
    if(qwq=='+') write(x+y);
    else if(qwq=='-') write(x-y);
    else if(qwq=='*' ) write(x*y);
    else if(qwq=='/'&&y!=0) write(x/y);
    else if(qwq=='/'&&y==0) cout<<"Divided by zero!";
    else cout<<"Invalid operator!";
    return 0;
}

by YeZhetia @ 2022-05-14 08:58:57

好的,非常感谢您


by wuyinan2026ji7ban @ 2022-08-26 13:30:12

还有更简单的:

#include<iostream>
using namespace std;
int main()
{
    int a,b;
    char c;
    cin>>a>>b>>c;
    switch(c)
    {
        case '+':
            cout<<a+b<<endl;
            break;
        case '-': 
            cout<<a-b<<endl;
            break;
        case '*':
            cout<<a*b<<endl;
            break;
        case '/':
            if(b==0)
            {
                cout<<"Divided by zero!<<endl;
            } else{
                cout<<a/b<<endl;
            }
            break;
        default:
            cout<<"Invalid operator!"<<endl;

    }
    return 0;
} 

|