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

c++上课题目

1. 引用的声明及访问

代码:

#include<iostream>
using namespace std;
int x = 5, y =10;
int &r = x;//r为x的变量别名
void print()
{
    cout << "x=" << x << "y" << y << "r=" << r << endl;
    cout << "  Address of x" << &x << endl;//输入变量x的内存地址
    cout << "Address of y" << &y << endl;
    cout << "Address of r" << &r << endl;
}
int main() 
{
    print() ;//调用输出函数
    r = y;//赋值
    print() ;
    y = 100;//再赋值
    x = y - 10;//x,y同时改变
    print();
    return 0;
}

运行结果:

x=5 y=10 r=5
  Address of x: 0x7ffeefbff53c
Address of y: 0x7ffeefbff538
Address of r: 0x7ffeefbff53c
x=10 y=10 r=10
  Address of x: 0x7ffeefbff53c
Address of y: 0x7ffeefbff538
Address of r: 0x7ffeefbff53c
x=90 y=100 r=90
  Address of x: 0x7ffeefbff53c
Address of y: 0x7ffeefbff538
Address of r: 0x7ffeefbff53c

2. 通过引用参数修改实际参数的值

代码:

#include <iostream>
using namespace std;
void swap ( int &x, int &y )//引用参数成为实际参数变量额、的别名
{
    int t = x;
    x = y;
    y = t;
}
int main () 
{
    int a = 3, b = 5, d = 20, c = 10;
    cout << "a=" << "  b=" << b << endl ;
    swap (a,b) ;
    cout << "a=" << a << " b=" << b << endl;
    swap ( c, d);
    cout << "c=" << c << " d=" << d << endl;
}

运行结果:

a = 3 b = 5
a =5 b = 3
c =10 d = 20
c = 20 d =10

3. 三种参数的使用示例

#include<iostream>
using namespace std;
int Fun(const int &x, int &y,int z)
{
    y++;
    z++;
    return y;
}
int main () 
{
    int a= 1, b = 3, c = 3, d = 0;
    cout << "a=" << a << "B=" << b << "c=" << c << "d=" << d << endl;
    d =Fun(a, b, c);
    cout << "a=" << a << " b=" << b << "c=" << c << "d=" << d << endl;
    return 0;
}

结果:

a=1 b=2 c=3 d=0
a=1 b=3 c=3 d=3

4.动态空间管理示例

#include<iostream>
#include <ctime>
#include <cmath>
#include <iomanip>
using namespace std;
const int N =30;
int main() 
{
    int *p, *sum, i;
    sum= new int (0);
    p = new int (N);
    if(p==NULL) {
        cout << "allocation failure.\n";
        return 0;
    }
    srand( time(NULL));
    for(i =0; i < N;i++)
    {
        p[i] = rand() % 100;
        if(p[i] %2)
            (*sum) ++;
    }
    for(i=0;i <N;i++)
    {
        cout << setw(4) << p[i];
        if((i+1) % 10==0)
        cout << endl;
    }
    cout << "the number of odd is:" << *sum << endl;
    delete []p;
    delete sum;
    return 0;
}

运行结果:

  43  66  71  26  11  46  64  80  24  31
 36  56  42  88  40  82  10  85  68  69
 84  47  91  44  14  57  88  40  56  18
the number of odd is: 10

5. 异常处理的步骤

#include<iostream>
using namespace std;
int divide(int x, int y) 
{
    if(y == 0) throw y;
    return x /y;

}
int main () 
{
    int a = 10 , b= 5, c =0 ;
    try
    {
        cout <<"a/b" << divide ( a,b) << endl;
        cout <<"b/a" << divide ( b,a) << endl;
        cout <<"a/c" << divide ( a,c) << endl;
        cout <<"c/b" << divide ( c,b) << endl;
    }
    catch (int ) 
    {
        cout << "except of divide zero" << endl;

     }
     cout << "calculate finished" << endl;
     return 0;
     
}

运行结果

 a/b2
b/a0.5
except of divide zero
calculate finished

6.课后习题

1.
#include <iostream>
using namespace std;
int main()
{
    int a, b, c;
    char ch;
    cin >> a >> ch >> b >> c;
    cout << a << endl << ch << endl << b << endl << c;
    return 0;
}

运行结果:

1
A
2
3
2.
#include <iostream>
using namespace std;
int main()
{
    int arr[4] = {1, 2, 3, 4}, i;
    int *a = arr;
    int &p = a; 
    p++;
    *p = 100;
    cout << *a << "\t" << *p << endl;
    for (i = 0; i < 4; i++)
        cout << arr[i] << "\t";
    cout << endl;
    int b = 10;
    p = &b;
    cout << *a << "\t" << *p << endl;
    for (i = 0; i < 4; i++)
        cout << arr[i] << "\t";
    cout << endl;
    return 0;
}

运行结果:

100  100
1  100  3  4
10  10
1  100  3  4
3.
#include <iostream>
using namespace std;
int i = 0;
int main()
{
    int i = 5;
    {
        int i = 7;
        cout << "i=" << i << "::i=" << ::i << endl;
        cout << "::i=" << ::i << endl;
        ::i = 1;
        cout << "::i=" << ::i << endl;
    }
    cout << "i=" << i << endl;
    cout << "please input x,y:";
    ::i = 6;
    i += ::i;
    ::i = 100;
    cout << "i=" << i << endl;
    cout << "::i=" << ::i << endl;
    return 0;
}

运行结果:

i=7::i=0
::i=0
::i=1
i=5
please input x,y:
i=6
::i=100
4.
#include <iostream>
using namespace std;
void f(double x = 50.6, int y = 10, char z = 'A');
int main()
{
    double a = 216.34;
    int b = 2;
    char c = 'E';
    f();
    f(a);
    f(a, b);
    f(a, b, c);
    return 0;
}
void f(double x, int y, char z)
{
    cout << "x=" << x << "\t" << "y=" << y << "\t";
    cout << "z=" << z << endl;
}

运行结果:

x=50.6   y=10   z=A
x=216.34   y=10   z=A
x=216.34   y=2   z=A
x=216.34   y=2   z=E
5.
#include <iostream>
using namespace std;
int & s(const int &a, int &b)
{
    b += a;
    return b;
}
int main()
{
    int x = 500, y = 1000, z = 0;
    cout << x << '\t' << y << '\t' << z << '\n';
    s(x, y);
    cout << x << '\t' << y << '\t' << z << '\n';
    z=s(x, y);
    cout << x << '\t' << y << '\t' << z << '\n';
    s(x, y) = 200;
    cout << x << '\t' << y << '\t' << z << '\n';
    return 0;
}

运行结果:

500    1000    0
500    1500    0
500    2000    2000
500    200    2000
6.
#include <iostream>
using namespace std;
void fun(int x, int &y)
{
    x += y;
    y += x;
}
int main()
{
    int x = 5, y = 10;
    fun(x, y);
    fun(y, x);
    cout << "x=" << x << ",y=" << y << endl;
    return 0;
}

运行结果:

x=25 , y=25

完成方式:根据书本例题,在vs code上打出代码,并运行结果,复制到markdown里面,我有时还使用豆包,我认为比起deepseek来豆包的反应时间更短,而且用了这么就我也习惯了他的使用方式。有事因为某些原因,代码不能运行也可以直接在豆包里面,得到解答并直接运行.

相关文章:

  • GEE:计算长时间序列NPP与NDVI之间的相关系数
  • Vue3 TransitionGroup组件深入解析:结合Element Plus实践指南
  • iOS逆向工程专栏 第13篇:iOS动态分析基础
  • golang实现读取excel文件并转换为JSON格式
  • 51单片机编程学习笔记——74HC138译码器
  • 通用网盘客户端(基于webdav协议)
  • 《JavaScript解题秘籍:力扣队列与栈的高效解题策略》
  • 【Mac】2025-MacOS系统下常用的开发环境配置
  • 私有云基础架构
  • 全面了解机器学习:回归、分类、分割与检测任务
  • OpenCV:从入门到实战的全方位指南
  • DeepSeek助力学术写作:150个提示词解读
  • Goby 漏洞安全通告| Ollama /api/tags 未授权访问漏洞(CNVD-2025-04094)
  • HarmonyOS NEXT开发进阶(十一):应用层架构介绍
  • selenium用例执行过程采集操作形成测试报告上的回复
  • 基于 openEuler 22.09 的 OpenStack Yoga 部署
  • Java 大视界 -- Java 大数据中的联邦学习激励机制设计与实践(111)
  • Git 2.48.1 官方安装与配置全流程指南(Windows平台)
  • Attentive Eraser论文笔记
  • 神经机器翻译:联合学习对齐和翻译
  • 五一“大车流”来了,今日午后G40沪陕高速开始迎来出沪高峰
  • 国家发改委下达今年第二批810亿超长期特别国债资金,支持消费品以旧换新
  • 新质观察|重塑低空经济的系统安全观
  • 人民日报社论:做新时代挺膺担当的奋斗者
  • 视频丨伊朗港口爆炸事件灭火工作已完成80%
  • 2025厦门体育产业采风活动圆满举行