C++ question.
I want to write all output to the file "output.txt", but it will write just first character. Can you fix it?
#include
#include
#include
#include
#include
using namespace std;
using std::cin;
using std::cout;
using std::string;
// remove dashes convert letters to upper case
string normalize(const string &isbn) {
string ch;
for (char i : isbn) {
if (i == '-') {
continue; // if "-" then skip it
}
if (isalpha(i)) {
i = toupper(i); // Check uppercase
}
ch += i;
}
return ch;
}
// return the number of digits in to the string
size_t numDigits(string &str) {
size_t numDigits = 0;
for (char ch : str) {
if (isdigit(ch)) {
++numDigits;
}
}
return numDigits;
}
enum ValidationCode {
Ok, // validation passed
NumDigits, // wrong number of digits
ExtraChars,// extra characters in isbn
Done
};
enum ValidationCode validate(string &isbn) {
int Done = 4;
string normal = normalize(isbn);
size_t count = numDigits(normal);
if (normal.size() == Done)
exit(0);
if (count != 10) {
return NumDigits;
}
if (normal.size() == 10 || normal.size() == 11 && normal[10] == 'X') {
return Ok;
}
return ExtraChars;
}
int main() {
// open a file
ofstream file("output.txt");
//The following code is taken from (https://en.cppreference.com/w/cpp/io/basic_ios/fail)
// check if the file can open
if(!file) // operator! is used here
{
std::cout << "File opening failed\n";
return EXIT_FAILURE;
} //end of borrowed code
// read a file
//The following code is referenced from (https://stackoverflow.com/questions/7868936/read-file-line-by-line-using-ifstream-in-c)
std::ifstream ifs("test_data.txt");
// check if the file can read
if (ifs.fail())
{
std::cerr << "test_data.txt could not read" << std::endl;
return -1;
} //end of referenced code
std::string str;
while (ifs >> str){
switch (validate(str)) {
case Ok:
cout << str << " is a valid isbn\n";
file << str << " is a valid isbn\n";
break;
case NumDigits:
cout << str << " doesn't have 10 digits\n";
file << str << " doesn't have 10 digits\n";
break;
case ExtraChars:
cout << str << " has extra characters\n";
file << str << " has extra characters\n";
break;
default:
cout << "ERROR: validate(" << str << ") return an unknown status\n";
file << "ERROR: validate(" << str << ") return an unknown status\n";
break;
}
}
ifs.close();
file.close();
}
test_data.txt
1-214-02031-3
0-070-21604-5
2-14-241242-4
2-120-12311-x
0-534-95207-x
2-034-00312-2
1-013-10201-2
2-142-1223
3-001-0000a-4
done
In: Computer Science
Using the sample code included at the end of the document to create the tables, Please write an anonymous PL/SQL program the following problems.
Problem 1. Print out estimated charge for rental ID 1 if the customer returns the tool in time. The charge is computed by the price in the price_tool table * number of units the customer plans to rent. E.g., if a customer rents a tool hourly for 5 hours, and the hourly rate for the tool is $6, the estimated charge should be $30. [30 points]
Problem 2. [30 points] Print out name of tools rented by Susan and their due time.
--------- Sample code
drop table rental cascade constraints;
drop table tool_price cascade constraints;
drop table tool cascade constraints;
drop table category cascade constraints;
drop table cust cascade constraints;
drop table time_unit cascade constraints;
create table cust(
cid int, -- customer id
cname varchar(50), --- customer name
cphone varchar(10), --- customer phone
cemail varchar(30), --- customer email
primary key(cid)
);
insert into cust values(1,'John','1234567888','[email protected]');
insert into cust values(2,'Susan','1235555555','[email protected]');
insert into cust values(3,'David','1237777777','[email protected]');
create table category(
ctid int, --- category id
ctname varchar(30), --- category name
parent int, --- parent category id
primary key(ctid),
foreign key (parent) references category
);
insert into category values(1,'mower',null);
insert into category values(2,'electric mower',1);
insert into category values(3,'gasoline mower',1);
insert into category values(4,'carpet cleaner',null);
create table tool
(tid int, --- tool id
tname varchar(50), -- tool name
ctid int, --- category id
quantity int,
primary key (tid),
foreign key (ctid) references category
);
insert into tool values(1,'21 inch electric mower',2,2);
insert into tool values(2,'30 inch large gasoline mower',3,2);
insert into tool values(3,'small carpet cleaner',4,2);
insert into tool values(4,'large carpet cleaner',4,2);
create table time_unit
(tuid int, --- time unit id
len interval day to second, --- length of unit, can be 1 hour, 1 day, etc.
min_len int, --- minimal number of units
primary key (tuid)
);
--- hourly minimal four hours.
insert into time_unit values(1, interval '1' hour, 4);
-- hourly minimal 1 day
insert into time_unit values(2, interval '1' day, 1);
--- weekly
insert into time_unit values(3, interval '7' day, 1);
create table tool_price
(tid int, --- too id
tuid int, --- time unit id
price number,
primary key(tid,tuid),
foreign key(tid) references tool,
foreign key(tuid) references time_unit
);
--- mower, $20 per 4 hours. $30 per day
insert into tool_price values(1,1,5.00);
insert into tool_price values(1,2,30);
insert into tool_price values(1,3,120);
insert into tool_price values(2,1,7.00);
insert into tool_price values(2,2,40);
insert into tool_price values(2,3,160);
insert into tool_price values(3,1,6.00);
insert into tool_price values(3,2,32);
insert into tool_price values(3,3,125);
insert into tool_price values(4,1,7.00);
insert into tool_price values(4,2,40);
insert into tool_price values(4,3,160);
create table rental
(
rid int, --- rental id
cid int, --- customer id
tid int, --- tool id
tuid int, --- time unit id
num_unit int, --- number of units, if unit = 1 hour, num_unit = 5 means 5 hours.
start_time timestamp, -- rental start time
end_time timestamp, --- suppose rental end_time
return_time timestamp,--- actual return time
credit_card varchar(20),
total number, --- total charge
primary key (rid),
foreign key(cid) references cust,
foreign key(tid) references tool,
foreign key(tuid) references time_unit
);
-- John rented a mower for 4 hours,
insert into rental values(1,1,1,1,4,timestamp '2019-08-01 10:00:00.00',null,null,'123456789',null);
-- susan rented a small carpet cleaner for one day
insert into rental values(2,2,3,2,1,timestamp '2019-08-11 10:00:00.00',null,null,'123456789',null);
--susan also rented a small mower for 5 hours, before 8 am case
insert into rental values(3,2,1,1,5,timestamp '2019-08-12 21:00:00.00',null,null,'123456789',null);
--david also rented a small carpet cleaner for 4 hours, after 10 pm case
insert into rental values(4,3,3,1,4,timestamp '2019-08-13 19:00:00.00',null,null,'12222828828',null);
commit;
In: Computer Science
Use the median of 3 partitioning algorithm (given in the next page) to implement quick sort. This algorithm chooses the pivot element as the median of the 3 elements namely: A[p], A[r], and A[(p+r)/2].(Java language)
Quicksort(A,p,r)
1 if p
2 N = r- p +1
3 m = Median3(A, p, p + N/2 , r)
4 exchange A[m] with A[r]
5 q = Partition (A,p,r)
6 Quicksort(A,p, q-1)
7 Quicksort(A,q+1,r)
In: Computer Science
Write a function of interest to you. It should that take at least 3 inputs from different types and return at least three different values.
Call your function from the main.
Print the resultant outputs in the main (not in the function).
In: Computer Science
Construct a DFA machine to recognize the language of all binary
numbers which are a multiple of 5.
L = { w belongs {0,1}* : w is a binary number and is a multiple of
5}
Example: 101 belongs to L; 1010 belongs to L; 110 doesn't belong to
L.
In: Computer Science
Upsetfowl inC++
knockoff version of the Angry Birds game. The starter program is a working first draft of the game.
1. Correct the first FIXME by moving the intro text to a function named PrintIntro. Development suggestion: Verify the program has the same behavior before continuing.
2. Correct the second FIXME. Notice that the function GetUsrInpt will need to return two values: fowlAngle and fowlVel.
3. Correct the third FIXME. Notice that the function LaunchFowl only needs to return the value fowlLandingX, but needs the parameters fowlAngle and fowlVel.
4. Correct the fourth FIXME. Notice that the function DtrmnIfHit only needs to return the value didHitSwine, but needs the parameters fowlLandingX and swineX. The main should now look like the following code and the program should behave the same as the first draft: intmain(){ doublefowlAngle=0.0;
//angleoflaunchoffowl(rad) doublefowlVel=0.0;//velocityoffowl(m/s) doubleswineX=0.0;//distancetoswine(m) doublefowlLandingX=0.0;
//fowl’shoriz.dist.fromstart(m) booldidHitSwine=false;
//didhittheswine? srand(time(0)); swineX=(rand()%201)+50; PrintIntro(); GetUsrInpt(swineX,fowlAngle,fowlVel); fowlLandingX=LaunchFowl(fowlAngle,fowlVel); didHitSwine=DtrmnIfHit(fowlLandingX,swineX); return0; }
5. Modify the program to continue playing the game until the swine is hit. Add a loop in main that contains the functions GetUsrInpt, LaunchFowl, and DtrmnIfHit.
6. Modify the program to give the user at most 4 tries to hit the swine. If the swine is hit, then stop the loop. Here is an example program execution (user input is highlighted here for clarity):
WelcometoUpsetFowl! TheobjectiveistohittheMeanSwine. TheMeanSwineis84metersaway. Enterlaunchangle(deg):45 Enterlaunchvelocity(m/s):30 Time 1 x= 0 y= 0 Time 2 x= 21 y= 16 Time 3 x= 42 y= 23 Time 4 x= 64 y= 20 Time 5 x= 85 y= 6 Time 6 x=106 y=-16 Missed'em...
TheMeanSwineis84metersaway. Enterlaunchangle(deg):45 Enterlaunchvelocity(m/s):25 Time 1 x= 0 y= 0 Time 2 x= 18 y= 13 Time 3 x= 35 y= 16 Time 4 x= 53 y= 9 Time 5 x= 71 y= -8 Hit'em!!!
In: Computer Science
In MIPS assembly:
Ask the user to enter two numbers and an operation (+ - / *).
Print the expression back to the user with a blank after the = sign (since the calculation is not being completed.
In: Computer Science
C++ Language
Write a program that reads the numbers.txt file and stores it into an array of integers. Use the sample functions in the text and in the notes from chapter 8 to sort the array in ascending order (low to high). You can use either method (bubble or selection sort) to accomplish this. Then store the sorted array into an output file sorted Numbers.txt. There are 100 values both positive and negative integers in this file.
In: Computer Science
In: Computer Science
Compare between connectionless and connection-oriented services by explaining how they work. You should explain their advantages and disadvantages.
In: Computer Science
A host with IP address 202.11.35.91 is in a subnet that can contain at most 14 hosts.
a. What is its subnet mask, in dotted-quad notation? [4]
b. What is the subnet's address? [4]
c. What is the last possible IP address in that subnet? [4]
10. For the following IP address and conditions, calculate the information listed below for the Network which contains this address: 173.70.34.3 Subnet Mask: 255.255.240.0
a. Default Subnet Mask: [2]
b. Subnet Address: [3]
c. First Usable Host Address: [3]
d. Last Usable Host Address: [3]
e. Broadcast Address: [3]
f. Number of Usable Subnets: [3]
g. Number of Usable Hosts/Subnet: [3]
11. Given the following IP address and conditions, calculate the information listed below for the 3rd
available subnet: 134.6.9.7 You need at least 30 subnets
a. Subnet Mask: [3]
b. Subnet Address (for 3rd subnet): [4]
c. First Usable Host Address: [3]
d. Last Usable Host Address: [3]
e. Broadcast Address: [3]
f. Number of Usable Subnets: [3]
g. Number of Usable Hosts/Subnet: [3]
In: Computer Science
Translate this algorithm to code on C
compare(char array[ ])
word = split(array, " "); //gets the first word before the blank space
if word == Hello {
print("I am saying hello");
}else if (word == Bye)
print("I am saying goodbye");
}
Example---------------------
char array = "Hello Ana";
compare(array);
char array1 = "Bye Ana"
compare(array1);
result
"I am saying hello"
"I am saying goodbye"
In: Computer Science
Java homework question:
Explain how class (static) variables and methods differ from their instance counterparts. Give an example of a class that contains at least one class variable and at least one class method. Explain why using a class variable and method rather than an instance variable and method would be the correct choice in the example you select.
In: Computer Science
def DMStoDD(degree, minutes, seconds):
dd = abs(degree) + abs(minutes)/60 + abs(seconds)/3600
if degree > 0:
return dd
else:
return -dd
print(DMStoDD(-30, 30, 00))
In this script, add a main program that collects DD or DMS input from the user, then converts it to the other form and reports the converted value. Directions: The main program needs to gather a string from the keyboard using input() and detect whether it is DD or DMS (Hint: use the split function). Then it must pass the proper DD or DMS values to the functions above to convert. The DMS input is separated by comma and contains no space. The degree field could be negative to represent a negative value. (E.g. 43,4,23 or -43,4,23)
In: Computer Science
Discuss by no more than a paragraph the following terms:
ATM
VLANS
Wireless Lans
NATS
Point-To-Point WANS
In: Computer Science