分多文件完成的循环计算完整版

空间循环编程实验题目
(1) 1+2+3+4+5……+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! +…,最后一项精度不低于0.000001 (可以商量)
(6) 求 C     (可以商量)
(7) 2*3/4*5*6 + 3*4/5*6*7 - 4*5/6*7*8 + 5*6/7*8*9 -.....。最后一项精度不低于0.000001 (独立完成)
(8) 有一分数序列 1/2,2/3,3/5,5/8,8/13,13/21,…求出这个序列的前200 项之和 (独立完成)
(9) 求圆周率PI: PI/4 = 1- 1/3 + 1/5 -1/7 +.....(独立完成)
(10) 输出 n 行金字塔(独立完成)
*
**
***
****
*****
****
***
**
*
(11) 输出正/反九九乘法表(可以商量)
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
1*4....
……   ……    …… 
1*1=2 1*2=3 1*3=4 1*4 1*5…… 1*9=9
2*2=4  ...
3*3=9...
4*4=16....
(12) 在程序中定义一个变量,赋予1—100的某个数值,要求用户猜这个数字,比较两个数字的大小,把结果提示给用户。直到猜对为止。(独立完成)


//cal.h

#pragma once


#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

//解决error C2011: 'Point' : 'class' type redefinition错误
#ifndef CAL_H //如果没有定义POINT_H这个宏,则编译以下代码
#define CAL_H 0  //定义宏 ,避免此后重复编译


class cal{

public:
string str;//参数,函数需要
int number;

cal();//默认构造函数
cal(const string& );//构造函数,设置初始值
bool is_All_digit(const string& str);
double cal7();//2*3/4*5*6 + 3*4/5*6*7 - 4*5/6*7*8 + 5*6/7*8*9 -.....
double cal8();//1/2,2/3,3/5,5/8,8/13,13/21,…
double cal9();//(9) 求圆周率PI: PI/4 = 1- 1/3 + 1/5 -1/7 +.....
int guess_number(); //根据大小提示猜数字
};

#endif

//show_shape.h

#pragma once
#include "stdafx.h"
#include <iostream>
#include <string>
#include "cal.h"

using namespace std;
class show_shape: public cal{
public:
void show_star(int);
void show_pyramid();//print pyramid
void show_multipletable();//显示乘法表
};

//cal.cpp

#include "stdafx.h"
#include <iostream>
#include <string>
#include "cal.h"
#include "windows.h"//利用时间,设定srand种子,使得产生真正随机数,否则每次都一样
using namespace std;

cal::cal(){
str = 1;
number = 1;
}

cal::cal(const string& a){//从键盘读入数据时
str = a;
}

bool cal::is_All_digit(const string& str){
for (int i = 0; i < str.length(); i++){
if (!isdigit(str[i])){
return false;
}
}
const char* str_char = str.c_str();
cal::number = atoi(str_char);//修改类成员变量
return true;


}

//(7) 2*3/4*5*6 + 3*4/5*6*7 - 4*5/6*7*8 + 5*6/7*8*9 -.....
//精度这个问题,实在是。。。。溢出啊。。。。
//最后一项精度不低于0.000001
double cal::cal7(){
int cal7_1, cal7_2;
double result = 0, item = 0;
int flag;
for (int i = 2;; i++){
cal7_1 = i*(i + 1);
cal7_2 = (i + 2)*(i + 3)*(i + 4);
item = double(cal7_1) / double(cal7_2);
result = result + item;
if (item >= 0.000001){
continue;
}
else{
flag = i;
break;
}


}
// cout << "the calculation ends at " << flag << " * " << flag + 1 << " / " << flag + 2 << " * " << flag + 3 << " * " << flag + 4 << endl;
return result;
}

//(8) 有一分数序列 1/2,2/3,3/5,5/8,8/13,13/21,…求出这个序列的前200 项之和
double cal::cal8(){
double up = 1, down = 2, result = 0, tem = 0;
for (int i = 1; i <= cal::number; i++){
result = result + up / down;
tem = up;
up = down;
down = tem + down;
}
return result;
}

//(9) 求圆周率PI: PI/4 = 1- 1/3 + 1/5 -1/7 +.....
double cal::cal9(){
double term = 1;
double result = 0;
int i = 0;
while (term >= 0.00000001){
term = 1 / ( 2 * double(i) + 1 );
if (i % 2 == 0){
result = result + term;
}
else{
result = result - term;
}
i++;
}
return result;
}

//在程序中定义一个变量,赋予1—100的某个数值,要求用户猜这个数字,比较两个数字的大小,把结果提示给用户。直到猜对为止
int cal::guess_number(){
srand((unsigned)GetCurrentTime());//设置随机种子,使得rand结果为真正随机数,否则每次都一样
int predefined_number = rand() % 100 + 1;
string try_number;//不能为string类赋值0,否则出现严重错误
while (1){
cout << "input the number in your mind: " << endl;
cin >> try_number;
if (!cal::is_All_digit(try_number)){
cout << "input error" << endl;
}
else{
if (cal::number > predefined_number){
cout << "try smaller one " << endl;
}
else if (cal::number < predefined_number){
cout << "try larger one" << endl;
}
else{
cout << "congratulations! The answer is " << cal::number << endl;
break;
}
}
}
return cal::number;
}


//show_shape.cpp

#include "stdafx.h"
#include <iostream>
#include <string>
#include "show_shape.h"
#include <iomanip>//控制对齐
#include <fstream>

using namespace std;

void show_shape::show_star(int star_number){
for (int j = 1; j <= star_number; j++){
cout << "*";
}
cout << endl;

}


void show_shape::show_pyramid(){
for (int i = 1; i <= show_shape::number / 2; i++){
show_shape::show_star(i);
}
if (!show_shape::number % 2 == 0){
show_shape::show_star(show_shape::number / 2 + 1);
}
for (int i = 1; i <= show_shape::number / 2; i++){
show_shape::show_star(show_shape::number / 2 + 1 - i);
}
}

//(11) 输出正/反九九乘法表
void show_shape::show_multipletable(){
ofstream outfile;//创建文件类对象
outfile.open("out.txt");//打开文件
cout.flags(ios::left);//设置左对齐
for (int z = 1; z <= 9; z++){
for (int j = z; j <= 9; j++){
outfile << setw(12 * (z - 1)) << " ";
for (int i = z; i <= j; i++){
outfile << i << " * " << j << " = " << i * j << "  ";
}
outfile << endl;
}
}
outfile.close();//停止编辑文件
}


//main.cpp

#include "stdafx.h"
#include <iostream>
#include <string>
#include "cal.h"
#include "show_shape.h"
//#include "cal.cpp" //不需要包涵cpp文件,编译的时候自然编译
using namespace std;

void main(){
cal mycal;
cout << "2*3/4*5*6 + 3*4/5*6*7 - 4*5/6*7*8 + 5*6/7*8*9 -....." << mycal.cal7() << endl;
cout << "1- 1/3 + 1/5 -1/7 +.....=" << mycal.cal9() << endl;
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 type of calculation you want(1:1/2+2/3+3/5+5/8+8/13+13/21+…; 2: show pyramid; 3: 九九乘法表; 4:猜数字游戏)" << endl;
int type;
cin >> type;
switch (type){
case 1:
{
 cout << "input the number of the sequence" << endl;
 string number;
 cin >> number;
 cal mycal2(number);
 if (mycal2.is_All_digit(number)){
 cout << "1/2+2/3+3/5+5/8+8/13+13/21+…=" << mycal2.cal8() << endl;
 }
 else{
 cout << "input error" << endl;
 break;
 }
}
case 2:
{
 cout << "input the hight of the pyramid" << endl;
 string number;
 cin >> number;
 show_shape show1;
 if (show1.is_All_digit(number)){
 cout << "show pyramid" <<  endl;
 show1.show_pyramid();
 }
 else{
 cout << "input error" << endl;
 }
 break;
}
case 3:
{
 show_shape show2;
 show2.show_multipletable();
 break;
}
case 4:
{
 cal mycal_guess;
 mycal_guess.guess_number();
 break;
}
default:
cout << "input error" << endl;
break;
}
}
else{
cout << "input error" << endl;
}
}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值