- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我需要编写一个程序来确定学生的平均水平。如果学生的姓氏是 Константинопольский,我不明白如何灵活地执行 char name
?
struct student {
char group[5];
char name[21];
char exam[5];
char test[12];
};
void student_average_scope(FILE *file) {
struct student this;
int i;
int sum;
while (fgets((char *)&this, sizeof(this), file) != NULL) {
this.name[20] = '\0';
this.exam[4] = '\0';
for (i = 0, sum = 0; i < 4; i++) {
sum += this.exam[i] - '0';
}
printf("%s - %.1f\n", this.name, (float)sum / 4);
}
fclose(file);
}
我的学生名单:
4273 Багров Д. С. 5454 знззз
4273 Нуйикн А. А. 4333 знзнз
4272 Галкин Г. А. 5445 ззззз
4273 Иванов А. А. 3433 знззн
4272 Козлов И. И. 4443 ззззз
4272 Козлов В. И. 4444 знззз
4272 Бобров П. Н. 4543 знззз
4272 Шмелев И. И. 4443 знззн
最佳答案
应@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;
}
输出:
We scanned 4273 Багров Д. С. 5454 знззз
That's a total of 6 elements
请注意,我将您命名为 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;
}
哪些输出:
We scanned 4273 Багров Д. С. 5454 знззз
That's a total of 4 elements -1079150400
注意 Багров Д 中的尾随空格。 С.
,这可能是也可能不是问题,但很容易删除。
为方便起见,此代码可在 ideone 上获得:http://ideone.com/4gFlxf#sthash.KQfhcYxr.dpuf
这个例子只是简单地介绍了 scanf 的可能性,我鼓励您探索它的 manpage发现更多可能性。
--
关于如何计算平均分:
#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;
}
关于c - 数据结构中灵活的可变长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27458642/
为什么禁用类型像 type t = A of int | B of string * mutable int 虽然允许此类类型: type t = A of int | B of string * i
我正在寻找一种类似结构的数据结构,我可以从中创建多个实例并具有某种类型提示而不是不可变的。 所以我有这样的东西: class ConnectionConfig(NamedTuple): nam
我需要转到引用的结构: class SearchKnot { var isWord : Bool = false var text : String = "" var to
如sec 10.4.3中所述 当控制进入执行时,执行以下步骤 功能对象F(调用者)中包含的功能代码的上下文 提供thisArg,而调用方提供argumentsList: 如
i make a game that start display Activity indicator And activity indicator bottom display UiLable wi
编辑:我在这里不断获得支持。只是为了记录,我认为这不再重要。自从我发布它以来我就不再需要它了。 我想在 Scala 中执行以下操作... def save(srcPath: String, destP
使用可变对象作为 Hashmap 键是一种不好的做法吗?当您尝试使用已修改足以更改其哈希码的键从 HashMap 中检索值时,会发生什么? 例如,给定 class Key { int a; /
如果您在Kotlin中访问List类型的Java值,则将获得(Mutable)List!类型。 例如。: Java代码: public class Example { public stati
我编写了 str 类(内置)的以下扩展,以便执行以下操作:假设我有字符串 "Ciao" ,通过做"Ciao" - "a"我想要的结果是字符串 "Cio" 。这是执行此操作的代码,并且运行良好: cla
使用可变对象作为 Hashmap 键是一种不好的做法吗?当您尝试使用已修改足以更改其哈希码的键从 HashMap 中检索值时,会发生什么? 例如,给定 class Key { int a; /
我正在为我的公司设计一个数据库来管理商业贷款。每笔贷款都可以有担保人,可以是个人或公司,在借款业务失败时作为财务支持。 我有 3 个表:Loan、Person 和 Company,它们存储明显的信息。
我使用二进制序列化从 C# 类中保存 F# 记录。一切正常: F#: type GameState = { LevelStatus : LevelStatus
import javax.swing.JOptionPane; public class HW { public static void main(String[] args) { Strin
使用 flatbuffer mutable 有多少性能损失? 是否“正确”使用 FlatBuffers 来拥有一个应该可编辑的对象/结构(即游戏状态) 在我的示例中,我现在有以下类: class Ga
std::function create_function (args...) { int x = initial_value (args...); return [x] () mut
我需要在 for 循环中找到用户输入的字符。我通常会这样做 如果(句子[i] == 'e') 但是因为在这里,'e' 将是一个单字母字符变量,我不知道如何获取要比较的值。我不能只输入 if (sent
我有一个这样的算法: let seed: Foo = ... let mut stack: Vec = Vec::new(); stack.push(&seed); while let Some(ne
这个问题可能看起来非常基础,但我很难弄清楚如何做。我有一个整数,我需要使用 for 循环来循环整数次。 首先,我尝试了—— fn main() { let number = 10; // An
如果我有以下结构: struct MyStruct { tuple: (i32, i32) }; 以及以下函数: // This will not compile fn function(&mut s
我希望在每个 session 的基础上指定列的默认值。下面的脚本不起作用,但描述了我想如何使用它。我目前使用的是 MySQL 5.5.28,但如果需要可以升级。 CREATE TABLE my_tbl
我是一名优秀的程序员,十分优秀!