В файле из n элементов(букв и цифр) вы должны найти максимальное ччисло
Пример
acf3ca54sa432cv
ответ: 432
C/C++
C++. ПОМОГИТЕ СРОЧНО!!!!!
#include <iostream>
#include <fstream>
#include <cctype>
#include <cstdlib>
using namespace std;
int main()
{
const char filename[] = "input.txt";
const int n = 1000;
char str[n];
int i = 0;
char num_str[10];
int num_tmp,num_max = 0;
FILE * ifs = fopen(filename, "r");
int c;
while((c = fgetc(ifs)) != EOF)
{
str[i++] = c;
}
str[i] = '\0';
fclose(ifs);
int len = i;
i = 0;
while(str[i] && i < len)
{
if(isdigit(str[i]))
{
int j = 0;
while(isdigit(str[i]) && j < 10)
{
num_str[j] = str[i];
j++;
i++;
}
num_str[j] = '\0';
num_tmp = atoi(num_str);
if(!num_max) num_max = num_tmp;
else if(num_tmp > num_max) num_max = num_tmp;
}
i++;
}
cout << endl << num_max << endl;
return 0;
}
#include <fstream>
#include <cctype>
#include <cstdlib>
using namespace std;
int main()
{
const char filename[] = "input.txt";
const int n = 1000;
char str[n];
int i = 0;
char num_str[10];
int num_tmp,num_max = 0;
FILE * ifs = fopen(filename, "r");
int c;
while((c = fgetc(ifs)) != EOF)
{
str[i++] = c;
}
str[i] = '\0';
fclose(ifs);
int len = i;
i = 0;
while(str[i] && i < len)
{
if(isdigit(str[i]))
{
int j = 0;
while(isdigit(str[i]) && j < 10)
{
num_str[j] = str[i];
j++;
i++;
}
num_str[j] = '\0';
num_tmp = atoi(num_str);
if(!num_max) num_max = num_tmp;
else if(num_tmp > num_max) num_max = num_tmp;
}
i++;
}
cout << endl << num_max << endl;
return 0;
}
/*
* (c) Cranium, aka Череп. 2014
* GNU GPL
*
* Game "Oldschool Snake
*
*/
#include
#include
#include "CScreen.h"
#include "CGame.h"
using namespace std;
int main() {
setlocale(LC_ALL, "Russian");
try {
CScreen screen;
screen.cursor_show(false);
screen.text_attr((WORD)0x0a);
screen.cls();
CGame game(screen, 80, 24, 120);
game.logo();
game.read_top10();
game.top10(false);
game.pak(18);
do {
game.game_loop();
game.top10(true);
} while (game.once_more());
game.goodbye();
}
catch(CSScreenException& ex) {
cerr << "*** " << ex.what() << endl;
}
return 0;
}
CScreen.h
/*
* (c) Cranium, aka Череп. 2014
* GNU GPL
*
*/
#ifndef __CSCREEN_H__
#define __CSCREEN_H__
#include
/*
Класс исключения для класса CScreen
*/
class CSScreenException {
public:
CSScreenException(int _err) : err(_err) {}
const char *what(); // возвращает строку с описанием ошибки
int err; // код ошибки
};
/*
Класс CScreen содержит системозависимые вызовы для вывода на консоль.
Данная реализация предназначена для ОС MS Windows 2000 Professional
и более поздних.
Система координат:
(0, 0) - левый верхний угол экрана
ось X - горизонтально вправо
ось Y - вертикально вниз (положительное направление)
*/
class CScreen {
public:
CScreen();
~CScreen();
void cursor_show(bool visible); // показать/скрыть курсор
void text_attr(WORD attr); // установить цвет текста/фона
void pos(int x, int y, char ch = 0); // позиционирование курсора и
// вывод символа, если ch != 0
void pos_str(int x, int y, const char *str); // позиционирование курсора
// и вывод строки
void cls(); // очистка экрана
private:
HANDLE hConsoleOutput;
CONSOLE_CURSOR_INFO oldCursorInfo, curCursorInfo;
WORD oldTextAttr;
};
#endif // __CSCREEN_H__
CScreen.cpp
/*
* (c) Cranium, aka Череп. 2014
* GNU GPL
*
*/
#include "CScreen.h"
#include
const char *msgs[] = {
"",
"Failed GetStdHandle(): INVALID_HANDLE_VALUE",
"Failed GetConsoleCursorInfo()",
"Failed SetConsoleCursorInfo()",
"Failed SetConsoleCursorPosition()"
};
const char *CSScreenException::what() {
return msgs[err];
}
CScreen::CScreen() {
hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
if (hConsoleOutput == INVALID_HANDLE_VALUE)
throw CSScreenException(1); // "INVALID_HANDLE_VALUE"
if (!GetConsoleCursorInfo(hConsoleOutput, &oldCursorInfo))
throw CSScreenException(2);
curCursorInfo.dwSize = oldCursorInfo.dwSize;
curCursorInfo.bVisible = oldCursorInfo.bVisible;
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(hConsoleOutput, &csbi);
oldTextAttr = csbi.wAttributes;
}
CScreen::~CScreen() {
SetConsoleCursorInfo(hConsoleOutput, &oldCursorInfo);
SetConsoleTextAttribute(hConsoleOutput, oldTextAttr);
}
void CScreen::cursor_show(bool visible) {
curCursorInfo.bVisible = visible;
if (!SetConsoleCursorInfo(hConsoleOutput, &curCursorInfo))
throw CSScreenException(3);
}
void CScreen::text_attr(WORD attr) {
SetConsoleTextAttribute(hConsoleOutput, attr);
}
void CScreen::pos(int x, int y, char ch) {
COORD point;
point.X = static_cast(x);
point.Y = static_cast(y);
if (!SetConsoleCursorPosition(hConsoleOutput, point))
throw CSScreenException(4);
if (ch > 0)
_putch(ch);
}
void CScreen::pos_str(int x, int y
* (c) Cranium, aka Череп. 2014
* GNU GPL
*
* Game "Oldschool Snake
*
*/
#include
#include
#include "CScreen.h"
#include "CGame.h"
using namespace std;
int main() {
setlocale(LC_ALL, "Russian");
try {
CScreen screen;
screen.cursor_show(false);
screen.text_attr((WORD)0x0a);
screen.cls();
CGame game(screen, 80, 24, 120);
game.logo();
game.read_top10();
game.top10(false);
game.pak(18);
do {
game.game_loop();
game.top10(true);
} while (game.once_more());
game.goodbye();
}
catch(CSScreenException& ex) {
cerr << "*** " << ex.what() << endl;
}
return 0;
}
CScreen.h
/*
* (c) Cranium, aka Череп. 2014
* GNU GPL
*
*/
#ifndef __CSCREEN_H__
#define __CSCREEN_H__
#include
/*
Класс исключения для класса CScreen
*/
class CSScreenException {
public:
CSScreenException(int _err) : err(_err) {}
const char *what(); // возвращает строку с описанием ошибки
int err; // код ошибки
};
/*
Класс CScreen содержит системозависимые вызовы для вывода на консоль.
Данная реализация предназначена для ОС MS Windows 2000 Professional
и более поздних.
Система координат:
(0, 0) - левый верхний угол экрана
ось X - горизонтально вправо
ось Y - вертикально вниз (положительное направление)
*/
class CScreen {
public:
CScreen();
~CScreen();
void cursor_show(bool visible); // показать/скрыть курсор
void text_attr(WORD attr); // установить цвет текста/фона
void pos(int x, int y, char ch = 0); // позиционирование курсора и
// вывод символа, если ch != 0
void pos_str(int x, int y, const char *str); // позиционирование курсора
// и вывод строки
void cls(); // очистка экрана
private:
HANDLE hConsoleOutput;
CONSOLE_CURSOR_INFO oldCursorInfo, curCursorInfo;
WORD oldTextAttr;
};
#endif // __CSCREEN_H__
CScreen.cpp
/*
* (c) Cranium, aka Череп. 2014
* GNU GPL
*
*/
#include "CScreen.h"
#include
const char *msgs[] = {
"",
"Failed GetStdHandle(): INVALID_HANDLE_VALUE",
"Failed GetConsoleCursorInfo()",
"Failed SetConsoleCursorInfo()",
"Failed SetConsoleCursorPosition()"
};
const char *CSScreenException::what() {
return msgs[err];
}
CScreen::CScreen() {
hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
if (hConsoleOutput == INVALID_HANDLE_VALUE)
throw CSScreenException(1); // "INVALID_HANDLE_VALUE"
if (!GetConsoleCursorInfo(hConsoleOutput, &oldCursorInfo))
throw CSScreenException(2);
curCursorInfo.dwSize = oldCursorInfo.dwSize;
curCursorInfo.bVisible = oldCursorInfo.bVisible;
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(hConsoleOutput, &csbi);
oldTextAttr = csbi.wAttributes;
}
CScreen::~CScreen() {
SetConsoleCursorInfo(hConsoleOutput, &oldCursorInfo);
SetConsoleTextAttribute(hConsoleOutput, oldTextAttr);
}
void CScreen::cursor_show(bool visible) {
curCursorInfo.bVisible = visible;
if (!SetConsoleCursorInfo(hConsoleOutput, &curCursorInfo))
throw CSScreenException(3);
}
void CScreen::text_attr(WORD attr) {
SetConsoleTextAttribute(hConsoleOutput, attr);
}
void CScreen::pos(int x, int y, char ch) {
COORD point;
point.X = static_cast(x);
point.Y = static_cast(y);
if (!SetConsoleCursorPosition(hConsoleOutput, point))
throw CSScreenException(4);
if (ch > 0)
_putch(ch);
}
void CScreen::pos_str(int x, int y
Похожие вопросы
- C++,помогите срочно!!! Ни как не могу понять как решить данную задачу
- Программирование на C. Помогите бездарю
- C++ ПОМОГИТЕ ПЛИИЗ
- Задача на c++ помогите решить без рандомного заполнения массива
- Читаю книжку по C, помогите с указателями
- C++ Помогите люди!
- C++ ПОМОГИТЕ ПОЖАЛУЙСТА
- Задача на C++, помогите решить.
- Знающие C++, помогите пожалуйста:)
- Можно через delete [ ] ? c++ помогите пожалуйста!