繁體
|
簡體
Sclub交友聊天~加入聊天室當版主
(檢舉)
分享
新浪微博
QQ空间
人人网
腾讯微博
Facebook
Google+
Plurk
Twitter
Line
標題:
AOOP lab 3
[打印本頁]
作者:
Smallcat
時間:
2019-10-23 21:25
標題:
AOOP lab 3
head:
#ifndef RESISTOR_H
#define RESISTOR_H
using namespace std;
#include<string>
#include<iostream>
enum colorre{BLACK=0, BROWN=1, RED=2, ORANGE=3, YELLOW=4, GREEN=5, BLUE=6, VIOLET=7, GREY=8, WHITE=9};
enum colorto{SILVER=10, GOLD=5};
class resistor
{
colorre colorre1,colorre2,colorre3;
colorto colortole;
string color;
double tolerance;
public:
double r;
resistor();
resistor(double a);
~resistor();
void setresistor(string a);
void setresistor(double a);
void string_to_enum();
colorre toenum1(string a);
colorto toenum2(string a);
void outcolorresistor();
void computeresistor();
void outresistor();
resistor operator+(resistor other);
resistor operator||(resistor other);
};
#endif // RESISTOR_H
複製代碼
作者:
Smallcat
時間:
2019-10-23 21:25
cpp:
#include "resistor.h"
#include<iostream>
#include<string>
using namespace std;
resistor::resistor()
{
r = 1;
}
resistor::resistor(double a){
r = a;
}
resistor::~resistor(){
}
void resistor::setresistor(string s){
color = s;
}
void resistor::setresistor(double a){
r = a;
}
colorre resistor::toenum1(string a){
if(a=="BLACK"){
return BLACK;
}
if(a=="BROWN"){
return BROWN;
}
if(a=="RED"){
return RED;
}
if(a=="ORANGE"){
return ORANGE;
}
if(a=="YELLOW"){
return YELLOW;
}
if(a=="GREEN"){
return GREEN;
}
if(a=="BLUE"){
return BLUE;
}
if(a=="VIOLET"){
return VIOLET;
}
if(a=="GREY"){
return GREY;
}
if(a=="WHITE"){
return WHITE;
}
}
colorto resistor::toenum2(string a){
if(a=="SILVER"){
return SILVER;
}
if(a=="GOLD"){
return GOLD;
}
}
void resistor::string_to_enum(){
int head=0,find,index;
string sub;
find = color.find(" ");
sub = color.substr(head,find);
colorre1 = toenum1(sub);
head = find+1;
find = color.find(" ",find+1);
index = find - head;
sub = color.substr(head,index);
colorre2 = toenum1(sub);
head = find+1;
find = color.find(" ",find+1);
index = find - head;
sub = color.substr(head,index);
colorre3 = toenum1(sub);
head = find+1;
find = color.find(" ",find+1);
index = find - head;
sub = color.substr(head,index);
colortole = toenum2(sub);
}
void resistor::outcolorresistor(){
cout<<color<<endl;
}
void resistor::computeresistor(){
double sq=1;
for(int i=0;i<colorre3;i++){
sq*=10;
}
r = (10*colorre1 +colorre2)*sq;
tolerance = double(colortole);
}
void resistor::outresistor(){
cout<<r<<" +-"<<tolerance<<"%"<<endl;
}
resistor resistor::operator+(resistor other){
return resistor(this->r+other.r);
}
resistor resistor::operator||(resistor other){
return resistor(1/(1/(this->r)+1/other.r));
}
複製代碼
作者:
Smallcat
時間:
2019-10-23 21:25
main:
#include <iostream>
#include<resistor.h>
using namespace std;
int main()
{
resistor r;
string a;
char s[50];
cin.getline(s,50);
a = s;
while(a != "0 0 0 0"){
r.setresistor(a);
//r.outcolorresistor();
r.string_to_enum();
r.computeresistor();
r.outresistor();
cin.getline(s,50);
a = s;
}
double r1,r2,r3,r4;
resistor R1,R2,R3,R4,R5;
double re;
cin>>r1>>r2>>r3>>r4;
while(r1){
R1.setresistor(r1);
R2.setresistor(r2);
R3.setresistor(r3);
R4.setresistor(r4);
R5 = R1+R2;
R5 = R5||R3;
R5 = R5 + R4;
cout<<R5.r<<endl;
cin>>r1>>r2>>r3>>r4;
}
}
複製代碼
歡迎光臨 小貓貓大聯盟!幻與想的境界\(0w0)/ (http://smallcat.utmall.com/)
Powered by Discuz! 7.2