• R/O
  • SSH
  • HTTPS

Commit

Tags
No Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

練習用です。いろんなものがごちゃまぜです。


Commit MetaInfo

Revision49 (tree)
Time2015-10-06 09:26:41
Authorbellyoshi

Log Message

(empty log message)

Change Summary

Incremental Difference

--- TeachYourSelf/strtype.cpp (revision 48)
+++ TeachYourSelf/strtype.cpp (revision 49)
@@ -8,9 +8,10 @@
88 int len;
99 public:
1010 strtype(char *ptr);
11+ strtype(const strtype &s);
1112 ~strtype();
12- void show();
13- friend char *get_string(strtype *s);
13+ char *get(){return p;}
14+ int getlen(){return len;}
1415 };
1516
1617 strtype::strtype(char *ptr)
@@ -26,6 +27,17 @@
2627 this->len = l;
2728 }
2829
30+strtype::strtype(const strtype &s)
31+{
32+ len = s.len;
33+ p = new char[len];
34+ if (!p){
35+ cout << "メモリ割り当てエラー";
36+ exit(1);
37+ }
38+ strncpy(p, s.p,len);
39+}
40+
2941 strtype::~strtype()
3042 {
3143 cout << "pを解放する。\n";
@@ -32,23 +44,16 @@
3244 delete p;
3345 }
3446
35-char *get_string(strtype *s)
47+void show(strtype x)
3648 {
37- return s->p;
49+ cout << x.get() << "-長さ:" << x.getlen() << "\n";
3850 }
3951
40-void strtype::show()
41-{
42- cout << p << "-長さ:" << len << "\n";
43-}
44-
4552 int main()
4653 {
4754 strtype s1("This is a test."), s2("I like C++.");
48- s1.show();
49- s2.show();
50- cout << get_string(&s1) << "\n";
51- cout << get_string(&s2) << "\n";
55+ show(s1);
56+ show(s2);
5257
5358 return 0;
5459 }
\ No newline at end of file
--- TeachYourSelf/dateclass.cpp (revision 48)
+++ TeachYourSelf/dateclass.cpp (revision 49)
@@ -1,10 +1,13 @@
11 #include <iostream>
22 #include <cstdio>
3+#include <time.h>
4+
35 using namespace std;
46
57 class date {
68 int day, month , year;
79 public:
10+ date(time_t t);
811 date(char *str);
912 date(int m,int d, int y){
1013 day = d;
@@ -20,14 +23,23 @@
2023 {
2124 sscanf(str, "%d%*c%d%*c%d", &month, &day, &year);
2225 }
23-
26+date::date(time_t t)
27+{
28+ struct tm *ptml;
29+ ptml = localtime(&t);
30+ day = ptml->tm_mday;
31+ month = ptml->tm_mon + 1;
32+ year = ptml->tm_year + 1900;
33+}
2434 int main()
2535 {
2636 date sdate("12/31/99");
2737 date idate(12,31,99);
38+ date ndate(time(NULL));
2839
2940 sdate.show();
3041 idate.show();
42+ ndate.show();
3143
3244 return 0;
3345 }
\ No newline at end of file
--- TeachYourSelf/array.cpp (revision 48)
+++ TeachYourSelf/array.cpp (revision 49)
@@ -4,51 +4,50 @@
44
55 class arrays {
66 int size;
7- char *p;
7+ int *p;
88 public:
9- arrays(int num);
9+ arrays(int sz){
10+ p = new int[sz];
11+ if(!p) exit(1);
12+ size = sz;
13+ cout << "通常のコンストラクタを使う。\n";
14+ }
1015 ~arrays(){delete [] p;}
11- char &put(int i);
12- char get(int i);
13-};
14-
15-arrays::arrays(int num)
16-{
17- p = new char[num];
18- if(!p){
19- cout << "メモリ割り当てエラー\n";
20- exit(1);
16+ arrays(const arrays &a);
17+ void put(int i,int j){
18+ if (i >=0 && i < size) p[i] = j;
2119 }
22- size = num;
23-}
24-char &arrays::put(int i)
25-{
26- if(i < 0 || i >size-1){
27- cout << "境界エラー!!!\n";
28- exit(1);
20+ int get(int i){
21+ return p[i];
2922 }
30- return p[i];
31-}
23+};
3224
33-char arrays::get(int i)
25+arrays::arrays(const arrays &a)
3426 {
35- if(i < 0 || i > size-1){
36- cout << "境界エラー!!!\n";
37- exit(1);
27+ size = a.size;
28+ p = new int[a.size];
29+ if(!p)exit(1);
30+ for(int i = 0;i < a.size; i++){
31+ p[i] = a.p[i];
3832 }
39- return p[i];
33+ cout << "コピーコンストラクタを使う。\n";
4034 }
4135
4236 int main()
4337 {
44- arrays a(10);
45- a.put(3) = 'x';
46- a.put(2) = 'R';
47-
48- cout << a.get(3) << a.get(2);
38+ arrays num(10);
39+ for(int i = 0; i < 10; i++){
40+ num.put(i, i);
41+ }
42+ for(int i = 0; i < 10; i++){
43+ cout << num.get(i) << ' ';
44+ }
4945 cout << "\n";
46+ arrays x = num;
47+ for(int i = 0; i < 10; i++){
48+ cout << x.get(i) << ' ';
49+ }
50+ cout << "\n";
5051
51- a.put(11) = 'l';
52-
5352 return 0;
5453 }
\ No newline at end of file