syrian developers community
c++
Quicksort algorithm and character count
Nov 26th
- #include <iostream>
- using namespace std;
- int preProcess(char *, int);
- void quickSort(char *, int, int);
- int partition(char *, int, int);
- void swap(int &, int &);
- void specialSort(char *, int, int);
- void genReport(char *, int);
- // Checks if the user was planning to quit
- bool isQuit(char *);
- const int stringSize = 255;
- int main()
- {
- char toCheck[stringSize];
- // Do while the user doesn’t enter ~quit
- do
- {
- cout << “Ready: “;
- cin.getline(toCheck, sizeof(toCheck));
- // Strip spaces
- int newSize = preProcess(toCheck, strlen(toCheck));
- // New array with new size
- char newToCheck[newSize];
- // Loop to fill the new array
- for(int n = 0; n < newSize; n++)
- {
- newToCheck[n] = toCheck[n];
- }
- newToCheck[newSize] = ‘\0‘;
- // Check if the user choosed to quit
- if(isQuit(newToCheck))
- {
- return false;
- }
- // Separate a to z into another array
- char alphaLower[newSize];
- int n = 0;
- for(int i = 0; i < newSize; i++)
- {
- if((int)newToCheck[i] >= 97 && (int)newToCheck[i] <= 122)
- {
- alphaLower[n] = newToCheck[i]; More >
Calculate grade average with weights
Nov 26th
- #include <cstdlib>
- #include <iostream>
- using namespace std;
- bool isitvalid(double[]);
- bool printarray(double[], int);
- int main()
- {
- // To hold the weights and the grades
- double weights[4];
- double grades[4];
- // To hold the averages
- double avrGrade = 0;
- double avrClassGrade = 0;
- int numOfStudents = 0;
- char respChar = ‘N’;
- int revGrade;
- bool anotherStudent = false;
- // Do until the user decides not to enter anymore students
- do
- {
- // Loop for the user to enter the weight
- for(int i = 0; i < 4; i++)
- {
- cout << “Please enter weight for grade (e.g. 0.3) #” << i + 1 << “: “;
- cin >> weights[i];
- }
- // Loop for the user to enter the grades (until all are valid)
- do
- {
- for(int i = 0; i < 4; i++)
- {
- cout << “Please enter grade #” << i + More >
Palindrome Application This is probably one of the strangest applications I built because the requirements forced me to not make use of any header files except iostream. Thus, instead of using a string object to manipulate the palindrome, I had to use character arrays. Furthermore, the function that tests the string for a palindrome had to be a recursive function (which is a good approach actually). But what’s a palindrome anyway? A palindrome is a sequence that reads the same in either direction. The first two lines in the application are the typical: #include using namespace std; Next we have the function prototypes and a constant holding the initial size of the array: // Cleans the character array int clean(char *, int); // Recursive function bool isPalindrome(char *, int, int); // Checks if the user was planning to quit bool isQuit(char *); // The initial size of the arrays const int stringSize = 255; The isPalindrome function is the recursive function that does the actual work, but first we’re going to start with the main() function: int main() { char toCheck[stringSize]; char origStr[stringSize]; // Do while the user doesn’t enter ~quit do { cout << "Enter palindrome: "; cin.getline(origStr, sizeof(toCheck)); // Make a copy that will later have its spaces removed and lowercased for(int i = 0; i < stringSize; i++) { toCheck[i] = origStr[i]; } // Strip spaces and lowercase all chars int newSize = clean(toCheck, strlen(toCheck)); // New array with new size char newToCheck[newSize]; // Loop to fill the new array for(int n = 0; n < newSize; n++) { newToCheck[n] = toCheck[n]; } // Check if the user choosed to quit if(isQuit(newToCheck)) { return false; } // Run the function and show the result if(isPalindrome(newToCheck, 0, newSize - 1)) { cout << "Yes, " << origStr << " is a palindrome!\r\n"; } else { cout << "No, " << origStr << " is not a palindrome!\r\n"; } } while(true); system("PAUSE"); return 0; } The origStr array is the one that contains the original string, while toCheck will be the one that's manipulated by being stripped of all the characters that are not numbers or uppercase/lowercase letters. This is done using the clean function that you'll get to see right away. The isQuit function checks if the user has entered ~quit in the command prompt, case in which it exists the application: bool isQuit(char* phrase) { char quit[6] = "~quit"; // Compare the char array to '~quit' for(int i = 0; i < 5; i++) { if(phrase[i] != quit[i]) { return false; } } return true; } Obviously, using strings would have made the above function much simpler, but the purpose of this code is to not use the string.h header file. We basically loop through the array and check to see if the first 5 characters match ~quit by comparing it to a character array that actually contains the the string quit. Let's now review the clean function, which cleans the passed character array of all spaces and characters that are not numbers, uppercase or lowercase letters. We do this by checking the ASCII value of the characters while looping through each element in the array. If the ASCII value is not one of a number, uppercase or lowercase letter, it's not added to the new array. We also return the size of the new array back to main(). int clean(char* phrase, int size) { int currChar = 0; // Loop through the original array for(int i = 0; i < size; i++) { // Only allow letters and numbers for the characters if(((int)phrase[i] >= 48 && (int)phrase[i] <= 57) || ((int)phrase[i] >= 65 && (int)phrase[i] <= 90) || ((int)phrase[i] >= 97 && (int)phrase[i] <= 122)) { // Lower’em phrase[currChar] = tolower(phrase[i]); currChar++; } } // We’re out with the new number of chars return currChar; } Finally there’s the last function, the one that does the hard work: bool isPalindrome(char* phrase, int start, int end) { // If the nearest to the left matches its mirror character to the right if(phrase[start] == phrase[end]) { // If we arrived at the middle of the string (considering length can be odd or even) if(start == end || start + 1 == end) { // We’re good and finished return true; } else { // We’re good and moving on to next characters return isPalindrome(phrase, start + 1, end – 1); } } else { // We found two characters that don’t much, we’re out of here return false; } } This function start checking the string from both side – beginning and end – and moves one character at a time towards the middle of the string. It compares character by character to see if they are identical. For as long as a character that’s not identical is found, the function keeps calling itself. Otherwise, if non-matching characters are found, the function immediately returns false. It’s also important to notice that from the application’s point of view there are two types of palindromes: odd in length and even in length. The ones that are even in length, such as “ABBA” have no middle character, so we compare A with A, B with B and we’re done. What about “CIVIC” though? We compare C with C, I with I, and then both our start and end variables hit the common middle character V. That’s why there is an if condition that checks wherther we arrived at the same character, case in which it returns true. It also checks to see if start + 1 is the same as end, case in which we arrived at the middle of the string, but we’re in a even length string and there’s no middle character. That’s it with this tutorial, if you compile the application using the code above, everything should work just fine as I have tested it with (what I think are) all the possible palindrome cases. You should also be able to easily convert this application into one that uses strings instead of character arrays, although in the end you’ll still want to use an array inside the isPalindrome function, because it makes comparison between characters so easy.
Nov 26th
- #include <iostream>
- #include <fstream>
- using namespace std;
- void FileState(ifstream &in);
- int main(int argc, char *argv[])
- {
- ifstream inFile(“C:\\NEWS.txt”);
- if(!inFile)
- {
- cout << “Cannot open input file.\n“;
- return 1;
- }
- char currChar;
- while(inFile.get(currChar))
- {
- cout << currChar;
- FileState(inFile);
- }
- FileState(inFile);
- inFile.close();
- system(“PAUSE”);
- return 0;
- }
- void FileState(ifstream &inFile)
- {
- ios::iostate currState;
- currState = inFile.rdstate();
- if(currState & ios::eofbit)
- {
- cout << “End of File\n“;
- }
- else if(currState & ios::failbit)
- {
- // Recoverable I/O error
- cout << “Reading Failure\n“;
- }
- else if(currState & ios::badbit)
- {
- // Unrecoverable I/O error
- cout << “Stream Corrupted\n“;
- }
- }
Palindrome Detector using C++
Nov 26th
This is probably one of the strangest applications I built because the requirements forced me to not make use of any header files except iostream. Thus, instead of using a string object to manipulate the palindrome, I had to use character arrays. Furthermore, the function that tests the string for a palindrome had to be a recursive function (which is a good approach actually). But what’s a palindrome anyway? A palindrome is a sequence that reads the same in either direction.
The first two lines in the application are the typical:
#include <iostream>
using namespace std;
Next we have the function prototypes and a constant holding the initial More >
Reverse a linked list
Nov 26th
- NodePtr revList = NULL
- NodePtr currNode = currHead;
- while(currNode != NULL)
- {
- // Set the head to the next node
- currHead=currHead->next;
- // Link currNode to the reversed list
- currNode->next=revList;
- revList=currNode;
- // Move currNode to next node
- currNode=currHead;
- currHead=revList;
- }
Overloading Operators: Creating a Rational Class
Nov 26th
Probably one of the most interesting features of object oriented programming is the possibility to overload operators. This allows you to create your own class that handles the +, -, * and all the other arithmetical operators. It also allows you to override comparison operators such as ==, < and <= as well. By being able to do all this, you can create your own data types, just like int and float is. One of these data types is the rational type, which we will be creating today. The class we’re creating is slightly incomplete since it’s missing a few operators, however it does contain the More >