编程打卡:面向对象程序设计测试

#include <iostream>
#include <iomanip>
#include <string>
#include <bitset>
using namespace std;
int main () {
    int x;
    cin >> oct >> x;
    cout << dec << x << endl;
    cout << setw(20) << setfill('*') << "I am a student!" << endl;
    double pi = 3.1415926;
    cout << setprecision(8) << pi << endl;
    cout << setprecision(6) << pi << endl;
    cout << setprecision(4) << pi << endl;
    bitset<64> bin(*((unsigned long long*)&pi));
    cout << bin << endl;
}
 
#include <iostream>
#include <iomanip>
#include <string>
#include <bitset>
using namespace std;
int main () {
    int x;
    cin >> oct >> x;
    cout << dec << x << endl;
    cout.width(20);
    cout.fill('*');
    cout.flags(ios::left);
    cout << "I am a student!" << endl;
    double pi = 3.1415926;
    cout.precision(8);
    cout.flags(ios::fixed);
    cout << pi << endl;
    cout.precision(6);
    cout.flags(ios::fixed);
    cout << pi << endl;
    cout.precision(4);
    cout.flags(ios::fixed);
    cout << pi << endl;
    bitset<64> bin(*((unsigned long long*)&pi));
    cout << bin << endl;
}
 
#include <iostream>
#include <string>
using namespace std;
int main()
{
    char str1[20], str2[20], str3[20];
    cin.get(str1, 20, '/');
    cout << str1 << endl;
    cin.get(str2, 20, '/');
    cout << str2 << endl;
    cin.get(str3, 20, '/');
    cout << str3 << endl;
    return 0;
}
 
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;

int main()
{
    ofstream outfile("Prime.txt");
    if (!outfile)
    {
        cerr << "error: unable to open output file: "
             << "Prime.txt" << endl;
        return -1;
    }

    outfile << 2 << endl;
    for (int i = 3; i < 1000; i += 2)
    {
        bool isPrime = true;
        for (int j = 3; j <= sqrt(i); j += 2)
        {
            if (i % j == 0)
            {
                isPrime = false;
                break;
            }
        }
        if (isPrime)
            outfile << i << endl;
    }

    outfile.close();
    return 0;
}

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

int main()
{
    string filename;
    cout << "Please input the filename: ";
    cin >> filename;
    ifstream infile(filename);
    if (!infile)
    {
        cout << "Error opening " << filename << endl;
        return -1;
    }
    int n;
    infile >> n;
    double x, y, z;
    for (int i = 0; i < n; i++)
    {
        infile >> x >> y >> z;
        cout << x << " " << y << " " << z << endl;
    }
    infile.close();
    return 0;
}
 
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct Teacher {
    int id;
    string name;
    char gender;
};
int main () {
    ofstream outfile("teacher.dat", ios::binary);
    int N;
    cin >> N;
    for (int i = 0; i < N; i++) {
        Teacher t;
        cin >> t.id >> t.gender >> t.name;
        outfile << i + 1 << " " << t.id << t.name << t.gender << endl;
    }
    outfile.close();
}
 
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;

int main()
{
    ifstream in("f1.txt", ios::in | ios::binary);
    ofstream out("f2.txt", ios::out | ios::binary);
    if (!in || !out)
    {
        cout << "Error opening file!" << endl;
        return 0;
    }
    int a[100];
    int i = 0;
    while (in.read((char *)&a[i], sizeof(int)))
    {
        i++;
    }
    sort(a, a + i);
    for (int j = 0; j < i; j++)
    {
        out.write((char *)&a[j], sizeof(int));
        cout << a[j] << " ";
    }
    cout << endl;
    in.close();
    out.close();
    return 0;
}
 
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    ofstream outfile("M99.txt");
    if (!outfile)
    {
        cerr << "open error!" << endl;
        abort();
    }
    for (int i = 1; i <= 9; i++)
    {
        for (int j = 1; j <= i; j++)
            outfile << j << "*" << i << "=" << i * j << "\t";
        outfile << endl;
    }
    outfile.close();
    return 0;
}
 
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include <cctype>
using namespace std;

int main()
{
    ifstream in("hello.cpp");
    if (!in)
    {
        cerr << "open file error!" << endl; 
        return -1;
    }
    string line;
    vector<string> words;
    while (getline(in, line))
    {
        string::size_type beg = 0, end;
        while (beg != line.size())
        {
            while (beg != line.size() && isspace(line[beg]))
                ++beg;
            end = beg;
            while (end != line.size() && !isspace(line[end]))
                ++end;
            if (beg != end)
                words.push_back(line.substr(beg, end - beg));
            beg = end;
        }
    }
    cout << "words: " << words.size() << endl;
    sort(words.begin(), words.end());
    vector<string>::iterator end_unique = unique(words.begin(), words.end());
    words.erase(end_unique, words.end());
    cout << "unique words: " << words.size() << endl;
    return 0;
}
 
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
    ofstream outfile("testl.txt");
    if (!outfile)
    {
        cerr << "open error!" << endl;
        abort();
    }
    outfile << "已成功写人文件!" << endl;
    outfile.close();
    return 0;
}
 
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
    ifstream infile("test1.txt");
    if (!infile)
    {
        cout << "open error!" << endl;
        return 1;
    }
    
    string s;
    while (getline(infile, s))
    {
        cout << s << endl;
    }
}
 
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
    ofstream outfile("testl.txt", ios::app);
    if (!outfile)
    {
        cerr << "open error!" << endl;
        abort();
    }
    outfile << "已成功添加字符!" << endl;
    outfile.close();

    ifstream infile("testl.txt");
    if (!infile)
    {
        cerr << "open error!" << endl;
        abort();
    }
    string str;
    while (getline(infile, str))
    {
        cout << str << endl;
    }
    infile.close();
    return 0;
}
 
#include <iostream>
#include <fstream>
using namespace std;
class Dog
{
private:
    int weight;
    int age;
public:
    Dog(int w, int a):weight(w), age(a){}
    void show()
    {
        cout << "weight: " << weight << " age: " << age << endl;
    }
    void save(ofstream &out)
    {
        out.write((char*)&weight, sizeof(weight));
        out.write((char*)&age, sizeof(age));
    }
    void load(ifstream &in)
    {
        in.read((char*)&weight, sizeof(weight));
        in.read((char*)&age, sizeof(age));
    }
};
int main()
{
    Dog dog1(5, 10);
    ofstream out("dog.dat", ios::binary);
    dog1.save(out);
    out.close();
    Dog dog2(0, 0);
    ifstream in("dog.dat", ios::binary);
    dog2.load(in);
    in.close();
    dog2.show();
    return 0;
}
 
#include <iostream>
using namespace std;
int main()
{
    int a;
    cout << "请输入一个十进制整数:";
    cin >> a;
    cout << "十进制:" << a << endl;
    cout << "八进制:" << oct << a << endl;
    cout << "十六进制:" << hex << a << endl;
    return 0;
}
 
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{   
    string s1, s2;
    cin >> s1 >> s2;
    ifstream infile(s1);
    ofstream outfile(s2);
    string s;
    int i = 1;
    while (getline(infile, s))
    {
        outfile << i << " " << s << endl;
        i++;
    }
    infile.close();
    outfile.close();
    return 0;
}
 
#include <iostream>
#include <fstream>
#include <string>
#include <map>
using namespace std;
int main()
{
    wifstream in("hello.txt");
    wofstream out("result.txt");
    map<wchar_t, int> m;
    wchar_t c;
    while (in >> c)
    {
        m[c]++;
    }
    for (map<wchar_t, int>::iterator iter = m.begin(); iter != m.end(); iter++)
    {
        out << iter->first << L" " << iter->second << endl;
    }
    return 0;
}
posted @ 2023-05-18 22:19  satou_matsuzaka  阅读(19)  评论(0编辑  收藏  举报

This is a Test

メイドノココロハ アヤツリドール