c語言第二次實驗報告


·實驗題目,設計思路,實現方法

實驗題目:

11-5 打印楊輝三角(20 分)

本題要求按照規定格式打印前N行楊輝三角。

輸入格式:

輸入在一行中給出N(1)。

輸出格式:

以正三角形的格式輸出前N行楊輝三角。每個數字占固定4位。

輸入樣例:

6

輸出樣例:

        1
       1   1
      1   2   1
     1   3   3   1
    1   4   6   4   1
   1   5  10  10   5   1

設計思路:

通過找規律發現除了最兩端的“1”外,每行的第n個數等於上一行的第n-1個數與第n個數之和。

實現方法:

定義二維數組,利用for循環完成對數據的求和並賦值,利用對格式的控制使二維數組呈三角形輸出。

12-2 統計字符出現次數(20 分)

本題要求編寫程序,統計並輸出某給定字符在給定字符串中出現的次數。

輸入格式:

輸入第一行給出一個以回車結束的字符串(少於80個字符);第二行輸入一個字符。

輸出格式:

在一行中輸出給定字符在給定字符串中出現的次數。

輸入樣例:

programming is More fun!
m

輸出樣例:

2

設計思路:
將字符串看成字符數組,與指定字符一一比較。
實現方法:
定義一個為0的值,利用for循環和if語句,若出現字符與指定字符相同,該值+1,最后輸出該值即出現次數。

13-4 使用函數的選擇法排序(25 分)

 
              

本題要求實現一個用選擇法對整數數組進行簡單排序的函數。

函數接口定義:

void sort( int a[], int n );

其中a是待排序的數組,n是數組a中元素的個數。該函數用選擇法將數組a中的元素按升序排列,結果仍然在數組a中。

裁判測試程序樣例:

#include <stdio.h>
#define MAXN 10

void sort( int a[], int n );

int main()
{
    int i, n;
    int a[MAXN];

    scanf("%d", &n);
    for( i=0; i<n; i++ )
        scanf("%d", &a[i]);

    sort(a, n);

    printf("After sorted the array is:");
    for( i = 0; i < n; i++ )
        printf(" %d", a[i]);
    printf("\n");

    return 0;
}

/* 你的代碼將被嵌在這里 */

輸入樣例:

4
5 1 7 6

輸出樣例:

After sorted the array is: 1 5 6 7
設計思路:
從第一個元素開始,每個元素先找出之后最小的元素,在每個元素與對應最小的元素交換值。
實現方法:
利用雙重循環中將輪到得元素與其后最小元素交換實現從小到大的依次排序。

實驗題目:

14-1 使用函數實現字符串部分復制(20 分)

本題要求編寫函數,將輸入字符串t中從第m個字符開始的全部字符復制到字符串s中。

函數接口定義:

void strmcpy( char *t, int m, char *s );

函數strmcpy將輸入字符串char *t中從第m個字符開始的全部字符復制到字符串char *s中。若m超過輸入字符串的長度,則結果字符串應為空串。

裁判測試程序樣例:

#include <stdio.h>
#define MAXN 20

void strmcpy( char *t, int m, char *s );
void ReadString( char s[] ); /* 由裁判實現,略去不表 */

int main()
{
    char t[MAXN], s[MAXN];
    int m;

    scanf("%d\n", &m);
    ReadString(t);
    strmcpy( t, m, s );
    printf("%s\n", s);

    return 0;
}

/* 你的代碼將被嵌在這里 */

輸入樣例:

7
happy new year

輸出樣例:

new year
設計思路:
新建字符串,與指定字符串的指定位置開始累加賦值。
實現方法:
建立循環,初始值為指定位置的序號,在循環中對新建字符串一一賦值。

實驗題目:

15-5 建立學生信息鏈表(20 分)

本題要求實現一個將輸入的學生成績組織成單向鏈表的簡單函數。

函數接口定義:

void input();

該函數利用scanf從輸入中獲取學生的信息,並將其組織成單向鏈表。鏈表節點結構定義如下:

struct stud_node {
    int              num;      /*學號*/
    char             name[20]; /*姓名*/
    int              score;    /*成績*/
    struct stud_node *next;    /*指向下個結點的指針*/
};

單向鏈表的頭尾指針保存在全局變量headtail中。

輸入為若干個學生的信息(學號、姓名、成績),當輸入學號為0時結束。

裁判測試程序樣例:

#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;
}

/* 你的代碼將被嵌在這里 */

輸入樣例:

1 zhang 78
2 wang 80
3 li 75
4 zhao 85
0

輸出樣例:

1 zhang 78
2 wang 80
3 li 75
4 zhao 85
設計思路、實現方法:
構建鏈表。
 
             

·源程序

11-5 打印楊輝三角

#include<stdio.h>
int main()
{
int N,a[10][10],i,j,k;
scanf("%d",&N);
a[0][0]=1;
a[1][0]=1;
a[1][1]=1;
for(i=2;i<N;i++){
a[i][0]=1;
for(j=1;j<i;j++){
a[i][j]=a[i-1][j-1]+a[i-1][j];
}
a[i][i]=1;
}
for(i=0;i<N;i++){
for(j=(N-i-1);j>0;j--)printf(" ");
for(k=0;k<=i;k++)printf("%4d",a[i][k]);
printf("\n");
}
return 0;
}

12-2 統計字符出現次數

#include<stdio.h>
int main()
{
char str[80],a;
int k=0,i,count=0;
while((str[k]=getchar())!='\n')
k++;
scanf("%c",&a);
for(i=0;i<=k;i++){
if(str[i]==a)
count++;
}
printf("%d",count);
return 0;
}

13-4 使用函數的選擇法排序

void sort( int a[], int n )
{
int u,i,min,temp,k;
for(i=0;i<n-1;i++){
k=i;
for(u=i+1;u<n;u++){
if(a[u]<a[k])k=u;
}
temp=a[k];
a[k]=a[i];
a[i]=temp;
}
}

14-1 使用函數實現字符串部分復制

void strmcpy( char *t, int m, char *s )
{
int i,j;
j=0;
for(i=m-1;t[i]!='\0';i++)
s[j++]=t[i];
s[j]='\0';
}

15-5 建立學生信息鏈表

void input()
{
struct stud_node *p;
head = (struct stud_node*)malloc(sizeof(struct stud_node));
head->next = NULL;
p = head;
while(1)
{
tail = (struct stud_node*)malloc(sizeof(struct stud_node));
scanf("%d", &tail->num);
if (!tail->num) break;
scanf("%s%d", tail->name, &tail->score);
p->next = tail;
p= tail;
}
head = head->next;
}

 

三、遇到的問題和解決辦法。

1.將楊輝三角看作幾何圖形覺得無從下手,需要發現楊輝三角是一個變形的二維數組;

2.選擇排序雙重循環用的兩個變量i和u數量相同范圍不同,設元素個數為n,若i和u都小於n運行時出現錯誤,將i的范圍改為小於n-1和u為小於n才能正常運行;

3.對鏈表不熟悉,只能copy教材的步驟。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM