第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

怎樣只把int型數(shù)據(jù)讀出? 只把int型數(shù)據(jù)讀到一個(gè)int型的數(shù)組里

怎樣只把int型數(shù)據(jù)讀出? 只把int型數(shù)據(jù)讀到一個(gè)int型的數(shù)組里

C++
青春有我 2023-03-05 09:05:39
1 2 3 43 4 5 61.22 2.22 3.33 4.11·
查看完整描述

2 回答

?
月關(guān)寶盒

TA貢獻(xiàn)1772條經(jīng)驗(yàn) 獲得超5個(gè)贊

/*
假如要讀取文件chengji.txt中的數(shù)據(jù)。
文件中數(shù)據(jù)如下:
學(xué)生編號(hào) 數(shù)學(xué) 英語
1 80 90
2 66 67
怎樣求各學(xué)生的平均成績和總的平均成績
*/

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;

int main()
{
string line;
int head=0,count=0,num;
float math,english,sum_math=0,sum_english=0,average;

ifstream ifs("chengji.txt");
if(!ifs) return -1;

ofstream ofs("chengji_result.txt");
if(!ofs) return -2;

while(getline(ifs,line))
{
istringstream is(line);
if(head==0)
{
//跳過第一行的表頭
head=1;
continue;
}
is>>num>>math>>english;
if(count==0)
{
ofs<<"學(xué)生編號(hào)\t平均成績"<<endl;
}
ofs<<num<<"\t"<<(math+english)/2<<endl;
sum_math+=math;
sum_english+=english;
count++;
}

if(count>0)
{
ofs<<endl;
ofs<<"數(shù)學(xué)平均成績:"<<sum_math/count<<endl;
ofs<<"英語平均成績:"<<sum_english/count<<endl;
}

ifs.close();
ofs.close();
return 0;
}

 


查看完整回答
反對(duì) 回復(fù) 2023-03-08
?
慕后森

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超5個(gè)贊

fscanf 函數(shù)可以從文件按照你的格式讀取文件數(shù)據(jù)

但是,請(qǐng)必須保證你的文件內(nèi)容和你所期望讀取的數(shù)據(jù)格式是一致的

如果你想從文件讀取一個(gè) float 和一個(gè) int,可以像這樣子:


float fvar = 0.0f;int ivar = 0; // 假設(shè) file 是一個(gè)有效的文件指針 ...fscanf( file, "%f %d", & fvar, & ivar );


查看完整回答
反對(duì) 回復(fù) 2023-03-08
  • 2 回答
  • 0 關(guān)注
  • 111 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)