Project0(Get familiar with Linux) (Part 2)

Part 2

You need to compile/run a C program set_operation.c. The program contains several simple bugs, and you are going to detect and correct them using the gdb debugger.

Here are some details of the set_operation.c:

  • It is designed for doing linked list operation to compute  (AB)(BA) , where  A  and  B  are two sets input by users.
  • The logic is very simple:
    • copy  A  to a temporary set  A2
    • compute  A=AB  and  B=BA2
    • union  A  and  B
  • There are two sub-functions: output and check
    • "output" is used to output all elements in a linked list
    • "check" is used to check if an integer belongs to a linked list

The program after being corrected should run as follows:

---------------------------------
----Computing (A-B)union(B-A)----
---------------------------------
----input the number of elements of A: 3
1-th element: 3
2-th element: 4
3-th element: 5
----input the number of elements of B: 2
1-th element: 1
2-th element: 4
---- elements of (A-B)union(B-A) ----
1-th element: 3
2-th element: 5
3-th element: 1

Hints

Part 1

  • In the first script, you can use echo and redirection to output some texts into a file.
  • In the second script, you may use xarg (between find and ls) or pipe.
  • For processing the text file, you can write a loop to read each line in and process them one by one. But, using awk maybe more convenient.
  • To sort the data , you may use the command sort.

When you need some details of a command, use man. Output some informations properly when scripts are running. For example, for the second script, show the number of the instances of the file when it is created.

Part 2

  • Set some breakpoints, especially at some important statements(for example, loop or logical control)
  • Display some value of variables, such as the loop variables or the number in the linked list which is pointed at by the pointer currently.

Hand In

  • two script files named s1.sh and s2.sh.
  • source file after debugging and a lab report contains three parts:
    • A simple introduction of your project
    • Summarize the commands and their usages, which were used in compiling and debugging.
    • How you debugged the program (where are your breakpoints located, which variables you've watched, etc.) and the bugs you found.

General Advice

Try to use and get familiar with the shell commands first. Make good use of "man", "help" and "--help".

Start small, and get things working incrementally. For example, you can try to write some sub-functions in your script first, then test them before you combine them together. For debugging, you could build several test cases, and see why the running process of the program is different from your expectation.

#include <stdio.h>
#include <stdlib.h>
#define LEN sizeof(struct node)
struct node{
  int number;
  struct node *next;
};
void output(struct node *head, char *c); //output a linked list
int check(int num, struct node *head);  //find if a element is in a linked list
void output(struct node *head, char *c){
  struct node *p;
  printf("---- elements of %s ----\n",c);
  p=head; 
  int count=1; 
  while(p!=NULL){
    printf("%d-th element: ",count++);
    printf("%d\n",p->number);
    p=p->next;
  }
}
int check(int num, struct node *head){
  int sign = 0;
  struct node *p;
  p=head;
  while(p!=NULL){
    if(p->number==num) //the 1st bug:->-> to -> and delete the ->next
      sign=1;
    p=p->next;  
  }
  return sign;
}
///main// 
int main(){
  struct node *A_head;
  struct node *A2_head;
  struct node *B_head;
  int A_size, B_size;
  struct node *p1, *p2, *p3;
  int sign=0;
  int i;
  printf("---------------------------------\n");
  printf("----Computing (A-B)union(B-A)----\n");
  printf("---------------------------------\n");
  input the linked list A and B/ 
  ///input A
  printf("----input the number of elements of A: ");
  scanf("%d",&A_size);
  A_head=NULL;
  p1=A_head;
  for(i=0;i<A_size;i++){
    p2=(struct node*)malloc(LEN);
    printf("%d-th element: ",i+1);
    scanf("%d",&(p2->number));
    if(sign==0){
      A_head=p2;
      sign=1;
    }
    else{
      p1->next=p2;
    }
    p2->next=NULL;
    p1=p2;
  }
  ///input B
  sign=0;
  printf("----input the number of elements of B: ");
  scanf("%d",&B_size);
  B_head=NULL;
  p1=B_head;
  for(i=0;i<B_size;i++){
    p2=(struct node*)malloc(LEN);
    printf("%d-th element: ",i+1);
    scanf("%d",&(p2->number));
    if(sign==0){
      B_head=p2;
      sign=1;
    }
    else{
      p1->next=p2;
    }
    p2->next=NULL;
    p1=p2;
  }
  /copy A to A2//
  sign=0;
  A2_head=NULL;
  p3=A_head;
  p1=A2_head;
  for(i=0;i<A_size;i++){//the 2nd bug:<= to <
    p2=(struct node*)malloc(LEN);
    p2->number=p3->number;
    if(sign==0){
      A2_head=p2;
      sign=1;
    }
    else{
      p1->next=p2;
    }
    p2->next=NULL;
    p1=p2;
    p3=p3->next;
  }
  //A'=A-B///
  p2=p1=A_head;
  sign=0;
  for(i=0;i<A_size;i++){ //p1!=NULL){
    if(check(p1->number,B_head)){ //if this element is in B
      if(sign==0){//the 4th bug:check return 1 if the element is in B
        A_head=A_head->next;
        p2=p1=A_head;
      }
      else{
           p2->next=p1->next;
           p1=p1->next;
      } 
    } 
    else{
         if(sign==0){
           p1=p1->next;
           sign=1;
         } 
         else{
           p2=p2->next;
           p1=p1->next;
         }
    }
  }
  //output(A_head,"A-B");
  //B'=B-A2///
  p2=p1=B_head;
  sign=0;
  for(i=0;i<B_size;i++){ 
    if(check(p1->number,A2_head)){//the same with the 4th bug
      if(sign==0){
        B_head=B_head->next;
        p2=p1=B_head;
      }
      else{
           p2->next=p1->next;
           p1=p1->next;
      } 
    } 
    else{
         if(sign==0){
           p1=p1->next;
           sign=1;
         } 
         else{
           p2=p2->next;
           p1=p1->next;
         }
    }
  }
  //output(B_head,"B-A");
  ///computing (A-B)union(B-A), i.e., A' union B'/
  if(A_head==NULL)
    A_head=B_head;
  else if (A_head!=NULL && B_head!=NULL){
    p1=A_head;
    while(p1->next!=NULL){
      p1=p1->next;
    }
    p1->next=B_head; 
  }
  output(A_head,"(A-B)union(B-A)");
  
  //system("pause");//the 3rd bug:sh:1:pause: not found
  return 0;
} 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值