我的编程作业有问题 [英] problem with my programming assignment

查看:72
本文介绍了我的编程作业有问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好.
我得到了一个作业,该作业指定我必须从文本文件读取数据并将其写入,然后使用链接列表在文件中插入数据:在文件的开头,中间和结尾插入.

另外,我必须删除记录并使用冒泡排序和搜索进行排序.
我已经写了一段代码,但是我碰壁了,我不知道该怎么办...
我不想失去分数...

有什么帮助吗?

代码如下:


Hi everybody.
I got an assignment which specifies that i have to read data from a text file and write in it, insert data in the file by using linked list: insert at beginning, middle and end of the file.

Also I must delete a record and sort using bubble sort and searching.
I have written a piece of code but i have hit a wall and I don''t know what to do...
I don''t want to lose marks...

Any help please?

The code is as follows:


#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
//function prototype
int add_records();
int get_records();
int user_choice();
int search_records();
string buffer;
//structure
struct tabledata//creating linked list
{
int stud_id;//will store information
char name[100];//will store information
float code;//will store information
tabledata *next;//the reference to the next node
};
typedef struct tabledata node;
node *head,*current,*temp;

int main(int argc, char *argv[])
{
    cout<<"\t\t*************************************************"<<endl;
    cout<<"\t\t\tStructured Programming Assignment"<<endl;
    cout<<"\t\t*************************************************\n\n\n"<<endl;
    
    cout<<"\t\tStudent Name: Ashvind Anand MITTOO\n";
    cout<<"\t\tStudent ID: 101384\n";
    cout<<"\t\tCohort ID : BCNS/10B/FT"<<endl;
    cout<<"\t\tProgramme : BSc(Hons.) Computer Science with Network Security"<<endl;
    
    cout<<"\n\n\t\tQuestion 2\n"<<endl;
  
    system("PAUSE");
    system("CLS");
    
    //calling the functions to main
    user_choice();
    add_records();
    get_records();
}
//Function user_choice
int user_choice()
{
    char user;
    
    system("CLS");
    
    cout<<"Do you want to add/search/exit : ";
         cin>>user;
         if ((user==''A'') || (user==''a''))
        {
            get_records();
            add_records();
        }
        else if ((user==''e'') || (user==''E''))
        {
             exit(1);
         }
        else if ((user==''S'') || (user==''s'')) 
        {
             int search_records();
         }
         else
         {
             cout<<"Invalid input!\n";
             system("PAUSE");
             system("CLS");
             user_choice();
         }
}
//function to add at the end of list
int add_records()
{
    int stud_id;
    char name[100];
    float code;
    char user;
    int counter=0;
    head=NULL;
    
    //opening file datatable for writing
    ofstream  fin;
    fin.open("tabledata.txt",ios::app);
    
     while(1)
     {
         cout<<"Do you want to enter a Data [Y/N/E]: ";
         cin>>user;
         if ((user==''N'') || (user==''n''))
        {
          //break out of the loop...
           break;
        }
        else if ((user==''E'') || (user==''e''))
        {
             user_choice();
         }
        else if ((user==''Y'') || (user==''y'')) 
         {
    current=new node;
    
    cout<<"\n\tEnter ID: "; 
    cin>>stud_id;
    current->stud_id=stud_id;
    
    
    cout<<"\tName: "; 
    //pause for user to enter Name
     cin.ignore(1,''\n'');
     cin.getline(name,100,''\n'');
     
     //insert into linked list
     strcpy(current->name,name);
     
     cout<<"\tCode: ";
     cin>>code;
     current->code=code;
     
     //saving to file tabledata.txt
     fin<<"\n "<<stud_id<<" "<<name<<"\t"<<code;
  
     //incrementing linked list and linking nodes
     current->next=head;
     head=current;
       }
     
     else
         {
          cout<<"\nInvalid input. Only Y/N/E allowed.Re-enter your choice:\n";
         }
     }  
     fin.close();
     get_records();
  
}
//function to get records from tabledata.txt
int get_records()
{
int stud_id;//declaration
string name;//declaration
float code;//declaration
    ifstream fin;//open output file tabledata.txt for reading
    fin.open("tabledata.txt");
   //Printing Header for Table 
    cout<<"\nID\tName\t\tCode\n";
          cout<<"-------------------------------\n";
          
    if(!fin)
    {
       cout<<"Can''t open tabledata.txt file";
       exit(1);        
       }
       
       else
       {
      while(!fin.eof())
        {
          fin>>stud_id>>name>>code;
          cout<<stud_id<<"\t"<<name<<"\t\t"<<code<<"\n\n";
          }
           
          /*while(head != NULL)
          {
            cout<<head->stud_id<<"\t"<<head->name<<"\t\t"<<head->code<<"\n\n";
            head=head->next;          
            }*/
            
        }
        
        fin.close();
}



代码块+可读性+重新标记[/edit]

作为解决方案添加的OP:

在末尾插入很好,并且也保存在文本文件中.我指的是我不知道如何在列表的开头,中间和结尾处插入.以及如何从此列表中删除记录???



[edit] code block + readability + retagged [/edit]

The OP added as a solution:

"well inserting at the end is working fine and it is saving in the text file too. the wall i refering to is that i don''t know how to insert at the beginning,middle and end of the list. and also how to delete a record from this list????"

推荐答案

在列表的开头和中间插入非常容易:
开头:
1)创建您的新元素.
2)将下一个"指针设置为当前头"值.
3)将"head"设置为指向新元素.

中:
1)创建您的新元素.
2)找到要插入的元素(称为中间")
3)将新元素"next"设置为"middle""next"的当前值
4)设置中间"下一个"以指向您的新元素.

删除甚至更容易:
1)在要删除的元素之前找到该元素. (上一个")
2)找到要删除的元素. (删除")
3)将上一个"的下一个"指针设置为删除"下一个"指针中的值.
4)取消分配删除"以防止内存泄漏.
Inserting in the beginning and middle of the list are quite easy:
Begining:
1) Create your new element.
2) Set the "Next" pointer to the current "head" value.
3) Set the "head" to point to the new element.

Middle:
1) Create your new element.
2) Find the element you want to insert after (call it "middle")
3) Set the new element "next" to the current value of "middle" "next"
4) Set "middle" "next" to point to your new element.

Deleting is even easier:
1) Find the element before the one you want to delete. ("previous")
2) Find the element you want to delete. ("delete")
3) Set the "next" pointer of "previous" to teh value in the "delete" "next" pointer.
4) Deallocate "delete" to prevent memory leaks.


这篇关于我的编程作业有问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆