/**
* This program Performs various number base conversions. It also verifies if
* a number is valid in its base.
* Author: M. Rahman
* Date: 06 September 2018
*/
public class NumberConversion {
public static String dec2any(String dec, int base) {
/**
* Converts a decimal value to a target base
* inputs:
* dec: the decimal value to be converted
* base: the target base
* output: 256-base as dotted decimal, hex as usual, bases
* over 10 except 256 and 16 are parenthesis separated.
*/
long rem = 0;
String any = "";
String remhex = "";
long num = Long.parseLong(dec);
if (base <= 1)
any = "ERROR!";
else if (base == 10)
any = dec;
else {
while (num > 0) {
// get remainder first then get the quotient
rem = num % (long) base;
num = num / (long) base;
if ((base > 1) && (base < 10))
any = rem + any;
else if (base == 16) {
switch ((int)rem) {
case 10: remhex = "A";
break;
case 11: remhex = "B";
break;
case 12: remhex = "C";
break;
case 13: remhex = "D";
break;
case 14: remhex = "E";
break;
case 15: remhex = "F";
break;
default : remhex = Long.toString(rem);
break;
}
any = remhex + any;
}
else if (base == 256) {
if (any.equals(""))
any = Long.toString(rem);
else
any = Long.toString(rem) + "." + any;
}
else {
any = "(" + Long.toString(rem) + ")" + any;
}
}
}
return any;
} // end of method dec2any
/**
*/
public static String any2dec(String any, int base) {
/**
* Converts a number from any base to decimal
* inputs
* any: the given number in any base; must be formatted
* appropriately, 256-base as dotted decimal, hex as usual,
* bases over 10 except 256 and 16 are parenthesis separated.
* base: base of the given number
* output: a decimal number as string, "ERROR!" for invalid input
*/
String dec = "";
long num = 0;
// if given number is not valid in base then flag error
if (!isValidInBase(any, base)) {
dec = "ERROR!";
}
else {
// parse the input
if (base == 10) {
dec = any;
}
else if (base < 10) {
int strlen = any.length();
for (int i = 0 ; i < strlen; i++) {
char ch = any.charAt(strlen - i - 1);
long digit = Long.parseLong(String.valueOf(ch));
num = digit * (long)Math.pow(base,i) + num;
}
dec = Long.toString(num);
}
else if (base == 16) {
int strlen = any.length();
for (int i = 0 ; i < strlen; i++) {
char ch = (any.toUpperCase()).charAt(strlen-i-1);
int digit = ch - 'A' + 10;
num = (long) digit * (long)Math.pow(base,i) + num;
}
dec = Long.toString(num);
}
else if (base == 256) {
String[] decimals = any.split("\\.");
int len = decimals.length;
for (int i = 0; i < len; i++) {
num = Long.parseLong(decimals [len-i-1]) * (long)Math.pow(base,i) + num;
}
dec = Long.toString(num);
}
else {
String temp = any.replaceAll(" ", "");
if ((temp.charAt(0) == '(') &&
(temp.charAt(any.length() - 1) == ')')) {
temp = temp.substring(1,any.length()-1);
String[] decimals = temp.split("\\)\\(");
int len = decimals.length;
for (int i = 0; i < len; i++) {
num = Long.parseLong(decimals [len-i-1]) * (long)Math.pow(base,i) + num;
}
dec = Long.toString(num);
}
else {
dec = "ERROR!";
}
}
}
return dec;
} // end of method any2dec
/**
*/
public static String any2any(String any, int frombase, int tobase) {
/**
* Converts a number from any base to any base
* inputs
* any: the given number to be converted
* frombase: the base of the given number
* tobase: target base
* output: a number in tobase as string, "ERROR!" for invalid input
* Example of a 43-base number: (34)(12)(42)(42)
*/
String temp = "ERROR!";
String retval = any2dec(any, frombase);
if (!retval.equals("ERROR!")) {
temp = retval;
retval = dec2any(temp, tobase);
}
else {
retval = temp;
}
return retval;
} // end of method any2any
/**
*/
public static boolean isValidInBase (String any, int base) {
/**
* Verifies whether the given number is a valid in the base
* inputs
* any: the given number to be converted
* base: the base of the given number
* output: true if valid number in the base, false otherwise
*/
String temp = "";
String regex = "";
boolean retval = false;
if (base <= 0)
retval = false;
else if (base ==1) {
if (Integer.parseInt(any) != 0)
retval = false;
else
retval = true;
}
else if (base < 10) {
regex = "[0-" + (base-1) + "]*";
if (!any.matches(regex))
retval = false;
else
retval = true;
}
else if (base == 10) {
regex = "[0-9]*";
if (!any.matches(regex))
retval = false;
else
retval = true;
}
else if (base == 16) {
regex = "[\\da-fA-F]+";
if (!any.matches(regex))
retval = false;
else
retval = true;
}
else if (base == 256) {
regex = "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4]" +
"[0-9]|25[0-5])\\.){0,}([0-9]|[1-9][0-9]|1" +
"[0-9]{2}|2[0-4][0-9]|25[0-5])$";
if (!any.matches(regex))
retval = false;
else
retval = true;
}
else {
// any other base
regex = "^([\\(]([0]|[1-9]|([1-9][0-9]+))[\\)])*$";
if (!any.matches(regex))
retval = false;
else {
temp = any.substring(1,any.length()-1);
String[] decimals = temp.split("\\)\\(");
retval = true;
int len = decimals.length;
for (int i = 0; i < len; i++) {
if (Long.parseLong(decimals [len-i-1]) >= (long)base)
retval = false;
}
}
}
return retval;
} // end of method isValidInBase
}
val1 = gets.to_i sym = gets.chomp val2 = gets.to_i def addition(val1, val2) return val1+val2 end add = addition(val1, val2) def subtraction(val1, val2) return val1-val2 end sub = subtraction(val1, val2) def division(val1, val2) return val1/val2 end div = division(val1, val2) def multiplication(val1, val2) return val1*val2 end mult = multiplication(val1, val2) if sym == "+" puts add elsif sym == "-" puts sub elsif sym == "/" puts div elsif sym == "*" puts mult else puts "error" end
retval = true;
}
else {
// any other base
regex = "^([\\(]([0]|[1-9]|([1-9][0-9]+))[\\)])*$";
if (!any.matches(regex))
retval = false;
else {
temp = any.substring(1,any.length()-1);
String[] decimals = temp.split("\\)\\(");
retval = true;
int len = decimals.length;
for (int i = 0; i < len; i++) {
if (Long.parseLong(decimals [len-i-1]) >= (long)base)
retval = false;
}
}
else if (base == 10) {
regex = "[0-9]*";
if (!any.matches(regex))
retval = false;
else
retval = true;
}
else
/** * This program Performs various number base conversions. It also verifies if * a number...
C++ problem where should I do overflow part? in this code do not write a new code for me please /////////////////// // this program read two number from the user // and display the sum of the number #include <iostream> #include <string> using namespace std; const int MAX_DIGITS = 10; //10 digits void input_number(char num[MAX_DIGITS]); void output_number(char num[MAX_DIGITS]); void add(char num1[MAX_DIGITS], char num2[MAX_DIGITS], char result[MAX_DIGITS], int &base); int main() { // declare the array = {'0'} char num1[MAX_DIGITS] ={'0'}; char...
Write a C++ program which performs +, -, *, / and $ on hexadecimal operands. The maximum length of any operand or a solution is 40 digits. The input will be in the following format: Op1 op op2 = There is no space between operands and operator. Note 5/2 = quotient 2, remainder 1 2$3 = 8 The output should be of the form 2*3=6. Read date from a file. TEST DATA (input.txt): AAAA+BBF= BFD+2DE= 100*AA= 100$5= 100/F= 10000000000000-1= AAAAABBBBBCCCCCDDDDDEEEEEFFFFF-ABCDEF0123456789ABCDEF=...
I need assistance with this code. Is there any way I can create this stack class (dealing with infix to postfix then postfix evaluation) without utilizing <stdio.h> and <math.h>? ____________________________________________________________________________________________ C++ Program: #include <iostream> #include <string> #include <stdio.h> #include <math.h> using namespace std; //Stack class class STACK { private: char *str; int N; public: //Constructor STACK(int maxN) { str = new char[maxN]; N = -1; } //Function that checks for empty int empty() { return (N == -1); } //Push...
I was asked to write a program that when divisible by 2- reverses the order of the letters in a sentence when divisible by 3- changes all lowercase letter into uppercase letters in a sentence when divisible by 5 skips the vowels in a sentence this is what I have so far. my reverseMode works fine. #include <iostream> #include <string> #include<cstring> using namespace std; void reverseMode(char* str); void upperMode(char* str); void goodbyeVowels(char* str); char str[50], rstr[50]; //initializing my character arrray...
Create a method based program to find if a number is prime and then print all the prime numbers from 1 through 500 Method Name: isPrime(int num) and returns a boolean Use for loop to capture all the prime numbers (1 through 500) Create a file to add the list of prime numbers Working Files: public class IsPrimeMethod { public static void main(String[] args) { String input; // To hold keyboard input String message; // Message...
My Question is: I have to modify this program, even a small modification is fine. Can anyone give any suggestion and solution? Thanks in Advanced. import java.util.*; class arrayQueue { protected int Queue[]; protected int front, rear, size, len; public arrayQueue(int n) { size = n; len = 0; Queue = new int[size]; front = -1; rear = -1; } public boolean isEmpty() { return front == -1; } public boolean isFull() { return front == 0 && rear ==size...
Here is a serial program in C and parallel program in OpenMP that takes in a string as input and counts the number of occurrences of a character you choose. Why is the runtime for the output for the OpenMP parallel program much longer? Serial Program #include <stdio.h> #include <string.h> #include <time.h> int main(){ char str[1000], ch; int i, frequency = 0; clock_t t; struct timespec ts, ts2; printf("Enter a string: "); gets(str); int len = strlen(str); printf("Enter a character...
CAN YU HELP ME CONSTRUCT A FLOW CHART FOR THE FOLLOW PROGRAM ? C++ CODE: #include<iostream> using namespace std; int main() { //declaring variable num int num; //prompting user to enter a number cout<<"Input a number to check prime or not:"; //reading input from user cin>>num; //loop to check user input for positive number while(num<0) { cout<<"Error! Positive Integers Only.\n"; cout<<"Input a number to check prime or not:"; cin>>num; } //initializing isPrime variable with false bool isPrime = true; //loop...
my program wont run on my computer and im not understanding why. please help. #include<iostream> #include<iomanip> #include<string> #include "bookdata.h" #include "bookinfo.h" #include "invmenu.h" #include "reports.h" #include "cashier.h" using namespace std; const int ARRAYNUM = 20; BookData bookdata[ARRAYNUM]; int main () { bool userChoice = false; while(userChoice == false) { int userInput = 0; bool trueFalse = false; cout << "\n" << setw(45) << "Serendipity Booksellers\n" << setw(39) << "Main Menu\n\n" << "1.Cashier Module\n" << "2.Inventory Database Module\n" << "3.Report Module\n"...
I need help debugging this Java program. I am getting this error message: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2 at population.Population.main(Population.java:85) I am not able to run this program. ------------------------------------------------------------------- import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Scanner; /* Linked list node*/ class node { long data; long year; String country; node next; node(String c,long y,long d) { country=c; year=y; data = d; next = null; } } public class Population { private static node head; public static void push(String...