You are on page 1of 4

//Tính tổng các số chứa trong file trên một dòng

#include <cmath>
void calSum(string fileName) {
long long S=0;
string a;
ifstream f;
f.open(fileName);
while(getline(f,a,' ')){ //Trong khi chưa hết file
long long num1 = 0;
int l1 = a.length();
//Chuyển chuỗi về dạng số
for(int i = l1 - 1; i >= 0; --i){
num1 += (int)(a[i] - '0') * pow(10, l1-i-1);
}
S += num1;
}
f.close();
cout<<S;
}
-----------------------------------------------------------------
//Capslock all char of string
/*Viết hàm void uppercase(string output) để đọc vào chuỗi S từ bàn phím, sau đó
chuyển tất các ký tự trong chuỗi S thành ký tự viết HOA và xuất kết quả ra file
output.
Chú ý: chỉ thay đổi các chữ cái in thường, các kí tự khác sẽ được giữ nguyên.
Đầu vào:
Biến "output" chứa tên file dùng để xuất kết quả.
Đầu ra:
Hàm đọc chuỗi S từ bàn phím và xử lý chuỗi như mô tả. Sau đó ghi chuỗi đã xử lý vào
file có tên được chứa trong biến “output” (hàm không trả về kết quả).
*/
void uppercase(string output) {
string s;
getline(cin,s);
string outstr;
char c;
ofstream f(output);
for (int n=0; n <= s.length()-1; n++){
if (97<=s[n]&&s[n]<=122){
c = (s[n] - 32);
outstr = outstr + c;
}
else outstr = outstr + s[n];//Nếu kí tự đã in hoa sẵn thì dùng lại
}
f<<outstr;
f.close();
}
-----------------------------------------------------------------
/*Viết hàm void threeChars(string fileName) đọc vào các hàng từ một file có đuôi
txt, mỗi hàng chứa một chuỗi có 3 kí tự. Xác định xem 3 ký tự trên mỗi hàng có đúng
thứ tự bảng chữ cái (ASCII) hay không, nếu đúng xuất ra "true", ngược lại xuất ra
"false". Chương trình sẽ lặp cho đến khi 3 ký tự đọc vào là "***".*/
void threeChars(string fileName) {
string str;
ifstream f;
f.open(fileName.c_str());
getline(f,str);
bool ascii = true;
while (str != "***"){
for (int i=0; i<=1; i++){
if (str[i]>str[i+1])
ascii = false;
}
getline(f,str);
if (ascii==true) cout<<"true"<<endl;
else cout<<"false"<<endl;
ascii=true;
}
f.close();
}
/*
Test:
123
abc
aab
***
Result:
true
true
true
*/
-----------------------------------------------------------------
/*Viết hàm void process(string fileName) đọc một file đuôi txt, dòng đầu gồm 2 số N
và M cách nhau bởi 1 khoảng trắng.
N dòng theo sau, mỗi dòng gồm M số thực cách nhau bởi 1 khoảng trắng.
Trả về giá trị lớn nhất của mỗi dòng và giá trị lớn nhất trong tất cả các số.
Các thư viện đã được include: iostream, fstream, string.
Đầu vào:
Biến "fileName" là tên file chứa dữ liệu đầu vào.
Đầu ra:
Giá trị lớn nhất của mỗi dòng và giá trị lớn nhất của tất cả các số, cách nhau bởi
ký tự khoảng trắng.
*/
void process(string fileName) {
ifstream f(fileName.c_str());
string s;
f>>s;
long int a,b;
a = atof(s.c_str());
f>>s;
b = atof(s.c_str());
f>>s;
long double max1 = atof(s.c_str());
for (int j=2; j<=b; j++){ //Bước cơ bản đầu tiên là cho giá trị của max là phần
tử đầu tiên, không phải là giá trị 0*************Thường hay quên chỗ này.
f>>s;
long double num = atof(s.c_str());
if (num>=max1) max1=num;
}
cout<<max1<<" ";
long double max2;
max2 = max1;
for (int i=2; i<=a; i++){
f>>s;
long double max1 = atof(s.c_str());
for (int j=2; j<=b; j++){
f>>s;
long double num = atof(s.c_str());
if (num>=max1) max1=num;
}
cout<<max1<<" "; //In ra t?ng s? l?n nh?t c?a m?i dòng
if (max1>=max2) max2=max1;
}
if (a!=0&&b!=0&& a>0&& b>0)
cout<<max2; //In ra s? l?n nh?t trong các s? l?n nh?t
f.close();
}
-----------------------------------------------------------------
/*Write a function void studendGrading(string fileName) that reads a txt file, the
first line consists of a positive integer N, N lines follow each line containing 4
real numbers, respectively, the scores of the subjects NMDT, KTLT, DSA and PPL of N
students.
The student's grade point average (GPA) will be the average of the above 4 score
columns. Students will be graded based on the following scores:
Grade A if the test score is >= 8 and no subject is below 5.
Grade B if 8 > GPA >= 6.5 and no subject below 5.
Grade C if 6.5 > GPA >= 5 and no subject below 5.
Grade D for the remaining cases.
Determine the number of students of each category and output the results to the
screen.
*/
float DTB(string a, string b, string c, string d){
return (atof(a.c_str())+atof(b.c_str())+atof(c.c_str())+atof(d.c_str()))/4.0;
}

bool fail (string a, string b, string c, string d){


if (atof(a.c_str())<5) return true;
if (atof(b.c_str())<5) return true;
if (atof(c.c_str())<5) return true;
if (atof(d.c_str())<5) return true;
return false;
}

void studentGrading(string fileName) {


ifstream f;
f.open(fileName.c_str());
string N;
getline(f,N,'\n');
int n = atof(N.c_str());
int A=0;int B=0; int C=0; int D=0;
for (int i=1; i<=n; i++){
string a, b, c, d;
getline(f,a,' ');
getline(f,b,' ');
getline(f,c,' ');
getline(f,d,'\n');
//cout<<DTB(a,b,c,d);
if (DTB(a,b,c,d) >=8 && fail(a,b,c,d)==false) A+=1;
else if (DTB(a,b,c,d) >= 6.5 && fail(a,b,c,d)==false) B+=1;
else if (DTB(a,b,c,d)>=5 && fail(a,b,c,d)==false) C+=1;
else D+=1;
}
f.close();
cout<<'A'<<' '<<A<<endl;
cout<<'B'<<' '<<B<<endl;
cout<<'C'<<' '<<C<<endl;
cout<<'D'<<' '<<D<<endl;
return;
}
-------------------------------------------------------------------
//Capslock all char of string
void uppercase(string output) {
string s;
getline(cin,s);
string outstr;
char c;
ofstream f(output);
for (int n=0; n <= s.length()-1; n++){
if (97<=s[n]&&s[n]<=122){
c = (s[n] - 32);
outstr = outstr + c;
}
else outstr = outstr + s[n];//Nếu kí tự đã in hoa sẵn thì dùng lại
}
f<<outstr;
f.close();
}

You might also like