Startpunkten
This commit is contained in:
13
Matteprogram/header.h
Normal file
13
Matteprogram/header.h
Normal file
@@ -0,0 +1,13 @@
|
||||
/*****************************************************/
|
||||
/*HEADERFIL FÖR MATTEMANNEN */
|
||||
/*****************************************************/
|
||||
|
||||
#include <iostream.h> //för in och utmatning
|
||||
#include <string.h> //för strcmp()
|
||||
#include <stdlib.h> //för atof()
|
||||
#include <math.h> //för beräkningar
|
||||
#include <conio.h> //för clrscr() och getch()
|
||||
#define MAXGRAD 10 //maximal polynomgrad för Newton-Raphson
|
||||
#define VARV 100 //max antal beräkningar Newton-Raphson gör
|
||||
#define DIFF 0.01 //procentuell differans mellan x & xo i Newton-Raphson
|
||||
#define BUFFERSIZE 100 //bufferstorlek för säker inmatning
|
||||
323
Matteprogram/lab_1.cpp
Normal file
323
Matteprogram/lab_1.cpp
Normal file
@@ -0,0 +1,323 @@
|
||||
/***************************************************************/
|
||||
/* Laboration 1 i C++ */
|
||||
/***************************************************************/
|
||||
/* Program som beräknar : */
|
||||
/* 1. Förstagradsekvationer */
|
||||
/* 2. Andragradsekvationer */
|
||||
/* 3. Newton-Raphson */
|
||||
/* 4. Pythagoras sats */
|
||||
/* 5. Cosinussatsen */
|
||||
/***************************************************************/
|
||||
/* Crille & Robin 971009 */
|
||||
/***************************************************************/
|
||||
|
||||
#include "header.h"
|
||||
|
||||
/***************************************************************/
|
||||
// Säker inmatning
|
||||
/***************************************************************/
|
||||
float mataIn()
|
||||
{
|
||||
char buffer[BUFFERSIZE];
|
||||
float f;
|
||||
do
|
||||
{
|
||||
cin >> buffer;
|
||||
if (strcmp(buffer,"0") == 0)
|
||||
return 0.0;
|
||||
else
|
||||
{
|
||||
f = atof(buffer);
|
||||
if (f!=0)
|
||||
return f;
|
||||
else
|
||||
cout <<"Inget tal, försök igen: ";
|
||||
}
|
||||
}while (!f);
|
||||
}
|
||||
|
||||
/***************************************************************/
|
||||
// Förstagradsekvation
|
||||
/***************************************************************/
|
||||
void funk1()
|
||||
{
|
||||
float a,b,x;
|
||||
|
||||
clrscr();
|
||||
cout <<"Förstagradsekvation"<< endl;
|
||||
cout <<"-------------------\n\n";
|
||||
cout <<"Ekvationen har formen ax + b = 0"<< endl;
|
||||
cout <<"\nAnge a: ";
|
||||
a = mataIn();
|
||||
cout <<"\nAnge b: ";
|
||||
b = mataIn();
|
||||
if (a != 0)
|
||||
{
|
||||
x = (-b / a);
|
||||
cout <<"\nRoten är " << x << endl;
|
||||
}
|
||||
else
|
||||
cout <<"\nDet där är ingen ekvation!\n\n\n";
|
||||
cout <<"\nTryck på någon tangent !";
|
||||
getch();
|
||||
}
|
||||
|
||||
/***************************************************************/
|
||||
// Andragradsekvation
|
||||
/***************************************************************/
|
||||
void funk2()
|
||||
{
|
||||
float a,b,c,d,x1,x2,x3;
|
||||
|
||||
clrscr();
|
||||
cout <<"Andragradsekvation\n";
|
||||
cout <<"------------------\n\n";
|
||||
cout <<"Ekvationen har formen ax^2 + bx + c = 0\n";
|
||||
cout <<"Ange a: ";
|
||||
a = mataIn();
|
||||
cout <<"\nAnge b: ";
|
||||
b = mataIn();
|
||||
cout <<"\nAnge c: ";
|
||||
c = mataIn();
|
||||
if (a == 0)
|
||||
{
|
||||
if (b != 0)
|
||||
{
|
||||
x1 = (-c / b);
|
||||
cout <<"Roten är: " << x1 << endl;
|
||||
}
|
||||
else
|
||||
cout <<"Det där är ingen ekvation !";
|
||||
}
|
||||
else
|
||||
{
|
||||
if (pow((b / (2*a)),2) - (c / a) < 0)
|
||||
cout <<"\nRötterna är inte reella\n\n\n";
|
||||
else
|
||||
{
|
||||
d=(b / (2 * a));
|
||||
x2= ( -d ) + sqrt (pow (d,2) - (c / a));
|
||||
x3= ( -d ) - sqrt (pow (d,2) - (c / a));
|
||||
cout <<"\nRot 1: "<< x2 << endl;
|
||||
cout <<"Rot 2: "<< x3 << endl;
|
||||
}
|
||||
}
|
||||
cout <<"Tryck på någon tangent !";
|
||||
getch();
|
||||
}
|
||||
|
||||
/***************************************************************/
|
||||
// Newton-Raphson
|
||||
/***************************************************************/
|
||||
void funk3()
|
||||
{
|
||||
double koeff[MAXGRAD];
|
||||
double f,d,x,x0;
|
||||
int i,a = 0,grad;
|
||||
|
||||
clrscr();
|
||||
cout <<"Newton - Raphson"<< endl;
|
||||
cout <<"----------------\n"<< endl;
|
||||
cout <<"Mata in gradtal: ";
|
||||
grad = mataIn();
|
||||
for (i = 0 ; i <= grad ; i++)
|
||||
{
|
||||
cout <<"\nMata in koefficienten för x^"<< i << " ";
|
||||
koeff[i] = mataIn();
|
||||
}
|
||||
cout <<"Mata in ett startvärde: ";
|
||||
x = mataIn();
|
||||
do
|
||||
{
|
||||
x0 = x;
|
||||
f = 0;
|
||||
for (i = 0 ; i <= grad ; i++)
|
||||
f = f + koeff[i] * pow (x0 , i);
|
||||
d = 0;
|
||||
for (i = 1 ; i <= grad ; i++)
|
||||
d = d + koeff[i] * i * pow (x0 , i-1);
|
||||
x = x0 - f/d;
|
||||
a++;
|
||||
}while ((a <= VARV) && (fabs(x - x0) > fabs(x * DIFF)));
|
||||
if (fabs (x - x0) < fabs(x * DIFF))
|
||||
cout <<"\nRoten är: "<< x << endl;
|
||||
else
|
||||
cout <<"\nRoten ej funnen efter "<< VARV <<" beräkningar.\n";
|
||||
getch();
|
||||
}
|
||||
|
||||
/***************************************************************/
|
||||
// Pythagoras sats
|
||||
/***************************************************************/
|
||||
void funk4()
|
||||
{
|
||||
char sort;
|
||||
float a,b,c;
|
||||
|
||||
clrscr();
|
||||
cout <<"Är hypotenusan känd? [j/n]";
|
||||
cin >> sort;
|
||||
if (sort == 'n')
|
||||
{
|
||||
clrscr();
|
||||
cout <<"Pythagoras sats\n";
|
||||
cout <<"---------------\n\n";
|
||||
cout <<"\nAnge ena sidan i triangeln: ";
|
||||
a = mataIn();
|
||||
cout <<"\nAnge andra sidan i triangeln: ";
|
||||
b = mataIn();
|
||||
if (a < 0 || b < 0)
|
||||
{
|
||||
cout <<"Otillåten inmatning !"<< endl;
|
||||
getch();
|
||||
}
|
||||
else
|
||||
{
|
||||
c = sqrt(pow(a,2) + pow(b,2));
|
||||
cout <<"\nHypotenusan är: "<< c <<"\n\n\n"<< endl;
|
||||
cout <<"Tryck på någon tangent !";
|
||||
getch();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
clrscr();
|
||||
cout <<"Pythagoras sats\n";
|
||||
cout <<"---------------\n\n";
|
||||
cout <<"\nAnge ena sidan i triangeln: ";
|
||||
a = mataIn();
|
||||
cout <<"\nAnge hypotenusan: ";
|
||||
b = mataIn();
|
||||
if (a < 0 || b < 0)
|
||||
{
|
||||
cout <<"Otillåten inmatning !"<< endl;
|
||||
getch();
|
||||
}
|
||||
else if (a > b)
|
||||
{
|
||||
cout <<"\nHypotenusan måste vara längst !";
|
||||
getch();
|
||||
}
|
||||
else
|
||||
{
|
||||
c = sqrt (pow(b,2) - pow(a,2));
|
||||
cout <<"\nSidan är är: "<< c;
|
||||
getch();
|
||||
}
|
||||
cout <<"Tryck på någon tangent !";
|
||||
getch();
|
||||
}
|
||||
}
|
||||
|
||||
/***************************************************************/
|
||||
// Cosinussatsen
|
||||
/***************************************************************/
|
||||
void funk5()
|
||||
{
|
||||
float a,b,c,x,y;
|
||||
int vad;
|
||||
|
||||
clrscr();
|
||||
cout <<"Cosinus-satsen"<< endl;
|
||||
cout <<"--------------"<< endl;
|
||||
cout <<"Vad vill du beräkna? "<< endl;
|
||||
cout <<"1. En sida"<< endl;
|
||||
cout <<"2. En vinkel"<< endl;
|
||||
vad = mataIn();
|
||||
|
||||
if (vad == 1)
|
||||
{
|
||||
clrscr();
|
||||
cout <<"Beräkning av en sida i en triangel"<< endl;
|
||||
cout <<"----------------------------------"<< endl;
|
||||
cout <<"Ange två sidor och dess"<< endl;
|
||||
cout <<"mellanliggande vinkel i grader"<< endl;
|
||||
cout <<"\nMata in första sidan: ";
|
||||
a = mataIn();
|
||||
cout <<"\nMata in andra sidan: ";
|
||||
b = mataIn();
|
||||
cout <<"\nMata in vinkeln: ";
|
||||
x = mataIn();
|
||||
if (a < 0 || b < 0 || x < 0)
|
||||
{
|
||||
cout <<"\nOtillåten inmatning !"<< endl;
|
||||
getch();
|
||||
}
|
||||
else
|
||||
{
|
||||
y = sqrt (pow (a , 2) + pow (b , 2) - (2 * a * b * cos(x*M_PI/180)));
|
||||
cout <<"\nSidan är " << y << endl;
|
||||
getch();
|
||||
}
|
||||
}
|
||||
if (vad == 2)
|
||||
{
|
||||
clrscr();
|
||||
cout <<"Beräkning av en vinkel i en triangel"<< endl;
|
||||
cout <<"----------------------------------"<< endl;
|
||||
cout <<"Ange tre sidor i en triangel."<< endl;
|
||||
cout <<"Den första skall vara motstående sida mot sökt vinkel!"<< endl;
|
||||
cout <<"\nMata in a: ";
|
||||
a = mataIn();
|
||||
cout <<"\nMata in b: ";
|
||||
b = mataIn();
|
||||
cout <<"\nMata in c: ";
|
||||
c = mataIn();
|
||||
if ((a > (b+c)) || (b > (a+c)) || (c > (b+a)))
|
||||
{
|
||||
cout <<"Otillåten inmatning !"<< endl;
|
||||
getch();
|
||||
}
|
||||
else
|
||||
{
|
||||
y = ((b * b) + (c * c) - (a * a)) / (2 * a * c);
|
||||
x = acos(y);
|
||||
cout <<"\nVinkel är " << x << " radianer eller "<< (x * (180/ (M_PI))) << " grader" << endl;
|
||||
getch();
|
||||
}
|
||||
}
|
||||
else
|
||||
cout <<"Fel val, försök igen !"<< endl;
|
||||
}
|
||||
|
||||
/***************************************************************/
|
||||
// Meny
|
||||
/***************************************************************/
|
||||
void meny()
|
||||
{
|
||||
clrscr();
|
||||
cout <<"Välkommen till Crille's & Robin's matteprogram.\n";
|
||||
cout <<"-----------------------------------------------\n\n";
|
||||
cout <<"1. Förstagradsekvationer \n";
|
||||
cout <<"2. Andragradsekvationer \n";
|
||||
cout <<"3. Newton-Raphsons metod \n";
|
||||
cout <<"4. Pythagoras sats \n";
|
||||
cout <<"5. Cosinussatsen \n";
|
||||
cout <<"0. Avsluta \n\n\n";
|
||||
cout <<"Gör ditt val! \n";
|
||||
}
|
||||
|
||||
/***************************************************************/
|
||||
//Main
|
||||
/***************************************************************/
|
||||
int main()
|
||||
{
|
||||
char val = 1;
|
||||
do
|
||||
{
|
||||
meny();
|
||||
cin >> val;
|
||||
switch (val)
|
||||
{
|
||||
case '1' : funk1();break;
|
||||
case '2' : funk2();break;
|
||||
case '3' : funk3();break;
|
||||
case '4' : funk4();break;
|
||||
case '5' : funk5();break;
|
||||
case '0' : cout <<"Programmet avslutat"<< endl;break;
|
||||
default: cout <<"Fel val"<< endl;
|
||||
}
|
||||
}
|
||||
while (val != '0');
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user