C Primer Plus(第五版) 第十四章 课后习题 6

2024-03-17 11:18

本文主要是介绍C Primer Plus(第五版) 第十四章 课后习题 6,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

提前在项目目录下建立信息文件。

#include <string.h>
#include <stdio.h>
#include <stdlib.h>

#define MAX_char 20
#define MAX_P 20
struct baseball
{
 int NO; //0~18
 char First[MAX_char];
 char Last[MAX_char];
 int games;
 int hit;
 int run;
 int rbi;
 
 float sucess;
};

void init(struct baseball *a,int num);
int main(int argc, char **argv)
{
 printf("hello world\n");
 
 FILE * in;
 char words[20];
 int count=0;//0~6
 int i=0;//0~5
 int index;
 struct baseball b[19];//0~18
 init(b,19);
 //FILE * out;
 char a[20]="BaseBall";
 strcat(a,".txt");
 if((in=fopen(a,"a+"))==NULL)
 {
  fputs("Can't opent BaseBall.txt.\n",stderr);
  exit(1);
 }
 while(fscanf(in,"%s",words)==1)
 {
  
   //(1)
  if(count==0)
  {
   index=atoi(words);
   b[index].NO=atoi(words);
   i=index;
  }
  if(count==1) strcpy(b[i].First,words);
  if(count==2) strcpy(b[i].Last,words);
  if(count==3) b[i].games+=atoi(words);
  if(count==4) b[i].hit+=atoi(words);
  if(count==5) b[i].run+=atoi(words);
  if(count==6) b[i].rbi+=atoi(words);
  count++;
  if(count%7==0)
  {
   count=0;
   //caculate sucess
   b[i].sucess=b[i].hit/(float)b[i].games;
  }
  //(2)
  /*switch(count){
  case 0:b[i].NO=atoi(words);break;
  case 1:strcpy(b[i].First,words);break;
  case 2:strcpy(b[i].Last,words);break;
  case 3:b[i].games=atoi(words);break;
  case 4:b[i].hit=atoi(words);break;
  case 5:b[i].run=atoi(words);break;
  case 6:b[i].rbi=atoi(words);break;
  default:break;
   //puts(words);
  }
  count++;
  if(count%7==0)
  {
   count=0;
   i++;
  }*/
 }
 
 
 i=0;
 while(i<19)
 {
 
  if(b[i].NO!=-1)
  {
   printf("%s %s \nNO:%d \nGames:%d \nHit:%d \nRun:%d \nRBI:%d\nSUCESS:%.2f\n",b[i].First,b[i].Last,b[i].NO,b[i].games,b[i].hit,b[i].run,b[i].rbi,b[i].sucess);
   puts("\n");
  }
 i++;
 }
 fclose(in);
 return 0;
}
void init(struct baseball *a,int num)
{
 int count=0;
 while(count++<num)
 {
  a->NO=-1;
  strcpy(a->First,"NULL");
  strcpy(a->Last,"NULL");
  a->games=0;
  a->hit=0;
  a->run=0;
  a->rbi=0;
  a->sucess=0.0;
  a++;
 }
}



这篇关于C Primer Plus(第五版) 第十四章 课后习题 6的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/818792

相关文章

将Mybatis升级为Mybatis-Plus的详细过程

《将Mybatis升级为Mybatis-Plus的详细过程》本文详细介绍了在若依管理系统(v3.8.8)中将MyBatis升级为MyBatis-Plus的过程,旨在提升开发效率,通过本文,开发者可实现... 目录说明流程增加依赖修改配置文件注释掉MyBATisConfig里面的Bean代码生成使用IDEA生

Spring Boot + MyBatis Plus 高效开发实战从入门到进阶优化(推荐)

《SpringBoot+MyBatisPlus高效开发实战从入门到进阶优化(推荐)》本文将详细介绍SpringBoot+MyBatisPlus的完整开发流程,并深入剖析分页查询、批量操作、动... 目录Spring Boot + MyBATis Plus 高效开发实战:从入门到进阶优化1. MyBatis

Spring Boot结成MyBatis-Plus最全配置指南

《SpringBoot结成MyBatis-Plus最全配置指南》本文主要介绍了SpringBoot结成MyBatis-Plus最全配置指南,包括依赖引入、配置数据源、Mapper扫描、基本CRUD操... 目录前言详细操作一.创建项目并引入相关依赖二.配置数据源信息三.编写相关代码查zsRArly询数据库数

mybatis-plus分页无效问题解决

《mybatis-plus分页无效问题解决》本文主要介绍了mybatis-plus分页无效问题解决,原因是配置分页插件的版本问题,旧版本和新版本的MyBatis-Plus需要不同的分页配置,感兴趣的可... 昨天在做一www.chinasem.cn个新项目使用myBATis-plus分页一直失败,后来经过多方

mybatis-plus 实现查询表名动态修改的示例代码

《mybatis-plus实现查询表名动态修改的示例代码》通过MyBatis-Plus实现表名的动态替换,根据配置或入参选择不同的表,本文主要介绍了mybatis-plus实现查询表名动态修改的示... 目录实现数据库初始化依赖包配置读取类设置 myBATis-plus 插件测试通过 mybatis-plu

MyBatis-Plus中Service接口的lambdaUpdate用法及实例分析

《MyBatis-Plus中Service接口的lambdaUpdate用法及实例分析》本文将详细讲解MyBatis-Plus中的lambdaUpdate用法,并提供丰富的案例来帮助读者更好地理解和应... 目录深入探索MyBATis-Plus中Service接口的lambdaUpdate用法及示例案例背景

MyBatis-Plus中静态工具Db的多种用法及实例分析

《MyBatis-Plus中静态工具Db的多种用法及实例分析》本文将详细讲解MyBatis-Plus中静态工具Db的各种用法,并结合具体案例进行演示和说明,具有很好的参考价值,希望对大家有所帮助,如有... 目录MyBATis-Plus中静态工具Db的多种用法及实例案例背景使用静态工具Db进行数据库操作插入

C++ Primer 标准库vector示例详解

《C++Primer标准库vector示例详解》该文章主要介绍了C++标准库中的vector类型,包括其定义、初始化、成员函数以及常见操作,文章详细解释了如何使用vector来存储和操作对象集合,... 目录3.3标准库Vector定义和初始化vector对象通列表初始化vector对象创建指定数量的元素值

springboot3.4和mybatis plus的版本问题的解决

《springboot3.4和mybatisplus的版本问题的解决》本文主要介绍了springboot3.4和mybatisplus的版本问题的解决,主要由于SpringBoot3.4与MyBat... 报错1:spring-boot-starter/3.4.0/spring-boot-starter-

mybatis和mybatis-plus设置值为null不起作用问题及解决

《mybatis和mybatis-plus设置值为null不起作用问题及解决》Mybatis-Plus的FieldStrategy主要用于控制新增、更新和查询时对空值的处理策略,通过配置不同的策略类型... 目录MyBATis-plusFieldStrategy作用FieldStrategy类型每种策略的作