c语言-结构体实例笔记
Posted acrifhy
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c语言-结构体实例笔记相关的知识,希望对你有一定的参考价值。
结构体实例
实例一览:
-
使用结构体存储学生的信息
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 <stdio.h>
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: Jack
Enter roll number: 23
Enter marks: 34.5
Displaying Information:
Name: Jack
Roll number: 23
Marks: 34.5
在此程序中,创建了一个学生结构体。该结构有三个成员:name (string),roll (integer) 和 marks (float)。
然后,创建一个结构变量s来存储信息并将其显示在屏幕上。
Add two distances (in inch-feet)
12英寸等于1英尺。
#include <stdio.h>
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 distance
Enter feet: 23
Enter inch: 8.6
Enter 2nd distance
Enter feet: 34
Enter inch: 2.4
Sum of distances = 57'-11.0"
在此程序中,定义了一个结构距离。 该结构具有两个成员:
feet - integer
inch - float
创建了结构体Distance的两个变量d1和d2。 这些变量以英尺和英寸为单位存储距离。
然后,计算这两个距离的总和并将其存储在结果变量中。最后,把结果打印在屏幕上。
Add two complex numbers by passing structures to a function
#include <stdio.h>
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 number
Enter the real and imaginary parts: 2.1
-2.3
For 2nd complex number
Enter the real and imaginary parts: 5.6
23.2
Sum = 7.7 + 20.9i
在此程序中,声明了一个名为complex的结构体。 它有两个成员:real和imag。 然后,我们从该结构中创建了两个变量n1和n2。
这两个结构体变量被传递给add()函数。 该函数计算总和并返回包含该总和的结构体。
最后,在main()函数中打印出复数的总和。
Calculate the difference between two time periods
#include <stdio.h>
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 periods
void 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: 13
34
55
Enter the stop time.
Enter hours, minutes and seconds: 8
12
15
Time Difference: 13:34:55 - 8:12:15 = 5:22:40
在此程序中,要求用户输入两个时间段,这两个时间段分别存储在结构体变量startTime和stopTime中。
然后,函数differenceBetweenTimePeriod()计算时间段之间的差。 在main()函数中显示结果而不返回它(使用引用技术调用)。
Store information of 10 students using structures
#include <stdio.h>
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: Tom
Enter marks: 98
For roll number2,
Enter name: Jerry
Enter marks: 89
.
.
.
Displaying Information:
Roll number: 1
Name: Tom
Marks: 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 <stdio.h>
#include <stdlib.h>
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: 2
Enter subject and marks:
Science 82
Enter subject and marks:
DSA 73
Displaying Information:
Science 82
DSA 73
文章来自微信公众号:内卡巴拉之树
欢迎关注:
以上是关于c语言-结构体实例笔记的主要内容,如果未能解决你的问题,请参考以下文章
go语言学习笔记 — 基础 — 复合数据类型 — 结构体:实例化结构体 — 为结构体分配内存并初始化
go语言学习笔记 — 基础 — 复合数据类型 — 结构体(9.2):结构体可见性规则,外层结构体实例访问嵌套结构体中内层结构体的方法(重要)