Second experiment Report of C language

Source: Internet
Author: User

A. Experimental topics, design ideas, implementation methods

11th Job (two-dimensional array):

11-5 printing Yang Hui triangle (20 points)

The first n rows of the Yang Hui triangle are required to be printed in the prescribed format.

Input Format:

The input is given N (1≤n≤10) in a row.

output Format:

Outputs the first n rows of the Yang Hui triangle in a positive triangle format. Each number occupies a fixed 4-bit position.

Input Sample:
6
Sample output:
        1       1   1      1   2   1     1   3   3   1    1   4   6   4   1   1   5  10  10   5   1

idea : Yang Hui triangle is a classic math problem, see the output sample, we can easily phase to use two-dimensional array, then print the number of 1, and then use the mathematical method, the value of an element is equal to the sum of the above adjacent two elements, and then control the printing space and newline.

Implementation method: A two-dimensional array in C language, and the mathematical operation of subscript.

two. Part of the source program

for (i=1;i<=n;i++)
{
for (j=1;j<=i;j++)
{
if (j==1) a[i][1]=1;
else if (j==i) a[i][i]=1;
else {
A[I][J]=A[I-1][J-1]+A[I-1][J];
}
}
}

three. Problems encountered and solutions, experience

The first thing to think about this problem is to print 1 of the elements, before the operation, there is a point is the application of simple mathematical methods, a[i][j]=a[i-1][j-1]+a[i-1][j], this problem is not high, try to do it again through.

12th time Job (string)

A. Experimental topics, design ideas, implementation methods 12-6 string converted to decimal integer (15 points)

Enter a string ending with #, which requires filtering out all non-hexadecimal characters (not case), forming a new string representing a hexadecimal number, and then converting it to a decimal number after the output. If the character "-" exists before the first hexadecimal character, the number is negative.

Input Format:

The input gives a non-empty string that ends with # in a row.

output Format:

Outputs the converted decimal number in a row. The title ensures that the output is within the long range.

Input Sample:
+-P-xf4+-1!#
Sample output:
-3905

Idea: The first step is to take into account the character of the non-law.

Two. Convert the valid characters into a binary conversion operation.

Three. Judge the positive or negative of the number.

Implementation method: First read the string into the array, and then each item is refined.

two. Part of the source program

while ((A[i]=getchar ())! = ' # ') i++;
a[i]= ' + ';
for (i=0;a[i]!= ' n '; i++) {
if (a[i]>= ' 0 ' &&a[i]<= ' 9 ') {
number=number*16+a[i]-' 0 '; h=1;
}else if (a[i]>= ' a ' &&a[i]<= ' F ') {
number=number*16+a[i]-' a ' +10;h=1;
}else if (a[i]>= ' a ' &&a[i]<= ' F ') {
number=number*16+a[i]-' a ' +10;h=1;
}
t=i;
if (h==1&&q==1) {
q=2;
for (j=0;j<t;j++) {
if (a[j]== '-') {
Flag=-flag;
Break
} } }}

three. Problems encountered and solutions, experience

This problem after the illegal characters, but also to classify the legal characters, sometimes not clear, and "if there is a character before the first hexadecimal character"-", then the number is negative", this set up a small trap, to the positive and negative signs to make a special judgment.

13th time Assignment (pointer)

A. Experimental topics, design ideas, implementation methods 13-5 Judging palindrome string (20 points)

The need to write a function to determine whether a given string of characters is a "palindrome". The so-called "palindrome" refers to both reading and reading the same string. such as "Xyzyx" and "Xyzzyx" are palindrome.

function Interface Definition:
bool palindrome( char *s );

palindromeThe function determines whether the input string is char *s a palindrome. Returns if it is returned true false .

example of a referee test procedure:
#include <stdio.h>#include <string.h>#define MAXN 20typedef enum {false, true} bool;bool palindrome( char *s );int main(){    char s[MAXN];    scanf("%s", s);    if ( palindrome(s)==true )        printf("Yes\n");    else        printf("No\n");    printf("%s\n", s);    return 0;}/* 你的代码将被嵌在这里 */
Input Sample 1:
thisistrueurtsisiht
Output Example 1:
Yesthisistrueurtsisiht
Input Sample 2:
thisisnottrue
Output Example 2:
Nothisisnottrue
思路:率先定义一个指针指向这个字符串,然后头尾相互比较字符是否相等,如果相等向中间移动,否则判断不为回文字符。
实现方法:按照题目给定的条件与主函数,利用指针判断。
two. Part of the source program

 for (int i=0;s[i]!= ' n '; i++)
count++;
if (count==1)
return 1;
count--;
While (Count>a)
    {
if (* (s+a) ==* (s+count))
k=1;
else k=0;
count--;
a++;
    }
return k;

three. Problems encountered and solutions, experience

First to calculate the length of the string, because there is no "string.h" header file, for (int i=0;s[i]!= ' "; i++) count++; To calculate, if the length of the pointer, remember to refer back, and if

14th Experiment Report (pointers and structures)

A. Experimental topics, design ideas, implementation methods
14-5 specifying the position output string (20 points)

The subject requires implementing a function that prints out all the characters in a given string from the position that matches the first character to the position that matches the second character, given a string and two characters.

function Interface Definition:
char *match( char *s, char ch1, char ch2 );

The function match should print s all the characters from the to and from ch1 ch2 , and return ch1 the address.

example of a referee test procedure:
#include <stdio.h>#define MAXS 10char *match( char *s, char ch1, char ch2 );int main(){    char str[MAXS], ch_start, ch_end, *p;    scanf("%s\n", str);    scanf("%c %c", &ch_start, &ch_end);    p = match(str, ch_start, ch_end);    printf("%s\n", p);    return 0;}
思路:核心思想还是分类讨论,对出现的字符是否会在字符串内进行分析,如果两个都在字符串内输出什么,如果第二个不在字符串内输出什么,
如果都不在字符串内输出什么。
实现方法:对指针的灵活运用,以及指向字符数组的分析。
two. Part of the source program
for (i=0;i<len;i++) {
if (S[I]==CH1) {
p=&s[i];
for (j=i;j<len;j++) {
if (S[J]!=CH2)
printf ("%c", S[j]);
if (S[J]==CH2) {
printf ("%c\n", S[j]);
return p;
}
}
printf ("\ n");
return p;
}
}
S[len-1]= ' \ n ';
p=&s[len-1];
return p;
three. Problems encountered and solutions, experience

The problem is not very big, but there is a very important sentence: ch1 the address returned. This sentence has led to a very important test point has been let me, that is, two characters are not within the string when the output is two blank lines,

But one of the empty line is to CH1 address point, can not directly output, this problem points troubled me for a long time, the next morning in the cafeteria whim just understand.

15th Assignment:

A Experimental topics, design ideas, implementation methods

15-5 establishing the Student Information link list (20 points)

The subject requires the realization of a simple function that organizes the input student scores into one-way lists.
function Interface Definition:

void input ();

The function uses scanf to get the student's information from the input and organizes it into a one-way linked list. The linked list node structure is defined as follows:

struct Stud_node {
int num; /* Study Number */
Char name[20]; /* Name * *
int score; /* Score */
struct Stud_node *next; /* Pointer to next node */
};

The head and tail pointers of a one-way list are stored in the global variables tail.

Enter information for a number of students (student number, name, score) when the input number is 0 o'clock end.
Example of a referee test procedure:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct Stud_node {
int num;
Char name[20];
int score;
struct Stud_node *next;
};
struct Stud_node *head, *tail;

void input ();

int main ()
{
struct Stud_node *p;

Head = tail = NULL;
Input ();
for (p = head; P! = NULL; p = p->next)
printf ("%d%s%d\n", P->num, P->name, P->score);

return 0;
}

/* Your code will be embedded here */

two. Part of the source program

if (head==null)
{
Head=p;
head->next=null;
}
if (tail!=null)
tail->next=p;
Tail=p;
tail->next=null;

 三.遇到的问题及解决方法,心得体会
一开始对链表不是十分熟悉,不是很懂链表创建的问题,经过对建立学生信息库的多次练习才明白,链表的强大之处以及链表结点的创建,以及对指针的应用更加
得心应手。

Second experiment Report of C language

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.