C++入门程序作业2
程序在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的更多相关文章
- C++入门程序作业3
/* 输出n位数据的格雷码 The gray code is a binary numeral system where two successive values differ in only on ...
- C++入门程序作业1
将一个int A[]={ , , ,}定义的可能重复的数字去掉重复的元素. 了解向量,容器如何使用,size,地址的关系,理解unique erase函数的返回值是什么参数 结果:将1,1,1,2 ...
- mybatis入门_mybatis基本原理以及入门程序
一.传统jdbc存在的问题 1.创建数据库的连接存在大量的硬编码, 2.执行statement时存在硬编码. 3.频繁的开启和关闭数据库连接,会严重影响数据库的性能,浪费数据库的资源. 4.存在大量的 ...
- 1.struts2原理和入门程序
Struts2是一个MVC的Web应用框架,是在Struts1和WebWork发展起来的,以WebWork为核心,采取拦截器机制来处理用户请求. 原理图: 分析步骤: 1.用户发送一个请求 2.请求的 ...
- springMVC2 1入门程序
1入门程序 .1需求 实现商品列表查询 .2需要的jar包 使用spring3.2.0(带springwebmvc模块) .1前端控制器 在web.xml中配置: <?xml version=& ...
- struts2入门程序
struts2入门程序 1.示例 搭建编程环境就先不说了,这里假设已经搭建好了编程环境,并且下好了strut2的jar包,接下来程序. 1.1 新建web项目 点击File->New->D ...
- Spring+SpringMVC+MyBatis深入学习及搭建(十二)——SpringMVC入门程序(一)
转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6999743.html 前面讲到:Spring+SpringMVC+MyBatis深入学习及搭建(十一)——S ...
- springmvc(一) springmvc框架原理分析和简单入门程序
springmvc这个框架真的非常简单,感觉比struts2还更简单,好好沉淀下来学习~ --WH 一.什么是springmvc? 我们知道三层架构的思想,并且如果你知道ssh的话,就会更加透彻的理解 ...
- python web入门程序
python2.x web入门程序 #!/usr/bin/python # -*- coding: UTF-8 -*- # 只在python2.x 有效 import os #Python的标准库中的 ...
随机推荐
- hsy单词
题意:略 在ac自动机上,一个节点出现的次数等于能通过fail到它的节点的次数之和.而叶节点就等于它被爬过的次数. #include <iostream> #include <cst ...
- myEclipse和eclipse从debug视图自动跳回default视图。
本来是吐槽文,找到了解决的插件,就改改标题了. debug的时候,可以从default视图自动跳转到debug视图,退出debug的时候,却不能自动切换回default视图. https://bugs ...
- windows WebStorm常用快捷键记录,常用的都在这儿找扒
~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Webstorm version 2018.2 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1. Ctrl + Shift + ...
- 关于mfc学习小记(bug小记)
关于控件问题 基本上出错的都是类型不为LPCTSTR 解决方式: 1.字符串转换 _T(str) 例:_T("你说的都对") 关于button控件 按照老旧的方式为其关联函数中添加 ...
- 下面程序的输出结果是____ A:11,10 B:11,11 C:10,10 D:10,11 int x=10; int y=x++; printf("%d,%d",(x++,y),y++);
下面程序的输出结果是____ A:11,10 B:11,11 C:10,10 D:10,11 int x=10; int y=x++; printf("%d,%d",(x++,y) ...
- 简易OA漫谈之工作流设计(四,怎么支持会签)
会签:就是一个步骤要所有人都审批完成才能完成. 上一步提交到会签步骤时,该步骤的每个人都能收到一条待办任务, 会签审批人每次提交时都检查该步骤所有会签人是否已经审批,如果全部审批就产生下一手任务. 演 ...
- Cognos命名空间不可用
1. 问题描述 启动Cognos失败,报错代码为0146. 2. 问题分析 namespace 配置有问题,检查configuration 3. 解决方案 如果检查不出问题,删除$COGNOS_HOM ...
- .net实现网易云音乐下载
客户端版的网易云音乐下载是需要vip的,网页版的虽然可以通过调试工具找到下载链接,但是用起来不是很方便,通过调试工具观察请求发现请求参数都是加密的,比如搜索歌曲的请求参数: 这个加密的实现肯定是写在j ...
- 针对小程序for循环绑定数据,实现toggle切换效果(交流QQ群:604788754)
如有更好的方法实现,可以留言或加群交流学习.谢谢(交流QQ群:604788754) WXML: <block wx:for="{{datanum}}" wx:for-inde ...
- ClientDataSet应用
最近维护一个项目,里面用到ClientDataSet,由于之前接触ClientDataSet比较少,所以这个星期补了一下关于ClientDataSet的知识,并在此记录下我所了解到的并应用到实际项目中 ...