将十六进制字符串转换为十进制整数
程序填空,不要改变与输入输出有关的语句。输入一个正整数repeat(0<repeat<10),做repeat次下列运算:输入一个以回车结束的字符串(少于80个字符),滤去...
程序填空,不要改变与输入输出有关的语句。
输入一个正整数 repeat (0<repeat<10),做 repeat 次下列运算:
输入一个以回车结束的字符串(少于80个字符),滤去所有的非十六进制字符后,组成一个新字符串(十六进制形式),然后将其转换为十进制数后输出。
输入输出示例:括号内为说明
输入:
2 (repeat=2)
10
+A
输出:
Dec = 16
Dec = 10
#include "stdio.h"
#include "string.h"
#define MAXLEN 80
int main(void)
{
char ch;
char str[MAXLEN], num[MAXLEN];
int i, k;
int repeat, ri;
long number;
scanf("%d", &repeat);
getchar();
for(ri = 1; ri <= repeat; ri++){
i = 0;
while((ch = getchar()) != '\n'){
str[i]=ch;
i++;
}
str[i] = 0;
/*---------*/
printf("Dec = %ld\n",number);
}
} 展开
输入一个正整数 repeat (0<repeat<10),做 repeat 次下列运算:
输入一个以回车结束的字符串(少于80个字符),滤去所有的非十六进制字符后,组成一个新字符串(十六进制形式),然后将其转换为十进制数后输出。
输入输出示例:括号内为说明
输入:
2 (repeat=2)
10
+A
输出:
Dec = 16
Dec = 10
#include "stdio.h"
#include "string.h"
#define MAXLEN 80
int main(void)
{
char ch;
char str[MAXLEN], num[MAXLEN];
int i, k;
int repeat, ri;
long number;
scanf("%d", &repeat);
getchar();
for(ri = 1; ri <= repeat; ri++){
i = 0;
while((ch = getchar()) != '\n'){
str[i]=ch;
i++;
}
str[i] = 0;
/*---------*/
printf("Dec = %ld\n",number);
}
} 展开
3个回答
展开全部
number=0;
for(k=0;k<i;k++)
{ if(str[k]>='a'&&str[k]<='f') number=(str[k]-'a'+10)+16*number;
else if(str[k]>='A'&&str[k]<='F') number=(str[k]-'A'+10)+16*number;
else if(str[k]>='0'&&str[k]<='9') number=(str[k]-'0')+16*number;}
#include "stdio.h"
#include "string.h"
#define MAXLEN 80
int main(void)
{
char ch;
char str[MAXLEN], num[MAXLEN];
int i, k;
int repeat, ri;
long number;
scanf("%d", &repeat);
getchar();
for(ri = 1; ri <= repeat; ri++){
i = 0;
while((ch = getchar()) != '\n'){
str[i]=ch;
i++;
}
str[i] = 0;
number=0;
for(k=0;k<i;k++)
{ if(str[k]>='a'&&str[k]<='f') number=(str[k]-'a'+10)+16*number;
else if(str[k]>='A'&&str[k]<='F') number=(str[k]-'A'+10)+16*number;
else if(str[k]>='0'&&str[k]<='9') number=(str[k]-'0')+16*number;}
printf("Dec = %ld\n",number);
}
}
for(k=0;k<i;k++)
{ if(str[k]>='a'&&str[k]<='f') number=(str[k]-'a'+10)+16*number;
else if(str[k]>='A'&&str[k]<='F') number=(str[k]-'A'+10)+16*number;
else if(str[k]>='0'&&str[k]<='9') number=(str[k]-'0')+16*number;}
#include "stdio.h"
#include "string.h"
#define MAXLEN 80
int main(void)
{
char ch;
char str[MAXLEN], num[MAXLEN];
int i, k;
int repeat, ri;
long number;
scanf("%d", &repeat);
getchar();
for(ri = 1; ri <= repeat; ri++){
i = 0;
while((ch = getchar()) != '\n'){
str[i]=ch;
i++;
}
str[i] = 0;
number=0;
for(k=0;k<i;k++)
{ if(str[k]>='a'&&str[k]<='f') number=(str[k]-'a'+10)+16*number;
else if(str[k]>='A'&&str[k]<='F') number=(str[k]-'A'+10)+16*number;
else if(str[k]>='0'&&str[k]<='9') number=(str[k]-'0')+16*number;}
printf("Dec = %ld\n",number);
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include "stdio.h"
#include "string.h"
#define MAXLEN 80
int main(void)
{
char ch;
char str[MAXLEN], num[MAXLEN];
int i, k;
int repeat, ri;
long number;
scanf("%d", &repeat);
getchar();
for(ri = 1; ri <= repeat; ri++)
{i = 0;
while((ch = getchar()) != '\n')
{
str[i]=ch;
i++;
}
str[i] = 0;
k=0;
ch=i;
for(i=0;i<ch;i++)
{
if((0<=str[i]<=9)&&(A<=str[i]<=F))
{num[k]=str[i];
k++;
}
}
for(i=0;i<k;i++)
{if(num[i]==A) str[i]=10;
if(num[i]==B) str[i]=11;
if(num[i]==C) str[i]=12;
if(num[i]==D) str[i]=13;
if(num[i]==E) str[i]=14;
if(num[i]==F) str[i]=15;
}
number=0;
for(i=0;i<k;i++)
{ch=num[i]*16^(k-1);
number=number+ch;
}
printf("Dec = %ld\n",number);
}
}
#include "string.h"
#define MAXLEN 80
int main(void)
{
char ch;
char str[MAXLEN], num[MAXLEN];
int i, k;
int repeat, ri;
long number;
scanf("%d", &repeat);
getchar();
for(ri = 1; ri <= repeat; ri++)
{i = 0;
while((ch = getchar()) != '\n')
{
str[i]=ch;
i++;
}
str[i] = 0;
k=0;
ch=i;
for(i=0;i<ch;i++)
{
if((0<=str[i]<=9)&&(A<=str[i]<=F))
{num[k]=str[i];
k++;
}
}
for(i=0;i<k;i++)
{if(num[i]==A) str[i]=10;
if(num[i]==B) str[i]=11;
if(num[i]==C) str[i]=12;
if(num[i]==D) str[i]=13;
if(num[i]==E) str[i]=14;
if(num[i]==F) str[i]=15;
}
number=0;
for(i=0;i<k;i++)
{ch=num[i]*16^(k-1);
number=number+ch;
}
printf("Dec = %ld\n",number);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
胡乱写了一个 你参考下
#include<iostream>
#include<string>
#include<functional>
#include<cctype>
#include<vector>
#include<algorithm>
using namespace std;
string add(const string &a,const string &b);
string multiply(const string &a,char b);
string multi(const string &a,const string &b);
int main(){
string b;
cin>>b;
//b="12345k,mbnw{6789abxcdEf";
b.erase(remove_if(b.begin(),b.end(),not1(ptr_fun(isxdigit))),b.end());
cout<<b.c_str();
//12345b6789abcdEf
vector<string> s;
string sum="1";
for(int j=0;j!=b.size();++j){
for(int i=1;i!=b.size()-j;++i)
sum = multi(sum,"16");
s.push_back(sum);
sum="1";
}
sum="0"; string first;
for(int j=0;j!=b.size();++j)
{
switch(b[j])
{
case 'a':
case 'A':
first="10";
break;
case 'b':
case 'B':
first="11";
break;
case 'c':
case 'C':
first="12";
break;
case 'd':
case 'D':
first="13";
break;
case 'e':
case 'E':
first="14";
break;
case 'f':
case 'F':
first="15";
break;
default:
first=string(1,b[j]);
}
sum=add(sum,multi(first,s[j]));
}
cout<<endl;
cout<<sum;
system("pause");
}
string add(const string &a,const string &b)
{
string sh,lo,r; int g=0;
if(a.size()>b.size())
{
sh=string(a.size()-b.size(),'0');
sh+=b; lo=a;
}
else
{
sh=string(b.size()-a.size(),'0');
sh+=a; lo=b;
}
r.resize(sh.size());
for(int i=sh.size()-1;i>=0;--i)
{
r[i]=sh[i]+lo[i]-48+g;
if(r[i]>'9')
{
r[i]-=10;
g=1;
}
else
{
g=0;
}
}
if(g==1)
r="1"+r;
return r;
}
string multiply(const string &a,char b)
{
string r;
r.resize(a.size()); int g=0;
if(b=='0')
return "0";
if(b=='1') //加快计算
return a;
for(int i=a.size()-1;i>=0;--i)
{
r[i]=(a[i]-48)*(b-48)%10+48+g;
if(r[i]>'9')
{
r[i]-=10;
g=((a[i]-48)*(b-48))/10+1;
continue;
}
g=((a[i]-48)*(b-48))/10;
}
if(g!=0)
r.insert(0,string(1,g+48));
return r;
}
string multi(const string &a,const string &b)
{
string r("0");
string *c=new string[b.size()];
for(int i=0;i<b.size();++i)
{
c[i]=multiply(a,b[b.size()-1-i])+string(i,'0');
r=add(r,c[i]);
}
delete[] c;
return r;
}
#include<iostream>
#include<string>
#include<functional>
#include<cctype>
#include<vector>
#include<algorithm>
using namespace std;
string add(const string &a,const string &b);
string multiply(const string &a,char b);
string multi(const string &a,const string &b);
int main(){
string b;
cin>>b;
//b="12345k,mbnw{6789abxcdEf";
b.erase(remove_if(b.begin(),b.end(),not1(ptr_fun(isxdigit))),b.end());
cout<<b.c_str();
//12345b6789abcdEf
vector<string> s;
string sum="1";
for(int j=0;j!=b.size();++j){
for(int i=1;i!=b.size()-j;++i)
sum = multi(sum,"16");
s.push_back(sum);
sum="1";
}
sum="0"; string first;
for(int j=0;j!=b.size();++j)
{
switch(b[j])
{
case 'a':
case 'A':
first="10";
break;
case 'b':
case 'B':
first="11";
break;
case 'c':
case 'C':
first="12";
break;
case 'd':
case 'D':
first="13";
break;
case 'e':
case 'E':
first="14";
break;
case 'f':
case 'F':
first="15";
break;
default:
first=string(1,b[j]);
}
sum=add(sum,multi(first,s[j]));
}
cout<<endl;
cout<<sum;
system("pause");
}
string add(const string &a,const string &b)
{
string sh,lo,r; int g=0;
if(a.size()>b.size())
{
sh=string(a.size()-b.size(),'0');
sh+=b; lo=a;
}
else
{
sh=string(b.size()-a.size(),'0');
sh+=a; lo=b;
}
r.resize(sh.size());
for(int i=sh.size()-1;i>=0;--i)
{
r[i]=sh[i]+lo[i]-48+g;
if(r[i]>'9')
{
r[i]-=10;
g=1;
}
else
{
g=0;
}
}
if(g==1)
r="1"+r;
return r;
}
string multiply(const string &a,char b)
{
string r;
r.resize(a.size()); int g=0;
if(b=='0')
return "0";
if(b=='1') //加快计算
return a;
for(int i=a.size()-1;i>=0;--i)
{
r[i]=(a[i]-48)*(b-48)%10+48+g;
if(r[i]>'9')
{
r[i]-=10;
g=((a[i]-48)*(b-48))/10+1;
continue;
}
g=((a[i]-48)*(b-48))/10;
}
if(g!=0)
r.insert(0,string(1,g+48));
return r;
}
string multi(const string &a,const string &b)
{
string r("0");
string *c=new string[b.size()];
for(int i=0;i<b.size();++i)
{
c[i]=multiply(a,b[b.size()-1-i])+string(i,'0');
r=add(r,c[i]);
}
delete[] c;
return r;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询