C语言编程。1)从键盘上输入6个整数放入数组a[6]中(2)并将各元素和平均值输出到一文本文件中保存

编写程序,要求:(1)从键盘上输入6个整数放入数组a[6]中(用while循环实现); (2)并将各元素和平均值输出到一文本文件中保存;(3)然后打开该文件,读取其中内容并显示在屏幕上。

第1个回答  2008-12-24
#include<iostream>
#include<fstream>
#include<string>
#include<vector>
using namespace std;
int main()
{
int a[6];
int i=0,sum=0;
int average;
cout<<"输入你想输入的六个数"<<endl;
while(i<6)
{
cin>>a[i];
sum=sum+a[i];
i++;
}
average=sum/6;
ofstream out("c:\\bbb.txt");
if(!out)
{
cout<<"file error!";
exit(1);
}
for (int j=0;j<6;j++)
{
out<<a[j]<<" ";
}
out<<average;
out.close();
ifstream in("c:\\bbb.txt");
vector<int> array;
int temp;
while(in>>temp)
{
array.push_back(temp);
}
for(int m=0;m<array.size();m++)
cout<<array[m]<<" ";
in.close();
return 0;
}
用C++写的只要变换一般的输入输出就OK了本回答被提问者采纳
第2个回答  2008-12-24
读写文件,仅供参考(将load.txt中的数字读出来写入write.txt)
#include <stdio.h>
main(){
FILE *out,*in;
int i,Q[100],m,a;
out=freopen("E:\load.txt","r",stdin);
in=freopen("E:\write.txt","aw",stdout); /*aw or w*/
m=0;
if(out){
while(scanf("%d",&a)!=EOF){
Q[m++]=a;
}
for(i=0;i<m;i++){
printf("%d ",Q[i]);
}
}
getchar();
}
第3个回答  2008-12-24
要用输入和输出流的!

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网