当前位置: 首页 > article >正文

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

复习题:

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.kler.cn/news/315245.html

相关文章:

  • 解决 Torch not compiled with CUDA enabled 问题 | MiniCPM3-4B 【应用开发笔记】
  • Android 短信验证码自动填充
  • Unity 设计模式 之 创建型模式 -【单例模式】【原型模式】 【建造者模式】
  • 【力扣】2376. 统计特殊整数
  • Linux:虚拟文件系统/proc和self进程
  • 某招标公告公示搜索引擎爬虫逆向
  • git配置SSH
  • 第二届Apache Flink极客挑战赛冠军比赛攻略_SkyPeaceLL队
  • 安卓开发,插件化换肤思路
  • 【Java】接口interface【主线学习笔记】
  • Mac使用gradle编译springboot-2.7.x源码
  • Hadoop分布式集群配置
  • SIP Servlets学习
  • ModbusTCP报文详解
  • 冒泡排序,选择排序,插入排序,归并排序,快速排序五种排序方法
  • thop计算模型复杂度(params,flops)
  • 云计算实训50——Kubernetes基础命令、常用指令
  • 怎样把PPT上顽固的图标删了
  • Html css水平居中+垂直居中+水平垂直居中的方法总结
  • WPF 自定义路由事件
  • HTTP的基本格式
  • 整合多方大佬博客以及视频 一文读懂 servlet
  • go/函数
  • Java笔试面试题AI答之单元测试JUnit(5)
  • 3. 什么是连接池?为什么使用数据库连接池?
  • AgentScope中带有@功能的多Agent组对话
  • Github Wiki 超链接 转 码云Gitee Wiki 超链接
  • hcia-openEuler V1.0师资题库-试卷2
  • MySQL数据库专栏(九)内联查询
  • 如何做系统架构?从动态系统思考的角度