Set 54:
In the given program, the function of function fun is to calculate the sum of the values in the data field of each node in the one-way linked list with head node and return it as the function value.
Please fill in the correct content in the underline of the program and delete the underline to make the program get the correct result.
Note: the source program is stored in blank1 under the examinee folder In C.
Do not add or delete lines, nor change the structure of the program!
Given source program:
#include <stdio.h> #include <stdlib.h> #define N 8 typedef struct list { int data; struct list *next; } SLIST; SLIST *creatlist(int *); void outlist(SLIST *); int fun( SLIST *h) { SLIST *p; int s=0; p=h->next; while(p) { s+= p->___1___; p=p->___2___; } return s; } main() { SLIST *head; int a[N]={12,87,45,32,91,16,20,48}; head=creatlist(a); outlist(head); / printf("\nsum=%d\n", fun(___3___)); } SLIST *creatlist(int a[]) { SLIST *h,*p,*q; int i; h=p=(SLIST *)malloc(sizeof(SLIST)); for(i=0; i<N; i++) { q=(SLIST *)malloc(sizeof(SLIST)); q->data=a[i]; p->next=q; p=q; } p->next=0; return h; } void outlist(SLIST *h) { SLIST *p; p=h->next; if (p==NULL) printf("The list is NULL!\n"); else { printf("\nHead "); do { printf("->%d", p->data); p=p->next; } while(p!=NULL); printf("->End\n"); } }
Problem solving ideas:
This problem is to calculate the sum of the values of the data fields of each node in the one-way linked list with head nodes.
The first place: accumulate the values in the data field, so: data should be filled in.
The second place: specify the next pointer of p, so it should be filled in: next.
The third place: function call. Head has been given in the main function, so: head should be filled in.
Given program modi1 The function of function fun in C is to replace all the substrings in the string indicated by s that are the same as the string indicated by t1 with the string indicated by t2, and the new string is placed in the array indicated by w. Here, the length of the string referred to by t1 and t2 is required to be the same. For example, when the content of the string indicated by s is "abcdabfab", the content of the substring indicated by t1 is "ab", and the content of the substring indicated by t2 is "99", the content of the result in the array indicated by w should be: "99cd99f99".
Please correct the mistakes in the program so that it can get the correct results.
Note: do not change the main function, do not add or delete lines, and do not change the structure of the program!
Given source program:
#include <stdio.h> #include <string.h> int fun (char *s, char *t1, char *t2 , char *w) { int i; char *p , *r, *a; strcpy( w, s ); while ( *w ) { p = w; r = t1; while ( r ) if ( *r == *p ) { r++; p++; } else break; if ( *r == '\0' ) { a = w; r = t2; while ( *r ){ *a = *r; a++; r++ } w += strlen(t2) ; } else w++; } } main() { char s[100], t1[100], t2[100], w[100]; printf("\nPlease enter string S:"); scanf("%s", s); printf("\nPlease enter substring t1:"); scanf("%s", t1); printf("\nPlease enter substring t2:"); scanf("%s", t2); if ( strlen(t1)==strlen(t2) ) { fun( s, t1, t2, w); printf("\nThe result is : %s\n", w); } else printf("Error : strlen(t1) != strlen(t2)\n"); }
Problem solving ideas:
First: judge whether the current character of the string is the string terminator, so it should be changed to: while(*r).
Second: the semicolon is missing after the statement.
The function fun is to delete the characters with even subscript in the string indicated by s, and put the new string formed by the remaining characters in the string into the index group indicated by t.
For example, when the content in the string referred to by s is "ABCDEFGHIJK",
The content in the index group of t shall be: "BDFHJ".
Note: some source programs have the file prog1 In C.
Do not change anything in the main function and other functions, and only fill in the curly brackets of function fun
Several statements you wrote.
Given source program:
#include <stdio.h> #include <string.h> void fun(char *s, char t[]) { } main() { char s[100], t[100]; printf("\nPlease enter string S:"); scanf("%s", s); fun(s, t); printf("\nThe result is: %s\n", t); NONO(); }
Problem solving ideas:
This problem is to generate another new string from one string as required. We use the for loop statement to solve this problem.
Reference answer:
void fun(char *s, char t[]) { int i, j = 0 ; for(i = 1 ; i < strlen(s); i+=2) t[j++] = s[i] ; t[j] = 0 ; }