博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c语言-结构体实例笔记
阅读量:796 次
发布时间:2019-03-25

本文共 7308 字,大约阅读时间需要 24 分钟。

结构体实例

在这里插入图片描述

实例一览:

  • 使用结构体存储学生的信息

    Store information of a student using structure

  • 计算二者距离(以英寸英尺为单位)

    Add two distances (in inch-feet)

  • 通过结构体传递给函数来计算两个复数相加

    Add two complex numbers by passing structures to a function

  • 计算两个时间段之间的差

    Calculate the difference between two time periods

  • 使用结构体存储10名学生的信息

    Store information of 10 students using structures

  • 使用结构体存储n名学生的信息

    Store information of n students using structures

Store information of a student using structure

#include 
struct student {
char name[50]; int roll; float marks;} s;int main() {
printf("Enter information:\n"); printf("Enter name: "); fgets(s.name, sizeof(s.name), stdin); printf("Enter roll number: "); scanf("%d", &s.roll); printf("Enter marks: "); scanf("%f", &s.marks); printf("Displaying Information:\n"); printf("Name: "); printf("%s", s.name); printf("Roll number: %d\n", s.roll); printf("Marks: %.1f\n", s.marks); return 0;}

输出:

Enter information:Enter name: JackEnter roll number: 23Enter marks: 34.5Displaying Information:Name: JackRoll number: 23Marks: 34.5

在此程序中,创建了一个学生结构体。该结构有三个成员:name (string),roll (integer) 和 marks (float)。

然后,创建一个结构变量s来存储信息并将其显示在屏幕上。

Add two distances (in inch-feet)

12英寸等于1英尺。

#include 
struct Distance {
int feet; float inch;} d1, d2, result;int main() {
// take first distance input printf("Enter 1st distance\n"); printf("Enter feet: "); scanf("%d", &d1.feet); printf("Enter inch: "); scanf("%f", &d1.inch); // take second distance input printf("\nEnter 2nd distance\n"); printf("Enter feet: "); scanf("%d", &d2.feet); printf("Enter inch: "); scanf("%f", &d2.inch); // adding distances result.feet = d1.feet + d2.feet; result.inch = d1.inch + d2.inch; // convert inches to feet if greater than 12 while (result.inch >= 12.0) {
result.inch = result.inch - 12.0; ++result.feet; } printf("\nSum of distances = %d\'-%.1f\"", result.feet, result.inch); return 0;}

输出:

Enter 1st distanceEnter feet: 23Enter inch: 8.6Enter 2nd distanceEnter feet: 34Enter inch: 2.4Sum of distances = 57'-11.0"

在此程序中,定义了一个结构距离。 该结构具有两个成员:

feet - integer

inch - float

创建了结构体Distance的两个变量d1和d2。 这些变量以英尺和英寸为单位存储距离。

然后,计算这两个距离的总和并将其存储在结果变量中。最后,把结果打印在屏幕上。

Add two complex numbers by passing structures to a function

#include 
typedef struct complex {
float real; float imag;} complex;complex add(complex n1, complex n2);int main() {
complex n1, n2, result; printf("For 1st complex number \n"); printf("Enter the real and imaginary parts: "); scanf("%f %f", &n1.real, &n1.imag); printf("\nFor 2nd complex number \n"); printf("Enter the real and imaginary parts: "); scanf("%f %f", &n2.real, &n2.imag); result = add(n1, n2); printf("Sum = %.1f + %.1fi", result.real, result.imag); return 0;}complex add(complex n1, complex n2) {
complex temp; temp.real = n1.real + n2.real; temp.imag = n1.imag + n2.imag; return (temp);}

输出:

For 1st complex numberEnter the real and imaginary parts: 2.1-2.3For 2nd complex numberEnter the real and imaginary parts: 5.623.2Sum = 7.7 + 20.9i

在此程序中,声明了一个名为complex的结构体。 它有两个成员:real和imag。 然后,我们从该结构中创建了两个变量n1和n2。

这两个结构体变量被传递给add()函数。 该函数计算总和并返回包含该总和的结构体。

最后,在main()函数中打印出复数的总和。

Calculate the difference between two time periods

#include 
struct TIME {
int seconds; int minutes; int hours;};void differenceBetweenTimePeriod(struct TIME t1, struct TIME t2, struct TIME *diff);int main() {
struct TIME startTime, stopTime, diff; printf("Enter the start time. \n"); printf("Enter hours, minutes and seconds: "); scanf("%d %d %d", &startTime.hours, &startTime.minutes, &startTime.seconds); printf("Enter the stop time. \n"); printf("Enter hours, minutes and seconds: "); scanf("%d %d %d", &stopTime.hours, &stopTime.minutes, &stopTime.seconds); // Difference between start and stop time differenceBetweenTimePeriod(startTime, stopTime, &diff); printf("\nTime Difference: %d:%d:%d - ", startTime.hours, startTime.minutes, startTime.seconds); printf("%d:%d:%d ", stopTime.hours, stopTime.minutes, stopTime.seconds); printf("= %d:%d:%d\n", diff.hours, diff.minutes, diff.seconds); return 0;}// Computes difference between time periodsvoid differenceBetweenTimePeriod(struct TIME start, struct TIME stop, struct TIME *diff) {
while (stop.seconds > start.seconds) {
--start.minutes; start.seconds += 60; } diff->seconds = start.seconds - stop.seconds; while (stop.minutes > start.minutes) {
--start.hours; start.minutes += 60; } diff->minutes = start.minutes - stop.minutes; diff->hours = start.hours - stop.hours;}

输出:

Enter the start time.Enter hours, minutes and seconds: 133455Enter the stop time.Enter hours, minutes and seconds: 81215Time Difference: 13:34:55 - 8:12:15 = 5:22:40

在此程序中,要求用户输入两个时间段,这两个时间段分别存储在结构体变量startTime和stopTime中。

然后,函数differenceBetweenTimePeriod()计算时间段之间的差。 在main()函数中显示结果而不返回它(使用引用技术调用)。

Store information of 10 students using structures

#include 
struct student {
char firstName[50]; int roll; float marks;} s[10];int main() {
int i; printf("Enter information of students:\n"); // storing information for (i = 0; i < 5; ++i) {
s[i].roll = i + 1; printf("\nFor roll number%d,\n", s[i].roll); printf("Enter first name: "); scanf("%s", s[i].firstName); printf("Enter marks: "); scanf("%f", &s[i].marks); } printf("Displaying Information:\n\n"); // displaying information for (i = 0; i < 5; ++i) {
printf("\nRoll number: %d\n", i + 1); printf("First name: "); puts(s[i].firstName); printf("Marks: %.1f", s[i].marks); printf("\n"); } return 0;}

输出:

Enter information of students: For roll number1,Enter name: TomEnter marks: 98For roll number2,Enter name: JerryEnter marks: 89...Displaying Information:Roll number: 1Name: TomMarks: 98...

在此程序中,将创建一个学生结构体。 该结构具有三个成员:name (string), roll (integer) and marks (float)。

然后,我们创建了具有5个元素的结构体数组s,以存储5个学生的信息。

该程序使用for循环,从用户处获取5名学生的信息,并将其存储在结构体数组中。 然后使用另一个for循环,将用户输入的信息将显示在屏幕上。

Store information of n students using structures

该程序要求用户存储noOfRecords的值,并使用malloc()函数动态地为noOfRecords结构体变量分配内存。

#include 
#include
struct course {
int marks; char subject[30];};int main() {
struct course *ptr; int noOfRecords; printf("Enter the number of records: "); scanf("%d", &noOfRecords); // Memory allocation for noOfRecords structures ptr = (struct course *)malloc(noOfRecords * sizeof(struct course)); for (int i = 0; i < noOfRecords; ++i) {
printf("Enter subject and marks:\n"); scanf("%s %d", (ptr + i)->subject, &(ptr + i)->marks); } printf("Displaying Information:\n"); for (int i = 0; i < noOfRecords; ++i) {
printf("%s\t%d\n", (ptr + i)->subject, (ptr + i)->marks); } free(ptr); return 0;}

输出:

Enter the number of records: 2Enter subject and marks:Science 82Enter subject and marks:DSA 73Displaying Information:Science     82DSA     73

文章来自微信公众号:内卡巴拉之树

欢迎关注:

在这里插入图片描述

转载地址:http://btnyk.baihongyu.com/

你可能感兴趣的文章