Skip to content

Commit 8e24bbd

Browse files
committed
new code
1 parent 7e1773f commit 8e24bbd

File tree

2 files changed

+146
-0
lines changed

2 files changed

+146
-0
lines changed

4_0_students_linked_list.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#include <iostream>
2+
3+
using std::cout;
4+
using std::cin;
5+
using std::endl;
6+
7+
8+
struct listNode {
9+
int studentNum;
10+
int grade;
11+
listNode * next;
12+
};
13+
14+
typedef listNode * studentCollection;
15+
16+
int main()
17+
{
18+
studentCollection sc;
19+
listNode* node1 = new listNode;
20+
node1->studentNum = 1001;
21+
node1->grade = 78;
22+
listNode* node2 = new listNode;
23+
node2-> studentNum = 1012;
24+
node2-> grade = 93; listNode* node3 = new listNode;
25+
node3-> studentNum = 1076;
26+
node3-> grade = 85;
27+
sc = node1;
28+
node1-> next = node2;
29+
node2-> next = node3;
30+
node3-> next = NULL;
31+
node1 = node2 = node3 = NULL;
32+
33+
}
34+
35+
36+
void addRecord(studentCollection& sc, int stuNum, int gr)
37+
{
38+
listNode * newNode = new listNode;
39+
newNode->studentNum = stuNum;
40+
newNode->grade = gr;
41+
newNode->next = sc;
42+
sc = newNode;
43+
}
44+
45+
double averageRecord(studentCollection sc)
46+
{
47+
int count = 0;
48+
double sum = 0;
49+
listNode * loopPtr = sc;
50+
while (loopPtr != NULL)
51+
{
52+
sum += loopPtr->grade;
53+
count++;
54+
loopPtr = loopPtr->next;
55+
}
56+
double average = sum / count;
57+
return average;
58+
}
59+

4_0_variable_length_string.cpp

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#include <iostream>
2+
3+
using std::cout;
4+
using std::endl;
5+
6+
typedef char * arrayString;
7+
8+
void append(arrayString &str, char ch);
9+
void concatenate(arrayString &str1, arrayString str2);
10+
char characterAt(arrayString str, int position);
11+
int getLength(arrayString str);
12+
13+
int main()
14+
{
15+
arrayString str = new char[5];
16+
str[0] = 'T'; str[1] = 'e'; str[2] = 's'; str[3] = 't'; str[4] = 0;
17+
cout << "The string: " << str << endl;
18+
19+
cout << "Character at position " << 1 << " is " << characterAt(str, 1) << endl;
20+
21+
cout << "Before append: " << str << endl;
22+
append(str, '!');
23+
cout << "After append: " << str << endl;
24+
25+
arrayString str1 = new char[5];
26+
str1[0] = 'T'; str1[1] = 'e'; str1[2] = 's'; str1[3] = 't'; str1[4] = 0;
27+
28+
arrayString strToAdd = new char[4];
29+
strToAdd[0] = 'b'; strToAdd[1] = 'e'; strToAdd[2] = 'd'; strToAdd[3] = 0;
30+
31+
cout << "Before concatenate: " << str1 << endl;
32+
cout << "String to add: " << strToAdd << endl;
33+
concatenate(str1, strToAdd);
34+
cout << "After concatenate: " << str1 << endl;
35+
cout << (void*) str1 << " " << (void*)strToAdd << endl;
36+
37+
}
38+
39+
char characterAt(arrayString str, int position)
40+
{
41+
return str[position];
42+
}
43+
44+
void append(arrayString &str, char ch)
45+
{
46+
int oldLen = getLength(str);
47+
arrayString newStr = new char[oldLen + 2];
48+
49+
for (int i = 0; i < oldLen; i++)
50+
{
51+
newStr[i] = str[i];
52+
}
53+
newStr[oldLen] = ch;
54+
newStr[oldLen + 1] = 0;
55+
delete[] str;
56+
str = newStr;
57+
}
58+
59+
void concatenate(arrayString &str1, arrayString str2)
60+
{
61+
int len1 = getLength(str1);
62+
int len2 = getLength(str2);
63+
int newLen = len1 + len2;
64+
arrayString newStr = new char[newLen+1];
65+
for (int i = 0; i < len1; i++)
66+
{
67+
newStr[i] = str1[i];
68+
}
69+
for (int i = len1; i < newLen; i++)
70+
{
71+
newStr[i] = str2[i - len1];
72+
}
73+
newStr[newLen] = 0;
74+
delete[] str1;
75+
str1 = newStr;
76+
}
77+
78+
int getLength(arrayString str)
79+
{
80+
int len = 0;
81+
while(str[len] != 0)
82+
{
83+
len++;
84+
}
85+
return len;
86+
}
87+

0 commit comments

Comments
 (0)