Skip to content

Commit 8306f33

Browse files
author
sam
committed
Initial commit
0 parents  commit 8306f33

File tree

41 files changed

+860
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+860
-0
lines changed

Diff for: .gitattributes

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
jerome 2200 100
2+
jeromeB BBB 100
3+
jeromeC CCC 100
4+
zxc123 zxc123 123456
5+
qaz123 qaz123 123456789
6+
85513 dd 0
7+
sfs 4 2
8+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
#define _GNU_SOURCE
2+
#define MAX_SIGNALS 30
3+
4+
#include <string.h>
5+
#include <unistd.h>
6+
#include <sys/types.h>
7+
#include <sys/stat.h>
8+
#include <fcntl.h>
9+
#include <signal.h>
10+
#include <stdio.h>
11+
#include <stdlib.h>
12+
13+
typedef struct person_info
14+
{
15+
char name[20];
16+
char ID[20];
17+
int deposit;
18+
struct person_info *next;
19+
}person_list;
20+
21+
typedef struct LinkedList_s {
22+
person_list *head;
23+
person_list *tail;
24+
} LinkedList;
25+
26+
LinkedList *new_LinkedList ()
27+
{
28+
LinkedList *this;
29+
30+
this = (LinkedList*) malloc (sizeof(LinkedList));
31+
32+
this->head = NULL;
33+
this->tail = NULL;
34+
35+
return this;
36+
}
37+
38+
void insert(LinkedList * this, char name[20], char ID[20], int deposit)
39+
{
40+
person_list *p;
41+
p = (person_list*) malloc (sizeof(person_list));
42+
strcpy(p->name, name);
43+
strcpy(p->ID, ID);
44+
p->deposit = deposit;
45+
p->next = NULL;
46+
if (this->head==NULL)
47+
{
48+
this->head = p;
49+
this->tail = p;
50+
}
51+
else
52+
{
53+
this->tail->next = p;
54+
this->tail = p;
55+
}
56+
}
57+
58+
void search(LinkedList *this)
59+
{
60+
char name[20];
61+
printf("input you wanna search name\n");
62+
scanf("%s",name);
63+
64+
person_list *p;
65+
p = this->head;
66+
while(p!=NULL && strcmp(p->name, name)!=0)
67+
p=p->next;
68+
69+
if(p==NULL)
70+
printf("No this DATA\n");
71+
else
72+
printf("Name:%s- ID:%s- Deposit:%d\n", p->name, p->ID, p->deposit);
73+
74+
return;
75+
}
76+
77+
void delete(LinkedList *this)
78+
{
79+
char name[20];
80+
printf("input you wanna delete name\n");
81+
scanf("%s",name);
82+
83+
person_list *p, *q;
84+
p = this->head;
85+
q = NULL;
86+
while(p!=NULL && strcmp(p->name, name)!=0)
87+
{
88+
q=p;
89+
p=p->next;
90+
}
91+
92+
if(p==NULL)
93+
printf("No this DATA\n");
94+
else
95+
{
96+
q->next=p->next;
97+
printf("delete this DATA\n");
98+
}
99+
100+
return;
101+
}
102+
103+
void display(LinkedList *this)
104+
{
105+
person_list *p;
106+
p=this->head;
107+
while(p!=NULL)
108+
{
109+
printf("Name:%s- ID:%s- Deposit:%d\n", p->name, p->ID, p->deposit);
110+
p=p->next;
111+
}
112+
return;
113+
}
114+
115+
LinkedList *list;
116+
void exit_handler()
117+
{
118+
FILE *fp;
119+
fp = fopen ("contacts.txt", "w");
120+
121+
person_list *p;
122+
p=list->head;
123+
while(p!=NULL)
124+
{
125+
fprintf(fp, "%s %s %d\n", p->name, p->ID, p->deposit);
126+
p=p->next;
127+
}
128+
fprintf(fp,"end\n");
129+
fclose(fp);
130+
}
131+
132+
void readfile(LinkedList *this)
133+
{
134+
char name[20];
135+
char ID[20];
136+
int deposit;
137+
FILE *fp;
138+
fp = fopen ("contacts.txt", "r");
139+
if(fp!=NULL)
140+
{
141+
while(!feof(fp))
142+
{
143+
fscanf(fp, "%s",name);
144+
if(!strcmp(name,"end")) break;
145+
fscanf(fp, "%s",ID);
146+
fscanf(fp, "%d",&deposit);
147+
insert(list, name, ID, deposit);
148+
}
149+
}
150+
fclose(fp);
151+
}
152+
153+
int main()
154+
{
155+
atexit (exit_handler);
156+
char name[20];
157+
char ID[20];
158+
int deposit;
159+
int i;
160+
for (i=0;i<MAX_SIGNALS;i++)
161+
signal (i, exit);
162+
/*set signal for all exit*/
163+
list=new_LinkedList();
164+
readfile(list);
165+
int option;
166+
while(1)
167+
{
168+
printf("Select option\n");
169+
printf("1.insert\n2.search\n3.delete\n4.list all\n5.exit\n ");
170+
scanf("%d",&option);
171+
switch(option)
172+
{
173+
case 1:
174+
printf("input your name\n");
175+
scanf("%s",name);
176+
printf("input your ID\n");
177+
scanf("%s",ID);
178+
printf("input your deposit\n");
179+
scanf("%d",&deposit);
180+
insert(list, name, ID, deposit);
181+
break;
182+
case 2:
183+
search(list);
184+
break;
185+
case 3:
186+
delete(list);
187+
break;
188+
case 4:
189+
display(list);
190+
case 5:
191+
return 0;
192+
}
193+
}
194+
return 0;
195+
}
196+
197+
198+
199+
200+
201+
202+
203+
204+
205+
206+
207+
208+
209+
210+
211+
212+
213+
Binary file not shown.

Diff for: Define my own fstream class/.main.cpp.swo

12 KB
Binary file not shown.

Diff for: Define my own fstream class/lib_functions.a

3.55 KB
Binary file not shown.

Diff for: Define my own fstream class/main.cpp

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include "myfstream.h"
2+
3+
int main() {
4+
string ofileStr, ifileStr;
5+
myfstream ifile, ofile;
6+
char data[50];
7+
int inNumber = 0;
8+
9+
cout << "Please enter input file name:";
10+
cin >> ifileStr;
11+
cout << "Please enter output file name:";
12+
cin >> ofileStr;
13+
ifileStr += ".txt";
14+
ofileStr += ".txt";
15+
16+
char ofileName[ofileStr.length()];
17+
strcpy(ofileName, ofileStr.c_str());
18+
char ifileName[ifileStr.length()];
19+
strcpy(ifileName, ifileStr.c_str());
20+
21+
ifile.openfile(ifileName);
22+
ofile.openfile(ofileName);
23+
24+
ifile.readfile(data, 15);
25+
data[15] = ' ';
26+
//ifile >> data;
27+
ofile << data;
28+
29+
ifile.closefile();
30+
ofile.closefile();
31+
32+
return 0;
33+
}

Diff for: Define my own fstream class/main.o

5.04 KB
Binary file not shown.

Diff for: Define my own fstream class/mainprog

14.5 KB
Binary file not shown.

Diff for: Define my own fstream class/makefile

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
default:mainprog
2+
3+
main.o: main.cpp
4+
g++ -c main.cpp
5+
6+
myfstream.o: myfstream.cpp
7+
g++ -c myfstream.cpp
8+
9+
lib_functions.a: myfstream.o
10+
ar -r lib_functions.a myfstream.o
11+
12+
mainprog: lib_functions.a main.o
13+
g++ main.o lib_functions.a -o mainprog
14+
15+
clean:
16+
rm -f *.o
17+
rm -f mainprog
18+
rm -f lib_functions.a
19+
rm -f overload.txt write.txt

Diff for: Define my own fstream class/myfstream.cpp

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include "myfstream.h"
2+
3+
void myfstream::openfile(const char* filename) {
4+
this->fileID = open(filename, O_RDWR | O_CREAT, S_IRWXU);
5+
}
6+
7+
void myfstream::closefile() {
8+
close(this->fileID);
9+
}
10+
11+
void myfstream::readfile(char* str, size_t strsize) {
12+
read(this->fileID, str, strsize);
13+
str += '\0';
14+
}
15+
16+
void myfstream::writefile(const char* str, size_t strsize) {
17+
write(this->fileID, str, strsize);
18+
}
19+
20+
istream& myfstream::operator>>(char* str) {
21+
for (int n = 0; *(str + n) != '\0'; n++)
22+
read(this->fileID, str + n, 1);
23+
}
24+
25+
ostream& myfstream::operator<<(char* str) {
26+
for (int n = 0; *(str + n) != '\0'; n++)
27+
write(this->fileID, str + n, 1);
28+
}

Diff for: Define my own fstream class/myfstream.h

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <fcntl.h>
2+
#include <sys/stat.h>
3+
#include <sys/types.h>
4+
#include <unistd.h>
5+
#include <cstring>
6+
#include <iostream>
7+
using namespace std;
8+
9+
class myfstream {
10+
public:
11+
size_t fileID;
12+
void openfile(const char* filename);
13+
void closefile();
14+
void readfile(char* str, size_t strsize);
15+
void writefile(const char* str, size_t strsize);
16+
istream& operator>>(char* str);
17+
ostream& operator<<(char* str);
18+
};

Diff for: Define my own fstream class/myfstream.o

3.25 KB
Binary file not shown.

Diff for: FinalProject/Makefile

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
TARGETS = number
2+
3+
all: $(TARGETS)
4+
5+
clean:
6+
rm -f $(TARGETS)
7+
8+
%: %.c
9+
gcc -g -o $@ $^ -lpthread -lm
10+
11+
12+
13+
14+
15+
16+
17+
18+
19+
20+
21+
22+
23+
24+
25+
26+
27+
28+

Diff for: FinalProject/number

16.1 KB
Binary file not shown.

0 commit comments

Comments
 (0)