POJ 2930 加减乘除

时间限制: 
1000ms
内存限制:  
65536kB
描述
根据输入的运算符对输入的整数进行简单的整数运算。运算符只会是加+、减-、乘*、除/、求余%、阶乘!六个运算符之一。输出运算的结果,如果出现除数为零,则输出“error”,如果求余运算的第二个运算数为0,也输出“error”。
输入
输入为一行。先输入第一个整数,空格输入运算符,然后再空格输入第二个整数,回车结束本次输入。如果运算符为阶乘!符号,则不输入第二个整数,直接回车结束本次输入。
输出
输出为一行。输出对输入的两个(或一个)数,根据输入的运算符计算的结果,或者“error”。
样例输入
12 + 3454 – 253 * 645 / 05 !34 % 0
样例输出
462918error120error
 
(1)、源代码:
#include   <iostream>
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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值