习题
1.7.2 01
#include
#include
using namespace std;
int stringToInt(string s)
{
int res = 0;
if (s.size() == 16)
{
res = s[0] - '0';
int i = 1;
while (i < s.size())
{
res = res*2+(int)(s[i] - '0');
i++;
}
}
return res;
}
int main()
{
string s;
cin >> s;
cout << stringToInt(s);
system("pause");
return 0;
}
1.7.2 02
#include
#include
using namespace std;
long long stringToInt(string s)
{
long long res = 0;
int i = 0;
if (s.size() == 32)
{
while (i < 32)
{
if (s[i] == '0')
{
res = res * 16 + 0;
}
if (s[i] == '1')
{
res = res * 16 + 1;
}
if (s[i] == '2')
{
res = res * 16 + 2;
}
if (s[i] == '3')
{
res = res * 16 + 3;
}
if (s[i] == '4')
{
res = res * 16 + 4;
}
if (s[i] == '5')
{
res = res * 16 + 5;
}
if (s[i] == '6')
{
res = res * 16 + 6;
}
if (s[i] == '7')
{
res = res * 16 + 7;
}
if (s[i] == '8')
{
res = res * 16 + 8;
}
if (s[i] == '9')
{
res = res * 16 + 9;
}
if (s[i] == 'A' || s[i] == 'a')
{
res = res * 16 + 10;
}
if (s[i] == 'B' || s[i] == 'b')
{
res = res * 16 + 11;
}
if (s[i] == 'C' || s[i] == 'c')
{
res = res * 16 + 12;
}
if (s[i] == 'D' || s[i] == 'd')
{
res = res * 16 + 13;
}
if (s[i] == 'E' || s[i] == 'e')
{
res = res * 16 + 14;
}
if (s[i] == 'F' || s[i] == 'f')
{
res = res * 16 + 15;
}
i++;
}
}
return res;
}
int main()
{
string s;
cin >> s;
cout << stringToInt(s);
system("pause");
return 0;
}
1.7.2 03
#include
#include
using namespace std;
string ToString(int x)
{
string res = "";
while (x)
{
res += to_string(x%2);
x /= 2;
}
reverse(res.begin(),res.end());
return res;
}
int main()
{
int x;
cin >> x;
cout << ToString(x);
system("pause");
return 0;
}
1.7.2 04
#include
#include
using namespace std;
char sign[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
string ToString(int x)
{
string res = "";
while (x)
{
res += sign[x % 16];
x /= 16;
}
reverse(res.begin(), res.end());
return res;
}
int main()
{
int x;
cin >> x;
cout << ToString(x);
system("pause");
return 0;
}
1.7.2 05
import java.util.*;
public class solve {
static String addByBase(String a, String b, int base) {
StringBuffer ans = new StringBuffer();
int n = Math.max(a.length(), b.length()), carry = 0;
for (int i = 0; i < n; i++) {
carry += i < a.length() ? (a.charAt(a.length() - 1 - i) - '0') : 0;
carry += i < b.length() ? (b.charAt(b.length() - 1 - i) - '0') : 0;
ans.append((char) (carry % base + '0'));
carry /= base;
}
if (carry > 0) {
ans.append('1');
}
ans.reverse();
return ans.toString();
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String a = sc.next();
String b = sc.next();
int base = sc.nextInt();
System.out.println(addByBase(a, b, base));
}
}
1.7.2 06
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
Scanner s = new Scanner(System.in);
while (s.hasNext()) {
long i = Long.parseLong(s.next().replaceAll("\+", ""), 16) +
Long.parseLong(s.next().replaceAll("\+", ""), 16);
if (i < 0)
System.out.println("-" + Long.toHexString(-i).toUpperCase());
else
System.out.println(Long.toHexString(i).toUpperCase());
}
}
}
1.7.2 07
#include
using namespace std;
int main()
{
__int64 a, b, sum;
while (scanf_s("%I64X%I64X", &a, &b) != EOF)
{
sum = a * b;
if (sum >= 0)
printf("%I64Xn", sum);
else
printf("-%I64Xn", -sum);
}
return 0;
}
1.7.2 08
Java源代码
public class test {
public static void main(String[] args){
int Y =0 ;
int X = (Y + 4) * 3;
}
}
Java源代码反汇编后的结果
public class test {
public test();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."":()V
4: return
public static void main(java.lang.String[]);
Code:
0: iconst_0
1: istore_1
2: iload_1
3: iconst_4
4: iadd
5: iconst_3
6: imul
7: istore_2
8: return
}
1.7.2 09
#include
#include
int main()
{
unsigned int a = 1;
unsigned int b = -1;
printf("a is 0x%Xn", a);
printf("b is 0x%Xn", b);
printf("a-b is 0x%llXn", (unsigned long long)a - (unsigned long long)b);
return 0;
}