Lab cpp 02


Laboratorium nr 2
Temat: Strumień wejścia i wyjścia. Formatowanie danych.
Windows: Struktura programu
1) Konsola: strumienie wejścia i wyjścia
Uwaga: W języku C++ oprócz standardowych funkcji printf() i scanf() wprowadzono nowy mechanizm obsługi
wejścia i wyjścia  nazywany dalej strumieniem. Mechanizm ten oparty jest na modyfikacji operatora << tak, aby
spełniał on funkcję wyjścia, zaś operatora >> tak, aby zapewniał obsługę wejścia. Deklaracje zawarte są w pliku
nagłówkowym iostream.h
a) Strumień wejścia cout <<
C C++
#include #include
#include #include
int main(){ int main(){
int i=1; int i=1;
printf("Czesc\nLaboratorium z C++ cout << "Czesc\n" << "Laboratorium z
numer=%d\n",i); C++ numer=" << i << "\n";
system("PAUSE"); system("PAUSE");
return 0; return 0;
} }
b) Strumień wyjścia cin >>
C C++
#include #include
#include #include
int main(){ int main(){
int i; int i;
printf("Podaj numer lab.:"); cout << "Podaj numer lab.:";
scanf("%d",&i); cin >> i;
printf("Czesc\nLaboratorium z C++ cout << "Czesc\n" << "Laboratorium z
numer=%d\n",i); C++ numer=" << i << "\n";
system("PAUSE"); system("PAUSE");
return 0; return 0;
} }
c) Tworzenie i zapisywanie do pliku
C C++
#include #include
#include #include
#include
int main(){
char plik[12]; int main(){
FILE *wsk_pliku; ofstream strumien_plik;
int i=1; ifstream strumien_file;
char plik[12], bufor[80];
printf("Podaj nazwe pliku:");
int i=1;
gets(plik);
if((wsk_pliku=fopen(plik,"w")) !=NULL){ cout << "Podaj nazwe pliku:";
fprintf(wsk_pliku, "Czesc\n cin >> plik;
Laboratorium z C++ numer=%d\n",i); strumien_plik.open(plik);
fclose(wsk_pliku); strumien_plik << "Czesc\n" <<
"Laboratorium C++ numer=" << i <<"\n";
}
else printf("Blad otwarcia pliku\n"); strumien_plik.close();
strumien_file.open(plik);
system("PAUSE");
strumien_file >> bufor;
return 0;
} strumien_file.close();
cout << bufor << "\n";
system("PAUSE");
return 0;
}
Formatowania danych:
Kod Znaczenie
\n nowa linia
\t tabulator
%d, %s. itp. ! nie jest wymagane  strumień sam rozpoznaje typ danej, na podstawie przesłanego obiektu
hex Postać szesnastkowa liczby
dec Postać dziesiętna liczby
setw(int w) Ustawienie szerokości pola, np. setw(5)
setprecision(int pr) Określenie ilości miejsc liczb typu float
flush Opróżnienie bufora strumienia
ws Pominięcie spacji
itd. ....
Zadanie : Opracowuj prosty program kalkulatora z wykorzystaniem strumieni wejścia i wyjścia. Program
powinien posiadać krótkie menu oraz możliwość zapisywania i odtwarzania historii operacji do i z pliku
zewnętrznego.
2) Windows: struktura programu
#include
/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
/* Make the class name into a global variable */
char szClassName[ ] = "WindowsApp";
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)
{
HWND hwnd; /* This is the handle for our window */
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass */
/* The Window structure */
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
wincl.style = CS_DBLCLKS; /* Catch double-clicks */
wincl.cbSize = sizeof (WNDCLASSEX);
/* Use default icon and mouse-pointer */
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL; /* No menu */
wincl.cbClsExtra = 0; /* No extra bytes after the window class */
wincl.cbWndExtra = 0; /* structure or the window instance */
/* Use Windows's default color as the background of the window */
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
/* Register the window class, and if it fails quit the program */
if (!RegisterClassEx (&wincl))
return 0;
/* The class is registered, let's create the program*/
hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
"Windows App", /* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
544, /* The programs width */
375, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);
/* Make the window visible on the screen */
ShowWindow (hwnd, nFunsterStil);
/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}
/* The program return-value is 0 - The value that PostQuitMessage() gave */
return messages.wParam;
}
/* This function is called by the Windows function DispatchMessage() */
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) /* handle the messages */
{
case WM_DESTROY:
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}


Wyszukiwarka

Podobne podstrony:
Lab cpp
Lab cpp
Lab cpp
Lab cpp
Lab cpp
Lab cpp
Lab cpp
Lab cpp
Lab cpp
Lab cpp
Lab cpp
Lab cpp
lab 2
T2 Skrypt do lab OU Rozdział 6 Wiercenie 3
IE RS lab 9 overview
lab pkm 3
CPP (2)
lab chemia korozja

więcej podobnych podstron