10.12.2012, 01:26. Просмотров 8045. Ответов 12
добрый вечер !нужно проверить мою программу с потоками. переделала, но что-то не так( закомменченная программа, моя на основе которой написала с потоками)
функция в одну транзакцию считывает 3 значения, проверяет их, если неправильные выдает соотв. эррор и переходит на следующую транзакцию. если eof прекращает читать транзакцию ( еще мне надо включить пункт, если введено с клавиатуры CTRL+D (gedit) тоже прекратить чтение транзакции (как это сделать?)
C++ |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
| #include <iostream>
#include<iomanip>
#include <sstream>
#include <string>
#include <fstream>
using namespace std;
/*int main()
{
float number_of_shares=0;
float total=0;
float price_per_share,total_of_shares=0,total_of_spendings=0;
int count=1; */
void process_all_transactions(istream& in, ostream& err,double& total_of_spendings,int& total_of_shares);
int main ()
{
double tot_spendings;
int tot_shares;
cout<<"Enter data for transaction "<<count<<endl;
cout<<"Number of shares:"<<endl;
cout<<"Price per share:"<<endl;
cout<<"Optional transaction comment"<<endl;
process_all_transactions(cin,cerr,tot_spendings,tot_shares);
system("pause");
return 0;
}
void process_all_transactions (istream& in , ostream& err , double& total_of_spendings , int& total_of_shares)
{
string line_transaction;
while(!cin.eof()) //poka ne konec faila
{
istringstream iss; // v potok
int number_of_shares;
float price_per_share;
if (getline(cin,line_transaction))
{
cout<< "No valid data"<< endl;
}
else
{
iss.str(line_transaction); //iss chitaet iz peremennoj
if (iss >> number_of_shares >> price_per_share)
{
if(price_per_share <0 || (-number_of_shares>total_of_shares))
{
cout<< "No valid data"<< endl;
}
else
{
tot_shares += number_of_shares;
tot_spendings += number_of_shares*price_per_share;
}
}
else
{
cout<< "No valid data"<< endl;
}
}
}
/*
cout<<"This program calculates your profit/loss for all your investments in one share."<<endl;
cout<<"Use a positive number for purchase, and a negative for sell."<<endl;
cout<<"Enter zero (0) shares to exit the program."<<endl;
for (;;)
{
cout<<"Enter data for transaction "<<count<<endl;
cout<<"Number of shares:"<<endl;
cin>>number_of_shares;
if (number_of_shares<0)
{
while(-number_of_shares>total_of_shares)
{
cout<<"You do not have that many shares, try again.:"<<endl;
cin>>number_of_shares;
}
}
if (number_of_shares==0)
{
cout<<"Bye.Have a nice day."<<endl;
break;
}
cout<<"Price per share:"<<endl;
cin>>price_per_share;
if (price_per_share<0)
{
cout<<"price can;t be negative , try again:";
cin>>price_per_share;
}
if (number_of_shares<0)
{
cout<<"You gained $"<<-number_of_shares*price_per_share<<"on this transaction.";
}
else
{
cout<<"You spent $"<<number_of_shares*price_per_share<<"on this transaction."<<endl;}
total=number_of_shares*price_per_share;
total_of_shares+=number_of_shares;
total_of_spendings+=total;
if (total_of_spendings<0)
{
cout<<"Your total profit amount to $"<<-total_of_spendings;
}
else
{
cout<<"Your total spendings amount to $"<<total_of_spendings;
}
cout << " and you own "<<total_of_shares<<" shares. "<<endl;
cout<<endl;
}
system("pause");
return 0;
}*/ |
|