1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
|
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
// The struct MyString is used to store a string, with len recording the number of characters the string has.
struct MyString
{
char s[500];
int len;
};
void assignment(struct MyString & dest, const char src[]);
// Transfers the content of the char array src to dest.
void substr(struct MyString & src, int index, int length, struct MyString & dest);
// Transfers the content of MyString src, in the range of [index, index+length-1], to dest.
// In case src does not have length characters, whatever it has is transfered.
int find(struct MyString & src, char c);
// Returns -1 if c is not found in src; otherwise, returns the first index from the lower end where src.s[index]==c.
istream & getWord(istream & inFile, struct MyString & word);
// Returns from inFile a word that is made of non-space, printable characters.
// This function will discard all spaces appearing before the actual word.
void getline(istream & inFile, struct MyString & line);
// Returns a line of characters from inFile until a newline is seen.
// The newline character '\n' will be discarded.
ostream & print(ostream & outFile, struct MyString & s);
// Outputs MyString s to outFile.
// When cout is passed in as the first argument, s is printed on the console.
ostream & print(ostream & outFile, const char s[]);
// Outputs the C string s to outFile.
// When cout is passed in as the first argument, s is printed on the console.
ostream & print(ostream & outFile, char c);
// Outputs the character c to outFile.
// When cout is passed in as the first argument, c is printed on the console.
int main(){
// reads in three words from console.
struct MyString word1;
struct MyString word2;
struct MyString word3;
getWord(cin, word1); getWord(cin, word2); getWord(cin, word3);
print(cout, word1); print(cout, ' ');
print(cout, word2); print(cout, ' ');
print(cout, word3); print(cout, '\n');
// reads in two lines from a file.
struct MyString line1;
struct MyString line2;
ifstream inFile;
inFile.open("my.txt");
getline(inFile, line1);
getline(inFile, line2);
print(cout, "The first two lines in my.txt are: \n");
print(cout, line1); print(cout, '\n');
print(cout, line2); print(cout, '\n');
// retrieves the first name.
struct MyString name;
struct MyString firstName;
assignment(name, "John James Johnson");
int i = find(name, ' ');
substr(name, 0, i, firstName);
print(cout, "The first name of John James Johnson is ");
print(cout, firstName); print(cout, '.'); print(cout, '\n');
return 0;
}
void assignment(struct MyString & dest, const char src[])
{
int i=0;
while (src[i]!=0)
{
dest.s[i] = src[i];
i = i+1;
}
dest.len = i;
}
void substr(struct MyString & src, int index, int length, struct MyString & dest)
{
int i=0;
while (i < length && index+i < src.len)
{
dest.s[i] = src.s[index+i];
i = i+1;
}
dest.len = i;
}
int find(struct MyString & src, char c)
{
int i = 0;
int found = 0;
int err = -1;
while (i < 500)
{
if (src.s[i]==c)
{
found = 1;
break;
}
i = i + 1;
}
if (found !=0)
return i;
else
return err;
}
// IO functions
istream & getWord(istream & inFile, struct MyString & word)
{
// bites off all non-printable and whitespace characters!
char c = inFile.peek(); // peeks, but does not consume, the next character in inFile.
while (c!=-1 && (!isprint(c) || isspace(c)))
{
inFile.get();
c = inFile.peek();
}
int i=0;
c = inFile.peek();
while (isprint(c) && !isspace(c))
{
c = inFile.get();
word.s[i] = c;
i = i+1;
c = inFile.peek();
}
word.len = i;
return inFile;
}
void getline(istream & inFile, struct MyString & line)
{
/*
*/
}
ostream & print(ostream & outFile, const char s[])
{
int i=0;
while (s[i]!=0)
{
outFile<< s[i];
i = i+1;
}
return outFile;
}
ostream & print(ostream & outFile, struct MyString & s)
{
int i=0;
while (i < s.len)
{
outFile << s.s[i];
i = i+1;
}
return outFile;
}
ostream & print(ostream & outFile, char c)
{
outFile << c;
return outFile;
}
|