Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1. #include<iostream>
  2. #include <cstdlib>
  3. #include <math.h>
  4. #include "polynomial.h"
  5. #include <cctype>
  6. #include <fstream>
  7. using namespace std;
  8.  
  9. typedef enum { READ_ERROR, READ_INT, READ_STRING } readValue_t;
  10. readValue_t getIntOrString(string txt, unsigned int &n, string &s)
  11. {
  12. char decyzja;
  13. cout << txt;
  14. decyzja = cin.peek(); // zagladamy bez wyjmowania
  15. if(isdigit(decyzja))
  16. {
  17. cin >> n;
  18. return READ_INT;
  19. }
  20. else
  21. {
  22. getline(cin, s);
  23. return READ_STRING;
  24. }
  25. return READ_ERROR;
  26. }
  27. inline double readDoubleFromStream(istream &str)
  28. {
  29. double f;
  30. str >> f;
  31. return f;
  32. }
  33.  
  34. int main()
  35. {
  36. fstream plik;
  37. unsigned int stopien;
  38. double x;
  39. readValue_t res;
  40. istream *read = NULL;
  41. //ifstream infs;
  42. string nazwaPliku,txt;
  43. cout<<"Podaj stopien wielomianu W(x), lub podaj nazwe pliku w ktorym zostala zapisana informacja o nim, plik ten musi znajdowacsie w katalogu z tym programem"<<endl;
  44. //cin>>txt;
  45. res = getIntOrString(txt, stopien, nazwaPliku);
  46. //cout<<"wartosc res "<<res<<endl;
  47. do
  48. {
  49. switch(res)
  50. {
  51. case READ_INT:
  52. {
  53. read=&cin;
  54. break;
  55. }
  56. break;
  57. case READ_STRING:
  58. cout << endl << "Ok, plik to " << nazwaPliku << endl;
  59. plik.open(nazwaPliku.c_str());
  60. if(plik.fail())
  61. {
  62. cout << "Nie moge otworzyc pliku: " << nazwaPliku << endl;
  63. break;
  64. };
  65. //default:
  66. //cout<<"wystapil blad"<<endl;
  67. //continue;
  68. }
  69. plik>>stopien;
  70. cout << "Stopien z pliku to: " << stopien << endl;
  71. read = &plik;
  72. break;
  73. }
  74. while(read==NULL);
  75. double val;
  76. Polynomial p1(stopien);
  77. for (int i=stopien; i>=0; i++)
  78. {
  79. if(res==READ_INT)
  80. {
  81. cout<<"Wprowadzaj wspolczynniki wielomianu W(x) od najwyzszej potegi"<<endl;
  82. for(int i=stopien; i>=0; i--)
  83. {
  84. cin >> val;
  85. p1.setCoefficient(i,val);
  86. }
  87. }
  88. x = readDoubleFromStream(*read);
  89. p1.setCoefficient(i, val);
  90. }
  91.  
  92. cout<<"Postac wielomianu: "<<p1;
  93. //cout<<"Podaj wartosc x dla ktorej chcesz obliczyc wartosc wielomianu W(x)"<<endl;
  94. cin>>x;
  95. p1.value(x);
  96. return 0;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement