題解 | #學(xué)生基本信息輸入輸出#
學(xué)生基本信息輸入輸出
http://www.fangfengwang8.cn/practice/58b6a69b4bf943b49d2cd3c15770b9fd
#include <iostream> #include<cmath> #include <sstream> #include<iomanip> #include <vector> using namespace std; int main() { string str; // 學(xué)生ID和成績(jī)輸入字符串 getline(cin, str); // 從標(biāo)準(zhǔn)輸入獲取學(xué)生ID和成績(jī)輸入字符串 stringstream ss(str); // 將輸入字符串轉(zhuǎn)換為流 int stu_id; // 學(xué)生ID vector<double> scores(3); // 學(xué)生各科成績(jī),長(zhǎng)度為3 ss >> stu_id; // 從流中獲取學(xué)生ID for (int i = 0; i < 3; i++) { // 循環(huán)三次,獲取各科成績(jī) ss.ignore(); // 忽略流中的換行符 ss >> scores[i]; // 從流中獲取各科成績(jī),并保存到成績(jī)數(shù)組中 scores[i] = round(scores[i] * 100) / 100.0; // 四舍五入保留2位小數(shù) } cout << fixed << setprecision(2) << "The each subject score of No. " // 輸出學(xué)生ID和各科成績(jī) << stu_id << " is " << scores[0] << ", " << scores[1] << ", " << scores[2] << "." << endl; return 0; }