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

news/2024/9/21 16:25:54 标签: c++, 算法, 开发语言

复习题:

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.niftyadmin.cn/n/5669053.html

相关文章

序列化方式二——JSON之fastjson

fastjson&fastjson2(版本&#xff1a;1.2.83_noneautotype) 扩展点 Fastjson通过其丰富的扩展点显著增强了用户定制序列化和反序列化行为的灵活性&#xff0c;完美契合了实际开发中的多样化需求。在SpringBoot与SpringCloud的集成环境中&#xff0c;开发者能够利用Seriali…

吃透这本大语言模型入门指南,LLM就拿下了

这本书简直就是学习大模型的福音&#xff01; 内容介绍&#xff1a; 介绍和解释大型语言模型的基本原理、工作机制和应用。作者从简单易懂的角度出发&#xff0c;深入浅出地介绍了语言模型的发展历程、背后的数学原理以及最新的研究成果 本书首先对大型语言模型的基本概念进行…

【数据结构与算法】LeetCode:双指针法

文章目录 LeetCode&#xff1a;双指针法正序同向而行&#xff08;快慢指针&#xff09;移除元素移动零&#xff08;Hot 100&#xff09;删除有序数组中的重复项颜色分类&#xff08;Hot 100&#xff09;压缩字符串移除链表元素删除排序链表中的重复元素删除排序链表中的重复元素…

TLC/TK Adv学习笔记1 - Py版本+美化

Python下重点 tkinter.ttk 模块自 Tk 8.5 开始引入&#xff0c;它提供了对 Tk 风格的部件集的访问。 它还带来了一些额外好处包括在 X11 下的反锯齿字体渲染和透明化窗口&#xff08;需要有 X11 上的混合窗口管理器&#xff09;。 tkinter.ttk 的基本设计思路&#xff0c;就是…

【2024华为杯数学建模竞赛】E题 解题思路 | 视频特征提取

这高速公路应急车道紧急启用模型 问题 1解题思路解题思路 问题 2解题思路 问题 3解题思路 问题 1 某路段&#xff08;长度约5000m&#xff0c;行车道2应急车道1&#xff09;上有四个视频观测点&#xff08;见示意图1&#xff09;。请基于该路段四个视频数据解决如下问题&#x…

《探索云原生与相关技术》

在当今的科技领域中&#xff0c;云原生&#xff08;Cloud Native&#xff09;已经成为了一个热门的话题。它代表着一种构建和运行应用程序的全新方式。 云原生的概念 云原生是一套技术体系和方法论&#xff0c;旨在充分利用云计算的优势来构建更具弹性、可扩展性和高效性的应…

Spring Tx事务 总结

1.简介 Spring Tx事务是对Spring AOP的再次封装,事务主要使用场景是处理数据库提交数据时&#xff0c;异常处理中需要回滚的情况&#xff0c;通过Spring Tx事务可以简化该操作。 2.代码 如果要使用事务&#xff0c;需要在pom.xml中加入以下依赖项: <dependency><…

代码随想录算法训练营Day9

232.用栈实现队列 Collection——List——Vector类——Stack类 class MyQueue {Stack<Integer> stackIn;Stack<Integer> stackOut;public MyQueue() {stackInnew Stack();stackOutnew Stack();} public void push(int x) {stackIn.push(x);}public int pop() {no…