/********课本例题 1 **********/
#include<iostream>
using namespace std;
int main()
{
cout<<"number of bytes in int is:"<<sizeof(int)<<endl;
cout<<"number of bytes in long int is:"<<sizeof(long)<<endl;
cout<<"number of bytes in short int is:"<<sizeof(short)<<endl;
return 0;
}
/********改写例题1 **********/
#include<iostream>
using namespace std;
int main()
{
cout<<"number of bytes in float is:"<<sizeof(float)<<endl;
cout<<"number of bytes in double is:"<<sizeof(double)<<endl;
cout<<"number of bytes in long double int is:"<<sizeof(long double)<<endl;
return 0;
}
/********改写例题2 **********/
#include<iostream>
using namespace std;
int main()
{
cout<<"number of bytes in signed is:"<<sizeof(signed)<<endl;
cout<<"number of bytes in unsigned is:"<<sizeof(unsigned)<<endl;
cout<<"number of bytes in long double int is:"<<sizeof(double)<<endl;
cout<<"number of bytes in unsingned long int is:"<<sizeof(unsigned long int)<<endl;
return 0;
}
/********课本例题2.1 布尔类型 **********/
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
bool flag = true;
cout<<flag<<endl;
cout<<boolalpha<<flag<<endl;
cout<<flag + 5<<endl;
flag = 0;
cout<<"执行语句 flag = 0;后flag的值为:"<<boolalpha<<flag<<endl;
flag = 0.0;
cout<<"执行语句 flag = 0;后flag的值"<<boolalpha<<flag<<endl;
return 0;
}
/********改写 1 **********/
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
bool flag = false;
cout<<flag<<endl;
cout<<boolalpha<<flag<<endl;
cout<<flag + 5<<endl;
flag = 0;
cout<<"执行语句 flag = 0;后flag的值为:"<<boolalpha<<flag<<endl;
flag = 0.0;
cout<<"执行语句 flag = 0.0;后flag的值为:"<<boolalpha<<flag<<endl;
return 0;
}
/********改写 2 **********/
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
bool flag = false;
cout<<flag<<endl;
cout<<boolalpha<<flag<<endl;
cout<<flag + 5<<endl;
flag = 0;
cout<<"执行语句 flag = 0;后flag的值为:"<<boolalpha<<flag<<endl;
flag = 8;//整型常量且非0
cout<<"执行语句 flag = 8;后flag的值为:"<<boolalpha<<flag<<endl;
return 0;
}
/********课本例题2.2 赋值语句 **********/
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int a, b, c, d;
a = 4;
b = a;
c = d= 6;
c *= a;
d %= a + b;
cout<<"a = "<<a<<endl;
cout<<"b = "<<b<<endl;
cout<<"c = "<<c<<endl;
cout<<"d = "<<d<<endl;
return 0;
}
/********改写 1 **********/
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int a, b, c, d;
a = 4;
b = a;
c = d= 6;
c %= a;
d *= a + b;
cout<<"a = "<<a<<endl;
cout<<"b = "<<b<<endl;
cout<<"c = "<<c<<endl;
cout<<"d = "<<d<<endl;
return 0;
}
/********课后例题2.3 **********/
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
short i, j, m, n;
i = 1000;
j = 1000;
m = i + j;
n = i * j;
cout<<"m = "<<m<<endl;
cout<<"n = "<<n<<endl;
return 0;
}
/********改写1 **********/
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
short i, j, m, n;
i = 1000;
j = 100;m = i + j;n = i * j;cout<<"m = "<<m<<endl;cout<<"n = "<<n<<endl;//未溢出<但是为神马显示-31027?求解释啊,老师!!!>return 0;}
/********改写2 **********/
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
short i, j, m, n;
i = 1000;
j = 10;
m = i * j;
n = i / j;
cout<<"m = "<<m<<endl;
cout<<"n = "<<n<<endl;
}
/********课后例题2.4**********/
#include <iostream>
using namespace std;
int main()
{
int i = 6, j, k, temp;//temp 是神马意思来?
j = ++i;
k = i++;
++i = 1;
cout<<"i = "<<i<<endl;
cout<<"j = "<<j<<endl;
cout<<"k = "<<k<<endl;
return 0;
}
/********课后例题2.5**********/
#include <iostream>
using namespace std;
int main()
{
char ch;
cout<<"please input a character:";
cin>>ch;
ch = ch>= 'a'&&ch <='z'?ch -'a' + 'A' : ch;
cout<<"The result is:"<<ch<<endl;
return 0;
}
/********课后例题2.7**********/
#include <iostream>
using namespace std;
int main()
{
int ab, ac;
double b = 3.14;
char c ='A';
ab = int(b);
ac = int(c);
cout<<"b = "<<b<<endl;
cout<<"ab = "<<ab<<endl;
cout<<"c = "<<c<<endl;
cout<<"ac = "<<ac<<endl;
return 0;
}
/**************任意给三边求周长l 以及面积 s*************/
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
int a, b, c, l, p, s;
cout<<"请输入a的长度:"<<endl;
cin>>a;
cout<<"请输入b的长度:"<<endl;
cin>>b;
cout<<"请输入c的长度:"<<endl;
cin>>c;
l = a + b + c;
l /= 2;
p = l;
s = sqrt(p*(p-a)*(p-b)*(p-c));
cout<<"l = "<<l<<endl;
cout<<"s = "<<s<<endl;
return 0;
}
/**********课后题目3(1)************/
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
int e, f, g;
cout<<"请输入e的值:"<<endl;
cin>>e;
cout<<"请输入f的值:"<<endl;
cin>>f;
cout<<"请输入g值:"<<endl;
cin>>g;
float m, n, k;
cout<<"请输入m的值:"<<endl;
cin>>m;
cout<<"请输入n的值:"<<endl;
cin>>n;
k = (e + f) / g + sqrt ((double)n) * 1.2 / g + m;
cout<<"k = "<<k<<endl;
return 0;
}
/**********课后题目3(1)************/
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
int e = 1, f = 4, g = 2;
float m = 10.5, n = 4.0, k;
k = (e + f) / g + sqrt ((double)n) * 1.2 / g + m;
cout<<"k = "<<k<<endl;
return 0;
}
/**********计算一元二次方程************/
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
int a, b, c, m, A, B, D;
cout<<" 请输入a: "<<endl;
cin>>a;
cout<<" 请输入b: "<<endl;
cin>>b;
cout<< "请输入c: " <<endl;
cin>>c;
m = b * b - 4 * a * c;
A = ( -b + sqrt( m ) )/( 2 * a );
B = ( -b - sqrt( m ) )/( 2 * a );
if (m >= 0)
{
if (m == 0)
cout<<"A = "<<A<<endl;
else
cout<<"A = "<<A<<endl;
cout<<"B = "<<B<<endl;
}
else
cout<<"D = 尼玛!!!无解 "<<D<<endl;
return 0;
}/**********计算一元二次方程************/
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
float a, b, c, m, A, B, D, e, f;
cout<<" 请输入a: "<<endl;
cin>>a;
cout<<" 请输入b: "<<endl;
cin>>b;
cout<< "请输入c: " <<endl;
cin>>c;
m = b * b - 4 * a * c;
A = ( -b + sqrt( m ) )/( 2 * a );
B = ( -b - sqrt( m ) )/( 2 * a );
e = -b / ( 2 * a );
f = sqrt(-m )/( 2 * a );
if (m >= 0)
{
if (m == 0)
cout<<"A = "<<A<<endl;
else
cout<<"A = "<<A<<endl;
cout<<"B = "<<B<<endl;
}
else
cout<<"A = "<<e<<'+'<<f<<" i"<<endl;
cout<<"B = "<<e<<'-'<<f<<" i"<<endl;
return 0;
}