In: Computer Science
This code needs to run a working Reverse Polish Calculator, but when I input my commands it only pops my inputs never push them. So my answer is always zero.
Here is my code.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
struct Node
{
int element;
struct Node *next;
};
//Global variable
struct Node *top = NULL;
void push(int ele)
{
struct Node *newNode;
newNode = (struct Node *) malloc(sizeof(struct Node));
newNode->element = ele;
newNode->next = top;
top = newNode;
}
void pop()
{
struct Node *temp;
temp = top;
if(top == NULL)
printf("Stack is empty!!!\n");
else
{
top = top->next;
printf("%d is popped from the stack\n", temp->element);
free(temp);
}
}
bool isEmpty()
{
if(top == NULL)
return true;
else
return false;
}
int returnTopElement()
{
return top->element;
}
void printStack()
{
struct Node *temp;
temp = top;
printf("Stack : ");
while(temp != NULL)
{
printf("%d ", temp->element);
temp = temp->next;
}
printf("\n");
}
int oneByOne(){
char test; //taking character input
int num = 0;
int num1;
int num2;
int atleastone = 0;
printf("Enter your inputs separated by a space and the
operation.\n");
printf("Enter ! when you are done.\n");
while(1){
scanf("%c",&test); //take input of current character
printf("%c is user character\n", test);
if(test == '!'){break;} //terminates character
if(test == ' ' || test == '\n'){ //allows for space or newline
input
if(atleastone) //check if at least one previous digit scanned
push(num);
num = 0, atleastone = 0;
continue;
}
else if(test>='0' && test <='9'){
atleastone = 1;
num = num*10 + (test - '0');
continue;
}
if(top == NULL){
printf("This operation is invalid.\n");
break;
}
num1 = returnTopElement(); pop();
if(top == NULL){
printf("This operation is invalid.\n");
break;
}
num2 = returnTopElement(); pop();
if(test == '+');
push(num2+num1); //allows addition
if(test == '-');
push(num2-num1); //allows subtraction
if(test == '*');
push(num2*num1); //allows multiplication
if(test == '/');
push(num2/num1); //allows division
}
if(top == NULL)
return -1;
return returnTopElement();
}
int arrayInput(char** inputArray){
int i = 0;
int atleastone = 0;
int num = 0;
int num1;
int num2;
char test;
while((*inputArray)[i] != '\0'){
test = (*inputArray)[i];i++; //gets char input from function
if(test == '!'){break;} //terminate character
if(test == ' ' || test == '\n'){ //allows for space or newline
input
if(atleastone)
push(num);
num = 0, atleastone = 0;
continue;
}
else if(test >= '0' && test <= '9'){
atleastone = 1;
num = num*10 + (test- '0');
continue;
}
if(top == NULL){
printf("This operation is invalid.\n");
break;
}
num2 = returnTopElement(); pop();
if(test == '+');
push(num2+num1); //allows addition
if(test == '-');
push(num2-num1); //allows subtraction
if(test == '*');
push(num2*num1); //allows multiplication
if(test == '/');
push(num2/num1); //allows division
}
if(top == NULL)
return -1;
return returnTopElement();
}
int main(int argc, const char * argv[]) {
printf("Check oneByone function: \n");
int ans = oneByOne();
if(top == NULL){
printf("The input is invalid.\n");
}
else{
printf("The answer is %d\n", ans);
}
char * e = "23 45 + 3 *";
char ** expression = &e;
printf("\nChecking arrayInput function for %s \n",
*expression);
printf("The above expression is hardcoded in the code itself.
\n");
ans = arrayInput(expression);
if(top == NULL){
printf("The input expression is invalid. \n");
}
else{
printf("The answer of the input expression is %d\n", ans);
}
return 0;
}
In: Computer Science
Explain the importance of awareness when dealing with employees security. Provide an example of two security policies that would help prevent internal employee fraud.
In: Computer Science
2.20 Domain Lab 3.1 -- Velocity Calculator
You may (or may not) need the following constants:
Velocity Calculator Your program should ask the user (in this order) for:
Your program should then calculate and display the resulting final velocity based on the inputs provided.
In: Computer Science
in java please:
Given any integer, print an English phrase that describes the integer (e.g. “One Thousand, Two Hundred Thirty Four”). An ArrayList must be used in your program.
In: Computer Science
In Python:
Write a function called sum_odd that takes two parameters, then calculates and returns the sum of the odd numbers between the two given integers. The sum should include the two given integers if they are odd. You can assume the arguments will always be positive integers, and the first smaller than or equal to the second.
To get full credit on this problem, you must define at least 1 function, use at least 1 loop, and use at least 1 decision structure.
Examples:
sum_odd(0, 5) should return the value 9 as (1 + 3 + 5) = 9
sum_odd(6, 10) should return the value 16
sum_odd(13, 20) should return the value 64
sum_odd(7, 11) should return the value 27
In: Computer Science
Write a recursive and an iterative function to calculate the nth element in a Fibonacci sequence. A Fibonacci sequence is defined as the element 1, followed by another 1, and each element thereafter is the sum of the previous two elements. For example, the first 9 elements of a Fibonacci sequence are:
This famous sequence was originally used to predict the growth of rabbit populations!
Once you have each of the functions working for n equal to 40, determine which method is more efficient by timing the two separate function calls and printing out the time required for each method call to return the 40th element in the sequence. Return the 40th element to main and print it. After that, print out a complete Fibonacci sequence from element 1 to element 40, along with its position number.
. .
. .
This last part should not be timed.
The timer function you need for this Project is:
Date d1 = new Date();
long milliseconds = D1.getTime();
OR
long start = System.currentTimeMillis();
Turn in the source, output, and a short paragraph explaining the reason the two methods took different amounts of time to execute.
Please write in JAVA
and please write recursive part and iterative part separate!
In: Computer Science
In: Computer Science
There are PRO's and CON's to mounting and emulating the suspect system. Post a short discussion item on some PRO's and CON's of booting up a forensic image.
In: Computer Science
Write down the address of the network, show your calculation.
Write also the broadcast address
2. On a network with an IP address of 133.133.133.13 and a subnet mask of 255.255.248.0 what is the network ID? Show your calculation. Write also the broadcast address
4. Your company has leased a Class C network whose network ID is 222.91.37.0. You want to create 30 subnets within this network. Show how many bits, you would need to change, and the range of addresses in one of the subnets.
In: Computer Science
In: Computer Science
1.Which of the following is a DML operation on table employee?
change an employee's stree address
cancel the insertion of 3 new employees in table employee
add a column phone2 to table employee to limit the salary amount to be at most $200,000
add a constraint in table employee to limit the salary amount to be at most $200,000
In: Computer Science
15.1 File IO Warm-up
For this exercise, you will receive two string inputs, which are the names of the text files. You need to check if the input file exists. If input file doesn't exist, print out "Input file does not exist."
You are required to create 4 functions:
void removeSpace (ifstream &inStream, ofstream &outStream): removes white space from a text file and write to a new file. If write is successful, print out "Text with no white space is successfully written to outFileName."
int countChar (ifstream &inStream): returns number of character in input file.
int countWord(ifstream &inStream): returns number of words in input file.
int countLines(ifstream &inStream): returns number of lines in input file.
After calling each function, you have to reset the state flags and move the stream pointer to beginning of file. You can do that by closing and re-opening the file, or by calling member functions:
inStream.clear(); inStream.seekg(0);
Example:
Input: input.txt output.txt
Output:
Text with no white space is successfully written to output.txt.
Number of characters is: 154
Number of words is: 36
Number of lines is: 10
here is the code in C++:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void removeSpace(ifstream &inStream, ofstream
&outStream);
int countChar (ifstream &inStream);
int countWords(ifstream &inStream);
int countLines (ifstream &inStream);
int main()
{
string input, output;
cin >> input >> output;
ifstream inStream(input);
ofstream outStream(output);
if(inStream.fail())
cout << "Input file does not exist.\n" ;
else
{
removeSpace(inStream, outStream);
cout << "Text with no white space is successfully written to
outFileName.\n";
inStream.clear();
inStream.seekg(0, inStream.beg);
cout << "Number of characters is: " <<
countChar(inStream) << endl;
inStream.clear();
inStream.seekg(0, inStream.beg);
cout << "Number of words is: " <<
countWords(inStream) << endl;
inStream.clear();
inStream.seekg(0, inStream.beg);
cout << "Number of lines is: " <<
countLines(inStream) << endl;
inStream.close();
}
outStream.close();
return 0;
}
//define your functions here
In: Computer Science
In: Computer Science