首页 > 编程语言 >编程打卡:面向对象程序设计测试

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

时间:2023-05-18 22:35:02浏览次数:42  
标签:std cout int 编程 namespace 面向对象 using 打卡 include

#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;
}

标签:std,cout,int,编程,namespace,面向对象,using,打卡,include
From: https://www.cnblogs.com/sugar-refinery/p/17413479.html

相关文章

  • 打卡
    1.问题:输出所有的“水仙花数”。所谓“水仙花数”是指一个3位数,其各位数字立方之和等于该数本身。例如,153是一个水仙花数,因为153=1³+5³+3³。2.思路:水仙花数的解题思路是把给出的某个三位数的个位、十位、百位分别拆分,并求其立方和。3.程序流程图: 4.代码实现:#include<ios......
  • C语言编程—常量
    常量是固定值,在程序执行期间不会改变。这些固定的值,又叫做字面量。常量可以是任何的基本数据类型,比如整数常量、浮点常量、字符常量,或字符串字面值,也有枚举常量。常量就像是常规的变量,只不过常量的值在定义后不能进行修改。常量可以直接在代码中使用,也可以通过定义常量来使用。整数......
  • 5.18打卡
    一、实验内容定义一个Dog类,包括体重和年龄两个数据成员及其成员函数,声明一个实例dog1,体重5,年龄10,使用I/O流把dog1的状态写入磁盘文件。再声明一个实例dog2,通过读取文件dog1的状态赋给dog2。分别用文本方式和二进制方式操作文件。二、实验代码#include<bits/stdc++.h>usingna......
  • 实验四 函数与异常处理编程
    print(sum)sum=42print(sum)definc(n):sum=n+1print(sum)returnsumsum=inc(7)+inc(7)print(sum)deffunc1(a,b,c,d,e,f):return[a,b,c,d,e,f]deffunc2(a,b,c,*,d,e,f):return[a,b,c,d,e,f]deffunc3(a,b,c,/,d,e,f):re......
  • 从C到C++:学习C++的高级语法、STL和面向对象编程
    好的,那我为您写一篇有关C++编程的文章,详情如下。标题:从C到C++:学习C++的高级语法、STL和面向对象编程开头:C++语言是对C语言的扩展,是一种面向对象的程序设计语言。它具有丰富的数据类型、函数模板、类模板、标准模板库等高级特性。掌握高级语法、STL和面向对象编程,可以使程序员更加高......
  • Java面向对象之构造方法
    方法重载Overload  1.概念:一个类中的一组方法 相同的方法名字 不同的参数列表 构成了方法重载参数的不同体现在 参数的个数 参数的类型 参数的顺序三个方面  2.作用:为了便于记忆和调用使得方法调用时更加的灵活  3.自己也可以设计方法重载   ......
  • 面向对象的编程(OOP)知识点总结
    软件构造期末考试将近,在此我将从OOP这一章节开始复习,记录下该章节中的重点内容。知识点概要:OOP基本概念:对象、类、属性、方法、接口和枚举OOP的独特特征:——封装与信息隐藏——继承与重写——多态、子类型、重载——静态与动态分派Java中一......
  • C++趣味编程
    分糖果1#include<iostream>2usingnamespacestd;3intmain()4{5inti,count=0;6inta[10]={10,2,8,22,16,4,10,6,14,20};7intb[10]={10,2,8,22,16,4,10,6,14,20};8do{9a[0]=b[0]/2+b[9]/2;10for(intj=1......
  • 5-18打卡
    递归写爬楼梯#include<stdio.h>//定义一个函数,用来打印每次爬的台阶数voidprint_steps(intsteps[],intn){printf("一种可能的方法是:");for(inti=0;i<n;i++){printf("%d",steps[i]);}printf("\n");}//定义一个递......
  • 每日打卡
    把真分数分解为埃及分数问题描述:古埃及人用的分数都是分子为一的分数,将真分数拆分成埃及分数问题分析:1如果分子为1直接输出2.分母是分子的倍数,化简后输出3.如果不能消去的话可以分出一个a/b+1和c出来代码:#include<stdio.h>intmain(){ longinta,b,c; printf("请输入a......