POJ 2930 加减乘除

时间限制: 
1000ms
内存限制:  
65536kB
描述
根据输入的运算符对输入的整数进行简单的整数运算。运算符只会是加+、减-、乘*、除/、求余%、阶乘!六个运算符之一。输出运算的结果,如果出现除数为零,则输出“error”,如果求余运算的第二个运算数为0,也输出“error”。
输入
输入为一行。先输入第一个整数,空格输入运算符,然后再空格输入第二个整数,回车结束本次输入。如果运算符为阶乘!符号,则不输入第二个整数,直接回车结束本次输入。
输出
输出为一行。输出对输入的两个(或一个)数,根据输入的运算符计算的结果,或者“error”。
样例输入
12 + 3454 – 253 * 645 / 05 !34 % 0
样例输出
462918error120error
 
(1)、源代码:
#include  
using   namespace   std;
 
int   main(){
                 int   i, n, a, b, sum;
                 char   c;
                
                 while (cin >> a >> c){
                                 switch (c){
                                 case   '*' :cin >> b;
                                                cout << a * b << endl;
                                                 break ;
                                 case   '/' :cin >> b;
                                                 if (0 == b)
                                                                cout <<   "error\n" ;
                                                 else
                                                                cout << a/b << endl;
                                                 break ;
                                 case   '%' :cin >> b;
                                                 if (0 == b)
                                                                cout <<   "error\n" ;
                                                 else
                                                                cout << a%b << endl;
                                                 break ;
                                 case   '+' :cin >> b;
                                                cout << a + b << endl;
                                                 break ;
                                 case   '-' :cin >> b;
                                                cout << a - b << endl;
                                                 break ;
                                 case   '!' :sum = 1;
                                                 for (i = 2; i <= a; i++)
                                                                                 sum *= i;
                                                cout << sum << endl;
                                                 break ;
                                }
                }
      return   0;
}
  
(2)、解题思路:略
(3)、可能出错:略
 
 

转载于:https://www.cnblogs.com/lydf-2012/archive/2012/05/11/2496622.html

你可能感兴趣的:(POJ 2930 加减乘除)