空间循环编程(1)

 

// 计算(1).1+2+3+...+n

// 计算(2).1 - 1/2 + 1/3 - 1/4 + 1/5 - 1/6 …… 1/n

// 计算(3)1!+ 2!+ 3!+ 4!+ …… + n!

//计算(4)1! + 3! - 5! + 7! - 9! ……(2*n+1)!

///计算(5)sin(x)=x/1! - x3/3! + x5/5! -x7/7! +…

//问题:大阶乘计算,溢出处理

#include "stdafx.h"
#include <iostream>
#include <ctype.h>//用于判断字符输入是否为数字
#include <string>// 而不是string.h,否则出现cin>>string错误,no operator
#include <stdlib.h>// atof
#include <math.h>//幂运算
using namespace std;

int flag=0; // 全局变量,所有函数均能使用,修改

bool isAllDigit_1(const string& str)//逐个比较字符
{
int i;
for (i = 0; i != str.length(); i++)
{
if (!isdigit(str[i]))
{
return false;
}
}
return true;
}

void showError(int type){
switch (type){
case 1:
cout << "wrong input" << endl;
break;
case 2:
cout << "cal1 out of range" << endl;
break;
case 3:
cout << "cal2 out of range" << endl;
break;
case 4:
cout << "cal3 out of range" << endl;
break;
case 5:
cout << "cal4 out of range" << endl;
break;
case 6:
cout << "cal5_1 out of range" << endl;
break;
case 7:
cout << "cal5 out of range" << endl;
break;
}
}

int additerator1(int fraction){//1+2+3+...+n
//递归(自己调用自己)
int result;
if (fraction == 1){
result = 1;
}
else{
result = fraction+additerator1(fraction - 1);
if (result < 0){
flag = 2;
throw "out of range";
}
}
return result;
}
int additerator2(int fraction){//1+2+3+...+n
//迭代(A不断的调用B)
int sum = 0;
for (int i = 1; i <= fraction; i++){
sum = sum + i;
if (sum < 0){
flag = 2;
throw "out of range";
}
}
return sum;
}

double cal2(int fraction){ //1 - 1/2 + 1/3 - 1/4 + 1/5 - 1/6 …… 1/n
/ /整型和整型运算结果为整型。如2/3等于0;5/2等于2
double result;
if (fraction == 1){
result = 1;
}
else if(fraction%2==0){//偶数
result = cal2(fraction - 1) - (double(1) / double(fraction));
if (result < 0){
flag = 3;
throw "out of range";
}
}
else{
result = cal2(fraction - 1) + (double(1) / double(fraction));
if (result < 0){
flag = 3;
throw "out of range";
}
}
return result;
}

int cal3(int fraction){// 1!+ 2!+ 3!+ 4!+ …… + n!
//阶乘溢出
//递归函数中想要退出,不能一次性退出,只能逐次退出
int result;
if (fraction == 1){
result = 1;
}
else if (fraction == 2){
result = 1 + 1 * 2;
}
else{
result = fraction*(cal3(fraction-1) - cal3(fraction - 2))+cal3(fraction-1);
if (result < 0){
flag = 4;
throw "out of range";
}
}
return result;
}

int cal4(int fraction){//1! + 3! - 5! + 7! - 9! ……(2*n+1)!
//阶乘溢出
int result;
if (fraction == 1){
result = 1;
}
else if (fraction == 2){
result = 1 + 1 * 2 * 3;
}
else{
if (fraction % 2 == 0){//2,4,6.....->3!,7!....
result = cal4(fraction - 1) - (2 * fraction - 1)*(2 * fraction - 2)*(cal4(fraction - 1) - cal4(fraction - 2)); //注意这里面的加减号
}
else{
result = cal4(fraction - 1) - (2 * fraction - 1)*(2 * fraction - 2)*(cal4(fraction - 1) - cal4(fraction - 2));
if (result < 0){
flag = 5;
throw "out of range";
}
}
}
return result;
}

int cal5_1(int i){//计算1!,2!,3!等
//阶乘溢出
int result;
if (i == 1){
result = 1;
}
else{
result = i*cal5_1(i - 1);
if (result < 0){
flag = 6;
throw "out of range";
}
}
return result;
}

double cal5(double fraction){//sin(x)=x/1! - x3/3! + x5/5! -x7/7! +…
//阶乘溢出
double pow_cal;
double result = 0.0;
int under;
double term = 0;
for (int i = 1;; i = i + 2){
pow_cal = pow(fraction, double(i));
under = cal5_1(i);
if (pow_cal < 0){
flag = 7;
throw "out of range";
}
if (under < 0){
flag = 7;
throw "out of range";
}
term = pow_cal / double(under);
cout << "term=" << term<<endl;
if ((i / 2) % 2 == 1){
result = result - term;
}
else{
result = result + term;
}
if (term < 0.000001){
break;
}
}
return result;
}

void main()
{
//char fraction;  //isdigit是对输入的字符作判断,而你定义的i为整形,isdigit就根据你输入的整数所对应的字符作判断,如果输入的整数不是数字字符所的ASCII码,就是输出0 
//所以这里的fraction要定义为char以用于下面的isdigit(fraction)
// 但isdigit只能用于判断char型,即输入两位数是不能用,即不能区分2d和20
string fraction;
char judge;  
while (1){
cout << "wanna continue?(Y or N)" << endl;
cin >> judge;
if (judge == 'N'){// ''和""有区别,用双引号表示字符串,单引号表示字符。字符串里,哪怕你只放了一个字母,事实上,系统会自动给它加一个\0。表示结束。也就是说,字符串比你看到的多占一个位置。
break;
}
else if(judge == 'Y'){
cout << "input the base number: " << endl;
cin>>fraction;
if (isAllDigit_1(fraction)){

const char *fraction_char = fraction.c_str(); 字符串的表示在C++里面有C风格字符串和string类2种。在系统内部通常都储存为一个字符数组,C风格字符串形式表示时通常是用char*类型指针操纵,string形式表示是用string类的对象操纵。
//如果下面直接用fraction会产生不能使用atoi的后果
int fraction_int = atoi(fraction_char);
int fraction_double = atof(fraction_char);

int cal1_result = additerator1(fraction_int);
int cal1_1_result = additerator2(fraction_int);
double cal2_result = cal3(fraction_int);
int cal3_result=cal3(fraction_int);
int cal4_result = cal4(fraction_int);
int cal5_1_result = cal5_1(fraction_int);
double cal5_result = cal5(fraction_int);

if (flag == 0){//判断唔溢出错误后输出,但问题是递归中溢出运算已经进行了,浪费资源,我觉得不如直接在子程序中用throw抛出错误
cout << "1+2+3+...+n= " << cal1_result << endl;
cout << "1+2+3+...+n= " << cal1_1_result << endl;
cout << "1 - 1/2 + 1/3 - 1/4 + 1/5 - 1/6 …… 1/n= " << cal2_result << endl;
cout << "1!+ 2!+ 3!+ 4!+ …… + n!= " << cal3_result << endl;
cout << "1! + 3! - 5! + 7! - 9! ……(2*n+1)!= " << cal4_result << endl;
cout << "1!,2!,3!= " << cal5_1_result  << endl;
cout << "sin(x)=x/1! - x3/3! + x5/5! -x7/7! +…= " << cal5_result << endl;
/*cout << "calculate" << endl;*/
}
else{
showError(flag);
flag = 0;//显示完错误信息后,还原flag,以保证之后的计算正常
}
}
else{
showError(1);
continue;
}
}
else{
showError(1);
continue;
}
}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值