用 C 语言编程 - 从具有函数的结构中计算 char 元素的出现次数
Programming in C - count occurences of a char element from structure with function
我有一个函数有问题。我有一个包含学生信息的结构,如姓名、身份证号码、标记等。我能够阅读和打印所有需要的内容,但问题是当我想计算输入的相等名称时。例如,如果我输入计数名称 Iva,它出现了 3 次,则计数为 3。它 returns 0 是我到目前为止使用的。这是代码:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
struct student {
char name [31];
char ID[11];//personal code
long FN;//student number
};
int BrS;
student MasStud[35]; //array for number of students
char bf[5];
void readStud(student *st){
int i;
printf("-------------------\n");
printf("Enter name of a student:\n"); gets_s(st->name);
printf("Enter ID");gets_s(st->ID);
printf("Enter FN:"); scanf_s("%ld",&st->FN);
gets_s(bf);
}
void CountStudName(student*st){
int count, j, n, i;
char name1[31];
int FreqArr[31];
strcpy(st->name,name1);
printf("Search name occurrence: "); gets(name1);
for (i=0; i<BrS; i++)
FreqArr[i]=-1;
count=1;
for (j=i+1;j<BrS;j++)
{
if (strcmp(st->name,name1)==0)j ++;
count++;
FreqArr[j]=count;
}
printf("The searched name is %d times present\n", count);
}
void main() {
int i;
printf("Enter number of students:"); scanf_s("%d",&BrS);
gets_s(bf);
printf("-------------------\n");
for (i=0; i<BrS;i++) readStud(&MasStud[i]);
printf("-------------------\n");
CountStudName(&MasStud[i]);
_getch();
}
预先感谢您的帮助。
首先你应该知道 VS 默认会为你创建一个 .cpp
空项目,如果你不创建 .c
项目你将不会收到很多重要的错误。(我认为您正在使用 .cpp
)
如果你想使用 student
而不是 struct student
你应该使用 typedef
(如果你在 .cpp
项目中编码你不会收到任何错误错误。
typedef struct student {
char name[31];
char ID[11];//personal code
long FN;//student number
}student;
您还需要向 gets
发送两个参数,看 char*_Buffer
和 rsize_t _Size
。您只发送一个,但无论如何使用 gets
是不安全的 fgets
或 scanf
如下所示:
scanf("%31s",st->name)//this will prevent buffer overflow
至于你的CountStudName
函数在你调用的时候注意,你已经在MasStud
这里遍历了数组for (i=0; i<BrS;i++)
。因此,当您发送 CountStudName(&MasStud[i]);
函数时,该元素中不会存储任何特殊内容。
我重写了这个函数来取一个名字并搜索它并打印有多少学生有这个名字:
void CountStudName(student* st) {
int count, j, n, i;
char name1[31];
printf("Search name occurrence: ");
scanf("%31s", name1);
count = 0;
for (j = 0; j < BrS; j++)
{
if (strcmp(st[j].name, name1) == 0)
{
count++;
}
}
printf("The searched name is %d times present\n", count);
}
你应该在 main
中这样称呼它:CountStudName(MasStud);
我有一个函数有问题。我有一个包含学生信息的结构,如姓名、身份证号码、标记等。我能够阅读和打印所有需要的内容,但问题是当我想计算输入的相等名称时。例如,如果我输入计数名称 Iva,它出现了 3 次,则计数为 3。它 returns 0 是我到目前为止使用的。这是代码:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
struct student {
char name [31];
char ID[11];//personal code
long FN;//student number
};
int BrS;
student MasStud[35]; //array for number of students
char bf[5];
void readStud(student *st){
int i;
printf("-------------------\n");
printf("Enter name of a student:\n"); gets_s(st->name);
printf("Enter ID");gets_s(st->ID);
printf("Enter FN:"); scanf_s("%ld",&st->FN);
gets_s(bf);
}
void CountStudName(student*st){
int count, j, n, i;
char name1[31];
int FreqArr[31];
strcpy(st->name,name1);
printf("Search name occurrence: "); gets(name1);
for (i=0; i<BrS; i++)
FreqArr[i]=-1;
count=1;
for (j=i+1;j<BrS;j++)
{
if (strcmp(st->name,name1)==0)j ++;
count++;
FreqArr[j]=count;
}
printf("The searched name is %d times present\n", count);
}
void main() {
int i;
printf("Enter number of students:"); scanf_s("%d",&BrS);
gets_s(bf);
printf("-------------------\n");
for (i=0; i<BrS;i++) readStud(&MasStud[i]);
printf("-------------------\n");
CountStudName(&MasStud[i]);
_getch();
}
预先感谢您的帮助。
首先你应该知道 VS 默认会为你创建一个 .cpp
空项目,如果你不创建 .c
项目你将不会收到很多重要的错误。(我认为您正在使用 .cpp
)
如果你想使用 student
而不是 struct student
你应该使用 typedef
(如果你在 .cpp
项目中编码你不会收到任何错误错误。
typedef struct student {
char name[31];
char ID[11];//personal code
long FN;//student number
}student;
您还需要向 gets
发送两个参数,看 char*_Buffer
和 rsize_t _Size
。您只发送一个,但无论如何使用 gets
是不安全的 fgets
或 scanf
如下所示:
scanf("%31s",st->name)//this will prevent buffer overflow
至于你的CountStudName
函数在你调用的时候注意,你已经在MasStud
这里遍历了数组for (i=0; i<BrS;i++)
。因此,当您发送 CountStudName(&MasStud[i]);
函数时,该元素中不会存储任何特殊内容。
我重写了这个函数来取一个名字并搜索它并打印有多少学生有这个名字:
void CountStudName(student* st) {
int count, j, n, i;
char name1[31];
printf("Search name occurrence: ");
scanf("%31s", name1);
count = 0;
for (j = 0; j < BrS; j++)
{
if (strcmp(st[j].name, name1) == 0)
{
count++;
}
}
printf("The searched name is %d times present\n", count);
}
你应该在 main
中这样称呼它:CountStudName(MasStud);