第二章 简单程序设计

2.16 数据类型

C++语言有哪几种数据类型?简述其值域。编程显示你使用的计算机中的各种数据

类型的字节数。

cpp 复制代码
#include<iostream>

using namespace std;

int main() {
	cout << "int 的大小:\t" << sizeof(int) << " 字节.\n";
	cout << "The size of a short int is:\t" << sizeof(short int) << " bytes.\n";
	cout << "The size of a long int is:\t" << sizeof(long) << " bytes.\n";
	cout << "The size of a char is:\t\t" << sizeof(char) << " bytes.\n";
	cout << "The size of a float is:\t" << sizeof(float) << " bytes.\n";
	cout << "The size of a double is:\t" << sizeof(double) << " bytes.\n";
	cout << "The size of a bool is:\t\t" << sizeof(bool) << " bytes.\n";
	return 0;
}

结果:

2.17ASCII码

输出ASCII码为32~127的字符。

cpp 复制代码
#include<iostream>

using namespace std;

int main() {
	for (int i = 32; i <= 127; i++)
	{
		cout << i << "的ASCLL码: " << char(i) << " \n";
	}
	return 0;
}

结果:

2.21变量有几种存储类型

采用堆栈方式分配内存空间,属于一时性存储,其存储空间可以被若干变量多次覆盖使用。

2.24 布尔

cpp 复制代码
#include<iostream>

using namespace std;

int main() {
	int a = 1;
	int b = 2;
	int c = 0;
	int d = -2;
	cout << "a:"<<bool(a)<<"\tb:" <<bool(b)<< "\tc:"<<bool(c)<<"\td:"<<bool(d) << endl;
	return 0;
}

下列各式的结果:

cpp 复制代码
#include<iostream>

using namespace std;

int main() {
	int a = 1;
	cout << !a<<endl;					//0
	cout << (!a | a) << endl<<endl;		//1

	cout << ~a << endl;					//-2
	cout << (~a | a) << endl<<endl;		//-1

	cout << (a ^ a )<< endl;//异或				//0
	cout << (a >> 2) << endl;//0001--> 0000		//0

	int b = 3;
	cout << (b >> 1) << endl;//0011--> 0001		//1
	cout << (a ^ b) << endl;//0001 0011--->0010	//2

	return 0;
}

2.25 选择语句

编写一个完整的程序,实现功能:向用户提问"现在正在下雨吗?",提示用户输人Y

或N。若输入为Y,显示"现在正在下雨。";若输人为N,显示"现在没有下雨。";否则

继续提问"现在正在下雨吗?"。

2.26 Switch

写一个完整的程序,运行时向用户提问"你考试考了多少分?(0~100)",接收输人

后判断其等级显示出来。规则如下:

等级=

优90<=分数<=100

良80<=分数<90

中60<=分数く80

差0<=分数く60

cpp 复制代码
#include<iostream>

using namespace std;

//每个 case 后面跟着一个常量值

int main() {
	int score;
	cout << "你考试考了多少分?(0~100):";
	cin >> score;
	int m = score / 10;
	switch (m)
	{
	case 10:
	case 9:
		cout << "优" << endl;
		break;
	case 8:
		cout << "良" << endl;
		break;
	case 7:
	case 6:
		cout << "中" << endl;
		break;
	default:
		cout << "差" << endl;
		break;
	}
	return 0;
}

2.27 菜单程序

实现一个简单的菜单程序,运行时显示"Menu:A(dd)D(elete)S(ort)Q(uit),Selectone:"提示用户输人,A表示增加,D表示删除,S表示排序,Q表示退出,输入为A、D、S时分别提示"数据已经增加、删除、排序。"输人为Q时程序结束。

(1)要求使用if·else语句进行判断,用break、continue控制程序流程。

(2)要求使用switch语句。

cpp 复制代码
#include<iostream>

using namespace std;

int main() {
	char choice;
	while (1)
	{
		cout<< "Menu: A(dd)D(elete)S(ort)Q(uit), Select one .";
		cin >> choice;
		if (choice == 'A')
		{
			cout << "数据已经增加。" << endl;
			continue;
		}
		else if (choice == 'D')
		{
			cout << "数据已经删除。" << endl;
			continue;
		}
		else if (choice == 'S')
		{
			cout << "数据已经排序" << endl;
			continue;
		}
		else if (choice == 'Q')
			cout << "已退出" << endl;
			break;
	}
	return 0;
}

2.28 质数

用穷举法找出1~100的质数并显示出来。分别使用while、do··while、for循环语句实现。

cpp 复制代码
#include<iostream>

using namespace std;

int main() {
	int i = 2;
	while (i <= 100)
	{
		int flag = 1;
		int j = 2;//判断是否是质数
		int k = i / 2;//循环的次数
		while (j <= k) 
		{
			if (i % j == 0)//注意
			{
				flag = 0;
				break;
			}
			j++;
		}
		if (flag) {
			cout << i << " ";
		}
		i++;
	}
	return 0;
}
相关推荐
冷雨夜中漫步1 小时前
Python快速入门(6)——for/if/while语句
开发语言·经验分享·笔记·python
半桔2 小时前
【IO多路转接】高并发服务器实战:Reactor 框架与 Epoll 机制的封装与设计逻辑
linux·运维·服务器·c++·io
HABuo2 小时前
【linux文件系统】磁盘结构&文件系统详谈
linux·运维·服务器·c语言·c++·ubuntu·centos
我在人间贩卖青春3 小时前
C++之多重继承
c++·多重继承
颜酱3 小时前
图结构完全解析:从基础概念到遍历实现
javascript·后端·算法
m0_736919103 小时前
C++代码风格检查工具
开发语言·c++·算法
yugi9878383 小时前
基于MATLAB强化学习的单智能体与多智能体路径规划算法
算法·matlab
2501_944934733 小时前
高职大数据技术专业,CDA和Python认证优先考哪个?
大数据·开发语言·python
DuHz3 小时前
超宽带脉冲无线电(Ultra Wideband Impulse Radio, UWB)简介
论文阅读·算法·汽车·信息与通信·信号处理
Polaris北极星少女4 小时前
TRSV优化2
算法