C语言及程序设计进阶例程-13 结构体数组及其应用

贺老师教学链接  C语言及程序设计进阶 本课讲解

结构体数组应用举例

#include <stdio.h>
#include <string.h>
typedef struct
{
    char name[20];
    int count;
} Person;
int main( )
{
    Person person[3]= {{"Li",0},{"Zhang",0},{"Fun",0}};
    int i,j;
    char name[20];
    //输入10人投票情况,并确定给3人中哪个计票
    for(i=0; i<10; i++)
    {
        scanf("%s", name);
        for(j=0; j<3; j++)
            if(strcmp(name,person[j].name)==0)
                person[j].count++;
    }
    printf("\nResult:\n");
    //输出计票结果
    for(i=0; i<3; i++)
    {
        printf("%s: %d\n", person[i].name, person[i].count);
    }
    return 0;
}
时间: 2024-08-02 15:14:20

C语言及程序设计进阶例程-13 结构体数组及其应用的相关文章

C语言及程序设计进阶例程-12 结构体成员的引用

贺老师教学链接  C语言及程序设计进阶 本课讲解 结构体作函数参数 #include <stdio.h> struct Student { int num; char name[20]; char sex; int age; double score; char addr[30]; }; void print(struct Student s) { printf("%d %s %c\n", s.num, s.name, s.sex); //可再加-- return; } i

C语言及程序设计进阶例程-15 指向结构体的指针

贺老师教学链接  C语言及程序设计进阶 本课讲解 指向结构体变量的指针的应用 #include <stdio.h> #include <string.h> struct Student { int num; char name[12]; char sex; float score; }; int main( ) { struct Student stu; stu.num=10301; strcpy(stu.name, "Wang Fun"); stu.sex='

C语言及程序设计进阶例程-11 体验结构体

贺老师教学链接  C语言及程序设计进阶 本课讲解 结构体类型变量的定义方法 #include <stdio.h> struct Student { int num; char name[20]; char sex; int age; float score; char addr[30]; }; int main( ) { struct Student student1, student2; printf("%d\n", sizeof(student1)); return 0

C语言及程序设计进阶例程-16 当结构体成员为指针

贺老师教学链接  C语言及程序设计进阶 本课讲解 有问题吗? #include <stdio.h> #include <string.h> struct Test { int x; char *str; }; int main() { struct Test a; a.x=100; char s[]="Hello"; strcpy(a.str,s); printf("%d %s\n", a.x, a.str); return 0; } 正解-

C语言及程序设计进阶例程-39 银行储蓄系统(第六版)开发

[贺老师教学链接]   [C语言及程序设计进阶]   [本课讲解]  bank.h /*注意:运行前要建立空文件account.bin*/ #ifndef BANK_H_INCLUDED #define BANK_H_INCLUDED #include <malloc.h> typedef struct { int account; //账号 int password; //密码 char name[10]; //用户名 double balance; //账户余额 int status; /

C语言及程序设计进阶例程-2 一个程序,多个文件

贺老师教学链接  C语言及程序设计进阶 本课讲解 演示:建立多文件的项目main.c #include <stdio.h> int max(int x,int y); int main( ) { int a,b,c; printf("输入两数:"); scanf("%d %d", &a, &b); c=max(a,b); printf("max=%d\n", c); return 0; } max.c int max(

C语言及程序设计进阶例程-21 银行储蓄系统(第五版)开发

贺老师教学链接  C语言及程序设计进阶 本课讲解 可以直接下载完整的项目文件bank.zip运行(点击打开链接,解压后打开项目,或者自建项目后加入源文件和头文件) 下面的代码,分别展示各个头文件.源文件:Bank.h #ifndef BANK_H_INCLUDED #define BANK_H_INCLUDED #define upNum 2000 //系统最多容纳的用户数 struct record { int account; //账号 int password; //密码 char nam

C语言及程序设计进阶例程-24 查找问题及其求解

贺老师教学链接 C语言及程序设计进阶 本课讲解 顺序查找 #include <stdio.h> #define SIZE 10 int main( ) { int d[SIZE]={34, 43, 98, 72, 12, 47, 31, 43, 1, 78}; /*也可以通过键盘输入等方式给出数据*/ int i; int key; /*key表示待查找数据*/ int index=-1; /*用index表示查找结果--关键字key出现的位置*/ printf("Input a k

C语言及程序设计进阶例程-14 开发一个电子词典

贺老师教学链接  C语言及程序设计进阶 本课讲解 开发一个电子词典(下载词库点击打开链接) #include <stdio.h> #include<string.h> #include<stdlib.h> //定义词条类 typedef struct { char english[20]; char chinese[30]; char word_class[10]; } Word; Word words[8000]; //将词典数组设置成全局的结构体数组 int wor