cin.ignore的替代方案?

时间:2017-10-29 08:39:05

标签: c++

5 /*description: 
6 This program which approximates one root of a polynomial
7 in a closed interval of the real number line and use the 
8 bisection method to approximate the root.
9 */
10 
11 #include <iostream>
12 #include <string>
13 #include <sstream>
14 #include <vector>
15 #include <iomanip>
16 #include <cmath>
17      
18 using namespace std;
19 
20 double polynomial(vector<double>,double,double,int);  
21 
22 int main()
23 {
24     int iterations;
25     cout << "How many iterations? ";
26     cin >> iterations;//gets the number of iterations
27     
28     double left_point,right_point,middle;
29     cout << "Enter the endpoints of the interval containing the root: ";
30     cin >> left_point >> right_point;
31     cin.ignore();
32     
33     vector<double> coefficients;
34     cout << "Enter the polynomial coefficients, ascending in degree: ";
35     string line;
36     getline(cin,line);// gets an entire line of input
37     istringstream input_string(line); // use  input_string like cin
38        
39     double num;
40     while (input_string >>num)
41     {
42         coefficients.push_back(num);
43     }
44     
45     cout << "Root estimate: " << setprecision(8) //Print at least 8 significant digits
46          << polynomial(coefficients,left_point,right_point,iterations);
47     
48 }
49 
50 //function
51 double polynomial(vector<double> coefficients,double left_point,double right_point,int iterations)
52 {
53     double middle;
54     for (int n = 0; n < iterations; n++)
55     {
56         middle = (left_point + right_point) / 2; //use the bisection method
57         double sum1 = 0, sum2 = 0, sum3 = 0;
58         for (int i = 0; i < coefficients.size(); i++)
59         {
60             sum1 = sum1 + coefficients[i]*pow(left_point,i);
61             sum2 = sum2 + coefficients[i]*pow(middle,i);
62             sum3 = sum3 + coefficients[i]*pow(right_point,i);
63         }
64         
65         if (sum1*sum2 > 0)
66             left_point = middle;
67         else
68             right_point = middle;
69     
70     }
71     
72     return middle;  
73 }

以上是我的一位朋友为她的项目写的节目。但是她试图找到cin.ignore的替代品,因为她的教授禁止使用它。我无法想到,所以我在这里寻求帮助,它不能像其他任何其他库函数一样明确。

0 个答案:

没有答案