Sreams - 1
Sreams - 2
HOOHU"
#include <iostream.h>
class Complex
{
friend ostream & operator<<(ostream & ut, Complex & tal)
{
ut << "(" << tal.re << " + i" << tal.im << ")";
return ut;
}
6WU|PPDU
‡
'HW JUXQGOlJJDQGH EHJUHSSHW L &
VWDQGDUGELEOLRWHN I|U OlVQLQJ RFK VNULYQLQJ lU
VWU|PPDU
‡
(Q VWU|P lU HQ I|OMG DY E\WHV
‡
6WU|PPDU NDQ NRSSODV WLOO ROLND W\SHU DY \WWUH
PHGLHU W H[ VNLYPLQQHQ EDQGVWDWLRQHU
ELOGVNlUPDU WDQJHQWERUG RVY
‡
)LQHVVHQ lU DWW XU SURJUDPPHWV V\QSXQNW Vn
KDQWHUDV VWU|PPDU Sn HWW HQKHWOLJW VlWW
REHURHQGH YDG GH UnNDU YDUD NRSSODGH WLOO
public:
Complex(double real = 0.0, double imaginary = 0.0)
: re(real), im(imaginary){}
void operator++(int){re++; im++;}
void operator++(){++re; ++im;};
private:
double re;
double im;
};
int main()
{
Complex talet(1,1);
talet++;
//operator++(int);
++talet;
//operator++();
cout << talet << endl;
return 0;
}
1
2EMHNWHQ
FLQFRXWFHUUFORJ
Sreams - 3
.ODVVHQLRV
LRV
LVWUHDP
• cin
lU HWW REMHNW DY NODVVHQ
• cin
lU RIWDVW NQXWHQ WLOO WDQJHQWERUGHW
Sreams - 4
istream.
• cout cerr
ostream.
RFK
clog
lU REMHNW DY NODVVHQ
• cout cerr
RFK
clog
lU RIWDVW NQXWQD WLOO
RVWUHDP
ELOGVNlUPHQ
LIVWUHDP
LRVWUHDP
RIVWUHDP
LVWUVWUHDP
‡
RVWUVWUHDP
LVWULQJVWUHDP
RVWULQJVWUHDP
0HG KMlOS DY
cout
RFK
clog
NDQ EXIIUDGH
XWPDWQLQJDU VNH
‡
2P
cerr
DQYlQGV VNHU HQ XWPDWQLQJ WHFNHQ WLOO
VWDQGDUGXWHQKHWHQ GLUHNW XWDQ DWW I|UVW
PHOODQODJUDV L HQ EXIIHUW
IVWUHDP
VWULQJVWUHDP
‡
VWUVWUHDP
2EMHNWHQ cin lU GHNODUHUDG L ILOHQ LVWUHDPK
cout cerr RFK clog lU GHNODUHUDGH L ILOHQ
RFK
RVWUHDPK
‡
)LOHQ LRVWUHDPK LQFOXGHUDU ILOHUQD LVWUHDPK RFK
RVWUHDPK
2
Sreams - 5
Sreams - 6
7LOOVWnQGVIODJJRU
‡
, NODVVHQ LRV ILQQV HWW DQWDO IODJJRU VRP
EHVNULYHU HQ VWU|PV WLOOVWnQG
)ODJJRUQDVWLOOVWnQG
‡
'HW ILQQV lYHQ HQ XSSVlWWQLQJ PHGOHPVIXQNWLRQHU
L NODVVHQ LRV I|U DWW DYOlVD RFK lQGUD WLOOVWnQGHW
Sn IODJJRUQD
)ODJJD
IDLOELW
HRIELW
,QGLNHUDU
)XQNWLRQ
(IIHNW
XWVNULIWVRSHUDWLRQHQ PLVVO\FNDGHV
YRLG FOHDU
nWHUVWlOOHU DOOD IODJJRUQD
DWW HWW ILOVOXW LQWUlIIDGH YLG HWW
ERRO JRRG
JHU WUXH RP LQJHQ DY
DWW GHQ VHQDVWH LQOlVQLQJV HOOHU
IODJJRUQD lU VDWWD
WLGLJDUH OlVI|UV|N
EDGELW
HWW DOOYDUOLJDUH IHO VRP RIWDVW KDU
ERRO IDLO
JHU WUXH RP IDLOELW HOOHU
EDGELW lU VDWW
PHG VWU|PPHQV LQWHUQD EXIIHUW DWW
J|UD
ERRO HRI
JHU WUXH RP HRIELW lU VDWW
ERRO EDG
JHU WUXH RPEDGELW lU VDWW
ERRO RSHUDWRU
JHU UHVXOWDW IDLO
RSHUDWRU ERRO
JHU UHVXOWDWHW QRW IDLO
3
$QYlQGQLQJDY
WLOOVWnQGVIODJJRUH[HPSHO
Sreams - 7
(WWH[HPSHOWLOODQYlQGQLQJDY
WLOOVWnQGVIODJJRU
‡ 5lNQD WHFNHQ
‡ )HOIODJJRUQDV YlUGHQ
#include <iostream.h>
#include <iostream.h>
int main()
{
int antalTecken = 0;
char tecken;
int main()
{
int tal = 0;
cout << "Skriv några tecken, avsluta med <crtr>D" <<
endl;
while (true)
{
cin.get(tecken);
if (cin.eof()) break;
antalTecken++;
}
cout << endl << "Du har skrivit " << antalTecken;
cout << " tecken, med alla blanktecken inr„knade." <<
endl;
return 0;
}
‡
(WW DQQDW VlWW lU DWW XWQ\WWMD GHQ UHIHUHQV WLOO
LQGDWDVWU|PPHQ VRP PHGOHPVIXQNWLRQHQ JHW OlPQDU VRP
UHWXUYlUGH
while (bool(cin.get(tecken))) antalTecken++;
Sreams - 8
while (tal != -1)
{
if (cin.good())
cout << "Indata-strömmen är OK!" << endl;
cin >> tal;
if (cin.fail())
{
cout << "Inläsningen misslyckades.. ." << endl;
cin.clear();
cin.get();
Indata-strömmen är OK!
}
1
Indata-strömmen är OK!
}
23
return 0;
Indata-strömmen är OK!
}
qw
Inläsningen misslyckades.. .
Indata-strömmen är OK!
Inläsningen misslyckades.. .
Indata-strömmen är OK!
1
Indata-strömmen är OK!
-1
4
Sreams - 9
Sreams - 10
(WWH[HPSHOSnLQOlVQLQJDY
VWUlQJDU
)RUPDWHUDGLQPDWQLQJ
‡
9LG IRUPDWHUDG LQPDWQLQJ NDQ LQGDWD WRONDV RFK
#include <iostream.h>
RPYDQGODV L VDPEDQG PHG LQOlVQLQJHQ
‡
)RUPDWHUDG LQOlVQLQJ J|UV PHG KMlOS DY
LQPDWQLQJVRSHUDWRUQ µ!!µ
‡
int main()
{
char namn[30], bil[30];
cout << "Vad heter du: ";
cin >> namn;
1RUPDOW E|UMDU HQ LQOlVQLQJ PHG DWW DOOD EODQNWHFNHQ
KRSSDV |YHU IUDP WLOO GHW I|UVWD LFNH ULNWLJD WHFNQHW
‡
cout << "Vilken typ av bil kör du: ";
cin >> bil;
1RUPDOW OlVHV GlUHIWHU VDPWOLJD ULNWLJD WHFNHQ VRP
NDQ LQJn L YlUGHW 2P PDQ W H[
OlVHU LQGDWD WLOO HQ
KHOWDOVYDULDEHO Vn NRQVXPHUDV WHFNHQ L LQGDWDVWU|PPHQ
cout << namn << " kör en " << bil << endl;
Vn OlQJH GH lU VLIIURU
return 0;
‡
2EVHUYHUD DWW LQOlVQLQJHQ VWDQQDU GlUHIWHU Sn I|UVWD
}
WHFNQHW VRP LQWH LQJnU L GHW OlVWD YlUGHW
Utskrift och inmatning
==> test
Vad heter du: Hans Edy
Vilken typ av bil kör du: Hans kör en Edy
==>
5
0DQLSXODWRUHUYLG
IRUPDWHUDGLQPDWQLQJ
‡
Sreams - 11
,QOlVQLQJHQ DY GDWD NDQ VW\UDV PHG KMlOS DY
Sreams - 12
([HPSHOPHGPDQLSXODWRU
‡
PDQLSXODWRUHU (Q GHO DY PDQLSXODWRUHUQD lU
2VlNHU LQPDWQLQJ GHW ILQQV P|MOLJKHW DWW VNULYD I|UEL
VOXWHW Sn FKDUIlOWHW
GHNODUHUDGH L µLRPDQLSKµ
#include <iostream.h>
0DQLSXODWRU
VHWZQ
(IIHNW
int main()
{
char tecken[5];
//Vad händer om fler än 4 tecken skrivs in?
cin >> tecken;
return 0;
}
DQJHU KXU PnQJD WHFNHQ VRP
PD[LPDO U\PV L GHW WHFNHQIlOW
PDQ KDU WlQNW OlVD WLOO
ZV
KRSSD IUDP WLOO QlVWD LFNH EODQND
WHFNHQ
‡
VNLSZV
WHFNHQ VWDQGDUGLQVWlOOQLQJHQ
QRVNLSZV
GHF
KRSSD LQWH |YHU LQGDWD DY KHOWDOVW\S VND WRONDV
VRP GHFLPDOD WDO
RFW
LQGDWD DY KHOWDOVW\S VND WRONDV
VRP RNWDOD WDO
KH[
(Q VlNHU LQPDWQLQJ
KRSSD DOOWLG |YHU LQOHGDQGH YLWD
#include <iostream.h>
#include <iomanip.h>
int main()
{
char tecken[5];
//Endast 4 tecken läses in.
cin >> setw(5) >> tecken;
return 0;
}
LQGDWD DY KHOWDOVW\S VND WRONDV
VRP KH[DGHFLPDOD WDO
6
Sreams - 14
)XQNWLRQHUI|U
RIRUPDWHUDGLQOlVQLQJ
Sreams - 13
2IRUPDWHUDGLQPDWQLQJ
‡
9LG RIRUPDWHUDG LQPDWQLQJ OlVHV GDWD IUnQ LQVWU|PPHQ
)XQNWLRQ
%HVNULYQLQJ
SUHFLV VRP GHQ lU ODJUDG L VWU|PPHQ E\WH I|U E\WH
JFRXQW
‡
RIRUPDWHUDG LQPDWQLQJ
PHGOHPVIXQNWLRQHU L NODVVHQ LVWUHDP
‡
*HPHQVDPW I|U DOOD GHVVD IXQNWLRQHU lU DWW GH DOGULJ
JHW
/lVHU RFK UHWXUQHUDU QlVWD WHFNHQ L
VWU|PPHQ JHU (2) RP ILOVOXW
KRSSDU |YHU EODQNWHFNHQ $OOD WHFNHQ OlVHV L WXU RFK
SnWUlIIDV
RUGQLQJ
JHW F ‡
5HWXUQHUDU DQWDOHW WHFNHQ VRP
OlVWHV DY GHQ VHQDVWH IXQNWLRQHQ I|U
)RUPDWHUDG LQOlVQLQJ J|UV PHG VSHFLHOOD
)XQNWLRQHUQD NDQ VlWWD IODJJRUQD HRIELW RFK IDLOELW
/lVHU LQ QlVWD WHFNHQ L VWU|PPHQ WLOO
YDULDEHOQ µFµ
EHURHQGH Sn ILOVOXW OlVWHV HOOHU RP LQOlVQLQJHQ
PLVVO\FNDGHV
JHWOLQHVQW
3DUDPHWHUQ µWµ KDU GHIDXOWYlUGHW µ?Qµ
/lVHU LQ HWW DQWDO WHFNHQ WLOO
WHFNHQVWUlQJHQ µVµ WLOO GHVV DWW QlVWD
WHFNHQ lU VWRSSWHFNQHW µWµ 'HWWD WHFNHQ
OlVHV PHQ SODFHUDV LQWH L VWUlQJHQ µVµ
,QOlVQLQJHQ DYEU\WV WLGLJDUH RP µQ
7HFNHQ KDU OlVWV (WW QROOWHFNHQ
SODFHUDV DOOWLG VLVW L µVµ
UHDGV Q
/lVHU LQ µQµ VW WHFNHQ WLOO µVµ 2P ILOVOXW
SnWUlIIDV LQQDQ µQµ WHFNHQ lU OlVWD VlWWV
IODJJRUQD µHRIELWµ RFK µIDLOELWµ
7
(WWH[HPSHOSnRIRUPDWHUDG
LQOlVQLQJDYVWUlQJDU
Sreams - 15
#include <iostream.h>
Sreams - 16
)RUPDWHUDGXWPDWQLQJ
‡
9LG IRUPDWHUDG XWPDWQLQJ RPYDQGODV XWGDWD WLOO HQ
I|OMG DY µFKDUµ L VDPEDQG PHG XWPDWQLQJHQ
int main()
{
char namn[30], bil[30];
‡
)RUPDWHUDG XWPDWQLQJ J|UV PHG KMlOS DY
XWPDWQLQJVRSHUDWRUQ µµ
cout << "Vad heter du: ";
cin.getline(namn, 30, ’\n’);
‡
'H GDWD VRP PDWDV XW NDQ UHGLJHUDV Sn HQ PDVVD ROLND
VlWW PHG GH IRUPDWIODJJRU I|U XWPDWQLQJ VRP ILQQV L
NODVVHQ LRV
cout << "Vilken typ av bil kör du: ";
cin.getline(bil, 30, ’\n’);
‡
cout << namn << " kör en " << bil << endl;
return 0;
}
Utskrift och inmatning
==> test
Vad heter du: Hans Edy
Vilken typ av bil kör du: SAAB 900
Hans Edy kör en SAAB 900
==>
1nJUD DY GH YDQOLJDVWH IRUPDWIODJJRUQD lU
OHIW
YlQVWHUMXVWHUD
ULJKW
K|JHUMXVWHUD
GHF
GHFLPDO IRUP
RFW
RNWDO IRUP
KH[
KH[DGHFLPDO IRUP
IL[HG
XWVNULIW DY IO\WWDO Sn IDVW IRUP
VFLHQWLILF
XWVNULIW DY IO\WWDO PHG H[SRQHQW
VKRZEDVH
YLVD QXPHULVN EDV W H[ [ I|U KH[DGHF
ZLGWK
DQJHU PLQVWD DQWDO XWVNULIWSRVLWLRQHU
SUHFLVLRQ
DQJHU DQWDO GHFLPDOHU RP IRUPHQ lU
6WDQGDUGYlUGH
µIL[HGµ HOOHU µVFLHQWLILFµ , DQQDW IDOO
DQJHU µSUHFLVLRQµ DQWDO VLJQLILNDQWD
VLIIURU 6WDQGDUGYlUGH
8
0DQLSXODWRUHUYLG
IRUPDWHUDGXWPDWQLQJ
‡
Sreams - 17
)|U DWW VlWWD GH ROLND IRUPDWIODJJRUQD ILQQV HWW VWRUW
Sreams - 18
2IRUPDWHUDGXWPDWQLQJ
‡
DQWDO PDQLSXODWRUHU 1nJUD DY GH YDQOLJDUH lU
0DQLSXODWRU
(IIHNW
9LG RIRUPDWHUDG XWPDWQLQJ VNULYV GDWD WLOO XWVWU|PPHQ
SUHFLV VRP GH lU XWDQ DWW NRQYHUWHUDV
‡
2IRUPDWHUDG XWPDWQLQJ J|UV PHG VSHFLHOOD
PHGOHPVIXQNWLRQHU L NODVVHQ RVWUHDP
VHWZQ
6lWWHU YlUGHW ZLGWK WLOO µQµ
VHWSUHFLVLRQQ
6lWWHU YlUGHW SUHFLVLRQ WLOO µQµ
IOXVK
7|PPHU XWVNULIWVEXIIHUWHQ
HQGO
$OOD IXQNWLRQHUQD L WDEHOOHQ JHU VRP UHWXUYlUGH HQ
UHIHUHQV WLOO GHQ DNWXHOOD VWU|PPHQ
/lJJHU WLOO HWW UDGVOXWVWHFNHQ L VWU|PPHQ
RFK DQURSDU VHGDQ µIOXVKµ
HQGV
‡
)XQNWLRQ
%HVNULYQLQJ
SXW F 6NULYHU XW WHFNQHW F WLOO VWU|PPHQ
ZULWHV Q
6NULYHU XW µQµ WHFNHQ WLOO VWU|PPHQ IUnQ
/lJJHU LQ HWW QROOWHFNHQ L VWU|PPHQ
VKRZEDVH
QRVKRZEDVH
GHW WHFNHQIlOW VRP µVµ SHNDU Sn
OHIW
ULJKW
IOXVK
7|PPHU XWVNULIWVEXIIHUWHQ GYV
RVNULYQD WHFNHQ L XWVNULIWVEXIIHUWHQ
GHF
VNULYV XW
RFW
KH[
IL[HG
VFLHQWLILF
9
.RSSOLQJDYHQWH[WILOWLOO
VWU|P
Sreams - 19
Sreams - 20
/lVQLQJDYWH[WILO
#include <iostream.h>
#include <fstream.h>
#include <iostream.h>
#include <fstream.h>
int main()
{
double talet = 123456789.123456789;
int main()
{
ifstream inFile("mintext.txt");
char tecken;
ofstream outFile("mintext.txt");
cout << "LÄSER TEXTFILEN" << endl;
outFile << "Här kommer ett långt tal" << endl;
outFile << talet << endl;
outFile << "Hoppas att det går att läsa" << endl;
while (inFile.get(tecken))
{
cout << tecken;
}
inFile.close();
cout << "LÄSNINGEN KLAR" << endl;
return 0;
outFile.close();
return 0;
}
}
==> cat mintext.txt
Här kommer ett långt tal
1.23457e+08
Hoppas att det går att läsa
==>
Utskrift
LÄSER TEXTFILEN
Här kommer ett långt tal
1.23457e+08
Hoppas att det går att läsa
LÄSNINGEN KLAR
==>
1