C++程序设计(第二版)钱能-第3章--数据类型

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

第3章数据类型
3.9 练习3(Exercises 3)
1.模仿程序f030
2.cpp,打印整数-1234567的二进制位码。

解答:
#include <iostream>
using namespace std;
int main()
{
long int l = -1234567;
int* pa = (int*) &l;
for(int i = 31; i >= 0; i--)
cout << (*pa>>i & 1) << (i == 31 || i == 23 ? "-":"");
cout << endl;
return 0;
}
2.整型分long int、int、char、bool,浮点数分float、double、long double,试分别输出各类型的字节长度和位长,输出形式如:
long int: 4 byte 32 bits
解答:
#include <iostream>
using namespace std;
int main()
{
long int a = 1;
int b = 2;
char c = 'A';
bool d = 0;
float e = 3;
double f = 4;
long double g = 5;
cout << "long int:" << sizeof(a) << " byte " << 8*sizeof(a) << " bits" << endl;
cout << "int:" << sizeof(b) << " byte " << 8*sizeof(b) << " bits" << endl; cout << "char:" << sizeof(c) << " byte " << 8*sizeof(c) << " bits" << endl;
cout << "bool:" << sizeof(d) << " byte " << 8*sizeof(d) << " bits" << endl;
cout << "float:" << sizeof(e) << " byte " << 8*sizeof(e) << " bits" <<
endl;
cout << "double:" << sizeof(f) << " byte " << 8*sizeof(f) << " bits" << endl;
cout << "long double:" << sizeof(g) << "byte " << 8*sizeof(g) << " bits" <<endl;
return 0;
}
3.定义一个数组,数据为:6,3,7,1,4,8,2,9,11,5.请创建一个向量,把数组的初值赋给它,然后对该向量求标准差(均方差):
s=
解答:
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
int main()
{
int num[10] = {6, 3, 7, 1, 4, 8, 2, 9, 11, 5};
vector<int> vnum(num, num+10);
double ave, sum1 = 0, sum2 = 0, s;
for(vector<int>::iterator it = vnum.begin(); it != vnum.end(); it++) {
cout << *it << " ";
sum1 += *it;
}
ave = sum1/10;
for(int i = 0;i < 10;i ++)
sum2 += pow((vnum[i] - ave), 2);
s = sqrt(sum2/10);
cout<<" 的标准差是:"<<s<<endl;
return 0;
}
4.有一些日期,在文件abc.txt中,后面加*号的表示要加班的日期,试汇总每个月25号的天数,如果是加班日,则该天乘2。

解答:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
int sum=0;
ifstream in("abc.txt");
string s;
while(getline(in, s))
{
if(s.find("25")!=-1)
if(s.find('*')!=-1)
sum += 2;
else
sum++;
}
cout<<"所有每个月 25 号的天数为:"<<sum<<endl;
return 0;
}
5. 编制程序,将输人的一行字符以加密的形式输出,然后将其解密,解密的字符序列与输入的正文进行比较,吻合时输出解密的正文,否则输出解密失败。

加密时,将每个字符的ASCII码依次反复加上“4962873”中的数字,并在32(‘’)122(‘z’)之间做模运算。

解密与加密的顺序相反。

例如,对于输入正文“the result of3 and 2 is not 8”,则运行结果为:
xqk "zlvyuz" wm#7)gpl'5$ry"vvw$A
the result of 3 and 2 ls not 8
解答:
#include <iostream>
#include <string>
using namespace std;
const char key[] = "4962873";
string encode(string& str)
{
string result(str);
for(int i = 0; i < str.length(); i++)
{
result[i] += key[i%7] - '0';
if(result[i] > 'z')
{
result[i] -= 91;
}
}
return result;
}
string decode(string& str)
{
string result(str);
for(int i = 0; i < str.length(); i++)
{
result[i] -= key[i%7] - '0';
if (result[i] < ' ')
{
result[i] += 91;
}
}
return result;
}
int main()
{
string s;
getline(cin,s);
string t = encode(s);
cout << t << endl;
cout << (s == decode(t) ? s+"\n" : string("decode failed\n"));
return 0;
}
6.阅读下列程序,写出运行结果(应该知道的遍历数组的五种方法)。

//=====================================
#include <iostream>
using namespace std;
//-------------------------------------
int main()
{
int sum[5] = {0}; //存放每种方法的结果
int iArray[] = {1,4,2,7,13,32,21,48,16,30};
int size = sizeof(iArray)/sizeof(*iArray);
int* iPtr = iArray;
for(int n=0; n<size; ++n) //方法1
sum[3] += iPtr[n];
for(int n=0; n<size; ++n) //方法2
sum[2] += *(iPtr + n);
for(int n=0; n<size; ++n) //方法3
sum[1] += *iPtr ++;
for(int n=0; n<size; ++n) //方法4
sum[0] += iArray[n];
for(int n=0; n<size; ++n) //方法5
sum[4] += *(iArray + n);
for(int i=0; i<5; ++i)
cout<<sum[i]<<endl;
return 0;
}
解答:
174
174
174
174
174
7.试将下列程序中的指针改为引用。

//=====================================
#include <iostream>
using namespace std;
//-------------------------------------
void mySwap(int* a,int* b);
//-------------------------------------
int main()
{
int a = 16, b = 48;
cout << "a = " << a << ",b = " << b << endl; mySwap(&a, &b);
cout << "After Being Swapped: \n";
cout << "a = " << a << ",b = " << b << endl;
return 0;
}//-------------------------------------
void mySwap(int* a,int* b)
{
int temp = *a;
*a = *b;
*b = temp;
解答:
//=====================================
#include <iostream>
using namespace std;
//-------------------------------------
void mySwap(int& a,int& b);
//-------------------------------------
int main()
{
int a = 16, b = 48;
cout << "a = " << a << ",b = " << b << endl; mySwap(a,b);
cout << "After Being Swapped: \n";
cout << "a = " << a << ",b = " << b << endl;
return 0;
}//-------------------------------------
void mySwap(int& a,int& b)
{
int temp = a;
a = b;
b = temp;
}。

相关文档
最新文档