Created
October 27, 2016 19:30
-
-
Save shinmai/a2e9e8a3aa309e72f7e76d91d71f72a7 to your computer and use it in GitHub Desktop.
Vko1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cmake_minimum_required(VERSION 3.6) | |
project(vko1) | |
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") | |
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/bin) | |
add_executable("tehtava1-3" "tehtava1-3.cpp") | |
add_executable("tehtava4-6" "tehtava4-6.cpp") | |
add_executable("tehtava7-8" "tehtava7-8.cpp") | |
add_executable("tehtava9-10" "tehtava9-10.cpp" "funktiot.cpp") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string.h> | |
#include "funktiot.h" | |
int getInt() { | |
char * pEnd; | |
char buffer[256]; | |
fgets (buffer, 256, stdin); | |
return strtol(buffer,&pEnd, 10); | |
} | |
std::string getString() { | |
char buffer[256]; | |
fgets (buffer, 256, stdin); | |
return std::string(strtok(buffer, "\n")); | |
} | |
std::string kysy_nimi() { | |
printf("Anna käyttäjätunnus: "); | |
return getString(); | |
} | |
int kysy_pisteet() { | |
printf("Anna pisteet: "); | |
return getInt(); | |
} | |
void tulosta(std::string nimi, int pisteet) { | |
printf("%-12.12s %11.i\n", nimi.c_str(), pisteet); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef VKO1_FUNKTIOT_H | |
#define VKO1_FUNKTIOT_H | |
std::string kysy_nimi(); | |
int kysy_pisteet(); | |
void tulosta(std::string nimi, int pisteet); | |
#endif //VKO1_FUNKTIOT_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
int main() { | |
// Tehtävä 1 | |
// cout << "Game Over!" << std::endl; //Tehtävästä 1 | |
// Tehtävä 2-3 | |
char nimi[24]; | |
int score, maxScore; | |
cin >> nimi; | |
cin >> score; | |
cin >> maxScore; | |
printf("Game Over, %s!\n", nimi); | |
printf("Your score was %i out of %i (%.2f%)", score, maxScore, (score/(double)maxScore*100) ); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
int main() { | |
// Tehtävä 6 | |
char * pEnd; | |
char buffer[256]; | |
// Tehtävä 4 | |
while(true) { | |
int number = 0; | |
printf("Pelataan Kiinalaista Numeropeliä!\n"); | |
printf("Anna luku: "); | |
//Tehtävä 6 | |
//cin >> number; // Tehtävästä 4 | |
fgets (buffer, 256, stdin); | |
number = strtol(buffer,&pEnd, 10); | |
if (errno == ERANGE) | |
continue; | |
// Tehtävä 5 | |
if(number == 0) | |
break; | |
// Tehtävä 6 | |
if(number >= INT_MAX) | |
printf("Tasapeli!\n"); | |
else | |
printf("Voitin, minun on %i!\n", (++number)); | |
} | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <time.h> | |
using namespace std; | |
int getInt() { | |
char * pEnd; | |
char buffer[256]; | |
fgets (buffer, 256, stdin); | |
return strtol(buffer,&pEnd, 10); | |
} | |
int main() { | |
// Tehtävä 7 | |
srand(time(0)); | |
// Tehtävä 8 | |
// int minTarget = 1; // Tehtävästä 7 | |
// int maxTarget = 100; // Tehtävästä 7 | |
int minTarget, maxTarget; | |
printf("Pelataan numeronarvauspeliä!\n\nAnna arvattavan luvun minimiarvo: "); | |
errno=ERANGE; | |
while(errno==ERANGE) | |
minTarget = getInt(); | |
printf("\nAnna arvattavan luvun maksimiarvo: "); | |
errno=ERANGE; | |
while(errno==ERANGE && maxTarget<=minTarget) | |
maxTarget = getInt(); | |
printf("\n\n"); | |
int target = (rand() % maxTarget)+minTarget; | |
int guess = target - 1; | |
int guesses = 0; | |
while(target != guess) { | |
//Tehtävä 8 | |
printf("Arvaa luku (%i-%i): ", minTarget, maxTarget); | |
guess = getInt(); | |
if (errno == ERANGE || guess > maxTarget || guess < minTarget) | |
continue; | |
printf("\n"); | |
guesses++; | |
if(guess > target) | |
printf("Liian suuri!\n"); | |
else if(guess < target) | |
printf("Liian pieni!\n"); | |
else | |
break; | |
} | |
printf("Oikein! Teit %i arvausta.\n", guesses); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include "funktiot.h" | |
using namespace std; | |
int main() { | |
const int NUMBER = 10; | |
string nimi[NUMBER]; | |
int pisteet[NUMBER]; | |
int stored_values = 0; | |
while(stored_values < NUMBER) { | |
nimi[stored_values] = kysy_nimi(); | |
pisteet[stored_values] = kysy_pisteet(); | |
stored_values++; | |
} | |
for(int i=0;i<NUMBER;i++) | |
tulosta(nimi[i], pisteet[i]); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment