Startpunkten
This commit is contained in:
140
Mastermind/Lab_2.cpp
Normal file
140
Mastermind/Lab_2.cpp
Normal file
@@ -0,0 +1,140 @@
|
||||
/***************************************************************/
|
||||
/* Laboration 2 i C++ */
|
||||
/***************************************************************/
|
||||
/* Mastermind */
|
||||
/* Crille & Robin 971009 */
|
||||
/***************************************************************/
|
||||
|
||||
#include "lab_2.h"
|
||||
|
||||
typedef int secret[ARRAY];
|
||||
|
||||
/***************************************************************/
|
||||
/*SÄKER INMATNING */
|
||||
/***************************************************************/
|
||||
int mataIn()
|
||||
{
|
||||
char buffer[BUFFERSIZE];
|
||||
int f;
|
||||
|
||||
do {
|
||||
cin >> buffer;
|
||||
if (strcmp(buffer,"0") == 0)
|
||||
return 0;
|
||||
else {
|
||||
f = atoi(buffer);
|
||||
if (f!=0)
|
||||
return f;
|
||||
else
|
||||
cout <<"Inget tal, försök igen: ";
|
||||
}
|
||||
}while (!f);
|
||||
}
|
||||
|
||||
/***************************************************************/
|
||||
/*GENERERAR EN hemligt_tal - VEKTOR */
|
||||
/***************************************************************/
|
||||
void hemligt(secret &tal)
|
||||
{
|
||||
int i,j,slumpat_tal,size=10;
|
||||
int slump[] = {0,1,2,3,4,5,6,7,8,9};
|
||||
|
||||
srand(time(NULL));
|
||||
for (i=0; i<ARRAY; i++) {
|
||||
slumpat_tal = rand()%size;
|
||||
tal[i]= slump[slumpat_tal];
|
||||
for(j=slumpat_tal ; j<size-1 ; j++)
|
||||
slump[j] = slump[j+1];
|
||||
size--;
|
||||
}
|
||||
}
|
||||
|
||||
/*****************************************************************/
|
||||
/*GISSNINGEN in_data TILLDELAS SIN PLATS I gissn - VEKTORN */
|
||||
/*****************************************************************/
|
||||
void gissning(secret gissn, int a)
|
||||
{
|
||||
int in_data;
|
||||
|
||||
cout << "\nVad gissar du på: ----";
|
||||
gotoxy(19,a+3);
|
||||
in_data = mataIn();
|
||||
gissn[0] = ((in_data % 10000)/1000);
|
||||
gissn[1] = ((in_data % 1000)/100);
|
||||
gissn[2] = ((in_data % 100)/10);
|
||||
gissn[3] = (in_data % 10);
|
||||
}
|
||||
|
||||
/***************************************************************/
|
||||
/*SKRIVER UT S_R PÅ LÄMPLIGT STÄLLE */
|
||||
/***************************************************************/
|
||||
void kontroll(secret gissn, secret hemligt_tal, char help[])
|
||||
{
|
||||
int i,j;
|
||||
|
||||
for (j=0 ; j<ARRAY ; j++) {
|
||||
if (hemligt_tal[j] == gissn[j])
|
||||
help[j] = 'R';
|
||||
else {
|
||||
help[j] = '_';
|
||||
for (i=0 ; i<ARRAY ; i++) {
|
||||
if(gissn[j] == hemligt_tal[i])
|
||||
help[j] = 'S';
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/***************************************************************/
|
||||
/*SKRIVER UT HELPARRAYEN */
|
||||
/***************************************************************/
|
||||
void helparray(char help[],int a)
|
||||
{
|
||||
gotoxy(25,a+3);
|
||||
for (int i=0 ; i<ARRAY ; i++)
|
||||
cout << help[i];
|
||||
}
|
||||
|
||||
/***************************************************************/
|
||||
/*AVBRYTER PGA FÖR MÅNGA GISSNINGAR */
|
||||
/***************************************************************/
|
||||
void fel(secret &tal)
|
||||
{
|
||||
clrscr();
|
||||
cout <<"Du får inte fler försök!\n"
|
||||
<<"Det rätta svaret var: ";
|
||||
for (int i=0 ; i<ARRAY ; i++)
|
||||
cout << tal[i];
|
||||
}
|
||||
|
||||
/***************************************************************/
|
||||
/*MAIN */
|
||||
/***************************************************************/
|
||||
void main()
|
||||
{
|
||||
int omstart=1;
|
||||
while (omstart) {
|
||||
int hemligt_tal[ARRAY],gissn[ARRAY],a=0;
|
||||
char help[]={'_','_','_','_','\0'},replay;
|
||||
hemligt(hemligt_tal);
|
||||
clrscr();
|
||||
cout <<"MASTERMIND (AKA LAB_2) BY CRILLE & ROBIN"<< endl
|
||||
<<"UNREGISTRED VERSION. " << endl;
|
||||
do {
|
||||
a++;
|
||||
gissning(gissn,a);
|
||||
kontroll(gissn,hemligt_tal,help);
|
||||
helparray(help,a);
|
||||
}while (((help[0]!='R') || (help[1]!='R') ||
|
||||
(help[2]!='R') || (help[3]!='R'))&& a < TRIES);
|
||||
if (a >= TRIES)
|
||||
fel(hemligt_tal);
|
||||
else
|
||||
klar(a);
|
||||
cout <<"\nVill du spela en omgång till\ [J/N]:";
|
||||
cin >> replay;
|
||||
if (replay == 'n')
|
||||
omstart=0;
|
||||
}
|
||||
}
|
||||
|
||||
111
Mastermind/filhant.cpp
Normal file
111
Mastermind/filhant.cpp
Normal file
@@ -0,0 +1,111 @@
|
||||
#include "lab_2.h"
|
||||
|
||||
/***************************************************************/
|
||||
/*SKRIVER UT HIGHSCORE-LISTAN */
|
||||
/***************************************************************/
|
||||
void printhigh(spelare listarray[],int antal)
|
||||
{
|
||||
int i;
|
||||
clrscr();
|
||||
cout <<"HIGHSCORES\n"
|
||||
<<"----------\n";
|
||||
cout << setw(6) << "Plats:"
|
||||
<< setw(NAMESIZE) << "Namn:"
|
||||
<< setw(15) << "Gissningar:"
|
||||
<< setw(18) << "Datum:"
|
||||
<< endl;
|
||||
|
||||
for (i=0 ; i<antal ; i++)
|
||||
if (listarray[i].score != 0)
|
||||
cout << setw(6) << (i+1)
|
||||
<< setw(NAMESIZE) << listarray[i].namn
|
||||
<< setw(15) << listarray[i].score
|
||||
<< setw(12)
|
||||
<< listarray[i].datum.da_year << "-"
|
||||
<< (listarray[i].datum.da_mon+0) << "-"
|
||||
<< (listarray[i].datum.da_day+0)
|
||||
<< endl;
|
||||
getch();
|
||||
}
|
||||
|
||||
/***************************************************************/
|
||||
/*BUBBLESORT */
|
||||
/***************************************************************/
|
||||
void swap(spelare &element1, spelare &element2)
|
||||
{
|
||||
spelare temp;
|
||||
temp = element1;
|
||||
element1 = element2;
|
||||
element2 = temp;
|
||||
}
|
||||
void bubbleSort(spelare listarray[], const int storlek)
|
||||
{
|
||||
for (int i=0 ; i<storlek ; i++)
|
||||
for (int j=0 ; j<storlek-1 ; j++)
|
||||
if ((listarray[j].score > listarray[j+1].score) && (listarray[j+1].score !=0))
|
||||
swap (listarray[j], listarray[j+1]);
|
||||
else
|
||||
if (listarray[j].score == 0)
|
||||
swap (listarray[j], listarray[j+1]);
|
||||
}
|
||||
|
||||
/***************************************************************/
|
||||
/*LÄSER FRÅN FILEN */
|
||||
/***************************************************************/
|
||||
int las(spelare listarray[])
|
||||
{
|
||||
int x=0;
|
||||
fstream load("highscr.dat",ios::in| ios::binary);
|
||||
if(!load)
|
||||
return 0;
|
||||
while ((load.peek() != EOF) && (x < NAMESIZE))
|
||||
load.read((char *)&listarray[x++],sizeof(listarray[x]));
|
||||
load.close();
|
||||
return x;
|
||||
}
|
||||
|
||||
/***************************************************************/
|
||||
/*SKRIVER TILL FILEN */
|
||||
/***************************************************************/
|
||||
void skriv(spelare listarray[])
|
||||
{
|
||||
fstream save("highscr.dat",ios::out| ios::binary);
|
||||
if (!save)
|
||||
cout <<"Error!";
|
||||
else
|
||||
for (int i=0 ; i<=NAMESIZE ; i++)
|
||||
save.write((char *)&listarray[i], sizeof(listarray[i]));
|
||||
save.close();
|
||||
}
|
||||
|
||||
/***************************************************************/
|
||||
/*SPELAREN ÄR KLAR MED OMGÅNGEN, SKRIVER EV. IN SITT NAMN OSV. */
|
||||
/***************************************************************/
|
||||
void klar(int a)
|
||||
{
|
||||
int x;
|
||||
char spana;
|
||||
spelare p, listarray[NAMESIZE];
|
||||
cout << "\n\nRätt svar efter " << a << " gissningar" << endl;
|
||||
cout << "Tryck på en tangent..." << endl;
|
||||
getch();
|
||||
for (int i=0 ; i<NAMESIZE ; i++)
|
||||
listarray[i].score=0;
|
||||
x = las(listarray);
|
||||
if ((listarray[NAMESIZE-1].score > a) || (listarray[NAMESIZE-1].score==0))
|
||||
{
|
||||
cout <<"Du platsar på highscore-listan!"
|
||||
<<"\nSkriv in ditt namn: ";
|
||||
cin >> p.namn;
|
||||
getdate(&p.datum);
|
||||
p.score=a;
|
||||
listarray[NAMESIZE-1] = p;
|
||||
bubbleSort(listarray,NAMESIZE);
|
||||
skriv(listarray);
|
||||
}
|
||||
printhigh(listarray,5);
|
||||
cout <<"\n Vill du se hela listan? (J/N): ";
|
||||
cin >> spana;
|
||||
if (spana == 'j')
|
||||
printhigh(listarray,NAMESIZE);
|
||||
}
|
||||
31
Mastermind/lab_2.h
Normal file
31
Mastermind/lab_2.h
Normal file
@@ -0,0 +1,31 @@
|
||||
//Headerfil för Lab 2
|
||||
|
||||
#ifndef _lab_2_h
|
||||
#define _lab_2_h
|
||||
|
||||
#include <iostream.h> //för in och utmatning
|
||||
#include <string.h> //för strcmp()
|
||||
#include <stdlib.h> //för atoi()
|
||||
#include <conio.h> //för clrscr() och getch()
|
||||
#include <fstream.h> //för filhantering
|
||||
#include <iomanip.h> //för printhigh
|
||||
#include <dos.h> //för datumet
|
||||
|
||||
#define ARRAY 4 //arraystorlek för hemligt_tal
|
||||
#define BUFFERSIZE 100 //arraystorlek för säker inmatning
|
||||
#define TRIES 15 //antal gissnigsförsök
|
||||
#define NAMESIZE 20 //antal tecken på namnet
|
||||
|
||||
|
||||
typedef struct spelare
|
||||
{
|
||||
char namn[NAMESIZE];
|
||||
int score;
|
||||
struct date datum;
|
||||
}spelare;
|
||||
|
||||
void klar (int a);
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
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