Here's the C++ code i did for myself:
#include <iostream>
#include <fstream>
#include <ctime>
#include <cstdlib>
#include <string>
#include <cctype>
void terminate()
{
const unsigned short SEC = 5; // constant for 5 times
std::cout << "\nProgram will close in,\n"; // print
for (int c=SEC; c>=0; c--) {
if (c==0) { // if c equal with 0
exit(EXIT_SUCCESS); // close the console
}
else { // if c not reach 0
std::cout << c << " "; // print the value one by one
}
// system clock
clock_t start = clock();
clock_t end = CLOCKS_PER_SEC;
while ((clock()-start) < end) {}
}
}
int main()
{
const unsigned short size = 5;
double n[size];
std::ofstream fout; // creating object for output file
// get the number to assign to each array
for (int c=0; c<size; c++) {
std::cout << "Enter number #" << c+1 << ": ";
(std::cin >> n[c]).get();
}
fout.open("b4sort.txt"); // create a file #1
fout << "Number before sorting:\n"; // print output on the file #1
for (int c=0; c<size; c++) {
fout << n[c] << ", ";
}
fout.close(); // close that file
// sorting
for (int c=0; c<size-1; c++) {
for (int d=0; d<size-1-c; d++) {
if (n[d] > n[d+1]) { // swapping
double temp;
temp = n[d];
n[d] = n[d+1];
n[d+1] = temp;
}
}
}
fout.open("aftersort.txt"); // create a new file #2
fout << "Number after sorting:\n"; // print output on the file #2
for (int c=0; c<size; c++) {
fout << n[c] << ", ";
}
fout.close(); // close file #2
char filename[300]; // declare a string variable
char read; // declare a char variable
long alphabetic = 0; // alphabetic count value
long digit = 0; // digit count value
long punctuation = 0; // punctuation count value
long spacing = 0; // spacing count value
long unknown = 0; // unknown count value
std::ifstream fin; // creating object for input file
std::cout << "\n\nTo analysis each text now.\n"; // display this
std::cout << "Enter filename: "; // ask user to enter a filename
std::cin.getline(filename, 300);
fin.open(filename); // opening the file
if (fin.is_open()==false) { // if cannot open the file
std::cout << "\nOpen file = 0"; // the file can't be open
}
else { // else, the file can be open
fin.get(read); // read char one by one
while (fin.good()==true) { // test and get each character one by one
if (isalpha(read)) { // if the char is alphabetic
++alphabetic;
}
else if (isdigit(read)) { // if the char is numeric
++digit;
}
else if (ispunct(read)) { // if the char is punctuation
++punctuation;
}
else if (isspace(read)) { // if the char is space
++spacing;
}
else { // if the char is unknown character
++unknown;
}
fin.get(read); // get next character and test
}
}
fout.close(); // close the file
fout.open("textanalysis.odt"); // create a new file
// print on the new file
fout << filename << " text analysis.\n";
fout << "\nTotal: " << alphabetic+digit+punctuation+spacing+unknown << std::endl;
fout << "Alphabetic: " << alphabetic << std::endl;
fout << "Digit: " << digit << std::endl;
fout << "Punctuation: " << punctuation << std::endl;
fout << "Spacing: " << spacing << std::endl;
fout << "Unknown: " << unknown << std:: endl;
fout << "\nFinish!";
fout.close(); // close that file
terminate(); // invoke function to close console
return 0;
}
can someone show me how to turn this code into the small parts (function) one bye one, especially for that sorting and swapping parts.
sory for my bad english and i'm just a beginner in C++...