Upload Øving 3

master
Øyvind Skaaden 2019-02-01 12:33:44 +01:00
parent d2699650e8
commit 0de4420aed
9 changed files with 427 additions and 0 deletions

View File

@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.28307.271
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ObligatoriskeOppgaver", "ObligatoriskeOppgaver\ObligatoriskeOppgaver.vcxproj", "{B7F91FBF-D3FE-4DEC-BA17-75E818059272}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{B7F91FBF-D3FE-4DEC-BA17-75E818059272}.Debug|x64.ActiveCfg = Debug|x64
{B7F91FBF-D3FE-4DEC-BA17-75E818059272}.Debug|x64.Build.0 = Debug|x64
{B7F91FBF-D3FE-4DEC-BA17-75E818059272}.Debug|x86.ActiveCfg = Debug|Win32
{B7F91FBF-D3FE-4DEC-BA17-75E818059272}.Debug|x86.Build.0 = Debug|Win32
{B7F91FBF-D3FE-4DEC-BA17-75E818059272}.Release|x64.ActiveCfg = Release|x64
{B7F91FBF-D3FE-4DEC-BA17-75E818059272}.Release|x64.Build.0 = Release|x64
{B7F91FBF-D3FE-4DEC-BA17-75E818059272}.Release|x86.ActiveCfg = Release|Win32
{B7F91FBF-D3FE-4DEC-BA17-75E818059272}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {463F8BFA-463F-4621-8174-AEBED4A9F6DB}
EndGlobalSection
EndGlobal

View File

@ -0,0 +1,148 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>15.0</VCProjectVersion>
<ProjectGuid>{b7f91fbf-d3fe-4dec-ba17-75e818059272}</ProjectGuid>
<RootNamespace>ObligatoriskeOppgaver</RootNamespace>
<WindowsTargetPlatformVersion>10.0.17763.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<IncludePath>C:\Program Files\TDT4102\includes;$(IncludePath)</IncludePath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<IncludePath>C:\Program Files\TDT4102\includes;$(IncludePath)</IncludePath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<IncludePath>C:\Program Files\TDT4102\includes;$(IncludePath)</IncludePath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<IncludePath>C:\Program Files\TDT4102\includes;$(IncludePath)</IncludePath>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<SubSystem>Console</SubSystem>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<SubSystem>Console</SubSystem>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="cannonball.cpp" />
<ClCompile Include="main.cpp" />
<ClCompile Include="utilities.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="cannonball.h" />
<ClInclude Include="utilities.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

View File

@ -0,0 +1,36 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Header Files">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;hm;inl;inc;xsd</Extensions>
</Filter>
<Filter Include="Resource Files">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="main.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="cannonball.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="utilities.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="cannonball.h">
<Filter>Source Files</Filter>
</ClInclude>
<ClInclude Include="utilities.h">
<Filter>Source Files</Filter>
</ClInclude>
</ItemGroup>
</Project>

View File

@ -0,0 +1,118 @@
#include "cannonball.h"
#include "std_lib_facilities.h"
#define _USE_MATH_DEFINES
#include <math.h>
#include "utilities.h"
double acclY()
{
return -9.81;
}
double velY(double initVelocityY, double time)
{
return initVelocityY + acclY() * time;
}
double posY(double initPosition, double initVelocity, double time)
{
return initPosition + initVelocity * time + (acclY() *pow(time, 2)) / 2;
}
double posX(double initPosition, double initVelocity, double time)
{
return initPosition + initVelocity * time;
}
void printTime(double time)
{
int minSec = 60;
int hourSec = pow(minSec, 2);
int hours = time / hourSec;
int timeLeft = time - hours * hourSec;
int minutes = timeLeft / minSec;
timeLeft = timeLeft - minutes * minSec;
cout << hours << " hours, " << minutes << " minutes, " << timeLeft << " seconds" << "\n";
}
double flightTime(double initVelocityY)
{
return (-2 * initVelocityY) / acclY();
}
void getUserInput(double& theta, double& absVelocity)
{
double deg;
cout << "Angle of velocity: ";
cin >> deg;
theta = degToRad(deg);
cout << "Velocity: ";
cin >> absVelocity;
}
double degToRad(double deg)
{
return deg * M_PI / 180;
}
double getVelocityX(double theta, double absVelocity)
{
return absVelocity * cos(theta);
}
double getVelocityY(double theta, double absVelocity)
{
return absVelocity * sin(theta);
}
void getVelocityVector(double theta, double absVelocity, double& velocityX, double& velocityY)
{
velocityX = getVelocityX(theta, absVelocity);
velocityY = getVelocityY(theta, absVelocity);
}
double getDistanceTraveled(double velocityX, double velocityY)
{
return posX(0, velocityX, flightTime(velocityY));
}
double targetPractice(double distanceToTarget, double velocityX, double velocityY)
{
return abs(distanceToTarget - getDistanceTraveled(velocityX, velocityY));
}
void playTargetPractice(int rounds)
{
srand(static_cast<unsigned int>(time(nullptr)));
bool won = false;
double theta, absVelocity; // Initial angle and velocity
double initX, initY; // Initial velocities, in component-form
double fTime;
double distance;
int target = randomWithLimits(100, 1000);
for (int r = 1; r <= rounds; ++r)
{
getUserInput(theta, absVelocity);
getVelocityVector(theta, absVelocity, initX, initY);
printTime(flightTime(initY));
cout << "The cannonball flew " << getDistanceTraveled(initX, initY) << " meters" << endl;
distance = targetPractice(target, initX, initY);
cout << "The cannonball is " << distance << " meters away from target" << endl;
if (distance <= 5)
{
won = true;
break;
}
else
{
if (distance < 0)
cout << "The shot wat too short" << endl;
else
cout << "The shot was to far" << endl;
}
}
if (won)
cout << "Congratulate, you have won!" << endl;
else
cout << "Sorry, game over!" << endl;
}

View File

@ -0,0 +1,35 @@
#pragma once
#pragma region Task 1 and 2
double acclY();
double velY(double initVelocityY, double time);
double posY(double initPosition, double initVelocity, double time);
double posX(double initPosition, double initVelocity, double time);
void printTime(double time);
double flightTime(double initVelocityY);
#pragma endregion
#pragma region Task 4
void getUserInput(double& theta, double& absVelocity);
double degToRad(double deg);
double getVelocityX(double theta, double absVelocity);
double getVelocityY(double theta, double absVelocity);
void getVelocityVector(double theta, double absVelocity, double& velocityX, double& velocityY);
// Takes in initial velocity in x and y and gives the traveled distance in the x-axis
double getDistanceTraveled(double velocityX, double velocityY);
double targetPractice(double distanceToTarget, double velocityX, double velocityY);
#pragma endregion
void playTargetPractice(int rounds);

View File

@ -0,0 +1,45 @@
#include "std_lib_facilities.h"
#include "cannonball.h"
#include "utilities.h"
bool testDeviation(double compareOperant, double toOperand, double maxError, string name);
int main(){
#pragma region Testing - Task 3
/*
int space = 8;
for (double i = 0; i <= 5; i += 2.5) {
cout << setw(space) << acclY() << setw(space) << velY(25, i) << setw(space) <<
posY(0, 25, i) << setw(space) << posX(0, 50, i) << endl;
}
*/
#pragma endregion
#pragma region Task 4d
/*
double theta;
double absVelocity;
getUserInput(theta, absVelocity);
double initX, initY;
getVelocityVector(theta, absVelocity, initX, initY);
cout << "Distance traveled: " << getDistanceTraveled(initX, initY) << endl;
cout << "Distance from target: " << targetPractice(10, initX, initY) << endl;
*/
#pragma endregion
#pragma region Game - Task 5d
playTargetPractice(10);
#pragma endregion
return 0;
}
bool testDeviation(double compareOperant, double toOperand, double maxError, string name)
{
if (abs(compareOperant - toOperand) < maxError)
return true;
return false;
}

View File

@ -0,0 +1,8 @@
#include "utilities.h"
#include "std_lib_facilities.h"
int randomWithLimits(int min, int max)
{
return rand() % (max - min + 1) + min;;
}

View File

@ -0,0 +1,6 @@
#pragma once
int randomWithLimits(int min, int max);

Binary file not shown.