Showing posts with label Template. Show all posts
Showing posts with label Template. Show all posts

Write overloaded function templates for finding the roots of the linear (a * x + b = 0) and square (a * x2 + b * x + c = 0) uravneniy.Zamechanie: in function to send coefficients of the equations.

Solution For C Program - 

Write overloaded function templates for finding the roots of the linear (a * x + b = 0) and square (a * x2 + b * x + c = 0) uravneniy.Zamechanie: in function to send coefficients of the equations.


#include <iostream>
#include <math.h>
#include <windows.h>

using namespace std;

HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);



double findUnknown(int, int);
double findUnknown(double, double);

double findUnknown(int, int, int);
double findUnknown(double, double, double);

void testFindUnknow2IntType(int test, int a, int b , double expected)
{
double result = findUnknown(a,b);

if (expected == result)
{
SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN);
cout << test << " passed\n";
}
else
{
SetConsoleTextAttribute(hConsole, FOREGROUND_RED);
cout << test << " failed\n";
cout << result << " actual\n";
cout << expected << " expected\n\n";
}
}

void testFindUnknow2DoubleType(int test, double a, double b, double expected)
{
double result = findUnknown(a, b);

if (expected == result)
{
SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN);
cout << test << " passed\n";
}
else
{
SetConsoleTextAttribute(hConsole, FOREGROUND_RED);
cout << test << " failed\n";
cout << result << " actual\n";
cout << expected << " expected\n\n";
}
}

void testFindUnknow3IntType(int test, int a, int b, int c, double expected)
{
double result = findUnknown(a, b, c);

if (expected == result)
{
SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN);
cout << test << " passed\n";
}
else
{
SetConsoleTextAttribute(hConsole, FOREGROUND_RED);
cout << test << " failed\n";
cout << result << " actual\n";
cout << expected << " expected\n\n";
}
}

void testFindUnknow3DoubleType(int test, double a, double b, double c, double expected)
{
double result = findUnknown(a, b, c);

if (expected == result)
{
SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN);
cout << test << " passed\n";
}
else
{
SetConsoleTextAttribute(hConsole, FOREGROUND_RED);
cout << test << " failed\n";
cout << result << " actual\n";
cout << expected << " expected\n\n";
}
}


void main()
{
int menuButton = 0;
int show = 0;

int a = 0;
int b = 0;
int c = 0;

cout << "1.Line\n2.Square\n";
cin >> menuButton;

if (menuButton == 1)
{
/*cout << "1. a*x + b = 0" << endl;
cin >> a >> b;

cout << findUnknown(a, b) << endl;*/

testFindUnknow2IntType(1, 2, 4, -2);
testFindUnknow2IntType(2, 1, 5, -5);
testFindUnknow2DoubleType(3, 4, 2, 0.5);
testFindUnknow2DoubleType(4, 5, 2.5 , 0.5);

}

if (menuButton == 2)
{
/*cout << "2. a*x^2 + b*x + c = 0" << endl;
cin >> a >> b >> c;

show = findUnknown(a, b, c);
cout << "X2 = " << show << endl;*/

testFindUnknow3IntType(1, 1, -2, -3, -1);
testFindUnknow3IntType(2, 1, 5, 6, -3);
testFindUnknow3DoubleType(3, 0.5, 6, 0, -12);
testFindUnknow3DoubleType(4, 12, 5, -0.5, -0.5);

}
}


double findUnknown(int a, int b)
{
double x = b * (-1) / a;
return x;
}

double findUnknown(double a, double b)
{
double x = b / a;
return x;
}

double findUnknown(int a, int b, int c)
{
int discriminant = b*b - 4 * a * c;

double x1 = (b*(-1) - sqrt(discriminant)) / 2 * a;
//double x2 = (b*(-1) - sqrt(discriminant)) / 2 * a;

return x1;
}

double findUnknown(double a, double b, double c)
{
double discriminant = b*b - 4 * a * c;

double x1 = (b*(-1) - sqrt(discriminant)) / ( 2 * a );
//double x2 = (b*(-1) - sqrt(discriminant)) / ( 2 * a );

return x1;
}

Creates new main.c with empty main boilerplate template

How to write a C Program to Creates new main.c with empty main boilerplate template in C Programming Language ?


Solution:

  1. #define _GNU_SOURCE
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <unistd.h>
  8. #include <errno.h>
  9. #include <stdbool.h>
  10. #include <inttypes.h>
  11. /**
  12.  * Prints an optional error message, and kills process.
  13.  * At the end of the error message, a newline is printed.
  14.  * If msg is a NULL pointer then nothing will be printed.
  15.  */
  16. void die(int status, const char *msg)
  17. {
  18.     // print error message
  19.     if (msg) {
  20.         fprintf(stderr, "%s: %s\n", "mkcmain", msg);
  21.     }
  22.     exit(status);
  23. }
  24. int main(void)
  25. {    
  26.     const char *comment_text =
  27.         "/***************************************************************************\n"
  28.         " * Created by mkcmain                                                      *\n"
  29.         " **************************************************************************/\n\n";
  30.     size_t      comment_text_size;
  31.     const char  *cmain_text = "int main(int argc, char *argv[])\n"
  32.                               "{\n\n"
  33.                               "\treturn 0;\n"
  34.                               "}\n";
  35.     size_t      cmain_text_size;
  36.     const char  *filename   = "main.c";
  37.     char        *cw_dir;
  38.     char        *filepath;
  39.     FILE        *file;
  40.     struct stat sb;
  41.     // attempt to get the current working dir
  42.     errno  = 0;
  43.     cw_dir = get_current_dir_name();
  44.     if(NULL == cw_dir) {
  45.         perror("get_current_dir_name()");
  46.         die(EXIT_FAILURE, "could not get current directory name");
  47.     }
  48.     // allocate memory for path
  49.     if (NULL == (filepath = malloc((strlen(cw_dir) + 1 + strlen(filename) + 1) * sizeof(char)))) {
  50.         die(EXIT_FAILURE, "could not allocate memory for file path name"); }
  51.     // construct file path
  52.     strcpy(filepath, cw_dir);
  53.     strcat(filepath, "/");
  54.     strcat(filepath, filename);
  55.     free(cw_dir); // just noticed this was a  memory leak .. havent tested yet
  56.     // check to see if file exists
  57.     if (0 == stat(filepath, &sb)) {
  58.         bool abort = false;
  59.         // see if this is a regular file
  60.         if (!(sb.st_mode & S_IFREG)) {
  61.             abort = true;
  62.             fprintf(stderr, "%s: %s is not a regular file\n", "mkcmain", filepath);
  63.         } else {
  64.             // prompt user to replace file
  65.             char response;
  66.             fprintf(stdout, "%s: %jd byte file %s exists. Replace file? [y/N?] ",
  67.                     "mkcmain", (intmax_t) sb.st_size, filepath);
  68.             response = fgetc(stdin);
  69.             if (response != 'y' && response != 'Y') {
  70.                 abort = true;
  71.             }
  72.         }
  73.         if (abort) {
  74.             free(filepath);
  75.             die(EXIT_FAILURE, "no file written");
  76.         }
  77.     }
  78.     // try to open file for writing
  79.     errno = 0;
  80.     if (NULL == (file = fopen(filepath, "w"))) {
  81.         perror("fopen");
  82.         fprintf(stderr, "%s: could not open %s for writing\n", "mkcmain", filepath);
  83.         free(filepath);
  84.         die(EXIT_FAILURE, NULL);
  85.     }
  86.     // try writing comment block
  87.     comment_text_size = strlen(comment_text);
  88.     if (fwrite(comment_text, sizeof(char), comment_text_size, file)
  89.             != comment_text_size * sizeof(char))
  90.     {
  91.         fprintf(stderr, "%s: could not write to open file %s\n", "mkcmain", filepath);
  92.         free(filepath);
  93.         die(EXIT_FAILURE, NULL);
  94.     }
  95.     // try to write C main file text to file
  96.     cmain_text_size = strlen(cmain_text);
  97.     if (fwrite(cmain_text, sizeof(char), cmain_text_size, file)
  98.             != cmain_text_size * sizeof(char))
  99.     {
  100.         fprintf(stderr, "%s: could not write to open file %s\n", "mkcmain", filepath);
  101.         free(filepath);
  102.         die(EXIT_FAILURE, NULL);
  103.     }
  104.     // try to close the file
  105.     errno = 0
  106.     if (fclose(file)) {
  107.         perror("fclose()");
  108.         free(filepath);
  109.         die(EXIT_FAILURE, "encountered error during attempt to close file");
  110.     }
  111.     fprintf(stdout, "%s: successfully created %s\n", "mkcmain", filepath);
  112.     free(filepath);
  113.     exit(EXIT_SUCCESS);
  114. }