Skip to content

Commit a89c3cb

Browse files
committed
Chapter03
1 parent b4385c5 commit a89c3cb

File tree

3 files changed

+143
-0
lines changed

3 files changed

+143
-0
lines changed

Chapter03/3-3.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/* Excercise 3-3
2+
* Write a program to count how many times each distinct word
3+
* appears appears in its input
4+
*/
5+
6+
#include <iostream>
7+
#include <algorithm>
8+
#include <vector>
9+
10+
using namespace std;
11+
12+
int main(){
13+
vector<string> strlist;
14+
vector<string>::size_type count = 0;
15+
string x;
16+
17+
//invariant: count is the number of unique strings in the vector
18+
while (cin >> x){
19+
if (count == 0){
20+
strlist.push_back(x);
21+
++count;
22+
}
23+
else{
24+
int i;
25+
//invariant: have compared i strings in the vector
26+
for (i = 0; i < count; i++)
27+
if (strlist[i] == x)
28+
break;
29+
if (i == count){
30+
strlist.push_back(x);
31+
++count;
32+
}
33+
}
34+
}
35+
cout << count << " unique values found." << endl;
36+
37+
return 0;
38+
}

Chapter03/3-4.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/* Exercise 3-4
2+
* Write a program to report the length of the longest and
3+
* shortest string in its input
4+
*/
5+
6+
#include <iostream>
7+
#include <string>
8+
9+
int main(){
10+
std::string x, longest, shortest;
11+
longest = shortest = "";
12+
std::string::size_type llength, slength;
13+
llength = slength = 0;
14+
15+
16+
while (std::cin >> x){
17+
if (x.size() > longest.size())
18+
llength = (longest = x).size();
19+
20+
if (shortest == "" || x.size() < shortest.size())
21+
slength = (shortest = x).size();
22+
23+
}
24+
25+
/* note: this will print only the first encounters of
26+
* the longest and shortest strings
27+
*/
28+
std::cout << "The longest string and size: " << longest
29+
<< " " << llength << std::endl;
30+
std::cout << "The shortest string and size: " << shortest
31+
<< " " << slength << std::endl;
32+
return 0;
33+
}

Chapter03/3-5.cpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/* Exercise 3-5
2+
* Write a program that will keep track of grades of several
3+
* students at once. The program could keep two vectors in sync:
4+
* The first should hold the student's names, and the second the final
5+
* grades that can be computed as input read. For now, you should
6+
* assume a fixed number of homework grades
7+
*/
8+
9+
#include <iostream>
10+
#include <ios> /* to define streamsize */
11+
#include <iomanip> /* for the output manipulator setprecision */
12+
#include <string>
13+
#include <vector>
14+
15+
using std::cin; using std::setprecision;
16+
using std::cout; using std::string;
17+
using std::endl; using std::vector;
18+
using std::streamsize;
19+
20+
#define NUM_HW 3
21+
22+
int main(void){
23+
vector<string> nameList;
24+
vector<double> gradeList;
25+
bool done = false;
26+
27+
while (!done){
28+
//ask for student's name
29+
cout << "Please enter your first name: ";
30+
string name;
31+
cin >> name;
32+
cout << "Hello, " << name << "!" << endl;
33+
nameList.push_back(name);
34+
35+
//ask for midterm and final exam grades
36+
cout << "Please enter your midterm and final grades: ";
37+
double midterm, final;
38+
cin >> midterm >> final;
39+
40+
//ask for and read homework grades
41+
cout << "Enter all your homework grades: ";
42+
vector<double> homework;
43+
// a variable into which to read
44+
double x;
45+
int count = 0;
46+
double sum = 0;
47+
while (count < NUM_HW){
48+
cin >> x;
49+
sum += x;
50+
count++;
51+
}
52+
//calculate final grades to be inputted into the gradeList
53+
double finalgrade = 0.2 * midterm + 0.4 * final + 0.4 * sum/count;
54+
gradeList.push_back(finalgrade);
55+
56+
cout << "More students? (Y/N) ";
57+
string s;
58+
cin >> s;
59+
if (s != "Y")
60+
done = true;
61+
}
62+
63+
vector<string>::size_type size = nameList.size();
64+
int i;
65+
for (i = 0; i < size; i++){
66+
streamsize prec = cout.precision();
67+
cout << "Student: " << nameList[i] << ", " << "Grade: " << setprecision(3) << gradeList[i] << setprecision(prec) << endl;
68+
}
69+
70+
71+
return 0;
72+
}

0 commit comments

Comments
 (0)