C++ 实验四

来源:互联网 发布:博优化纤 官网 编辑:程序博客网 时间:2024/06/04 00:36

//极坐标转换直角坐标;#include <iostream>#include <math.h>#define K (0.017453292519943295769236907684886l)using namespace std;void convert(double &x, double &y, double radius, double phi);void convert(double *x, double *y, double radius, double phi);int main(void){double radius, phi;double x, y;cout << "plz input radius: ";cin >> radius;cout << "plz input phi: ";cin >> phi;convert(x, y, radius, phi);cout << "the answer of converting is: ";cout << '(' << x << ',' << y << ')' << endl;cout << '\n' << endl;convert(&x, &y, radius, phi);cout << "the answer of converting is: ";cout << '(' << x << ',' << y << ')' << endl;return 0;}void convert(double &x, double &y, double radius, double phi){x = radius * cos(phi * K);y = radius * sin(phi * K);}void convert(double *x, double *y, double radius, double phi){*x = radius * cos(phi * K);*y = radius * sin(phi * K);}//复数运算#include <iostream>using namespace std;struct cNum product(float hdu, struct cNum ssta);struct cNum product(struct cNum ssta, struct cNum isa);struct cNum{float real;float image;};int main(void){struct cNum isa, ssta, answer;float hdu;cout << "plz input a float number: ";cin >> hdu;cout << "plz input the real part of complex number: ";cin >> ssta.real;cout << "plz input the image part of complex number: ";cin >> ssta.image;answer = product(hdu, ssta);cout << "the answer is: ";cout << answer.real << '+' << answer.image << 'i' << endl;cout << "plz input another real part of complex number: ";cin >> isa.real;cout << "plz input another image part of complex number: ";cin >> isa.image;answer = product(ssta, isa);cout << "the answer is: ";cout << answer.real << '+' << answer.image << 'i' << endl;return 0;}struct cNum product(float hdu, struct cNum ssta){struct cNum answer;answer.real = hdu * ssta.real;answer.image = hdu * ssta.image;return answer;}struct cNum product(struct cNum ssta, struct cNum isa){struct cNum answer;answer.real = ssta.real * isa.real - ssta.image * isa.image;answer.image = ssta.real * isa.image + ssta.image * isa.real;return answer;}//计算生物节指数#include <iostream>#include <math.h>#include <time.h>using namespace std;class man{public:void computeday(int year, int month, int day);double getSum(double p, double e, double m);double compute_physiological_index(int age);double compute_emotional_index(int age);double compute_mentality_index(int age);man(int year, int month, int day);~man();int age;private:int year, month, day;double sum;double phy_index, emo_index, men_index;};int main(void){int year, month, day;cout << "plz input birth year of the douchebag: ";cin >> year;cout << "plz input birth month of the douchebag: ";cin >> month;cout << "plz input birth day of the douchebag: ";cin >> day;man *douchebag = new man(year, month, day);douchebag -> man::computeday(year, month, day);cout << "the index is: ";cout << douchebag -> getSum(douchebag -> man::compute_physiological_index(douchebag -> age),douchebag -> man::compute_emotional_index(douchebag -> age),douchebag -> man::compute_mentality_index(douchebag -> age)) << endl;return 0;}void man::computeday(int year, int month, int day){struct tm * currentTime;time_t ssta;int myear_count = 0, backup_year = year, totalday = 0, i = 0;ssta = time(NULL);currentTime = localtime(&ssta);int real_year = currentTime->tm_year + 1900;int real_month = currentTime->tm_mon + 1;const int daysamonth[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};for(; backup_year < real_year; backup_year++){if((backup_year%4==0 && backup_year%100!=0) || backup_year%400==0)myear_count++;}for(i = 1; i < real_month; i++){totalday += daysamonth[i];}for(i = 12; i > month; i--){totalday += daysamonth[i];}totalday += daysamonth[month] - day; totalday += (real_year - year - 1) * 365 + currentTime->tm_mday + myear_count;if((year%4==0 && year%100!=0) || year%400==0)totalday += 1;age = totalday;}double man::getSum(double p, double e, double m){return p + e + m;}double man::compute_physiological_index(int age){const double PI = 3.1415926;const double STEP = 23 / 2 / PI;int StepInPeriod = age % 23;phy_index = sin(StepInPeriod * STEP);return phy_index;}double man::compute_emotional_index(int age){const double PI = 3.1415926;const double STEP = 28 / 2 / PI;int StepInPeriod = age % 28;emo_index = sin(StepInPeriod * STEP);return emo_index;}double man::compute_mentality_index(int age){const double PI = 3.1415926;const double STEP = 33 / 2 / PI;int StepInPeriod = age % 33;men_index = sin(StepInPeriod * STEP);return men_index;}man::man(int year, int month, int day){this -> year = year;this -> month = month;this -> day = day;}man::~man(){cout << "this douchebag has been killed~" << endl;}

本次的实验内容十分糟糕.

第一题调教sin()函数也是费了很大功夫, 至于引用的做法形式上比指针简单, 毕竟是指针是老相好.

第二题没有什么特别之处, 结构体作为参数的调用也是可以把数据组织起来相当方便.

第三题啊第三题!

生物节指数到底是什么啊! 从小到大听都没听说过好吗!

在computeday()函数中为了计算出生到今日的实际天数, 加入了头文件<time.h>, 并调用期中的localtime()函数获得了本机的日期用于计算.

特别注意的一点是返回的tm -> year是本机年份减去1900的数值, 故需要手动加上1900获得实际的本机年即real_year,

                                            tm -> mon是本机月份减去1, 同理.

关于三类指数的计算关键在于把不同天数的周期映射至sin()函数中, 即可求出当天的实际值.

使用动态创建类的方法完成了本次任务, 感觉相当不适应类中private数据的操作, 对于各类函数的调用亦感十分麻烦, 希望今后多操作熟悉起来.



原创粉丝点击