Hex conversion summary + Blue Bridge past test questions calculator

Base conversion summary

Convert decimal number to K-base number:

string changetentok(long long num,int k){
    
    
		while(num!=0){
    
    
		int temp=num%k;
		num=num/k;
		if(temp<=9&&temp>=0)
		s.push_back(temp+'0');
		else s.push_back(temp-10+'A');
	}
	reverse(s.begin(),s.end());
	return s;
}

Converting a base-k number to base-10:

int changektoten(string str,int k){
    
    
	long long ans=0;
	for(int i=0;i<str.length();i++){
    
    
		if(str[i]>='0'&&str[i]<='9')ans=ans*k+(str[i]-'0');
		else ans=ans*k+(str[i]-'A'+10);
	}
	return ans;
}

Bluebridge past test questions calculator (hex conversion + simulation)**

topic:

img

answer:

#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;
long long change_num_k(string str,int k){
    
    
	long long ans=0;
	for(int i=0;i<str.length();i++){
    
    
		if(str[i]>='0'&&str[i]<='9') ans=ans*k+(str[i]-'0');
		else ans=ans*k+(str[i]-'A'+10);
	}
	return ans;
}

string getans(long long num,int k){
    
    
	if(num==0)return "0";
	string ans;
	while(num){
    
    
		int temp=num%k;
		if(temp>9)
		{
    
    
			ans.push_back(temp-10+'A');
		} 
		else {
    
    
			ans.push_back(temp+'0');
		} 
		num/=k;
	}
	reverse(ans.begin(),ans.end());
	return ans;
}

long long calculator(long long a,long long b,int t){
    
    
	if(t==1)return a+b;
	if(t==2)return a-b;
	if(t==3)return a*b;
	if(t==4)return a/b;
	if(t==5)return a%b;
}
int main(){
    
    
	int n,k=10;
	string str,op;
	int t=0;
	int flag=0; 
	long long num=0;
	scanf("%d",&n);
	while(n--){
    
    
		cin>>op; 
		//scanf("%s",&op);
		if(op=="NUM"){
    
    
			//scanf("%d",&str);
			cin>>str; 
			if(flag){
    
    
				num=calculator(num,change_num_k(str,k),t);
			}
			else{
    
    
				num=change_num_k(str,k);
				flag=1; 
			}
		}
		else if(op=="CHANGE"){
    
    
			scanf("%d",&k);
		}
		else if(op=="CLEAR"){
    
    
			flag=0;
		}
		else if(op=="ADD"){
    
    
			t=1;
		}
		else if(op=="SUB"){
    
    
			t=2;
		}
		else if(op=="MUL"){
    
    
			t=3;
		}
		else if(op=="DIV"){
    
    
			t=4;
		}
		else if(op=="MOD"){
    
    
			t=5;
		}
		else if(op=="EQUAL"){
    
    
			cout<<getans(num,k)<<endl; 
		}
	}
	return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325982762&siteId=291194637