免費論壇 繁體 | 簡體
Sclub交友聊天~加入聊天室當版主
分享
返回列表 發帖

AOOP lab 3

head:
  1. #ifndef RESISTOR_H
  2. #define RESISTOR_H

  3. using namespace std;

  4. #include<string>
  5. #include<iostream>


  6. enum colorre{BLACK=0, BROWN=1, RED=2, ORANGE=3, YELLOW=4, GREEN=5, BLUE=6, VIOLET=7, GREY=8, WHITE=9};
  7. enum colorto{SILVER=10, GOLD=5};

  8. class resistor
  9. {
  10.     colorre colorre1,colorre2,colorre3;
  11.     colorto colortole;
  12.     string color;
  13.     double tolerance;
  14. public:
  15.     double r;
  16.     resistor();
  17.     resistor(double a);
  18.     ~resistor();
  19.     void setresistor(string a);
  20.     void setresistor(double a);
  21.     void string_to_enum();
  22.     colorre toenum1(string a);
  23.     colorto toenum2(string a);
  24.     void outcolorresistor();
  25.     void computeresistor();
  26.     void outresistor();
  27.     resistor operator+(resistor other);
  28.     resistor operator||(resistor other);
  29. };

  30. #endif // RESISTOR_H
複製代碼
分享到: QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友

小貓貓2017了喔!
(點一下康娜醬傳送到小貓貓2017大事記)

cpp:
  1. #include "resistor.h"
  2. #include<iostream>
  3. #include<string>

  4. using namespace std;

  5. resistor::resistor()
  6. {
  7.     r = 1;
  8. }
  9. resistor::resistor(double a){
  10.     r = a;
  11. }
  12. resistor::~resistor(){

  13. }
  14. void resistor::setresistor(string s){
  15.     color = s;
  16. }
  17. void resistor::setresistor(double a){
  18.     r = a;
  19. }
  20. colorre resistor::toenum1(string a){
  21.     if(a=="BLACK"){
  22.         return BLACK;
  23.     }
  24.     if(a=="BROWN"){
  25.         return BROWN;
  26.     }
  27.     if(a=="RED"){
  28.         return RED;
  29.     }
  30.     if(a=="ORANGE"){
  31.         return ORANGE;
  32.     }
  33.     if(a=="YELLOW"){
  34.         return YELLOW;
  35.     }
  36.     if(a=="GREEN"){
  37.         return GREEN;
  38.     }
  39.     if(a=="BLUE"){
  40.         return BLUE;
  41.     }
  42.     if(a=="VIOLET"){
  43.         return VIOLET;
  44.     }
  45.     if(a=="GREY"){
  46.         return GREY;
  47.     }
  48.     if(a=="WHITE"){
  49.         return WHITE;
  50.     }
  51. }
  52. colorto resistor::toenum2(string a){
  53.     if(a=="SILVER"){
  54.         return SILVER;
  55.     }
  56.     if(a=="GOLD"){
  57.         return GOLD;
  58.     }

  59. }
  60. void resistor::string_to_enum(){
  61.     int head=0,find,index;
  62.     string sub;
  63.     find = color.find(" ");
  64.     sub = color.substr(head,find);
  65.     colorre1 = toenum1(sub);
  66.     head = find+1;
  67.     find = color.find(" ",find+1);
  68.     index = find - head;
  69.     sub = color.substr(head,index);
  70.     colorre2 = toenum1(sub);
  71.     head = find+1;
  72.     find = color.find(" ",find+1);
  73.     index = find - head;
  74.     sub = color.substr(head,index);
  75.     colorre3 = toenum1(sub);
  76.     head = find+1;
  77.     find = color.find(" ",find+1);
  78.     index = find - head;
  79.     sub = color.substr(head,index);
  80.     colortole = toenum2(sub);
  81. }
  82. void resistor::outcolorresistor(){
  83.     cout<<color<<endl;
  84. }
  85. void resistor::computeresistor(){
  86.     double sq=1;
  87.     for(int i=0;i<colorre3;i++){
  88.         sq*=10;
  89.     }
  90.     r = (10*colorre1 +colorre2)*sq;
  91.     tolerance = double(colortole);
  92. }
  93. void resistor::outresistor(){
  94.     cout<<r<<" +-"<<tolerance<<"%"<<endl;
  95. }
  96. resistor resistor::operator+(resistor other){
  97.     return resistor(this->r+other.r);
  98. }
  99. resistor resistor::operator||(resistor other){
  100.     return resistor(1/(1/(this->r)+1/other.r));
  101. }
複製代碼

小貓貓2017了喔!
(點一下康娜醬傳送到小貓貓2017大事記)

TOP

main:
  1. #include <iostream>
  2. #include<resistor.h>
  3. using namespace std;

  4. int main()
  5. {
  6.     resistor r;
  7.     string a;
  8.     char s[50];
  9.     cin.getline(s,50);
  10.     a = s;
  11.     while(a != "0 0 0 0"){
  12.         r.setresistor(a);
  13.         //r.outcolorresistor();
  14.         r.string_to_enum();
  15.         r.computeresistor();
  16.         r.outresistor();
  17.         cin.getline(s,50);
  18.         a = s;
  19.     }
  20.     double r1,r2,r3,r4;
  21.     resistor R1,R2,R3,R4,R5;
  22.     double re;
  23.     cin>>r1>>r2>>r3>>r4;
  24.     while(r1){
  25.         R1.setresistor(r1);
  26.         R2.setresistor(r2);
  27.         R3.setresistor(r3);
  28.         R4.setresistor(r4);
  29.         R5 = R1+R2;
  30.         R5 = R5||R3;
  31.         R5 = R5 + R4;
  32.         cout<<R5.r<<endl;
  33.         cin>>r1>>r2>>r3>>r4;
  34.     }
  35. }
複製代碼

小貓貓2017了喔!
(點一下康娜醬傳送到小貓貓2017大事記)

TOP

返回列表