/* * functions.cpp * * Created on: 06.12.2012 * Author: trifon */ #include #include #include #include "functions.h" using namespace std; const int N = 5; double triarea(double x1, double y1, double x2, double y2, double x3, double y3) { double a = distance(x1, y1, x2, y2); double b = distance(x2, y2, x3, y3); double c = distance(x1, y1, x3, y3); x1 = x1 * 7; x3 = x3 / 2; return heron(a, b, c); } double distance(double x1, double y1, double x2, double y2) { return sqrt(sqr(x2-x1) + sqr(y2-y1)); } inline double sqr(double a) { return a*a; } double heron(double a, double b, double c) { double p = (a+b+c)/2; return sqrt(p*(p-a)*(p-b)*(p-c)); } void readPoint(int n, double* px, double* py) { cout << "(x" << n; cout << ",y" << n; cout << ") = "; cin >> *px >> *py; } int main1() { double x[3], y[3]; /* readPoint(1, &x[0], &y[0]); readPoint(2, &x[1], &y[1]); readPoint(3, &x[2], &y[2]); */ /* readPoint(1, x, y); readPoint(2, x+1, y+1); readPoint(3, x+2, y+2); */ for(int i = 0; i < 3; i++) readPoint(i+1, x+i, y+i); cout << "Лицето е " << triarea(x[0], y[0], x[1], y[1], x[2], y[2]) << endl; return 0; } int f(int x) { static int n = 0; return x + n++; } int g() { int n; cin >> n; return n+1; } void swap(short* px, short* py) { cout << "Разменяме две малки цели числа!\n"; short tmp = *px; *px = *py; *py = tmp; } void swap(int* px, int* py) { cout << "Разменяме две цели числа!\n"; int tmp = *px; *px = *py; *py = tmp; // безсмислено: // px = py; // py = &tmp; } void swap(int* px, int* py, int* pz) { cout << "Разменяме три цели числа!\n"; int tmp = *px; *px = *py; *py = *pz; *pz = tmp; } void swap(double* pa, double* pb) { cout << "Разменяме две дробни числа!\n"; double tmp = *pa; *pa = *pb; *pb = tmp; } void swap(int& x, int& y) { int tmp = x; x = y; y = tmp; } int main2() { cout << f(0) << endl; cout << f(0) << endl; cout << f(0) << endl; cout << g() << g() << g() << endl; } void copy2(char* dest, char const* source) { strcpy(dest, source); strcat(dest, source); } int main3() { int a, b, c; cin >> a >> b >> c; swap(&a,&b); swap(a,b); // !!! swapr(2,3); // !!! int &x = 2; // !!! int &y = 3; // !!! swapr(a+1,b+1); // !!! swap(&2, &3); cout << a << ' ' << b << endl; char s1[] = "Hello, world!"; char s2[100] = ""; copy2(s2, s1); cout << s2 << endl; copy2(s2, "abc"); cout << s2 << endl; // !!! copy2("abc", s1); // char const* source = "abc"; // !!! char* dest = "abc"; swap(&a, &b, &c); // !!! int x; // !!! double x; cout << a << ' ' << b << ' ' << c << endl; double d = 1.2, e = 2.3; swap(&d, &e); cout << d << ' ' << e << endl; } void readArray(int arr[], int n, char const* name) { for(int i = 0; i < n; i++) { cout << name << "[" << i << "] = "; cin >> arr[i]; } } void writeArray(int arr[], int n, char const* name) { for(int i = 0; i < n; i++) cout << name << "[" << i << "] = " << arr[i] << endl; } int search(int arr[], int n, int x) { int i = 0; while (i < n && arr[i] != x) i++; /* if (i < n) return true; return false; */ if (i == n) return -1; return i; } bool equal(char const* s1, char const* s2) { return strcmp(s1, s2) == 0; } void findMinMax(int arr[], int n, int& min, int& max) { min = max = arr[0]; for(int i = 1; i < n; i++) { if (arr[i] < min) min = arr[i]; if (arr[i] > max) max = arr[i]; } } void writeMatrix(int matrix[][N], int n, int m) { for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) cout << matrix[i][j] << '\t'; cout << endl; } } int main4() { int a[N] = { 0 }, b[N] = {0}; readArray(a, N, "a"); readArray(b, N, "b"); // readArray(a + 2, 3, "a"); writeArray(a, N, "a"); int x; cout << "x = ";cin >> x; cout << search(a, N, x) << ' ' << search(b, N, x) << endl; cout << equal("abc","abc") << endl; int mina, maxa, minb, maxb; findMinMax(a, N, mina, maxa); findMinMax(b, N, minb, maxb); cout << mina << ' ' << maxa << endl; cout << minb << ' ' << maxb << endl; } const int NWORDS = 20, WORDSIZE = 100; /* * Прочита масив от низове, максимум NWORDS низа от по * максимум WORDSIZE символа. * Връща броя на прочетените низове */ int readWords(char w[][WORDSIZE]) { int i = 0; do { cout << "Въведете дума " << i+1 << " "; cin.getline(w[i], WORDSIZE); i++; } while (i < NWORDS && w[i-1][0] != '\0'); // i == NWORDS || w[i-1][0] == '\0' return i-1; } int searchWord(char (*w)[WORDSIZE], int nwords, char const* word) { int i = 0; while (i < nwords && !equal(w[i], word)) i++; // i == nwords || equal(w[i], word) if (i == nwords) return -1; return i; } int main5() { int m[N][N] = { { 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} }; // writeMatrix(m + 2, 3, 3); writeMatrix(m, N, N); char words[NWORDS][WORDSIZE]; int nwords = readWords(words); cout << nwords << endl; cout << searchWord(words, nwords, "Hello") << endl; } int* wrong(int y) { int x = y + 2; return &x; } int main6() { int* p = wrong(2); cout << *p << endl; *p = 10; wrong(20); cout << *p << endl; } int* pointMax(int* p, int* q) { if (*p > *q) return p; return q; } int& middle( int& x, int& y, int& z) { if (x <= y && y <= z || z <= y && y <= x) return y; if (y <= z && z <= x || x <= z && z <= y) return z; return x; } const int MAX = 1000; // int x; // !!!! int[10] f(); // double a[][MAX] = {0}, b[][MAX] = {0}; void multiplyMatrices(double a[][MAX], double b[][MAX], double c[][MAX], int n, int m, int p) { for(int i = 0; i < n; i++) for(int j = 0; j < p; j++) { c[i][j] = 0; for(int k = 0; k < m; k++) c[i][j] += a[i][k] * b[k][j]; } } int main7() { int a = 2, b = 5, c = 3; int* p = pointMax(&a, &b); cout << *p << endl; //!!!! *p-- <--> *(p--); (*p)--; cout << b << endl; // middle(a,b,c) = 8; int& m = middle(a,b,c); m = 8; cout << c << endl; } // strchr char const* my_strchr(char const* s, char c) { char const* p = s; while (*p != '\0' && *p != c) p++; // *p == '\0' || *p == c return p; } char const* my_strdiff(char const* s1, char const* s2) { char const *p = s1, *q = s2; while (*p == *q && *p != '\0' && *q != '\0') { p++; q++; } // *p != *q || *p == '\0' || *q == '\0' return p; } int main() { char str[] = "Hello, world!"; cout << my_strchr(str, 'l') << endl; cout << my_strchr(str, 't') << endl; cout << my_strdiff(str, "Help me!") << endl; }