程序代写代做代考 ECS 40 Practice Final 2

ECS 40 Practice Final 2

ECS 40 Practice Final 2

1. (30 points, All or nothing) In p7, the first two programs had your derived class objects print out base class data that

was declared private. Describe two techniques you used to achieve this. Your answer should be quite short.
Remember, we are just trying to determine if you wrote your own code—be specific as possible.

2. (28 points) Write a recursive implementation of strcmp. The prototype of strcmp is: int strcmp(const char *s1, const

char *s2). If s1 > s2 then strcmp returns a positive value. If s1 == s2 then strcmp returns 0. If s1 < s2 then strcmp returns a negative value. The header is provided. int strcmp(const char *s1, const char *s2) { 3. (12 points) Write a UNIX command line that will list the names of all the people who handed in main.cpp. I am in the home directory of cs40a. Each team turns into a directory in handin/p7; e.g. handin/p7/davis, handin/p7/bulcher, handin/p7/wong. Names are located in the first two lines of each main.cpp, so just print those lines. 4. (40 points) Based on the following shell script, test.sh, answer the questions. An example call would be test.sh a.out 300. Line # 1 #! /bin/bash 2 count=$2 3 4 while [[ $count -gt 0 ]] ; do 5 rm sk.temp >& /dev/null
6 ps | grep $1 > sk.temp
7 if test `cat sk.temp |wc -w ` -eq 0 ; then
8 exit 0
9 fi
10 sleep 10
11 (( count = count – 10 ))
12 done
13
14 pkill $1

a) (3 points) What does line #1 do?

For the rest of the parts of this questions, assume that the command line was: test.sh editor.out 50

b) (3 points)What would line #2 do?

c) (3 points) What does line #4 do?

d) (4 points) What does line 5 do? Be complete.

e) (4 points) What does line 6 do?

f) (4 points) What do lines 7 to 9 do?

g) (3 points) What does line 10 do?

h) (3 points) What does line 11 do?
i) (3 points) What does line 14 do?

j) (10 points) What does the whole script do in general?

5. (20 points)

a) (10 points) Assuming I have declared “queue Q”, and that the Front and Back indices are initially zero.
Show the contents of the queue after the following operations. (Note: there may be more positions than needed.)
Also provide Front and Back indices after all operations have been executed..

Q.push(7); Q.push(8); Q.pop(); Q.push(4); Q.push(2); Q.front(); Q.pop(); Q.push(-6); Q.back(); Q.push(3); Q.pop();

0 1 2 3 4 5 6 7 8

Front: _______________ Back:__________________

b) (10 points) Assuming I have declared “stack > st.) Show the state of the stack after the following
operations. Do this by filling in the values and drawing the pointers (or entering NULL) for each list node that
exists after the operations. Also provide the letter of the Node to which Top points after all operations are
completed. Assume that the nodes are created in alphabetical order. If a node no longer exists, cross out its name,
and make sure that no existing node points to it. (Note: there may be more nodes than needed.)

st.push(9); st.top(); st.push(3); st.pop(), st.push(4); st.top(), st.push(-1); st.pop(); st.push(2); st.push(6); st.pop()

Node A Node B Node C Node D Node E Node F

Top index : ________________

6. (171 points) For this section of the test, you will be writing parts of a program that reads information about products

from two files, and then provides the information when provided a UPC (Uniform Product Code) by the user. A SKU
is a unique string used by a company to identify a product. The class ProductInfo is derived from the class Product,
and has an additional int UPC. SKUs.txt provides a mapping between SKUs and UPCs. products.csv provides
information about a product based on its UPC with the format , ,

a) (58 points) Provide a complete ProductInfo.h. Besides the required preprocessor directives, your file should
contain all the appropriate pre-processor directives typical for large projects. The ProductInfo class is derived from the
Product class, and has only an additional int UPC. It contains only a constructor, a print method, and two overloaded
operators. Your file shold provide all of the implementation code for its member methods. You may wish to hold off
writing parts of this until you have written some of the later functions. Remember to use const where appropriate.

b) (27 points) Write the overloaded extraction operator for the ProductInfo class. The header is provided.

istream& operator>> (istream &is, ProductInfo &info){

c) (21 points) Write the readSKUs() function called from main. It reads SKU-UPC pairs from SKUs.txt into the map.

d) (37 points) Write the writeProductInfo() function called from main. It finds the UPC in map corresponding to
SKU, and then uses the UPC to find ProductInfo in the set and calls the ProductInfo::print() method.

e) (28 points) Given the following definitions, write the insert function for LinkedList that keeps the list sorted by the
UPC. The header is provided.

class ListNode {
ProductInfo info;
ListNode *next;
ListNode(const ProductInfo &i, ListNode *n) : info(i), next(n){}
friend class LinkedList;
};

class LinkedList{
ListNode *head;
public:
LinkedList():head(NULL){}
void insert(const ProductInfo &info);
};

void LinkedList::insert(const ProductInfo &info){

Handout

typedef map SKUtoUPCMap; // SKU’s are strings, UPC’s are ints

int main(int argc, char* argv[])
{
SKUtoUPCMap SKUtoUPCmap;
set productSet; // sorted by UPC
ProductInfo info;
string SKU;

readSKUs(SKUtoUPCmap); // reads SKU-UPC pairs from SKUs.txt into the map.
ifstream inf(“products.csv”); // a comma separated value file.
while(inf >> info)
productSet.insert(info);

do{
cout << "SKU: "; cin >> SKU;
writeProductInfo(SKU, SKUtoUPCmap, productSet);
// finds UPC in map corresponding to SKU, and then uses the UPC
// to find ProductInfo in set and calls the ProductInfo::print()
} while (SKU != “0”);

return 0;
}

[davis@lect15 finalA]$ cat SKUs.txt
401-553 719980001
532-447 839220931
742-999 732228991
665-782 739816438
655-992 786539201
443-892 356271839
[davis@lect15 finalA]$

[davis@lect15 finalA]$ cat products.csv
719980001,Tony Chachere’s Creole Seasoning,1.75
786539201,Dean Sniegowski P5 Solution,98.99
356271839,Kensington Track Ball,87.89
839220931,Bovine Online 7.0,4.5
732228991,Sony Walkman SRF-M35,24.99
739816438,ECS 40 Final Key,2999.99
[davis@lect15 finalA]$

[davis@lect15 finalA]$ products.out
SKU: 532-447
839220931 $ 4.50 Bovine Online 7.0
SKU: 665-782
739816438 $2999.99 ECS 40 Final Key
SKU: 447-232
Unknown SKU
SKU: 443-892
356271839 $ 87.89 Kensington Track Ball
SKU: 742-999
732228991 $ 24.99 Sony Walkman SRF-M35
SKU: 0
Unknown SKU
[davis@lect15 finalA]$

class Product {
string name;
protected:
float price;
public:
Product(string n, float p);
const string& getName()const; // returns the name of Product
float getPrice()const; // returns the price of the Product
void setName(const string &n); //copies n to name
};