博客
关于我
第7周编程作业
阅读量:518 次
发布时间:2019-03-08

本文共 3760 字,大约阅读时间需要 12 分钟。

首先,我们来详细分析并解决每个编程题目:

1. 函数重置两个变量的值

目标:编写一个函数,重置两个变量的值,使它们变为原值的平均值。思路:计算两个变量的总和,判断是否为偶数。根据情况计算平均值,并进行四舍五入处理,然后将结果保存到两个变量中。实现代码

#include 
using namespace std;void reset(int *a, int *b) { int sum = *a + *b; if (sum % 2 != 0) { *a = *b = (sum + 1) / 2; // 四舍五入,例如7+14=21,21/2=10.5,取11 } else { *a = *b = sum / 2; }}int main() { int x, y; cin >> x >> y; reset(&x, &y); cout << x << " " << y << endl; return 0;}

2. 数组求和函数

目标:编写一个函数,按次调用的方式累加数组元素的值。思路:使用静态变量来累加每次传入的值。实现代码

#include 
using namespace std;void add_array(int a) { static int sum = 0; sum += a; return sum;}int main() { int array[100], x; bool end = false; int count = 0; while (!end) { cin >> x; if (x == -1) { end = true; break; } array[count++] = x; } int total = 0; for (int i = 0; i < count; i++) { add_array(array[i]); } cout << add_array(0) << endl; // 总和通过指针返回 return 0;}

3. 数组清零函数

目标:将数组的前n个元素置零,返回指针。思路:从数组起始位置到指定位置-1的位置,依次赋值为0。实现代码

#include 
using namespace std;void emptyList(int *p, int empty_num) { for (int i = 0; i < empty_num; i++) { p[i] = 0; }}int main() { int array[100], *plist = array; int num; bool end = false; int count = 0; while (!end) { cin >> num; if (num == -1) { end = true; break; } array[count++] = num; } cin >> count; // 读取要清零的数量 emptyList(plist, count); for (int i = =0; i < 100; ++i) { if (i < count) { cout << 0 << " "; } else { if (i != 99) { continue; } cout << array[i] << endl; // 最后一个元素不添加空格 } } return 0;}

4. 函数指针加密处理

目标:编写两个加密函数,并用函数指针进行切换。凯撒加密函数

void caesar(char s[]) {    for (char *p = s; *p != 0; p++) {        if (*p >= 'a' && *p <= 'z') {            *p += 3; // 向后移动三位            if (*p > 'z') {                *p = 'a';            }        } else if (*p >= 'A' && *p <= 'Z') {            *p += 3; // 向后移动三位            if (*p > 'Z') {                *p = 'A';            }        }    }}

单双号加密函数

void oddeven(char s[]) {    int odd_pos = 0, even_pos = 0;    char *p = s;    while (*p != 0) {        if ((p - s) % 2 == 0) {            // 偶数位置            odd_pos++;            p++;        } else {            // 奇数位置            even_pos++;            p++;        }    }    // 交换奇偶位置    char temp;    for (int i = 0; i < min(odd_pos, even_pos); i++) {        temp = s[even_pos];        s[even_pos] = s[odd_pos];        s[odd_pos] = temp;        odd_pos++;        even_pos--;    }}

主函数

#include 
using namespace std;void caesar(char s[]);void oddeven(char s[]);int main() { char ch[40]; int chosen_method; cin >> ch; cin >> chosen_method; void (*fp)(char s[]) = nullptr; if (chosen_method == 1) { fp = caesar; } else if (chosen_method == 2) { fp = oddeven; } if (fp) { fp(ch); cout << ch << endl; } return 0;}

5. 通用函数区间平均值计算

目标:编写通用函数计算任意函数在区间内的平均值。通用函数

#include 
using namespace std;int avg(int (*fp)(int), int x1, int x2) { int sum = 0; int count = x2 - x1 + 1; for (int i = x1; i <= x2; ++i) { sum += fp(i); } int average = sum / count; return average;}

主函数

#include 
using namespace std;int a = 0, b = 0, c = 0, m = 0;int fun1(int x) { return 3*x*x + 2*x + 1; }int fun2(int x) { return pow(x, m); }int main() { cin >> a >> b >> c; cin >> m; cin >> x1 >> x2; int s1 = avg(fun1, x1, x2); int s2 = avg(fun2, x1, x2); cout << s1 << endl << s2 << endl; return 0;}

总结

每个任务都需要细致地分析需求,确保函数逻辑正确,尤其是处理数组、字符串和函数指针的部分,需要注意易引发的错误,如数组越界、静态变量的影响及函数指针的正确赋值和调用。通过逐一解决每个小问题,可以高效完成编程任务。

转载地址:http://dlziz.baihongyu.com/

你可能感兴趣的文章
Failed to get D-Bus connection: Operation not permitted解决
查看>>
【wp】HWS计划2021硬件安全冬令营线上选拔赛
查看>>
Python 之网络式编程
查看>>
网站故障公告1:使用阿里云RDS之后一个让人欲哭无泪的下午
查看>>
上周热点回顾(6.9-6.15)
查看>>
上周热点回顾(1.23-1.29)
查看>>
Python 简明教程 --- 20,Python 类中的属性与方法
查看>>
稀疏数组
查看>>
83. Remove Duplicates from Sorted List
查看>>
痞子衡嵌入式:串口调试工具pzh-com诞生记(2)- 界面构建(wxFormBuilder3.8.0)
查看>>
Nmap扫描工具介绍
查看>>
Oracle Orion tool check io(ORACLE Orion 工具查看以及校验IO)
查看>>
玩玩小爬虫——试搭小架构
查看>>
Python大神编程常用4大工具,你用过几个?
查看>>
linux kernel version magic 不一致导致的模块 加载 (insmod) 不上
查看>>
centos7一步一步搭建docker jenkins 及自定义访问路径重点讲解
查看>>
MySQL 1064 You have an error in your SQL syntax 错误解决办法
查看>>
【Flink】Flink 底层RPC框架分析
查看>>
MySQL错误日志(Error Log)
查看>>
oracle使用DBMS_RANDOM包生成随机数据
查看>>