Вы находитесь на странице: 1из 10

Average.c #include <fstream.h> void main () { ifstream f1; ofstream f2; f1.open("scores.96"); f2.open("final.

96"); int s1, s2, s3; float w1, w2, w3; f1 f1 f1 f2 } >> >> >> << s1 >> w1; s2 >> w2; s3 >> w3; (s1*w1+s2*w2+s3*w3);

Multiple.c
#include <iostream.h> int mult (int x, int y) { int result; result = 0; while (y != 0) { result = result + x; y = y - 1; } return(result); } int main () { int x, y; cout << "Enter two natural numbers: "; cin >> x >> y; cout << x << " * " << y << " = " << mult(x,y) << endl; return(0); } better algorithm for multiplication) #include <iostream.h> // Idea of the Russian Peasant Method (as old as 1700 B.C.) // x * n = 2x * (n/2) if n even // = x + x * (n-1) if n odd int fastmult (int x, int y) { int result; result = 0; while (y != 0) { if (y % 2 == 0) { x = 2*x; y = y/2;

} return(result);

} else { result = result + x; y = y-1; }

int main () { int x, y; cout << "Enter two natural numbers: "; cin >> x >> y; cout << x << " * " << y << " = " << fastmult(x,y) << endl; return(0); }

(count number of characters and lines in a file)


#include <iostream.h> #include <fstream.h> int main () { ifstream f1; char c; int numchars, numlines; f1.open("test");

numchars = 0; numlines = 0; f1.get(c); while (f1) { while (f1 && c != '\n') { numchars = numchars + 1; f1.get(c); } numlines = numlines + 1; f1.get(c); } cout << "The file has " << numlines << " lines and " << numchars << " characters" << endl; return(0); }

(merge sort)
#include <fstream.h> #include <iostream.h> int main () { ifstream f1, f2; ofstream f3; int i,j; f1.open("n1"); f2.open("n2"); f3.open("n1n2");

f1 >> i; f2 >> j; while (f1 && f2) { if (i < j) { while (i < j && f1 && f3) { f3 << i << endl; f1 >> i; } } else { while (j <= i && f2 && f3) { f3 << j << endl; f2 >> j; } } } while (f1) { f3 << i << endl; f1 >> i; } while (f2) { f3 << j << endl; f2 >> j; } return (0); }

grade.C
#include <iostream.h> #include <fstream.h> typedef int Bool; const Bool TRUE = 1; const Bool FALSE = 0; char printableBool (Bool i) { if (i==FALSE) return 'F'; else return 'T'; } int main () { ifstream f1, f2, f3check; ofstream f3; f1.open("exam"); if (!f1) { cout << "Cannot open file exam" << endl; return 1; } f2.open("key"); if (!f2) { cout << "Cannot open file key" << endl;

return 1; } f3check.open("result"); if (f3check) { cout << "File result already exists" << endl; return 1; } f3.open("result"); if (!f3) { cout << "Cannot open file result" << endl; return 1; } int runningSum; int ans, key, score; runningSum = 0; f1 >> ans; if (!f1) { cout << "Error reading from file exam" << endl; return 1; } f2 >> key; if (!f1) { cout << "Error reading from file key" << endl; return 1; } if (ans == key) { runningSum = runningSum + 25; score = TRUE; } else score = FALSE; f3 << printableBool(score) << endl; if (!f3) { cout << "Error writing to file result" << endl; return 1; } f1 >> ans; if (!f1) { cout << "Error reading from file exam" << endl; return 1; } f2 >> key; if (!f1) { cout << "Error reading from file key" << endl; return 1; } if (ans == key) { runningSum = runningSum + 25; score = TRUE; } else score = FALSE; f3 << printableBool(score) << endl; if (!f3) { cout << "Error writing to file result" << endl;

return 1;

f1 >> ans; if (!f1) { cout << "Error reading from file exam" << endl; return 1; } f2 >> key; if (!f1) { cout << "Error reading from file key" << endl; return 1; } if (ans == key) { runningSum = runningSum + 25; score = TRUE; } else score = FALSE; f3 << printableBool(score) << endl; if (!f3) { cout << "Error writing to file result" << endl; return 1; } f1 >> ans; if (!f1) { cout << "Error reading from file exam" << endl; return 1; } f2 >> key; if (!f1) { cout << "Error reading from file key" << endl; return 1; } if (ans == key) { runningSum = runningSum + 25; score = TRUE; } else score = FALSE; f3 << printableBool(score) << endl; if (!f3) { cout << "Error writing to file result" << endl; return 1; } f3 << endl << runningSum << endl; }

Grade2.c
#include <iostream.h> #include <fstream.h> typedef int Bool;

const Bool TRUE = 1; const Bool FALSE = 0; char printableBool (Bool i) { if (i==FALSE) { return 'F'; } else { return 'T'; } } int main () { ifstream f1, f2; ofstream f3; f1.open("exam"); f2.open("key"); f3.open("result"); int runningSum; int ans, key, score; runningSum = 0; f1 >> ans; f2 >> key; while (f1) { if (ans == key) { runningSum = runningSum + 25; score = TRUE; } else { score = FALSE; } f3 << printableBool (score) << endl; f1 >> ans; f2 >> key; } f3 << endl << runningSum << endl; }

GCD.c
#include <iostream.h> // Fundamental idea of Euclid's algorithm (one of the oldest known algorithms) // gcd(a,0) = a // gcd(a,b) = gcd(b,a%b) int gcd (int a, int b) { int temp; while (b != 0) {

} return(a);

temp = a % b; a = b; b = temp;

int main () { int x, y; cout << "Enter two natural numbers: "; cin >> x >> y; cout << "gcd(" << x << ", " << y << ") = " << gcd(x,y) << endl; return(0); } Fibonacci numbers) #include <iostream.h> // sequence is 0, 1, 1, 2, 3, 5, 8, 13, ... int fib (int i) { int pred, result, temp; pred = 1; result = 0; while (i > 0) { temp = pred + result; result = pred; pred = temp; i = i-1; } return(result);

int main () { int n; cout << "Enter a natural number: "; cin >> n; while (n < 0) { cout << "Please re-enter: "; cin >> n; } cout << "fib(" << n << ") = " << fib(n) << endl; return(0); } rob99.C #include <iostream.h> /* void main () { int num, count = 1; float sum = 0.0; cout << endl; while (count <= 3) {

cout << "Enter a number: "; cin >> num; sum = sum + num; count = count + 1; } cout << "Average is " << sum / count << endl; } // Try entering 8, 8, 8 ==> average is 6 which is bogus There are several ways of fixing the problem. Here is one of them: */ void main () { int num, count = 1; float sum = 0.0; cout << endl; while (count <= 3) { cout << "Enter a number: "; cin >> num; sum = sum + num; count = count + 1; } cout << "Average is " << sum / (count - 1) << endl;

Telephone.c #include <iostream.h> #include <ctype.h> #include <stdlib.h> typedef int Bool; const Bool TRUE = 1; const Bool FALSE = 0; void readLetter(char&); int computeCorrespondingDigit(char); Bool doOneLetter(); //returns flag saying whether you should quit void main () { Bool flag = FALSE; while (!flag) { flag = doOneLetter(); } } int exitLetter (char c) { return ((c == 'Q') || (c == 'Z')); } Bool doOneLetter() { char l; int i; readLetter(l); // returns upper case letter

if (exitLetter(l)) { cout << "Quit." << endl; return(TRUE); } else { i = computeCorrespondingDigit(l); cout << "The letter " << l << " corresponds to " << i << " on the telephone" << endl; return(FALSE); } } int computeCorrespondingDigit (char l) { switch (l) { case 'A' : case 'B' : case 'C' : return(2); case 'D' : case 'E' : case 'F' : return(3); case 'G' : case 'H' : case 'I' : return(4); case 'J' : case 'K' : case 'L' : return(5); case 'M' : case 'N' : case 'O' : return(6); case 'P' : case 'R' : case 'S' : return(7); case 'T' : case 'U' : case 'V' : return(8); case 'W' : case 'X' : case 'Y' : return(9); } } void readLetter(char& c) { cout << "Enter a letter: "; cin >> c; while (cin && !isalpha(c)) { cout << "You have not entered a letter. Try again: "; cin >> c; } if (!cin) {

cout << "Unrecoverable error... Exiting" << endl; exit(-1); } c = toupper(c); }

Вам также может понравиться