C 数据结构中的可变长度
我需要编写一个程序,找到学生的平均范围。我不明白如果一个学生的姓氏是卡夫·卡夫·卡夫·卡夫·卡夫·卡夫·卡夫·卡夫·卡夫·卡夫·卡夫·卡夫·卡夫·卡夫·卡夫·卡夫·卡夫,我怎么能灵活地使用C 数据结构中的可变长度,c,data-structures,C,Data Structures,我需要编写一个程序,找到学生的平均范围。我不明白如果一个学生的姓氏是卡夫·卡夫·卡夫·卡夫·卡夫·卡夫·卡夫·卡夫·卡夫·卡夫·卡夫·卡夫·卡夫·卡夫·卡夫·卡夫·卡夫,我怎么能灵活地使用char name 最简单的方法是增加char name的大小,以允许尽可能长的学生姓名。但是将原始数据直接读入结构是非常不安全的;最好使用临时字符串并对其进行解析 请注意,这样您就不再需要struct student,也不需要将nameachar*。仍然存在某种限制:读取缓冲区必须足够大,以读取每行的所有数据
char name
最简单的方法是增加
char name
的大小,以允许尽可能长的学生姓名。但是将原始数据直接读入结构是非常不安全的;最好使用临时字符串并对其进行解析
请注意,这样您就不再需要struct student
,也不需要将name
achar*
。仍然存在某种限制:读取缓冲区必须足够大,以读取每行的所有数据。就其本身而言,fgets
可以读取“太长”的行(它们不会以\n
结尾),但是您需要很多标志来确保您正在解析正确的数据。在这种情况下,更容易使缓冲区“确实足够大”
最简单的方法是增加
char name
的大小,以允许尽可能长的学生姓名。但是将原始数据直接读入结构是非常不安全的;最好使用临时字符串并对其进行解析
请注意,这样您就不再需要struct student
,也不需要将name
achar*
。仍然存在某种限制:读取缓冲区必须足够大,以读取每行的所有数据。就其本身而言,fgets
可以读取“太长”的行(它们不会以\n
结尾),但是您需要很多标志来确保您正在解析正确的数据。在这种情况下,更容易使缓冲区“确实足够大”
根据@Jongware的要求,这是一种基于scanf家族的解决方案,如果输入的结构相对恒定,它是解析字符串的一种非常方便的方法 给定输入字符串
4273 Багров Д. С. 5454 знззз
在本例中,我将假设我们所追求的常量模式是一个int后跟3个字符串,后跟一个int和一个字符串。还有其他的方法,我会回到这些
一个非常基本的演示:
#include <stdio.h>
int main(void) {
char * inputdata = "4273 Багров Д. С. 5454 знззз";
// variables to receive the scanned data
int firstint, secondint;
char firststring[32];
char secondstring[32];
char thirdstring[32];
char fourthstring[32];
// important, you should check whether the number of converted elements
// matches what you expect:
int scannedelements;
// let's scan the input
scannedelements = sscanf (inputdata,"%d %s %s %s %d %s",&firstint, &firststring, secondstring,
thirdstring,&secondint,fourthstring);
// and show what we found. Notice the similarity between scanf and printf
// but also note the subtle differences!!!
printf("We scanned %d %s %s %s %d %s\n",firstint, firststring, secondstring,
thirdstring,secondint,fourthstring);
printf("That's a total of %d elements %d\n",scannedelements);
return 0;
}
请注意,我将您命名为exam的字段扫描为一个整数,您可以通过digit=data%10的循环轻松地从中提取数字;数据=数据/10代码>
现在,第一组字符串被切分为3个不同的输出这一事实可能令人恼火。根据输出数据,我们可以指示sscanf读取,直到它遇到一个数字:
#include <stdio.h>
int main(void) {
char * inputdata = "4273 Багров Д. С. 5454 знззз";
// variables to receive the scanned data
int firstint, secondint;
char firststring[32];
char secondstring[32];
char thirdstring[32];
char fourthstring[32];
// important, you should check whether the number of converted elements
// matches what you expect:
int scannedelements;
// Alternatively, let's scan the group of 3 strings into 1 variable
scannedelements = sscanf (inputdata,"%d %[^0-9] %d %s",&firstint, firststring, &secondint,fourthstring);
// and show what we found.
printf("We scanned %d %s %d %s\n",firstint, firststring,secondint,fourthstring);
printf("That's a total of %d elements %d\n",scannedelements);
return 0;
}
注意БаГБББББББББББББББББББББББББББББББ。С。
,这可能是问题,也可能不是问题,但很容易消除
为方便起见,此代码在ideone上提供:
这个例子仅仅触及了scanf可能的表面,我鼓励您探索它以发现更多的可能性
--
关于如何计算平均分数:
#include <stdio.h>
int main(void) {
int inputdata = 24680;
int average = 0;
int number_digits = 0;
int digit = 0;
int digits = 0;
while (inputdata > 0) {
digit = inputdata % 10; // modulo by 10 is the last digit
average += digit;
digits++;
inputdata = inputdata / 10; // integer division by 10 = remove last digit
}
if (digits > 0) { // to avoid dividing by zero is some edge case
printf ("The average over %d scores is %.1f\n", digits, (double) average / digits);
} else {
printf ("As the input was 0, the average is 0");
}
return 0;
}
#包括
内部主(空){
int inputdata=24680;
整数平均=0;
整数位数=0;
整数位数=0;
整数位数=0;
同时(输入数据>0){
digit=inputdata%10;//模乘10是最后一位数字
平均+=位数;
数字++;
inputdata=inputdata/10;//整数除以10=删除最后一位
}
如果(数字>0){//避免被零除是一些边缘情况
printf(“超过%d分的平均值为%.1f\n”,位数,(双位数)平均值/位数);
}否则{
printf(“当输入为0时,平均值为0”);
}
返回0;
}
根据@Jongware的要求,这是一种基于scanf家族的解决方案,如果输入的结构相对恒定,它是解析字符串的一种非常方便的方法
给定输入字符串
4273 Багров Д. С. 5454 знззз
在本例中,我将假设我们所追求的常量模式是一个int后跟3个字符串,后跟一个int和一个字符串。还有其他的方法,我会回到这些
一个非常基本的演示:
#include <stdio.h>
int main(void) {
char * inputdata = "4273 Багров Д. С. 5454 знззз";
// variables to receive the scanned data
int firstint, secondint;
char firststring[32];
char secondstring[32];
char thirdstring[32];
char fourthstring[32];
// important, you should check whether the number of converted elements
// matches what you expect:
int scannedelements;
// let's scan the input
scannedelements = sscanf (inputdata,"%d %s %s %s %d %s",&firstint, &firststring, secondstring,
thirdstring,&secondint,fourthstring);
// and show what we found. Notice the similarity between scanf and printf
// but also note the subtle differences!!!
printf("We scanned %d %s %s %s %d %s\n",firstint, firststring, secondstring,
thirdstring,secondint,fourthstring);
printf("That's a total of %d elements %d\n",scannedelements);
return 0;
}
请注意,我将您命名为exam的字段扫描为一个整数,您可以通过digit=data%10的循环轻松地从中提取数字;数据=数据/10代码>
现在,第一组字符串被切分为3个不同的输出这一事实可能令人恼火。根据输出数据,我们可以指示sscanf读取,直到它遇到一个数字:
#include <stdio.h>
int main(void) {
char * inputdata = "4273 Багров Д. С. 5454 знззз";
// variables to receive the scanned data
int firstint, secondint;
char firststring[32];
char secondstring[32];
char thirdstring[32];
char fourthstring[32];
// important, you should check whether the number of converted elements
// matches what you expect:
int scannedelements;
// Alternatively, let's scan the group of 3 strings into 1 variable
scannedelements = sscanf (inputdata,"%d %[^0-9] %d %s",&firstint, firststring, &secondint,fourthstring);
// and show what we found.
printf("We scanned %d %s %d %s\n",firstint, firststring,secondint,fourthstring);
printf("That's a total of %d elements %d\n",scannedelements);
return 0;
}
注意БаГБББББББББББББББББББББББББББББ。С。
,这可能是问题,也可能不是问题,但很容易消除
为方便起见,此代码在ideone上提供:
这个例子仅仅触及了scanf可能的表面,我鼓励您探索它以发现更多的可能性
--
关于如何计算平均分数:
#include <stdio.h>
int main(void) {
int inputdata = 24680;
int average = 0;
int number_digits = 0;
int digit = 0;
int digits = 0;
while (inputdata > 0) {
digit = inputdata % 10; // modulo by 10 is the last digit
average += digit;
digits++;
inputdata = inputdata / 10; // integer division by 10 = remove last digit
}
if (digits > 0) { // to avoid dividing by zero is some edge case
printf ("The average over %d scores is %.1f\n", digits, (double) average / digits);
} else {
printf ("As the input was 0, the average is 0");
}
return 0;
}
#包括
内部主(空){
int inputdata=24680;
整数平均=0;
整数位数=0;
整数位数=0;
整数位数=0;
同时(输入数据>0){
digit=inputdata%10;//模乘10是最后一位数字
平均+=位数;
数字++;
inputdata=inputdata/10;//整数除以10=删除最后一位
}
如果(数字>0){//避免被零除是一些边缘情况
printf(“超过%d分的平均值为%.1f\n”,位数,(双位数)平均值/位数);
}否则{
printf(“当输入为0时,平均值为0”);
}
返回0;
}
我认为使用结构自动解析字符串不是一个好主意,您的问题是该选择的直接影响。只需将带有fgets的行eg读入字符数组,然后对其进行解析,例如使用sscanf。或者增加name
元素的大小以允许尽可能长的名称(内存效率低但非常简单),或者将其设置为char*
并使用malloc
。您还需要重新考虑您的“字符串解析”(尝试使用strtok
)。我也建议使用@fvu建议,但您可能对strtok
感兴趣。请查看此项,@fvu、Jongware和Iharob非常感谢您的回答!
#include <stdio.h>
int main(void) {
int inputdata = 24680;
int average = 0;
int number_digits = 0;
int digit = 0;
int digits = 0;
while (inputdata > 0) {
digit = inputdata % 10; // modulo by 10 is the last digit
average += digit;
digits++;
inputdata = inputdata / 10; // integer division by 10 = remove last digit
}
if (digits > 0) { // to avoid dividing by zero is some edge case
printf ("The average over %d scores is %.1f\n", digits, (double) average / digits);
} else {
printf ("As the input was 0, the average is 0");
}
return 0;
}