第三章 处理数据

书内代码部分

程序清单 3.1,limits.cpp

// limits.cpp -- some integer limits
#include <iostream>
#include <climits>   // use limits.h for older systems

int main()
{
	using namespace std;
	int n_int = INT_MAX;
	short n_short = SHRT_MAX;
	long n_long = LONG_MAX;
	long long n_llong = LLONG_MAX;

	// sizeof operator yields size of type or of variable
	cout << "int is " << sizeof(int) << " bytes." << endl;
	cout << "short is " << sizeof n_short << " bytes." << endl;
	cout << "long is " << sizeof n_long << " bytes." << endl;
	cout << "long long is " << sizeof n_llong << " bytes." << endl;
	cout << endl;

	cout << "Maximum values: " << endl;
	cout << "int: " << n_int << endl;
	cout << "short: " << n_short << endl;
	cout << "long: " << n_long << endl;
	cout << "long long: " << n_llong << endl << endl;

	cout << "Minimum int value = " << INT_MIN << endl;
	cout << "bits per byte = " << CHAR_BIT << endl;

	return 0;
}

输出结果:

程序清单 3.2,exceed.cpp

// exceed.cpp -- exceeding some integer limits
#include <iostream>
#include <climits>
#define ZERO 0

int main()
{
	using namespace std;
	short sam = SHRT_MAX;
	unsigned short sue = sam; // okay if variable sam already defined
	cout << "Sam has " << sam << " dollars and Sue has " << sue;
	cout << " dollars deposited." << endl
	<< "Add $1 to each account." << endl << "Now ";
	sam = sam + 1;
	sue = sue + 1;
	cout << "Sam has " << sam << " dollars and Sue has " << sue;
	cout << " dollars deposited.\nPoor Sam!" << endl;

	sam = ZERO;
	sue = ZERO;
	cout << "Sam has " << sam << " dollars and Sue has " << sue;
	cout << " dollars deposited." << endl;
	cout << "Take $1 from each account." << endl << "Now ";
	sam--;
	sue--;
	cout << "Sam has " << sam << " dollars and Sue has " << sue;
	cout << " dollars deposited." << endl << "Lucky Sue!" << endl;

	return 0;
}

 输出结果:

程序清单 3.3,hexoct.cpp

// hexoct1.cpp -- show hex and octal literals
#include <iostream>

int main()
{
	using namespace std;
	int chest = 42;         // decimal integer literal
	int waist = 0x42;       // hexadecimal integer literal
	int inseam = 042;       // octal integer literal

	cout << "Monsieur cuts a striking figure!\n";
	cout << "chest = " << chest << " (42 in decimal)\n";
	cout << "waist = " << waist << " (0x42 in hex)\n";
	cout << "inseam = " << inseam << " (042 in octal)\n";
	
	return 0;
}

输出结果:

程序清单 3.4,hexoct2.cpp

// hexoct2.cpp -- display values in hex and octal
#include <iostream>
using namespace std;

int main()
{
	using namespace std;
	int chest = 42;
	int waist = 42;
	int inseam = 42;

	cout << "Monsieur cuts a striking fiagure!" << endl;
	cout << "chest = " << chest << " (decimal for 42)" << endl;
	cout << hex; // manipulator for changing number base
	cout << "waist = " << waist << " (hexadecimal for 42)" << endl;
	cout << oct; // manipulator for changing number base
	cout << "inseam = " << inseam << " (octal for 42)" << endl;

	return 0;
}

输出结果:

程序清单 3.5,chartype.c

// chartype.cpp -- the char type
#include <iostream>

int main()
{
	using namespace std;
	char ch;
	cout << "Enter a character: " << endl;
	cin >> ch;
	cout << "Hola! ";
	cout << "Thank you for the " << ch << " character." << endl;

	return 0;
}

输出结果:

程序清单 3.6,morechar.cpp

// morechar.cpp -- the char type and int type contrasted
#include <iostream>

int main()
{
	using namespace std;
	char ch = 'M';  // assign ASCII code for M to ch
	int i = ch;     // store same code in an int
	cout << "The ASCII code for " << ch << " is " << i << endl;
	cout << "Add one to the character code:" << endl;
	++ch;
	cout << "The ASCII code for " << ch << " is " << i << endl;
	
	// using the cout.put() member function to display a char
	cout << "Displaying char ch using cout. put(ch): ";
	cout.put(ch);
	
	// using cout.put() to display a char constant
	cout.put('!');
	cout << endl << "Done" << endl;

	return 0;
}

输出结果:

程序清单 3.7,bondini.cpp

// bondini.cpp -- using escape sequences
#include <iostream>

int main()
{
	using namespace std;
	cout << "\aOperation \"HyperHype\" is now activated!\n";
	cout << "Enter your agent code:________\b\b\b\b\b\b\b\b";
	long code;
	cin >> code;
	cout << "\aYou entered " << code << "...\n";
	cout << "\aCode verified! Proceed with Plan Z3!\n";

	return 0;
}

输出结果:

程序清单 3.8,floatnum.cpp

// floatnum.cpp floating-point types
#include <iostream>

int main()
{
	using namespace std;
	cout.setf(ios_base::fixed, ios_base::floatfield); // fixed-point
	float tub = 10.0 / 3.0;    // good to about 6 places
	double mint = 10.0 / 3.0;  // good to about 15 places
	const float million = 1.0e6;

	cout << "tub = " << tub;
	cout << ", a million tubs = " << million * tub;
	cout << ", \nand ten million tubs = ";
	cout << 10 * million * tub << endl;

	cout << "mint = " << mint << " and a million mints = ";
	cout << million * mint << endl;

	return 0;
}

输出结果:

程序清单 3.9,fltadd.cpp

// fltadd.cpp -- precision problems with float
#include <iostream>

int main()
{
	using namespace std;
	float a = 2.34E+22f;
	float b = a + 1.0f;

	cout << "a = " << a << endl;
	cout << "b - a = " << b - a << endl;

	return 0;
}

输出结果:

程序清单 3.10,arith.cpp

// arith.cpp -- some C++ arithmetic
#include <iostream>

int main()
{
	using namespace std;
	float hats, heads;
	cout.setf(ios_base::fixed, ios_base::floatfield);
	cout << "Enter a number: ";
	cin >> hats;
	cout << "Enter another number: ";
	cin >> heads;

	cout << "hats = " << hats << "; heads = " << heads << endl;
	cout << "hats + heads = " << hats + heads << endl;
	cout << "hats - heads = " << hats - heads << endl;
	cout << "hats * heads = " << hats * heads << endl;
	cout << "hats / heads = " << hats / heads << endl;

	return 0;
}

输出结果 :

程序清单 3.11,divide.cpp

// divide.cpp -- integer and floating-point division
#include <iostream>

int main()
{
	using namespace std;
	cout.setf(ios_base::fixed, ios_base::floatfield);
	cout << "Integer division: 9/5 = " << 9 / 5 << endl;
	cout << "Floating-point division: 9.0/5.0 = ";
	cout << 9.0 / 5.0 << endl;
	cout << "Mixid division: 9.0/5 = " << 9.0 / 5 << endl;
	cout << "double constants: 1e7/9.0 = ";
	cout << 1.e7 / 9.0 << endl;
	cout << "float constants: 1e7f/9.0f = ";
	cout << 1.e7f / 9.0f << endl;

	return 0;
}

输出结果:

程序清单 3.12,modulus.cpp

// modulus.cpp -- uses % operator to convert lbs to stone
#include <iostream>

int main()
{
	using namespace std;
	const int Lbs_per_stn = 14;
	int lbs;

	cout << "Enter your weight in pounds: ";
	cin >> lbs;
	int stone = lbs / Lbs_per_stn;     // whole stone
	int pounds = lbs % Lbs_per_stn;    // remainder in pounds
	cout << lbs << " pounds are " << stone
		<< " stone, " << pounds << " pound(s).\n";

	return 0;
}

输出结果:

程序清单 3.13,assign.cpp

// init.cpp -- type changes on initialization
#include <iostream>

int main()
{
	using namespace std;
	cout.setf(ios_base::fixed, ios_base::floatfield);
	float tree = 3;       // int converted to float
	int guess(3.9832);    // double converted to int
	int debt = 7.2E12;    // result not defind in C++
	cout << "tree = " << tree << endl;
	cout << "guess = " << guess << endl;
	cout << "debt = " << debt << endl;

	return 0;
}

输出结果:

程序清单 3.14,typecast.cpp

// typecast.cpp -- forcing type changes
#include <iostream>

int main()
{
	using namespace std;
	int auks, bats, coots;

	// the following statement adds the values as double,
	// then converts the result to int
	auks = 19.99 + 11.99;
	
	// these statements add value as int
	bats = (int)19.99 + (int)11.99;   // old C syntax
	coots = int(19.99) + int(11.99);  // new C++ syntax
	cout << "auks = " << auks << ", bats = " << bats;
	cout << ", coots = " << coots << endl;

	char ch = 'Z';
	cout << "The code for " << ch << " is ";  // print as char
	cout << int(ch) << endl;                  // print as int
	cout << "Yes, the code is ";
	cout << static_cast<int>(ch) << endl;     // using static_cast
	
	return 0;
}

输出结果:

编程练习部分

 题目 1,方法:计算并打印。示例代码:3_1.cpp

#include <iostream>

const int inchPerFeet = 12;

int main()
{
	using namespace std;
	cout << "Please enter your height:___\b\b\b";
	int height;
	cin >> height;
	cout << "Your height is " << height / inchPerFeet << " feet and ";
	cout << height % inchPerFeet << " inches." << endl;

	return 0;
}

输出结果:

题目 2, 方法:计算并输出结果。示例代码:3_2.cpp

#include <iostream>

const double inchPerFeet = 12.0;
const double meterPerInch = 0.0254;
const double lbsPerKilo = 2.2;

double CaculateBmi(double height, double weight);

int main()
{
	using namespace std;
	cout << "Please enter your height in feet first: ";
	double heightFeet;
	cin >> heightFeet;
	cout << "Please enter your height in inch then: ";
	double heightInch;
	cin >> heightInch;
	cout << "Please enter your weight in pound: ";
	double pound;
	cin >> pound;
	double height = (heightFeet * inchPerFeet + heightInch) * meterPerInch;
	double weight = pound / lbsPerKilo;
	cout << "Your BMI is " << CaculateBmi(height, weight) << endl;

	return 0;
}

double CaculateBmi(double height, double weight)
{
	return weight / (height * height);
}

输出结果:

题目 3, 方法:计算并输出。示例代码:3_3.cpp

#include <iostream>

int main()
{
	using namespace std;
	const double minutesPerDegrees = 60.0;
	const double secondsPerMinutes = 60.0;
	cout << "Enter a latitude in degrees, minutes, and seconds:" << endl;
	cout << "First, enter the degrees: ";
	double degrees;
	cin >> degrees;
	cout << "Next, enter the minutes of arc: ";
	double minutes;
	cin >> minutes;
	cout << "Finally, enter the seconds of arc: ";
	double seconds;
	cin >> seconds;
	cout << degrees << " degrees, " << minutes << " minutes, " << seconds
		<< " seconds = " << degrees + minutes / minutesPerDegrees
		+ seconds / secondsPerMinutes / minutesPerDegrees << " degrees" << endl;

	return 0;
}

输出结果:

题目 4,方法:计算并打印。示例代码:3_4.cpp

#include <iostream>

int main()
{
	const int hoursPerDay = 24;
	const int minutesPerHour = 60;
	const int secondsPerMinutes = 60;
	using namespace std;
	cout << "Please enter the seconds: ";
	long long seconds;
	cin >> seconds;
	long long days = seconds / (hoursPerDay * minutesPerHour * secondsPerMinutes);
	long long hours = (seconds % (hoursPerDay * minutesPerHour * secondsPerMinutes)) /
		(minutesPerHour * secondsPerMinutes);
	long long minutes = (seconds % (minutesPerHour * secondsPerMinutes)) / secondsPerMinutes;
	long long second = seconds % secondsPerMinutes;
	cout << seconds << " seconds = " << days << " days, " << hours << " hours, " << minutes
		<< " minutes, " << second << " seconds" << endl;
	
	return 0;
}

输出结果:

题目 5,方法:计算并打印。示例代码:3_5.cpp

#include <iostream>

int main()
{
	using namespace std;
	cout << "Please enter the population of the world: ";
	long long worldPopulation;
	cin >> worldPopulation;
	cout << "Please enter the population of America: ";
	long long americaPopulation;
	cin >> americaPopulation;
	cout << "The population of the US is " << double(americaPopulation) / double(worldPopulation)
		* 100.0 << "% of the world population." << endl;

	return 0;
}

输出结果:

题目 6,方法:计算并打印结果。示例代码:3_6.cpp

#include <iostream>

int main()
{
	using namespace std;
	const double distance = 100.0;
	cout << "Please enter the distance you drived(miles): ";
	double miles;
	cin >> miles;
	cout << "Please enter the gasoline you consumed(gallons): ";
	double gasoline;
	cin >> gasoline;
	cout << "You consumed " << gasoline / miles * distance << " gallons gasoline per " << distance
		<< " miles." << endl;

	return 0;
}

输出结果:

题目 7,方法:计算并打印结果。示例代码:3_7.cpp

#include <iostream>

int main()
{
	using namespace std;
	const double distance = 100.0;
	const double milesPerKilometer = 0.6214;
	const double litersPerGallon = 3.875;
	cout << "Please enter the gasolines you consumed per " << distance << " kilometers(liter): ";
	double gasolinesInLiter;
	cin >> gasolinesInLiter;
	double distanceInMile = (distance * milesPerKilometer) / (gasolinesInLiter / litersPerGallon);
	cout << "You can drive " << distanceInMile << " miles per gallon" << endl;

	return 0;
}

输出结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Sqoop 是 Apache Hadoop 生态系统中的一个工具,用于在 Hadoop 和结构化数据存储之间进行数据传输。Sqoop 可以将结构化数据(如关系型数据库中的数据)导入到 Hadoop 中的 HDFS 或 Hive 中,也可以将数据从 Hadoop 导出到结构化数据存储中。使用 Sqoop 进行数据导入导出的步骤如下: 1. 安装 Sqoop:下载 Sqoop 的安装包并解压,配置环境变量。 2. 连接到数据源:Sqoop 支持连接到多种数据源,如 MySQL、Oracle、PostgreSQL 等。 3. 导入数据:使用 Sqoop 的 import 命令将数据数据源导入到 Hadoop 的 HDFS 或 Hive 中。 4. 导出数据:使用 Sqoop 的 export 命令将数据从 Hadoop 导出到数据源中。 Hive 是基于 Hadoop 的数据仓库工具,它提供了类 SQL 的查询语言 HQL,可以将结构化的数据文件映射成一张数据库表,并提供了丰富的数据分析功能。使用 Hive 进行数据处理和分析的步骤如下: 1. 安装 Hive:下载 Hive 的安装包并解压,配置环境变量。 2. 创建数据库:使用 Hive 的 create database 命令创建数据库。 3. 创建表:使用 Hive 的 create table 命令创建表,可以指定表的列名、数据类型等属性。 4. 加载数据:使用 Hive 的 load data 命令将数据加载到表中。 5. 执行查询:使用 Hive 的 select 命令执行查询操作,可以对表进行聚合、排序、过滤等操作。 6. 保存查询结果:使用 Hive 的 insert 命令将查询结果保存到表中或导出到文件中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值