习题笔记 钱能 第4章节

目录

EX0401  //简单 函数计算 pow() sqrt,定义都是double型的 

EX0402 // 无用 

EX0403  计算C1813,避免溢出 方法一如下 方法二如课本,人工去除无用部分

EX0404  浮点数的比较方法,对头文件的注释,精度宽度:cout.precision() or setprecision()

 

EX0405  **浮点数是不能用于位操作的 ,故先把浮点数空间映射成为整型实体

EX0406   (!(n%3)<<2)+(!(n%5)<<1)+(!(n%5)) 精髓在于: 将结果转为二进制编码,并由对应数字和switch操作进行输出。其次,运算符的优先级。

EX0407   代码无意义,掌握这样简约的风格 

EX0408  二进制转换成十进制        strlen()或者str.length() 操作


EX0401  //简单 函数计算 pow() sqrt,定义都是double型的 

#include<iostream>
using namespace std;
#include<cmath>
int main()
{ int x;
cout<<"input x"<<endl;
cin>>x;
double result=sqrt(pow(sin(x),2.5));
cout<<result;
system("pause");
}

EX0402 // 无用 

#include<iostream>
#include<cmath>
using namespace std;

int main()
{
int e=1,f=4,g=2;
 double m=10.5;double n=4.0,k;
 k=(e+f)/g+sqrt(n)*1.2/g+m;
 cout<<k<<endl;
 system("pause");
}

EX0403  计算C1813,避免溢出 方法一如下 方法二如课本,人工去除无用部分

#include<iostream>
#include<cmath>
using namespace std;

long long int get_jiecheng(int x);

int main()
{
long long int t;
t=get_jiecheng(18)/(get_jiecheng(13)*get_jiecheng(5));
cout<<t<<endl;
long int a;
cout<<"long int:"<<sizeof(a)<<endl;
system("pause");

}
long long int get_jiecheng(int x)
{ long long int result=1;
for(int i=1;i<=x;i++)
    result*=i;
cout<<result<<endl;
return result;
}

EX0404  浮点数的比较方法,对头文件的注释,精度宽度:cout.precision() or setprecision()

#include<iostream>
#include<fstream>  //for file"abc.txt"
#include<sstream>
#include<cmath>   //for abs()
using namespace std;
#include<string>
void test(string str,long long int c);
int main()
{const long long int c=20922789888000;

ifstream fin("abc.txt");
string str;
while(getline(fin,str))
    test(str,c);
system("pause");
}
void test(string str,long long int c)
{
long double a,b,t;
istringstream sin (str);
sin>>a;sin>>b;
t=a*b;
cout.precision(9);
if(fabs(t-c)<1e-6) cout<<a<<"     "<<b<<endl;
}
 

 

EX0405  **浮点数是不能用于位操作的 ,故先把浮点数空间映射成为整型实体

#include<iostream>
using namespace std;

int main()

{

const long double x=12345.67891023456;
char *temp = (char *) &x;
int k=1;
for(int i=0;i<5;i++) for(;k<9;k++) cout<<(temp+i)>>k&1;


}

EX0406   (!(n%3)<<2)+(!(n%5)<<1)+(!(n%5)) 精髓在于: 将结果转为二进制编码,并由对应数字和switch操作进行输出。其次,运算符的优先级。

#include<iostream>
using namespace std;
#include<fstream>
#include<sstream>
#include<string>


int main()
{string str;

    ifstream fin("abc.txt");
    int a;int  tag =0;
        while (getline(fin,str))
        {
            for(istringstream sin(str);sin>>a;)
            { if(a%3==0) tag+=1;if(a%5==0)  tag+=2;if(a%7==0) tag+=3;
            switch(tag){
                  case 1:  cout<<"3"<<endl;
                  case 2:   cout<<"5"<<endl;
                  case 3:   cout<<"7"<<endl;
            }}
        }
    system("pause");

}

EX0407   代码无意义,掌握这样简约的风格 

int a=1,b=2,c=3; 

int s,w=0,t=1;

s=(b<c &&c>0? a+b:0);

 

EX0408  二进制转换成十进制        strlen()或者str.length() 操作

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值