程序设计实验报告——结构体

用户自己建立数据类型——结构体

1.理解结构体类型,掌握结构体类型定义及简单使用;
2.掌握结构体类型数组的定义,以及在结构体类型数组上的常见操作,包括查找、插入、删除、排序等;
3.掌握在单链表上的常见操作,包括查找、插入、删除符合条件结点等等。

任务一:

计算两个复数之积:本题要求实现一个计算复数之积的简单函数。
struct complex multiply(struct complex x, struct complex y);
其中struct complex是复数结构体,其定义如下:
struct complex{
int real;
int imag;
};
其中主函数如下:
#include <stdio.h>
int main()
{
struct complex product, x, y;
scanf(“%d%d%d%d”, &x.real, &x.imag, &y.real, &y.imag);
product = multiply(x, y);
printf(“(%d+%di) * (%d+%di) = %d + %di\n”,
x.real, x.imag, y.real, y.imag, product.real, product.imag);
return 0;
}
【示例】
输入:
3 4 5 6
输出:
(3+4i) * (5+6i) = -9 + 38i

#include <stdio.h>
struct complex{
    int real;
    int imag;
};
struct complex multiply(struct complex x, struct complex y){
	struct complex product;
	 product.real = x.real * y.real - x.imag * y.imag;
	 product.imag = x.real * y.imag + x.imag * y.real;
	 return product;
}
int main()
{
    struct complex product, x, y;
    scanf("%d%d%d%d", &x.real, &x.imag, &y.real, &y.imag);
    product = multiply(x, y);
    printf("(%d+%di) * (%d+%di) = %d + %di\n", 
            x.real, x.imag, y.real, y.imag, product.real, product.imag);
    return 0;
}

任务二:

按等级统计学生成绩:本题要求实现一个根据学生成绩设置其等级,并统计不及格人数的简单函数。
int set_grade( struct student *p, int n );
其中p是指向学生信息的结构体数组的指针,该结构体的定义为:
struct student{
int num;
char name[20];
int score;
char grade;
};
n是数组元素个数。学号num、姓名name和成绩score均是已经存储好的。set_grade函数需要根据学生的成绩score设置其等级grade。等级设置:85-100为A,70-84为B,60-69为C,0-59为D。同时,set_grade还需要返回不及格的人数。
其中主函数如下:
#include <stdio.h>
#define MAXN 10
int main()
{ struct student stu[MAXN], *ptr;
int n, i, count;
ptr = stu;
scanf(“%d\n”, &n);
for(i = 0; i < n; i++){
scanf(“%d%s%d”, &stu[i].num, stu[i].name, &stu[i].score);
}
count = set_grade(ptr, n);
printf(“The count for failed (<60): %d”, count);
printf(“The grades:\n”);
for(i = 0; i < n; i++)
printf(“%d %s %c\n”, stu[i].num, stu[i].name, stu[i].grade);
return 0;
}
【示例】
输入:
10
31001 annie 85
31002 bonny 75
31003 carol 70
31004 dan 84
31005 susan 90
31006 paul 69
31007 pam 60
31008 apple 50
31009 nancy 100
31010 bob 78 输出:
The count for failed (<60):1
The grades:
31001 annie A
31002 bonny B
31003 carol B
31004 dan B
31005 susan A
31006 paul C
31007 pam C
31008 apple D
31009 nancy A
31010 bob B

#include <stdio.h>
#define MAXN 10
struct student{
    int num;
    char name[20];
    int score;
    char grade;
};
int set_grade( struct student *p, int n ){
	int i=0;
	while(n){
	if(p->score>=85 && p->score<=100) p->grade='A';
	if(p->score>=70 && p->score<=84) p->grade='B';
	if(p->score>=60 && p->score<=69) p->grade='C';
	if(p->score>=0 && p->score<=59){
		p->grade='D';i++;
	}
	p++;
	n--;
	} 
	return i;
}
int main()
{   struct student stu[MAXN], *ptr;
    int n, i, count;
    ptr = stu;
    scanf("%d\n", &n);
    for(i = 0; i < n; i++){
       scanf("%d%s%d", &stu[i].num, stu[i].name, &stu[i].score);
    } 
   count = set_grade(ptr, n);
   printf("The count for failed (<60): %d\n", count);
   printf("The grades:\n"); 
   for(i = 0; i < n; i++)
       printf("%d %s %c\n", stu[i].num, stu[i].name, stu[i].grade);
   return 0;
}

任务三:

单链表结点删除:本题要求实现两个函数,分别将读入的数据存储为单链表、将链表中所有存储了某给定值的结点删除。
链表结点定义如下:
struct ListNode {
int data;
ListNode *next;
};
读入函数:struct ListNode *readlist();
删除结点函数:Struct ListNode *deletem( struct ListNode *L, int m );
函数readlist从标准输入读入一系列正整数,按照读入顺序建立单链表。当读到−1时表示输入结束,函数应返回指向单链表头结点的指针。
函数deletem将单链表L中所有存储了m的结点删除。返回指向结果链表头结点的指针。
其中主函数和输出链表的函数如下:
#include <stdio.h>
#include <stdlib.h>
void printlist( struct ListNode *L )
{
struct ListNode *p = L;
while § {
printf(“%d “, p->data);
p = p->next;
}
printf(”\n”);
}
int main()
{
int m;
struct ListNode *L = readlist();
scanf(“%d”, &m);
L = deletem(L, m);
printlist(L);
return 0;
}
【示例】
输入:
10 11 10 12 10 -1
10
输出:
11 12

#include <stdio.h>
#include <stdlib.h>
struct ListNode {
    int data;
    struct ListNode *next;
};
struct ListNode *readlist();
struct ListNode *deletem( struct ListNode *L, int m );
void printlist( struct ListNode *L )
{
     struct ListNode *p = L;
     while (p) {
           printf("%d ", p->data);
           p = p->next;
     }
     printf("\n");
}

int main()
{
    int m;
    struct ListNode *L = readlist();
    scanf("%d", &m);
    L = deletem(L, m);
    printlist(L);

    return 0;
}

struct ListNode* readlist() {
    struct ListNode* L = NULL, *tail = NULL;
    while (1) {
        int x; scanf("%d", &x);
        if (x == -1) break;
        struct ListNode* p = (struct ListNode*)malloc(sizeof(struct ListNode));
        p->data = x, p->next = NULL;
        if (!L)  L = tail = p;
        else {
            tail->next = p;
            tail = p;
        }
    }
    return L;
}
struct ListNode* deletem(struct ListNode* L, int m) {
	struct ListNode* p = L, *q = NULL;
    for (; p;) {
        if (p->data == m) {
            if (q) {
                q->next = p->next;
            	free(p);
            	p = q->next;
            }
            else {
                L = L->next;
                free(p);
                p = L;
            }
        }
        else {
            q = p;
            p = p->next;
        }
    }
    return L;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值