Здравствуйте помогите пожалуйста с задачами:
1) Написать программу, которая считывает текст из файла и выводит на экран только вопросительные предложения из этого текста.
#include "stdafx.h"
#include <fstream>
#include <sstream>
#include <string>
#include <iostream>
using namespace std;
bool isLimit(char c)
{
char lim[]={' ','\t','\n'};
for (int i=0;i<sizeof(lim);++i)
if(c==lim[i])return true;return false;}
int _tmain(int argc, _TCHAR* argv[])
{
ifstream fin("infile.txt",ios::in|ios::nocreate);
if (!fin)
{
cout<<"oshibka otkritiy faila";
return 1;
}
int count=0; string word;
ostringstream sentence;
while(!fin.eof()){
char symb;
while(isLimit(symb=fin.peek()))
{
sentence<<symb;
if(symb=='\n')break;
fin.seekg(1,ios::cur);
}
fin>>word;
sentence<<word;
char last=word[word.size()-1];
if((last=='.')||(last=='!'))
{
sentence.str("");
continue;
}
if(last=='?')
{
cout<<sentence.str();
sentence.str("");
count++;
}
}
if(!count)cout<<"Voprositelnix predlozenii net";
return 0;
}
#include <fstream.h>
#include <iostream.h>
#include <string.h>
#include <stdlib.h>
int main(){
int const length=31;
struct Word{
char word[length];
};
char curword[length];
ifstream fin("example.txt", ios::in|ios::nocreate);
if(!fin){cout<<"ERROR"<<endl; return 1;}
int l=10000;
char *zuf=new char[l];
ofstream out("out.txt",ios::out|ios::nocreate);
if(!out){cout<<"ERROR"<<endl; return 2;}
int const more=1000000;
Word w2[more];
int i=0;
while(!fin.eof()){
if (i>more){cout<<"It is too long file!!! "<<endl;return 3;}
fin>>curword;
i++;
}
for(int g=0;g<i;g++)
for(int k=g;k<i;k++){
if(strcmp(w2[g].word, w2[k].word) > 0){
Word buf=w2[k];
w2[k]=w2[g];
w2[g]=buf;
}
}
int n=0;
while (n<i){
out<<w2[n].word;
n++;
}
return 0;
}
а как сделать?
"Как сделать" что? Открытие файла? Или программу? Файл открывается просто так:
ifstream fin("example.txt", ios::in);
Програмку не мне в программе главное чтобы класс String присутствовал и все.
ай все понял спасибо
а вот тут с ios::out как сделать?
#include <fstream.h>
#include <iostream.h>
#include <string.h>
int main(){
int const length=31;
struct Word{
char word[length];
};
char curword[length];
ifstream fin("example.txt", ios::in|ios::nocreate);
if(!fin){cout<<"ERROR"<<endl; return 1;}
int l=10000;
char *zuf=new char[l];
ofstream out("out.txt",ios::out|ios::nocreate);
if(!out){cout<<"ERROR"<<endl; return 2;}
int const more=1000000;
Word w2[more];
int i=0;
while(!fin.eof()){
if (i>more){cout<<"It is too long file!!! "<<endl;return 3;}
fin>>curword;
i++;
}
for(int g=0;g<i;g++)
for(int k=g;k<i;k++){
if(strcmp(w2[g].word, w2[k].word) > 0){
Word buf=w2[k];
w2[k]=w2[g];
w2[g]=buf;
}
}
int n=0;
while (n<i){
out<<w2[n].word;
n++;
}
return 0;
}
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ifstream fin("infile.txt", ios::in);
string str;
string all_text = "";
while(!fin.eof()) {
getline(fin, str);
all_text += str;
}
string sentence;
int count = 0, next = 0;
while(all_text.length() > 0) {
sentence = all_text.substr(0, (next = (all_text.find_first_of(".!?") + 1)));
all_text = all_text.substr(next);
if(sentence[sentence.length() - 1] == '?') {
cout << sentence << endl;
count += 1;
}
}
if(!count) {
cout << "no questions" << endl;
}
return 0;
}
Я с первой-то понял. А со второй?
не совсем понял а в коде это как и куда записать?
#include <string>
#include <vector>
#include <iostream>
#include <iterator>
#include <fstream>
using namespace std;
void split_string(const std::string& str, vector<string>& v,
const std::string& delim = ",")
{
std::string::size_type lpos = 0;
std::string::size_type pos = str.find_first_of(delim, lpos);
do
{
string s = str.substr(lpos, pos - lpos);
if(!s.empty()) {
v.push_back(s);
}
lpos = ( pos == std::string::npos ) ? std::string::npos : pos + 1;
pos = str.find_first_of(delim, lpos);
}
while(lpos != std::string::npos);
}
int main() {
ifstream fin("infile.txt", ios::in);
ofstream fout("outfile.txt", ios::out);
ostream_iterator<string> out_it(fout, "\n" );
string s;
vector<string> buffer;
while(!fin.eof()) {
getline(fin, s); // читаем очередную строку из файла
split_string(s, buffer, ".,:?! "); // разбиваем ее на слова и заносим их в buffer
}
fin.close();
sort(buffer.begin(), buffer.end()); // сортируем слова по алфавиту
// копируем в выходной файл с удалением дубликатов
unique_copy(buffer.begin(), buffer.end(), out_it);
fout.close();
return 0;
}
спасибо