当前位置:网站首页>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);
}
}
边栏推荐
- 6-9 statistics of single digits (15 points)
- allegro,orcad, net alias,port,off-page connector之间的异同点和如何选取
- 中国PETG市场预测及战略研究报告(2022版)
- 泰凌冲刺科创板:拟募资13亿 国家大基金与小米长江是股东
- [clean up the extraordinary image of Disk C]
- Redis:字符串类型数据的操作命令
- Article content typesetting and code highlighting
- 7-4 BCD decryption (10 points)
- Webpage connection database ~ simple implementation of addition, deletion, modification and query complete code
- Leetcode (4) -- find the median of two positively ordered arrays
猜你喜欢
全局事件总线
Leetcode(4)——寻找两个正序数组的中位数
Mysql多表查询 #子查询
Exercise 6-2 using functions to sum special A-string sequences
FPGA test method takes mentor tool as an example
28:第三章:开发通行证服务:11:在配置文件中定义属性,然后在代码中去获取;
7-10 calculate salary
[clean up the extraordinary image of Disk C]
Understanding of closures
Leetcode (4) - - trouver la médiane de deux tableaux ordonnés positifs
随机推荐
Collection of mobile adaptation related articles
Eight sorts
JS get DPI, PX to cm, cm to PX
Common plug-ins for vite project development
QT learning 24 layout manager (III)
7-10 calculate salary
中感微冲刺科创板:年营收2.4亿净亏1782万 拟募资6亿
Exercise 10-2 recursive factorial sum
Nucleic acid modified metal organic framework drug carrier | pcn-223 metal organic framework encapsulated ad adamantane | zif-8 encapsulated adriamycin (DOX)
allegro,orcad, net alias,port,off-page connector之间的异同点和如何选取
js 2023. String pair equal to the target string after connection
C library function - qsort()
Redis:Redis的数据结构、key的操作命令
QT learning 22 layout manager (I)
2021年区域赛ICPC沈阳站J-Luggage Lock(代码简洁)
关于回溯问题中的排列问题的思考(LeetCode46题与47题)
Scroll detection of the navigation bar enables the navigation bar to slide and fix with no content
JS input number and standard digit number are compared. The problem of adding 0 to 0
7-18 finding the single root of polynomial by dichotomy
一文了解微分段应用场景与实现机制