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-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类型每种策略的作

C++ Primer 多维数组的使用

《C++Primer多维数组的使用》本文主要介绍了多维数组在C++语言中的定义、初始化、下标引用以及使用范围for语句处理多维数组的方法,具有一定的参考价值,感兴趣的可以了解一下... 目录多维数组多维数组的初始化多维数组的下标引用使用范围for语句处理多维数组指针和多维数组多维数组严格来说,C++语言没

SpringBoot基于MyBatis-Plus实现Lambda Query查询的示例代码

《SpringBoot基于MyBatis-Plus实现LambdaQuery查询的示例代码》MyBatis-Plus是MyBatis的增强工具,简化了数据库操作,并提高了开发效率,它提供了多种查询方... 目录引言基础环境配置依赖配置(Maven)application.yml 配置表结构设计demo_st

解决mybatis-plus-boot-starter与mybatis-spring-boot-starter的错误问题

《解决mybatis-plus-boot-starter与mybatis-spring-boot-starter的错误问题》本文主要讲述了在使用MyBatis和MyBatis-Plus时遇到的绑定异常... 目录myBATis-plus-boot-starpythonter与mybatis-spring-b