// Copyright 1999 // College of Computer Science // Northeastern University Boston MA 02115 // This software may be used for educational purposes as long as this copyright // notice is retained at the top of all files // Should this software be modified, the words "Modified from Original" must be // included as a comment below this notice // All publication rights are retained. This software or its documentation may // not be published in any media either in whole or in part. /////////////////////////////////////////////////////////////////////////////// // Shell.cpp // // The common start program for student and faculty development // This program shows off most of the C++ covered in com1100 /////////////////////////////////////////////////////////////////////////////// // The standard include files that include traditional C and C++ headers and // many other Core Tools headers ... see CHeaders.h for additional details #include "IOTools.h" #include "Graphics.h" #include "Random.h" // #include "FileTool.h" not used in the code. // Function Prototypes // These functions are declared here, before they are called in main. // Their definitions are below main. void somestuff(); void otherstuff(); int F(int x, int y); void SmallChange(int& x, int y); void WriteArray(int A[], int N); int main(int argc, char* argv[]) { // Use the following line if you choose NOT to open any graphics windows // InitializeConsole(); // Build graphics window 0 GraphicsWindow GW0(300, 300); // Move the console below graphics window 0 ConsolePlaceBelow(0); // Give the console the focus for user interaction MakeConsoleForeground(); //////////// Enter the main program here // variables and assignment, cout PressReturn("\nVariables"); int x = 5; int y = 7, z = 9; cout << x << ' ' << y << ' ' << z << endl; x = y; y = z; z = x; cout << x << ' ' << y << ' ' << z << endl; x = y; y = z; z = x; cout << x << ' ' << y << ' ' << z << endl; // arithmetic PressReturn("\nArithmetic"); x = 5, y = 2, z = 3; cout << x * y + z << endl; cout << x * y / y / x << endl; cout << x / z << ' ' << x % z << endl; cout << y / z << ' ' << y % z << endl; cout << x - y * z - x << endl; // cin PressReturn("\nUsing cin"); x = 5, y = 2, z = 3; cout << "Enter integers x, y, z: "; cin >> x >> y >> z; // NO endl cout << x << ' ' << y << ' ' << z << endl; cin.ignore(); // skips over the end of line character // simple graphics PressReturn("\nSimple Graphics"); // the graphics window // (0, 0) is the upper right corner // The Basic Commands // verbs: Frame, Paint, Erase, Invert // objects: Rect, Oval, Circle // Also: DrawLine, ShowText, MoveTo, LineTo // SetForeColor(red, green, blue); SetForeColor(255, 0, 0); // bright red PaintRect(20, 60, 70, 180); // Upper left corner (20, 60) // Lower left corner (70, 180) EraseOval(20, 60, 70, 180); // White oval in the red rectangle SetForeColor(0, 255, 0); // bright green PaintCircle(150, 50, 30); // circle with center (150, 50) // radius = 30 SetForeColor(0, 0, 0); // black DrawLine(45, 120, 150, 50); // line from (45, 120) to (150, 50) // center of oval to center of circle MoveTo(150, 200); // move the pen but don't draw ShowText("(150, 200)"); // writes the text in the draw window, // upper left corner (100, 200) MoveTo(100, 200); // move the pen but don't draw LineTo(180, 250); // draws a line from last pen position to (180, 250) ShowText("(180, 250)"); // for, while, do .. while // for (start statement; continue condition; update statement){ // loop body // } PressReturn("\nFor Loops"); // What does this loop do? for (int N = 1; N <= 15; N += 3) cout << N <<" "<< N / 2 << endl; // Write a loop with this output: // 1 2 4 8 16 32 64 PressReturn("\nWhile Loops"); // while (continue condition){ // loop body // } x = 1; y = 1; while (x < 200){ cout << x << " " << y << endl; x = 2*x; y = y + x; } x = 100; do { cout << x << endl; x /= 3; } while (x > 1); // if, if else PressReturn("\nif, if else"); x = 100; if (x % 9 == 1) cout << "OK" << endl; if (x % 9 == 3) x = x - 11; else x = x + 4; cout << x << endl; cout << endl << "Try 100, 72, 35 for x" << endl; for (int i = 0; i < 3; i++){ cout << "enter x: "; cin >> x; // try 100, 72, 35 if (x % 5 == 0) if (x % 10 == 0) cout << "divisible by 10" << endl; else cout << "not divisible by 5" << endl; } cin.ignore(); // skips over the end of line character // logical operators // and && or || not ! // equals == < <= > >= != // What is the value of these expressions? // (3 < 5) or (7 < 2) // (3 < 5) and (7 < 2) // Write a C++ logical expression for each of the following: // x is between 2y and 3y // N is divisible by 10 but not by 5 // x is the highest integral power of 2 less than y // switch - We did not do switch this quarter // Simple Functions - Flow of Control PressReturn("\nFunctions - Flow of Control"); somestuff(); cout << "Now is the time" << endl; otherstuff(); cout << "For all good dogs" << endl; otherstuff(); somestuff(); cout << "To find their bones." << endl; // Functions PressReturn("\nFunctions"); cout << "Testing function F" << endl; x = 2; y = 9; while (x < y) { cout << x << ' ' << y << endl; x = F(x, y); } cout << x << ' ' << y << endl << endl; cout << "Testing SmallChange" << endl; int a = 3, b = 7; cout << "in main a = " << a << " b = " << b << endl; SmallChange(a, b); cout << "in main a = " << a << " b = " << b << endl; // These are the functions from the Little Function Lab. You should be able to write them. // distance, factorial, min, Money, IsSquare, IsLetter, Roll // Some other funtions to write: // CircleArea - argument radius, returns the area // CircleCircumference - argument radius, returns the circumference // RectArea - arguments height and width returns the area // CircleFits arguments, x, y, r, returns true if fits in small window // IsVowel char argument, true if char is a vowel // IsPrime returns true iff int argument is prime // argument a string, returns the number of words // assume only letters and blanks // IsOp returns true iff the char argument is '+', '-', '*', or '/' // One function that uses reference parameters to get and return // the user's firstname, lastname, street address, city, state, zipcode // Arrays PressReturn("\nArrays"); int A[8] = { 3, 5, 4, 2, -1, 7, 0, 6 }; int J; for (J = 0; J < 8; J++) if (A[J] < 1) A[J] = 1; WriteArray(A, 8); for (J = 0; J < 7; J++) A[J] += A[J+1]; WriteArray(A, 8); for (J = 0; J < 8; J++) A[J] = A[(J*J) % 8]; WriteArray(A, 8); // Strings PressReturn("\nStrings"); string advice = "Don't go into the woods today."; cout << "advice = " << advice << endl; cout << "The number of characters in the advice string is: " << advice.length() << endl; int letters = 0, spaces = 0, vowels = 0; char ch; for (J = 0; J < advice.length(); J++){ ch = advice[J]; // ch hold the current letter so we don't have to look it up again if (('a' <= ch && ch <= 'z') || ('A' <= ch && ch <= 'Z')){ letters++; // the current character is a letter if (('a' == ch || 'e' == ch || 'i' == ch || 'o' == ch || 'u' == ch) || ('A' == ch || 'E' == ch || 'I' == ch || 'O' == ch || 'U' == ch)) vowels++; // the current character is a vowel } else if (ch == ' ') spaces++; // the current character is a space } cout << letters << " letters " << vowels << " vowels " << spaces << " spaces" << endl; cout << "There are " << spaces + 1 << " words in the advice string." << endl; // Text Files PressReturn("\nText Files"); // This code opens the text file "twinkle.txt" that is in the local directory ifstream Fin("twinkle.txt"); // This is another way to do the same thing // ifstream Fin;" // Fname = twinkle.txt"; // Fin.open(Fname.c_str()); ofstream Fout("newtwink.txt"); // newtwink is open for writing // Now we read from the file and write the lowercase letters to "newtwink.txt" while (Fin.get(ch)){ if ('a' <= ch && ch <= 'z') Fout << ch; } cout << "The file 'newtwink.txt' should now exist." << endl; // The lines below make sure that the graphics windows remain open just // before the program terminates PressReturn("\nThe main program is about to terminate\n"); return 0; } // Functions definitions other than main void somestuff() { cout << "+=+=+=+" << endl; } void otherstuff() { for (int K = 0; K < 6; K++) cout << K << ' ' << " *** "; cout << endl; } int F(int x, int y){ if (x < y) return 2*x; else return 2*y;; } void WriteArray(int A[], int N){ for (int J = 0; J < N; J++) cout << J << ' ' << A[J] << endl; cout << endl; } void SmallChange(int& x, int y){ x = y + 10; y = x + 20; cout << "in SmallChange x = " << x << " y = " << y << endl; }