优化下面代码#include <stdio.h> #include <string.h> #include <math.h> int main(){ char binary[32]=""; fgets(binary, 32, stdin); binary[strlen(binary)-1] = 0; // 110 len=2 int len=strlen(binary); int n=0; 优化代码: 使用左移 int i=0; while(binary[i]){ n += (binary[i]-48)<<(len-1); i++; len--; } printf("%s 十进制: %d\n", binary, n); return 0; }

时间: 2024-04-10 22:27:46 浏览: 9
下面是对你提供的代码进行优化的改进版本: ```c #include <stdio.h> #include <string.h> #include <math.h> int main() { char binary[32] = ""; fgets(binary, sizeof(binary), stdin); binary[strcspn(binary, "\n")] = '\0'; int len = strlen(binary); int n = 0; for (int i = 0; i < len; i++) { n += (binary[i] - '0') << (len - i - 1); } printf("%s 十进制: %d\n", binary, n); return 0; } ``` 对比你之前的代码,主要做了以下优化: 1. 使用`strcspn`函数替代`strlen`函数来获取输入字符串的长度。这样可以避免多次调用`strlen`函数,提高效率。 2. 使用`'\0'`替代`0`来表示字符串结束符。 3. 使用`for`循环替代`while`循环,并使用直接的索引方式访问字符数组,避免使用额外的变量。 4. 在计算二进制转换为十进制时,使用`(binary[i] - '0')`来代替`(binary[i] - 48)`,使代码更加清晰易懂。 这些改进可以提高代码的可读性和性能。
相关问题

编写函数int conv(char hex[])将16进制的数字字符串转换为十进制整数,要求主函数中输入16进制数字字符串,调用conv函数输出其十进制整数。16进制数字字符串的位数要求在6位以下,1位以上,并以H或h结尾。 请只提供子函数的编写。#include <stdio.h> #include <stdlib.h> int conv(char hex[]); int main() { int m; char hex[7]; scanf("%s",hex); m=conv(hex); if(m==-1) printf("输入有误!"); else printf("%d\n",m); }

好的,在这里给出修改后的conv函数的实现,以满足要求: ```c #include <stdio.h> #include <string.h> #include <math.h> int conv(char hex[]) { int len = strlen(hex); if (len < 2 || len > 7 || (hex[len-1] != 'H' && hex[len-1] != 'h')) { // 位数不符合要求或者不以H或h结尾,返回-1表示输入有误 return -1; } int decimal = 0; int base = 1; for (int i = len - 2; i >= 0; i--) { if (hex[i] >= '0' && hex[i] <= '9') { decimal += (hex[i] - '0') * base; } else if (hex[i] >= 'A' && hex[i] <= 'F') { decimal += (hex[i] - 'A' + 10) * base; } else if (hex[i] >= 'a' && hex[i] <= 'f') { decimal += (hex[i] - 'a' + 10) * base; } else { // 非16进制字符,返回-1表示输入有误 return -1; } base *= 16; } return decimal; } ``` 主函数中的代码不需要修改,可以直接使用。

#include<stdio.h> #include<math.h> #include<string.h> #include <stdlib.h> char* change(char *s[], int n); int main(void) { char poem[4][20]; int n,i; for (i = 0; i < 4; i++) { scanf("%s", poem[i]); } printf("%s",change(poem, 4)); return 0; } char* change(char *s[], int n) { char* a = NULL; a = (char*)malloc(sizeof(char) * 10); int i; for (i = 0; i < n; i++) { a[2 * i] = s[i][0]; a[2 * i + 1] = s[i][1]; } a[2 * i] = '\0'; return a; }输入一首诗,这段代码并没有输出由每行诗首个字组成的字符串,原因出在哪里?

这段代码的问题在于,输入的诗歌没有按照要求存储在 char poem[4][20] 数组中。程序中使用了 scanf("%s", poem[i]) 来读取每一行诗,但是并没有限制每一行诗的长度。如果输入的某一行诗的长度超过了 19 个字符,就会导致数组越界,从而出现未定义的行为。 为了避免这种情况,可以使用 fgets() 函数来读取每一行诗,同时指定读取的最大字符数为 19(因为每一行最多只有 19 个字符,再加上一个空字符)。 修改后的代码如下所示: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> char* change(char *s[], int n); int main(void) { char poem[4][20]; int n, i; for (i = 0; i < 4; i++) { fgets(poem[i], sizeof(poem[i]), stdin); } printf("%s", change(poem, 4)); return 0; } char* change(char *s[], int n) { char* a = NULL; a = (char*)malloc(sizeof(char) * 10); int i; for (i = 0; i < n; i++) { a[2 * i] = s[i][0]; a[2 * i + 1] = s[i][1]; } a[2 * i] = '\0'; return a; } ``` 修改后的代码中使用了 fgets() 函数来读取每一行诗,同时使用 sizeof(poem[i]) 来指定最大的字符数。由于每一行最多只有 19 个字符,再加上一个空字符,因此最大的字符数为 20。

相关推荐

#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<windows.h> #include<string.h> #include<stdlib.h> #include<math.h> #include<malloc.h> typedef struct character_name { char name[100]; struct character_name* next; }char_name; typedef struct character_title { char title[100]; struct character_title* next; }char_title; typedef struct character_identity { char identity[100]; struct character_identity* next; }char_iden; typedef struct character_attribute { int strength; struct character_attribute* next; }char_att; typedef struct character_information { char_name* _name; char_title* _title; char_iden* _iden; char_att* _att; struct character_information* next; }char_inf; char_inf* initialization() { char_inf* node = (char_inf*)malloc(sizeof(char_inf)); node->_name = (char_name*)malloc(sizeof(char_name)); node->_title = (char_title*)malloc(sizeof(char_title)); node->_iden = (char_iden*)malloc(sizeof(char_iden)); node->_att = (char_att*)malloc(sizeof(char_att)); return node; } char_inf* ceshi, * current, * end; char_name* name_current, * name_end; char_title* title_current, * title_end; char_iden* iden_current, * iden_end; char_att* att_current, att_end; int main() { ceshi = initialization(); name_current = (char_name*)malloc(sizeof(char_name)); name_current = ceshi->_name; strcpy(name_current->name, "ceshi_1_1"); name_current->next = (char_name*)malloc(sizeof(char_name)); name_current = name_current->next; strcpy(name_current->name, "ceshi_1_2"); strcpy(ceshi->_title->title, "ceshi_2"); strcpy(ceshi->_iden->identity, "ceshi_3"); ceshi->_att->strength = 4; name_current = ceshi->_name; while (name_current->next != NULL) { printf("%s\n", name_current->name); name_current = name_current->next; } printf("%s\n%s\n%d\n", ceshi->_title->title, ceshi->_iden->identity, ceshi->_att->strength); return 0; } 为什么提示我访问权限报错

最新推荐

recommend-type

Elasticsearch初识与简单案例.pdf

Elasticsearch是一个基于Lucene的分布式全文搜索引擎,提供灵活且高效的搜索和分析功能。通过HTTP请求和客户端库,用户可以索引和搜索文档,执行复杂查询,进行数据分析,并享受高亮显示等特性。其高级功能如复合查询、聚合分析、滚动搜索等,使其适用于各种数据处理和分析场景。Elasticsearch还具有强大的监控和日志功能,确保集群稳定运行。总之,Elasticsearch是企业级搜索和分析的理想选择。
recommend-type

Python基于LSTM模型对全国的空气质量数据进行可视化分析预测源代码

介绍 对全国2019年1月至2023年12月的空气质量数据进行分析,绘制时间序列图,展示每月/每季度的平均AQI变化趋势。绘制不同省份和城市的平均AQI热力图。分析不同污染物的浓度分布和趋势。绘制空气质量等级分布图。 需求说明 对空气质量数据进行数据分析,并使用LSTM模型进行预测。 安装教程 pip install jupyter pip install numpy pandas matplotlib seaborn 使用说明 在项目路径下打开终端输入jupyter notebook就行
recommend-type

百问网linux桌面GUI,基于LVGL 8.x。.zip

百问网linux桌面GUI,基于LVGL 8.x。
recommend-type

基于Vue开发的XMall商城前台页面 PC端.zip

基于Vue开发的XMall商城前台页面 PC端.zip
recommend-type

2019年中国民航大学电子设计竞赛E题-自动导航运输车

2019年中国民航大学电子设计竞赛E题-自动导航运输车 全国大学生电子设计竞赛(National Undergraduate Electronics Design Contest),试题,解决方案及源码。计划或参加电赛的同学可以用来学习提升和参考
recommend-type

RTL8188FU-Linux-v5.7.4.2-36687.20200602.tar(20765).gz

REALTEK 8188FTV 8188eus 8188etv linux驱动程序稳定版本, 支持AP,STA 以及AP+STA 共存模式。 稳定支持linux4.0以上内核。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

:YOLOv1目标检测算法:实时目标检测的先驱,开启计算机视觉新篇章

![:YOLOv1目标检测算法:实时目标检测的先驱,开启计算机视觉新篇章](https://img-blog.csdnimg.cn/img_convert/69b98e1a619b1bb3c59cf98f4e397cd2.png) # 1. 目标检测算法概述 目标检测算法是一种计算机视觉技术,用于识别和定位图像或视频中的对象。它在各种应用中至关重要,例如自动驾驶、视频监控和医疗诊断。 目标检测算法通常分为两类:两阶段算法和单阶段算法。两阶段算法,如 R-CNN 和 Fast R-CNN,首先生成候选区域,然后对每个区域进行分类和边界框回归。单阶段算法,如 YOLO 和 SSD,一次性执行检
recommend-type

设计算法实现将单链表中数据逆置后输出。用C语言代码

如下所示: ```c #include <stdio.h> #include <stdlib.h> // 定义单链表节点结构体 struct node { int data; struct node *next; }; // 定义单链表逆置函数 struct node* reverse(struct node *head) { struct node *prev = NULL; struct node *curr = head; struct node *next; while (curr != NULL) { next
recommend-type

c++校园超市商品信息管理系统课程设计说明书(含源代码) (2).pdf

校园超市商品信息管理系统课程设计旨在帮助学生深入理解程序设计的基础知识,同时锻炼他们的实际操作能力。通过设计和实现一个校园超市商品信息管理系统,学生掌握了如何利用计算机科学与技术知识解决实际问题的能力。在课程设计过程中,学生需要对超市商品和销售员的关系进行有效管理,使系统功能更全面、实用,从而提高用户体验和便利性。 学生在课程设计过程中展现了积极的学习态度和纪律,没有缺勤情况,演示过程流畅且作品具有很强的使用价值。设计报告完整详细,展现了对问题的深入思考和解决能力。在答辩环节中,学生能够自信地回答问题,展示出扎实的专业知识和逻辑思维能力。教师对学生的表现予以肯定,认为学生在课程设计中表现出色,值得称赞。 整个课程设计过程包括平时成绩、报告成绩和演示与答辩成绩三个部分,其中平时表现占比20%,报告成绩占比40%,演示与答辩成绩占比40%。通过这三个部分的综合评定,最终为学生总成绩提供参考。总评分以百分制计算,全面评估学生在课程设计中的各项表现,最终为学生提供综合评价和反馈意见。 通过校园超市商品信息管理系统课程设计,学生不仅提升了对程序设计基础知识的理解与应用能力,同时也增强了团队协作和沟通能力。这一过程旨在培养学生综合运用技术解决问题的能力,为其未来的专业发展打下坚实基础。学生在进行校园超市商品信息管理系统课程设计过程中,不仅获得了理论知识的提升,同时也锻炼了实践能力和创新思维,为其未来的职业发展奠定了坚实基础。 校园超市商品信息管理系统课程设计的目的在于促进学生对程序设计基础知识的深入理解与掌握,同时培养学生解决实际问题的能力。通过对系统功能和用户需求的全面考量,学生设计了一个实用、高效的校园超市商品信息管理系统,为用户提供了更便捷、更高效的管理和使用体验。 综上所述,校园超市商品信息管理系统课程设计是一项旨在提升学生综合能力和实践技能的重要教学活动。通过此次设计,学生不仅深化了对程序设计基础知识的理解,还培养了解决实际问题的能力和团队合作精神。这一过程将为学生未来的专业发展提供坚实基础,使其在实际工作中能够胜任更多挑战。