前言
本文介绍了C语言:数组初值;字符串、字符数组、字符串数组;类型定义 typedef
【重拾C语言】六、批量数据组织(一)数组(数组类型、声明与操作、多维数组;典例:杨辉三角、矩阵乘积、消去法)-CSDN博客
https://blog.csdn.net/m0_63834988/article/details/133580645?spm=1001.2014.3001.5502
【重拾C语言】六、批量数据组织(二)线性表——分类与检索(主元排序、冒泡排序、插入排序、顺序检索、对半检索)_QomolangmaH的博客-CSDN博客
https://blog.csdn.net/m0_63834988/article/details/133620693?spm=1001.2014.3001.5501
编程序,输入一个班50名学生的”程序设计基础”课程成绩,按成绩由高到低的顺序输出,要求同时输出每个成绩是哪个学生的:
#include <stdio.h>
void bubbleSort(int studentIDs[], float scores[], int n) {
int i, j;
for (i = 0; i < n-1; i++) {
for (j = 0; j < n-i-1; j++) {
if (scores[j] < scores[j+1]) {
// 交换两个学生的成绩
float tempScore = scores[j];
scores[j] = scores[j+1];
scores[j+1] = tempScore;
// 交换两个学生的学号
int tempID = studentIDs[j];
studentIDs[j] = studentIDs[j+1];
studentIDs[j+1] = tempID;
}
}
}
}
int main() {
int studentIDs[50];
float scores[50];
int i;
// 输入学生的学号和成绩
for (i = 0; i < 50; i++) {
printf("请输入第%d个学生的学号和成绩:", i+1);
scanf("%d %f", &studentIDs[i], &scores[i]);
}
// 对成绩进行排序
bubbleSort(studentIDs, scores, 50);
// 输出排序后的成绩和学生信息
printf("按成绩由高到低排序后的结果为:\n");
for (i = 0; i < 50; i++) {
printf("学生学号:%d,成绩:%.2f\n", studentIDs[i], scores[i]);
}
return 0;
}
在C语言中,字符串是由字符组成的数组。可以使用字符数组来表示姓名。例如,可以定义一个字符数组来存储姓名,并对其进行操作和处理。
#include <stdio.h>
#include <string.h>
int main() {
char name[50]; // 定义一个字符数组来存储姓名,数组大小根据实际情况确定
printf("请输入姓名:");
scanf("%s", name); // 从用户输入中读取姓名并存储到字符数组中
printf("您输入的姓名是:%s\n", name); // 输出姓名
int length = sizeof(name) / sizeof(name[0]); // 计算数组的长度
printf("姓名数组的长度是:%d\n", length);
int length1 = strlen(name); // 计算姓名字符串的长度
printf("姓名占用的长度是:%d\n", length1);
return 0;
}
strcpy
、strcmp
等,用于处理字符串。#include <stdio.h>
#include <string.h>
int main() {
// 字符串
char str[] = "Hello, World!";
printf("字符串: %s\n", str);
// 字符数组
char charArr[] = {'H', 'e', 'l', 'l', 'o', '\0'};
printf("字符数组: %s\n", charArr);
// 字符串数组
char strArr[][20] = {"Apple", "Banana", "Orange"};
int numStrings = sizeof(strArr) / sizeof(strArr[0]);
printf("字符串数组:\n");
for (int i = 0; i < numStrings; i++) {
printf("%s\n", strArr[i]);
}
return 0;
}
C语言提供了许多用于字符串操作的库函数,例如字符串的复制、连接、比较等。可以使用这些函数来对字符串进行各种操作。
#include <stdio.h>
#include <string.h>
int main() {
char str1[50] = "Hello";
char str2[50] = "World";
// 字符串复制
strcpy(str1, str2);
printf("复制后的字符串: %s\n", str1);
// 字符串连接
strcat(str1, str2);
printf("连接后的字符串: %s\n", str1);
// 字符串比较
int result = strcmp(str1, str2);
if (result == 0) {
printf("字符串相等\n");
} else if (result < 0) {
printf("字符串1小于字符串2\n");
} else {
printf("字符串1大于字符串2\n");
}
return 0;
}
回文字是指正序和逆序相同的字符串。可以编写一个函数来判断一个字符串是否为回文字:
#include <stdio.h>
#include <string.h>
int isPalindrome(char str[]) {
int len = strlen(str);
int i, j;
for (i = 0, j = len - 1; i < j; i++, j--) {
if (str[i] != str[j]) {
return 0; // 不是回文字,返回0
}
}
return 1; // 是回文字,返回1
}
int main() {
char str[50];
printf("请输入一个字符串:");
scanf("%s", str);
if (isPalindrome(str)) {
printf("是回文字\n");
} else {
printf("不是回文字\n");
}
return 0;
}
字符串数组
编程序,输入一个班50名学生的成绩,按成绩由高到低的顺序 输出每名学生的学号、 姓名、成绩:
#include <stdio.h>
#include <string.h>
#define MAX_STUDENTS 50
#define MAX_NAME_LENGTH 50
void sortStudents(char students[][MAX_NAME_LENGTH+1], int scores[], int numStudents) {
int i, j;
char temp[MAX_NAME_LENGTH+1];
int tempScore;
for (i = 0; i < numStudents - 1; i++) {
for (j = 0; j < numStudents - i - 1; j++) {
if (scores[j] < scores[j + 1]) {
strcpy(temp, students[j]);
strcpy(students[j], students[j + 1]);
strcpy(students[j + 1], temp);
tempScore = scores[j];
scores[j] = scores[j + 1];
scores[j + 1] = tempScore;
}
}
}
}
int main() {
char students[MAX_STUDENTS][MAX_NAME_LENGTH+1];
int scores[MAX_STUDENTS];
int numStudents, i;
printf("请输入学生人数(最多50人):");
scanf("%d", &numStudents);
if (numStudents > MAX_STUDENTS) {
printf("学生人数超过最大限制。\n");
return 0;
}
for (i = 0; i < numStudents; i++) {
printf("请输入第%d名学生的学号:", i + 1);
scanf("%s", students[i]);
printf("请输入第%d名学生的成绩:", i + 1);
scanf("%d", &scores[i]);
}
sortStudents(students, scores, numStudents);
printf("\n按成绩由高到低的顺序输出学生信息:\n");
printf("学号\t成绩\n");
for (i = 0; i < numStudents; i++) {
printf("%s\t%d\n", students[i], scores[i]);
}
return 0;
}
结构体
可以使用结构体来表示成绩单,结构体可以包含姓名、科目和成绩等信息:
#include <stdio.h>
struct Score {
char name[50];
char subject[50];
int score;
};
int main() {
struct Score score1;
printf("请输入姓名:");
scanf("%s", score1.name);
printf("请输入科目:");
scanf("%s", score1.subject);
printf("请输入成绩:");
scanf("%d", &score1.score);
printf("姓名:%s\n", score1.name);
printf("科目:%s\n", score1.subject);
printf("成绩:%d\n", score1.score);
return 0;
}
(关于结构体的具体使用详见本系列后文)
#include <stdio.h>
typedef int Length; // 定义类型别名 Length,代表长度
int main() {
Length len = 10; // 使用类型别名 Length
printf("长度:%d\n", len);
return 0;
}
输出:
长度:10
注意:一定要把类型名与变量名区别开