当前位置:网站首页>Convert string to decimal integer
Convert string to decimal integer
2022-07-03 14:15:00 【Study hard 867】
Enter a to # Ending string , This problem requires that all non hexadecimal characters be filtered out ( Case insensitive ), Form a new string of hexadecimal digits , Then convert it to decimal number and output . If there is a character before the first hexadecimal character “-”, It means that the number is negative .
Input format :
The input gives one in a line to # The ending non empty string .
Output format :
Output the converted decimal number on one line . Ensure that the output is in the long integer range .
sample input :
+-P-xf4+-1!#
sample output :
-3905
Code :
#include <stdio.h>
int main() {
char a[50];
int b[50];
int i=0,sum=0,now=0,past=1,k,number=0,m=0;
while((a[i]=getchar())!='#') {
if(a[i]>='0'&&a[i]<='9') {
b[m++]=a[i]-'0';
number++;
} else if(a[i]>='A'&&a[i]<='F') {
b[m++]=a[i]-'7';
number++;
} else if(a[i]>='a'&&a[i]<='f') {
b[m++]=a[i]-'W';
number++;
}
i++;
}
now=i;
k=i;
sum=0;
for(i=0; i<number; i++) {
if(i>0) {
sum=sum*16+b[i];
} else if(i==0) {
sum=sum+b[i];
}
}
for(i=0; i<now; i++) {
if((a[i]>='0'&&a[i]<='9')||(a[i]>='A'&&a[i]<='F')||(a[i]>='a'&&a[i]<='f')) {
now=i;
break;
}
}
for(i=0; i<k; i++) {
if(a[i]=='-') {
past=i;
break;
}
}
if(past>now) {
printf("%d",sum);
} else {
printf("%d",-sum);
}
}
边栏推荐
- Simulated access
- TS code automatically generates JS
- NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线
- Generate directories from web content
- MongoDB数据库入门的常用命令
- 7-23 currency conversion (using array conversion)
- Current situation, analysis and prediction of information and innovation industry
- JS get DPI, PX to cm, cm to PX
- Comprehensive evaluation of good-looking, easy-to-use and powerful handwriting note taking software: notability, goodnotes, marginnote, handwriting, notes writers, collanote, collanote, prodrafts, not
- 中国PETG市场预测及战略研究报告(2022版)
猜你喜欢
Jiuyi cloud black free encryption free version source code
QT learning 22 layout manager (I)
[Jilin University] information sharing of postgraduate entrance examination and re examination
Redis:字符串類型數據的操作命令
泰凌冲刺科创板:拟募资13亿 国家大基金与小米长江是股东
牛客网:过河卒
Exercise 10-2 recursive factorial sum
小项目(servelt+jsp+mysql+EL+JSTL)完成一个登录功能的Servlet,具有增删改查的操作。实现登录身份验证,防止非法登录,防止多点登录,记住用户名密码功能。
FPGA测试方法以Mentor工具为例
好看、好用、强大的手写笔记软件综合评测:Notability、GoodNotes、MarginNote、随手写、Notes Writers、CollaNote、CollaNote、Prodrafts、Noteshelf、FlowUs、OneNote、苹果备忘录
随机推荐
jvm-类加载
x86汇编语言-从实模式到保护模式 笔记
JS Part III
JS get DPI, PX to cm, cm to PX
愉悦资本新双币基金近40亿元完成首次关账
Redis:字符串类型数据的操作命令
Comprehensive evaluation of good-looking, easy-to-use and powerful handwriting note taking software: notability, goodnotes, marginnote, handwriting, notes writers, collanote, collanote, prodrafts, not
Leetcode (4) - - trouver la médiane de deux tableaux ordonnés positifs
7-6 mixed type data format input
The small project (servlet+jsp+mysql+el+jstl) completes a servlet with login function, with the operation of adding, deleting, modifying and querying. Realize login authentication, prevent illegal log
虽然不一定最优秀,但一定是最努力的!
Metal organic framework material zif-8 containing curcumin( [email protected] Nanoparticles) | nano metal organic framework carry
使用并行可微模拟加速策略学习
Back to top implementation
Redis: commandes d'action pour les données de type chaîne
MongoDB索引
Onmenusharetimeline custom shared content is invalid, and the title and icon are not displayed
Exercise 10-1 calculate the sum of 1 to n using recursive functions
Redis:Redis的数据结构、key的操作命令
7-17 crawling worms (break exercise)