解讀一個如下的txt檔,利用linked list來儲存這些資料,insert表增加,delete表刪除,list為秀出所有資料:
insert, M009, 30
insert, M009, 70
insert, M008, 40
insert, M009, 90
insert, M009, 47
insert, M008, 77
insert, M005, 60
insert, M008, 66
list
delete, M008
list
insert, M008, 10
list
insert, M008, 11
list
delete, M008
list
delete, M009
list
程式碼如下(VS2008):
#include "stdafx.h"
#include <stdio.h>
#include <malloc.h>
#include <iostream>
#include <string>
#include <stdlib.h>
#include <fstream>
#include <conio.h>
using namespace std;
typedef struct SScore *scorePtr;
struct SScore
{
int score;
scorePtr next_sco;
};
typedef struct SStudent *studentPtr;
struct SStudent
{
char ID[20];
scorePtr score_head;
scorePtr score_current;
scorePtr score_Ptr;
studentPtr next_student;
};
studentPtr student_head;
studentPtr student_current;
studentPtr student_Ptr;
studentPtr search_ID(char* name)
{
student_Ptr = student_head;
if (!name || !student_Ptr)
{
printf("Error\n");
return NULL;
}
while (student_Ptr != NULL)
{
if ((strcmp(name, student_Ptr->ID) == 0))
{
return student_Ptr;
}
student_Ptr = student_Ptr->next_student;
}
return NULL;
}
//增加分數
void add_sco(studentPtr ptr, int score)
{
scorePtr new_score = (scorePtr)malloc(sizeof(SScore));
new_score->score = score;
new_score->next_sco = NULL;
if(ptr->score_head == NULL)
ptr->score_head = new_score;
else
ptr->score_current->next_sco = new_score;
ptr->score_current = new_score;
}
//建立學生ID
void creatID(char* id, int score)
{
studentPtr temp = search_ID(id);
if (!temp)
{
studentPtr new_student = (studentPtr)malloc(sizeof(SStudent));
strcpy(new_student->ID, id);
new_student->next_student = NULL;
new_student->score_head = new_student->score_current = new_student->score_Ptr = NULL;
add_sco(new_student, score);
if (student_head == NULL)
{
student_head = new_student;
}
else
{
student_current->next_student = new_student;
}
student_current = new_student;
}
else
{
add_sco(temp, score);
return;
}
}
void DeleteScore(studentPtr delete_student)
{
delete_student->score_Ptr = delete_student->score_head;
while (delete_student->score_Ptr != NULL)
{
scorePtr temp_score = delete_student->score_Ptr;
delete_student->score_Ptr = delete_student->score_Ptr->next_sco;
free(temp_score);
}
}
void DeleteID(char *id)
{
studentPtr delete_student = search_ID(id);
if (!delete_student)
{
printf("No this ID!\n");
return;
}
else
{
if (delete_student == student_head)
{
//student_head->next_student = NULL;
if (student_head->next_student == NULL) // 如果只有一個
{
student_head = NULL;
student_current = NULL;
}
else
student_head = delete_student->next_student;
}
else
{
studentPtr temp = student_head;
while(temp->next_student != delete_student) // search
temp = temp->next_student;
if (delete_student->next_student == NULL) // the end
temp->next_student = NULL;
else // 指向delete的下一個
temp->next_student = delete_student->next_student;
}
DeleteScore(delete_student);
free(delete_student);
}
}
void ListAll()
{
student_Ptr = student_head;
if (!student_Ptr)
{
printf("list是空的\n");
return;
}
while (student_Ptr != NULL)
{
printf("ID : %s\n", student_Ptr->ID);
student_Ptr->score_Ptr = student_Ptr->score_head;
while (student_Ptr->score_Ptr != NULL)
{
printf("Score : %d\n", student_Ptr->score_Ptr->score);
student_Ptr->score_Ptr = student_Ptr->score_Ptr->next_sco;
}
student_Ptr = student_Ptr->next_student;
}
}
void ReleaseResource()
{
student_Ptr = student_head;
while(student_Ptr != NULL)
{
studentPtr temp_student = student_Ptr;
temp_student->score_Ptr = temp_student->score_head;
while(temp_student->score_Ptr != NULL)
{
scorePtr temp_score = temp_student->score_Ptr;
temp_student->score_Ptr = temp_student->score_Ptr->next_sco;
free(temp_score);
}
student_Ptr = student_Ptr->next_student;
free(temp_student);
}
}
int _tmain(int argc, _TCHAR* argv[])
{
char whatdo[10];
char id[10];
int sco;
ifstream infile("D:\\bonus_test_data.txt");
infile >> whatdo;
int i = 0;
while(infile)
{
if (strcmp(whatdo, "insert,") == 0)
{
infile >> id >> sco;
whatdo[6] = '\0'; //把insert,的,號用掉
id[4] = '\0'; //把M00X,的,號用掉
cout << whatdo << endl
<< id << endl
<< sco << endl;
creatID(id, sco);
}
else if (strcmp(whatdo, "delete,") == 0)
{
infile >> id;
whatdo[6] = '\0';
id[4] = '\0';
cout << endl
<< whatdo << endl
<< id << endl << endl;
DeleteID(id);
}
else if (strcmp(whatdo, "list") == 0)
{
printf("讀到list,要印出全部list\n");
ListAll();
}
infile >> whatdo;
}
system("pause");
ReleaseResource();
return 0;
}
這是修改別人錯誤的程式碼就是,不過也算全改了..XD
0 意見:
張貼留言