txt文件中的值采用以下格式:
4
2 3
5 6
3 7
6 9
并且输出必须如下所示:
[2:3][5:6][3:7][6:9]
这是我的代码:
#include <iostream>
#include <stdio.h>
using namespace std;
class node {
public:
int info;
node* next;
node(){
next = NULL;
}
node (int value){
info = value;
next = NULL;
}
};
class list {
private:
node* head;
public:
list() { //Constructor
head = NULL;
}
void insert(int value){
if (head == NULL){
head = new node;
head -> info = value;
return;
}
node *temp = head;
while (temp) {
temp = temp -> next;
}
temp -> next = new node;
temp -> info = value;
}
void showlist(){
node* temp = head;
temp = temp -> next; //ignore first number in txt file
cout << "Liste \n" << endl;
while (temp){
printf ("%d", temp -> info);
//cout << temp -> info << endl;
temp = temp -> next;
}
}
~list() { //Destructor
node* temp = head;
while (head -> next != NULL) {
delete temp -> next;
temp -> next = NULL;
temp = temp -> next;
}
delete head -> next;
head -> next = NULL;
}
};
int main(int argc, const char * argv[]) {
list stone;
FILE* fp;
if (argc > 1)
fp = fopen(argv[1], "r");
else
fp = fopen("output.txt", "r");
if (!fp)
printf("Can't open file \n");
else {
int value, state;
int i = 0;
do {
state = fscanf(fp, "%d", &value);
if (state != EOF){
stone.insert(value);
}
stone.showlist();
}
while (state != EOF);
fclose (fp);
}
return 0;
}
没有错误,但如果我想执行它,我会收到崩溃报告。 我忽略了请求的格式作为开始。
答案 0 :(得分:1)
您的代码中存在多个错误。我将列出几个显而易见的问题:
node->next
时,都应该保证节点不是NULL while
循环后temp为NULL。解决方案可能为while (temp->next)
printf
和fgets
代替iostream
,并使用两个类。你更喜欢哪一个,C或C ++?选择一个你喜欢的。答案 1 :(得分:0)
insert
中的此块看起来不正确:
while (temp) {
temp = temp -> next;
}
temp -> next = new node;
在while
循环后,temp
为NULL
,因此您无法拨打temp -> next
。
答案 2 :(得分:0)
您似乎想要一个格式化的答案。然而你没有安排打印它
[2:3] [5:6] [3:7] [6:9]
//THE MODIFICATIONS I MADE ARE MINUTE. BUT THEY'LL GIVE YOU THE RESULT YOU DESIRE.
// MODIFY TEXT FILE FOR ANY MORE INPUTS IF YOU NEED TO INSERT MORE
#include <iostream>
#include <stdio.h>
#include<conio.h>
using namespace std;
class node {
public:
int info;
node* next;
node(){
next = NULL;
}
node (int value){
info = value;
next = NULL;
}
};
class list {
private:
node* head;
public:
list() { //Constructor
head = NULL;
}
void insert(int value){
if (head == NULL){
head = new node;
head -> info = value;
return;
}
node *temp = head;
while (temp->next) { //MODIFIED INSERT LOOP TERMINATION CONDN
temp = temp -> next;
}
temp -> next = new node;
temp -> info = value;
}
void showlist(){
node* temp = head;
// temp = temp -> next; //ignore first number in txt file _ UNNECESSARY - COMMENTED
cout << "List \n" << endl;
while (temp){
if(!temp->next){break;} //, temp -> info,(temp -> next)-> info);
else {printf ("[%d:%d]\n", temp -> info,(temp -> next)-> info);}
//cout << temp -> info << endl;
temp = (temp -> next)->next;
}
}
~list() { //Destructor
node* temp = head;
while (head -> next != NULL) {
delete temp -> next;
temp -> next = NULL;
temp = temp -> next;
}
delete head -> next;
head -> next = NULL;
}
};
int main(int argc, const char * argv[]) {
list stone;
FILE* fp;
if (argc > 1)
fp = fopen(argv[1], "r");
else
fp = fopen("inp.txt", "r"); //CHANGED NAME OF FILE
if (!fp)
printf("Can't open file \n");
else {
int value, state;
int i = 0;
do {
state = fscanf(fp, "%d", &value);
if (state != EOF){
stone.insert(value);
}
} while (state != EOF);
stone.showlist();
fclose (fp);
}
getch();
return 0;
}