C++ prime plus-2-编程练习

server/2024/9/22 23:50:27/

复习题:

1.它们叫作函数。
2.这将导致在最终的编译之前,使用iostream 文件的内容替换该编译指令。

3.它使得程序可以使用 std 名称空间中的定义。

4.cout << "Hello,world\n";

或cout<<"Hello,world"<<endl;
5.int cheeses;
6.cheeses = 32:

7.cin >> cheeses;
8. cout << "We have " << cheeses << " varieties of cheese\n":
9.调用函数 froop()时,应提供一个参数,该参数的类型为 double,而该函数将返回一个int值。例如,可以像下面这样使用它:
int gval =froop(3.14159);
函数 rattle()接受一个int参数且没有返回值。例如,可以这样使用它:
rattle(37):

函数prune()不接受任何参数且返回一个int值。例如,可以这样使用它:
ntresidue=prune();

10.当函数的返回类型为空时,不用在函数中使用返回值。然而,如果不提供返回值,则可以使用它:

return;

11.如果编译器指出 `cout` 是一个未知标识符,这通常意味着编译器没有识别到 `cout` 所依赖的库或者命名空间。在 C++ 中,`cout` 是标准库中的一个对象,用于输出数据到标准输出流(通常是屏幕)。以下是导致这种问题可能的原因,以及三种修复方法:

### 可能的原因:
1. **未包含必要的头文件**:`cout` 定义在 `<iostream>` 头文件中,如果未包含这个头文件,编译器将无法识别 `cout`。
2. **命名空间问题**:如果使用了 `using namespace std;` 或者没有正确地使用 `std::` 前缀,可能会导致命名空间相关的问题。
3. **编译器配置问题**:在某些情况下,如果编译器配置不正确,或者使用的编译器不支持 C++ 标准库,也可能导致这个问题。

### 修复方法:
1. **包含 `<iostream>` 头文件**:

   #include <iostream>int main() {std::cout << "Please enter your PIN:" << std::endl;return 0;}

 确保在代码的顶部包含了 `<iostream>` 头文件。

2. **使用 `std::` 前缀**:

   int main() {std::cout << "Please enter your PIN:" << std::endl;return 0;}

 在 `cout` 前使用 `std::` 前缀,明确指出 `cout` 来自 `std` 命名空间。

3. **添加 `using namespace std;`**:

   #include <iostream>using namespace std;int main() {cout << "Please enter your PIN:" << endl;return 0;}

 在包含 `<iostream>` 头文件后,添加 `using namespace std;` 声明,这样你就可以在不使用 `std::` 前缀的情况下使用 `cout`。

确保选择适合你代码风格和项目要求的修复方法。通常,推荐使用 `std::` 前缀,因为它可以避免命名冲突,并且使代码更加清晰。

编程练习答案:

1.

#include <iostream>
#include <string>int main() {// 定义姓名和地址变量std::string name = "张三";std::string address = "中国北京市海淀区中关村大街1号";// 显示姓名和地址std::cout << "姓名: " << name << std::endl;std::cout << "地址: " << address << std::endl;return 0;
}

2. 

#include <iostream>
int main(){long meters;double yards;std::cout << "please enter a distance in meters:";std::cin >> meters;yards = meters * 3.28084;std::cout << "the distance in yards is:" << yards <<"yards"<<std::endl;return 0;
}

3.

#include <iostream>void printFirstLine();
void printSecondLine();
void printThirdLine();void printFirstLine() {std::cout << "Three blind mice" << std::endl;
}void printSecondLine(){std::cout << "Three blind mice" << std::endl;
}void printThirdLine(){std::cout << "See how they run" << std::endl;
}
int main(){printFirstLine();printSecondLine();printThirdLine();printThirdLine();
}

4.

#include <iostream>
int main(){int age;int months;std::cout << "Enter your age:";std::cin >> age;months = age * 12;std::cout << "your age in months is:" << months<< std::endl;return 0;
}

5.

#include <iostream>// 函数声明
double celsiusToFahrenheit(double celsius);int main() {double celsius, fahrenheit;std::cout << "Please enter a Celsius value: ";std::cin >> celsius;// 调用函数转换摄氏温度到华氏温度fahrenheit = celsiusToFahrenheit(celsius);// 显示结果std::cout << celsius << " degrees Celsius is " << fahrenheit << " degrees Fahrenheit." << std::endl;return 0;
}// 用户定义的函数,将摄氏温度转换为华氏温度
double celsiusToFahrenheit(double celsius) {return 1.8 * celsius + 32.0;
}

6.

#include <iostream>// 函数声明
double lightYearsToAstronomicalUnits(double lightYears);int main() {double lightYears, astronomicalUnits;std::cout << "Enter the number of lightyears: ";std::cin >> lightYears;// 调用函数转换光年到天文单位astronomicalUnits = lightYearsToAstronomicalUnits(lightYears);// 显示结果std::cout << lightYears << " light years = " << astronomicalUnits << " astronomical units." << std::endl;return 0;
}// 用户定义的函数,将光年转换为天文单位
double lightYearsToAstronomicalUnits(double lightYears) {const double CONVERSION_FACTOR = 63240.0; // 1光年等于63240天文单位return lightYears * CONVERSION_FACTOR;
}

7.

#include <iostream>void displayTime(int hours,int minutes);int main(){int hours,minutes;std::cout <<"Enter the number of hours:";std::cin >> hours;std::cout << "Enter the number of minutes:";std::cin >> minutes;displayTime(hours,minutes);return 0;
}void displayTime(int hours,int minutes){std::cout << "Time:" << hours << ":" << minutes << std::endl;
}


http://www.ppmy.cn/server/120527.html

相关文章

Unity多语言插件I2 Localization国际化应用

【就不收费了&#xff0c;要个关注不过分吧】 【图片来自插件官网&#xff0c;侵删】 前言 目前游戏往往都不会仅局限于国内语言&#xff0c;为了适应产品都要做国际化适配&#xff0c;因此会用到这个插件&#xff0c;这个插件要付费&#xff0c;因此请前往unity官网进行下载…

远程Linux网络连接( Linux 网络操作系统 04)

接下来我们准备开始进入Linux操作系统的第二个模块的学习&#xff0c;不过在学习之前我们需要对如下进行简单的配置&#xff0c;通过外接辅助软件MobaXterm来进行虚拟操作系统的访问。接下来的课程我们会一直在MobaXterm中进行命令和相关知识的学习。 一、准备阶段 1.1 软件 …

【docker】命令之容器操作

一、前言 在上篇博客介绍了关于如何从应用市场&#xff0c;下载镜像后&#xff0c;对镜像的相关操作了。这篇博客呢我们就要讲解我们把镜像下载下来了&#xff0c;启动这个镜像后&#xff0c;就是我们说的容器了&#xff0c;那么容器的具体操作又有那些呢&#xff1f; 二、容器…

Spring 源码解读:实现自定义注解处理器

引言 注解在现代 Java 编程中扮演了至关重要的角色。无论是简化代码、增强可读性&#xff0c;还是将元数据与业务逻辑分离&#xff0c;注解都让我们的代码更加优雅和灵活。Spring 中大量使用了注解&#xff0c;特别是像 Autowired、Component 等注解&#xff0c;这些背后依赖的…

【微信小程序】连续拍照功能实现

前言&#xff1a; 最近在使用uniapp开发微信小程序&#xff0c;遇到这样一个需求&#xff0c;用户想要连续拍照&#xff0c;拍完之后可以删除照片&#xff0c;保留自己想要的照片&#xff0c;然后上传到服务器上。由于原生的方法只能一个个拍照上传&#xff0c;所以只能自己通过…

ETCD学习使用

一、介绍 etcd&#xff08;分布式键值存储&#xff09;是一个开源的分布式系统工具&#xff0c;用于可靠地存储和提供键值对数据。etcd 通常通过 HTTP 或 gRPC 提供 API&#xff0c;允许应用程序通过简单的接口与其交互。由于其可靠性和稳定性&#xff0c;etcd 在构建可扩展、分…

Android 系统WIFI AP模式

在 Android 系统中&#xff0c;AP 模式&#xff08;Access Point Mode&#xff0c;热点模式&#xff09;允许设备作为 Wi-Fi 热点&#xff0c;其他设备可以通过连接这个热点进行互联网访问或局域网通信。要让 Android 设备工作在 AP 模式&#xff0c;你可以通过应用层的 API 控…

Redis面试真题总结(三)

文章收录在网站&#xff1a;http://hardyfish.top/ 文章收录在网站&#xff1a;http://hardyfish.top/ 文章收录在网站&#xff1a;http://hardyfish.top/ 文章收录在网站&#xff1a;http://hardyfish.top/ 什么是缓存雪崩&#xff1f;该如何解决&#xff1f; 缓存雪崩是指…