C++_day2

作业:

  1. 整理思维导图
  2. 定义一个Student结构体,里面的成员有公有成员name、age,私有成员:score,从堆区连续分配3个结构体大小的空间,从键盘上输入3个学生的信息,分别存放到对应的位置上,按成绩的升序排序后输出三名学生的信息。


#include <iostream>

using namespace std;

typedef struct Student
{
public:
    string name;
    int age;
    
private:
    float score;
    
public:
    float set_score(float s)
    {
        score = s;
    }
    
    float get_score()
    {
        return score;
    }
    
};

int main()
{
    Student* p = new Student[3];
    
    for (int i=0; i<3; i++) {
        float s;
        cout << "请输入第" << i+1 << "个学生的name" << endl;
        cin >> p[i].name;
        
        cout << "请输入第" << i+1 << "个学生的age" << endl;
        cin >> p[i].age;
        
        cout << "请输入第" << i+1 << "个学生的score" << endl;
        cin >> s;
        p[i].set_score(s);
    }
    
    //冒泡排序
    for (int i=0;i<3-1;i++) {
        Student temp;
        for (int j=0;j<3-i-1; j++) {
            if(p[j].get_score() > p[j+1].get_score())
            {
                temp = p[j];
                p[j] = p[j+1];
                p[j+1] = temp;
            }
        }
        
    }
    
    for (int i=0; i<3;i++) {
        cout << "name:" << p[i].name <<endl;
        cout << "age:" << p[i].age <<endl;
        cout << "score:" << p[i].get_score() <<endl;
    }
    
    delete [] p;
    return 0;
}

#include <iostream> #include <cstdio> #include <cstring> using namespace std; const char* Haab_month_name[] = {"pop", "no", "zip", "zotz", "tzec", "xul", "yoxkin", "mol", "chen", "yax", "zac", "ceh", "mac", "kankin", "muan", "pax", "koyab", "cumhu", "uayet"}; int Tzolkin_day_number[20] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1, 2, 3, 4, 5, 6, 7}; const char *Tzolkin_day_name[] = {"imix", "ik", "akbal", "kan", "chicchan", "cimi", "manik", "lamat", "muluk", "ok", "chuen", "eb", "ben", "ix", "mem", "cib", "caban", "eznab", "canac", "ahau"}; struct Date { int day; int month; int year; }; // 把哈布历转换成天数 int haab_to_days(int day, const char* month, int year) { int total_days = day + (year * 365); int month_index; for (month_index = 0; month_index < 19; ++month_index) { if (strcmp(Haab_month_name[month_index], month) == 0) { break; } } total_days += month_index * 20; return total_days; } // 把天数转换成托尔金历 void days_to_tzolkin(int total_days, int* day_number, const char** day_name) { int tzolkin_day_count = total_days % 13; int tzolkin_day_index = tzolkin_day_count - 1; if (tzolkin_day_index < 0) tzolkin_day_index = 12; int tzolkin_day_number = Tzolkin_day_number[tzolkin_day_index]; const char *tzolkin_day_name = Tzolkin_day_name[(total_days % 20)]; *day_number = tzolkin_day_number; *day_name = tzolkin_day_name; } int main() { int N; cin >> N; cout << N << endl; for (int i = 0; i < N; ++i) { Date date; char dot; scanf("%d%c%d%s%d", &date.day, &dot, &date.month, Haab_month_name, &date.year); int total_days = haab_to_days(date.day, Haab_month_name, date.year); int tzolkin_day_number; const char *tzolkin_day_name; days_to_tzolkin(total_days, &tzolkin_day_number, &tzolkin_day_name); printf("%d %s %d\n", tzolkin_day_number, tzolkin_day_name, date.year); } return 0; }
06-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值