程序在Dev-C++5.5.3版本运行

结构体的使用

给结构体赋值,打印出结构体中学生姓名,分数,平均分

#include <iostream>
#include <cassert>
#include <algorithm>
#include <vector>
#include <string.h>//不用.h的话可能下面的strcpy用不了
#include <iterator>
using namespace std;

int main()
{
struct Student //声明结构体类型Student
{

char name[20];
float score1;
float score2;
float score3;
}student1, student2; //定义两个结构体类型Student的变量student1,student2

strcpy(student1.name,"小杨");
student1.score1=60;
student1.score2=70;
student1.score3=80;

strcpy(student2.name,"小李");
student2.score1=96;
student2.score2=97;
student2.score3=98;
int a=0;
a= (student1.score1+student1.score2+student1.score3)/3;
cout<<"学生1姓名:"<< student1.name<<endl;
cout<<"科目1:"<< student1.score1<<endl;
cout<<"科目2:"<< student1.score2<<endl;
cout<<"科目3:"<< student1.score3<<endl;
cout<<"科目均分:"<< a<<endl;

a= (student2.score1+student2.score2+student2.score3)/3;

cout<<"学生2姓名:"<< student2.name<<endl;
cout<<"科目1:"<< student2.score1<<endl;
cout<<"科目2:"<< student2.score2<<endl;
cout<<"科目3:"<< student2.score3<<endl;
cout<<"科目均分:"<< a<<endl;
return 0;
}

-----------------------------------------------------------------------------------------------

C++完成对.txt文件读取,打印所有内容到屏幕上:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream in;
in.open("d:\\haha.txt");//文件每层间一定要用双斜杠隔开
if(!in)
{
cout<<"出错啦"<<endl;
return 1;
}
char ch;
while(!in.eof())
{
in.read(&ch,1);
get(a)

cout<<ch;}
}
in.close();
return 0;
}

-----------------------------------------------------------------------------------------------

读出.txt文件中学生姓名,分数,平均分

txt文件只需要带有空格,该程序即可区分,txt文件内容如下{

小羊 65 69 68
小李 94 89 98

}

#include <iostream>
#include <fstream>

using namespace std;
struct Student
{

char name[20];
int s1;
int s2;
int s3;
int average;
};
const int N=2;
int main( )
{

int i, stuNum=0;
Student stu[N];
ifstream infile("haha.txt",ios::in);
if(!infile)
{
cout<<"open error!"<<endl;
return 0;
}
i=0;
while(!infile.eof())
{
infile>>stu[i].name>>stu[i].s1>>stu[i].s2>>stu[i].s3;
stu[i].average=(stu[i].s1+stu[i].s2+stu[i].s3)/3;
++stuNum;
++i;
}
infile.close();
//display
for(i=0; i<stuNum; ++i)
{
cout<<stu[i].name<<endl;
cout<<"三科分别为:"<<stu[i].s1<<" "<<stu[i].s2<<" "<<stu[i].s3<<" "<<endl;
cout<<"平均分:"<<stu[i].average<<endl;

}
return 0;
}

C++入门程序作业2的更多相关文章

  1. C++入门程序作业3

    /* 输出n位数据的格雷码 The gray code is a binary numeral system where two successive values differ in only on ...

  2. C++入门程序作业1

    将一个int A[]={ ,  ,  ,}定义的可能重复的数字去掉重复的元素. 了解向量,容器如何使用,size,地址的关系,理解unique erase函数的返回值是什么参数 结果:将1,1,1,2 ...

  3. mybatis入门_mybatis基本原理以及入门程序

    一.传统jdbc存在的问题 1.创建数据库的连接存在大量的硬编码, 2.执行statement时存在硬编码. 3.频繁的开启和关闭数据库连接,会严重影响数据库的性能,浪费数据库的资源. 4.存在大量的 ...

  4. 1.struts2原理和入门程序

    Struts2是一个MVC的Web应用框架,是在Struts1和WebWork发展起来的,以WebWork为核心,采取拦截器机制来处理用户请求. 原理图: 分析步骤: 1.用户发送一个请求 2.请求的 ...

  5. springMVC2 1入门程序

    1入门程序 .1需求 实现商品列表查询 .2需要的jar包 使用spring3.2.0(带springwebmvc模块) .1前端控制器 在web.xml中配置: <?xml version=& ...

  6. struts2入门程序

    struts2入门程序 1.示例 搭建编程环境就先不说了,这里假设已经搭建好了编程环境,并且下好了strut2的jar包,接下来程序. 1.1 新建web项目 点击File->New->D ...

  7. Spring+SpringMVC+MyBatis深入学习及搭建(十二)——SpringMVC入门程序(一)

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6999743.html 前面讲到:Spring+SpringMVC+MyBatis深入学习及搭建(十一)——S ...

  8. springmvc(一) springmvc框架原理分析和简单入门程序

    springmvc这个框架真的非常简单,感觉比struts2还更简单,好好沉淀下来学习~ --WH 一.什么是springmvc? 我们知道三层架构的思想,并且如果你知道ssh的话,就会更加透彻的理解 ...

  9. python web入门程序

    python2.x web入门程序 #!/usr/bin/python # -*- coding: UTF-8 -*- # 只在python2.x 有效 import os #Python的标准库中的 ...

随机推荐

  1. GlusterFS分布式文件系统的使用

    glusterfs是一款开源的分布式文件系统. 它具备高扩展.高可用及高性能等特性,由于其无元数据服务器的设计,使其真正实现了线性的扩展能力,使存储总容量可轻松达到PB级别,支持数千客户端并发访问. ...

  2. BUAAOO-Second-Summary

    #目录 homework & class & trainning : 两次上机.三次作业.四周课堂 code analysis & review : 为什么我没有bug,为什么 ...

  3. Lua 循环与流程控制

    1.Lua 语言提供了以下几种循环处理方式: 循环类型 描述 while 循环 在条件为 true 时,让程序重复地执行某些语句.执行语句前会先检查条件是否为 true. for 循环 重复执行指定语 ...

  4. css中position 定位的兼容性,以及定位的使用及层级的应用

    一.首先我们来看看定位的兼容性,当然是在IE6.7但是现在大多数公司都已经不考虑了 我们就作为一个了解吧: 1.在IE67下,子元素有相对定位的话,父级的overflow:hidden包不住子元素 解 ...

  5. [转]imageMagick 在nodejs中报错Error: spawn identify ENOENT的解决方案

    同时还有 Error: Could not execute GraphicsMagick/ImageMagick 这个问题, 也参考了 https://blog.csdn.net/chenxinpen ...

  6. rtmp详解

    文件下载地址: 中文:https://files.cnblogs.com/files/bugutian/rtmp_specification_1.0_cn.zip 英文:http://www.adob ...

  7. 开源HUSTOJ

    hustoj -- 请一定认真看完本页再动手安装,以免无谓的折腾!====== 根据你选择的发行版不同,从下面三个脚本里选一个来用. <b>不要相信百度来的长篇大论的所谓教程,那些都是好几 ...

  8. 比较推荐学习Linux系统应该看的书籍

    对于如何学习Linux,我想大家多多少少会有自己的一些想法--不管是学过Linux的还是没有学过Linux的.学习,对于我们来说,应该不是一件陌生的事:从小学开始,然后中学.大学.乃至于读硕读博,可以 ...

  9. unity中多个门的开关动画保持独立性

    List<Animation> storeAnimation; public void Awake() { storeAnimation = new List<Animation&g ...

  10. Linux安装TeamViewer

    1.下载teamview centos版本 官网只有rpm版本,附件中即为官网下载的teamview最新版本 (下载tar包方式,我这里打不开teamviewer的界面,所以这里不推荐) 官方下载地址 ...