当前位置:网站首页>Exercise 7-8 converting strings to decimal integers (15 points)
Exercise 7-8 converting strings to decimal integers (15 points)
2022-07-04 09:47:00 【skeet follower】
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
The code is as follows :
#include <stdio.h>
#include <string.h>
#include <math.h>
int main()
{
char str[80]= {'\0'};
int i=0,k=0,m,sum=0;
char c;
scanf("%c",&c);
int flag = 1,flag1=1;
while(c!='#')
{
if(c>='a'&&c<='f')
{
flag=0;
str[i]=c-87;
i++;
}
else if(c>='A'&&c<='F')
{
flag=0;
str[i]=c-55;
i++;
}
else if(c>='0'&&c<='9')
{
flag =0;
str[i]=c-48;
i++;
}
else if(c=='-'&&flag==1){// Determine whether there is a character before the first hexadecimal character “-”
flag1=-1;
}
scanf("%c",&c);
}
for(m=i-1;m>=0;m--){// Hexadecimal to decimal ;
sum+=str[m]*pow(16,k);
k++;
}
printf("%d",flag1*sum);
return 0;
}
边栏推荐
- Exercise 9-5 address book sorting (20 points)
- 【leetcode】29. Divide two numbers
- Golang Modules
- El Table Radio select and hide the select all box
- Investment analysis and future production and marketing demand forecast report of China's paper industry Ⓥ 2022 ~ 2028
- Global and Chinese market of air fryer 2022-2028: Research Report on technology, participants, trends, market size and share
- SQL replying to comments
- 华为联机对战如何提升玩家匹配成功几率
- Go context basic introduction
- 智能网关助力提高工业数据采集和利用
猜你喜欢
Hands on deep learning (39) -- gating cycle unit Gru
C language pointer classic interview question - the first bullet
PHP personal album management system source code, realizes album classification and album grouping, as well as album image management. The database adopts Mysql to realize the login and registration f
Svg image quoted from CodeChina
Summary of small program performance optimization practice
Hands on deep learning (32) -- fully connected convolutional neural network FCN
JDBC and MySQL database
libmysqlclient. so. 20: cannot open shared object file: No such file or directory
C # use gdi+ to add text to the picture and make the text adaptive to the rectangular area
回复评论的sql
随机推荐
El Table Radio select and hide the select all box
Fabric of kubernetes CNI plug-in
Golang 类型比较
智能网关助力提高工业数据采集和利用
Report on investment analysis and prospect trend prediction of China's MOCVD industry Ⓤ 2022 ~ 2028
【leetcode】540. A single element in an ordered array
2022-2028 global elastic strain sensor industry research and trend analysis report
Global and Chinese markets for laser assisted liposuction (LAL) devices 2022-2028: Research Report on technology, participants, trends, market size and share
Golang type comparison
Write a jison parser from scratch (2/10): learn the correct posture of the parser generator parser generator
Les différents modèles imbriqués de listview et Pageview avec les conseils de flutter
Luogu deep foundation part 1 Introduction to language Chapter 4 loop structure programming (2022.02.14)
Flutter 小技巧之 ListView 和 PageView 的各種花式嵌套
Launpad | Basics
2022-2028 global special starch industry research and trend analysis report
How can people not love the amazing design of XXL job
PHP book borrowing management system, with complete functions, supports user foreground management and background management, and supports the latest version of PHP 7 x. Database mysql
Golang Modules
2022-2028 global edible probiotic raw material industry research and trend analysis report
Lauchpad X | 模式