Поскольку автоматические переменные создаются при вызове функции, они не сохраняют свои значения от одного вызова к другому, и должны явно устанавливаться при каждом вызове. Если значения не установить, они будут содержать мусор.
[SP+4] // int text;
SP = SP - 4;
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define LINES 10 // Number of lines
#define CHARS 10 // Number of characters at line
char randomChar()
{
#if 0
float f = rand();
f /= RAND_MAX;
return (char) (10 * f + '0');
#else
static char n = 'A';
if (++n > 'Z' && n < 'a')
n = 'a';
if (n > 'z')
n = 'A';
return n;
#endif
}
void getputChar(FILE* infile, FILE* outfile)
{
char ch;
ch = fgetc(infile);
if (ferror(infile))
perror("Error reading file f1.txt");
fputc(ch, outfile);
if (ferror(outfile))
perror("Error writing file f2.txt");
}
int reverseOrder(FILE* infile, int pos, FILE* outfile)
{
int npos = pos;
char ch;
int count;
int n;
while (1) {
ch = fgetc(infile);
if (ferror(infile))
perror("Error reading file f1.txt");
if (ch == '\n') {
count = reverseOrder(infile, npos+1, outfile) + 1;
fseek(infile, pos, SEEK_SET);
if (ferror(infile))
perror("Error seeking file f1.txt");
for (n = 0; n < count; ++n)
getputChar(infile, outfile);
fputc('\n', outfile);
if (ferror(outfile))
perror("Error writing file f2.txt");
return count;
}
else if (ch == EOF) {
if (npos - pos == 0)
return 0;
else {
fseek(infile, pos, SEEK_SET);
if (ferror(infile))
perror("Error seeking file f1.txt");
getputChar(infile, outfile);
fputc('\n', outfile);
if (ferror(outfile))
perror("Error writing file f2.txt");
return 1;
}
}
else {
npos++;
}
}
}
int main()
{
FILE* file_ptr;
FILE* file_ptr1;
int n, nn;
// Creating file f1.txt
file_ptr = fopen("f1.txt", "w");
if (file_ptr == NULL) {
perror("Error creating file f1.txt");
}
srand(time(0));
for (n = 0; n < LINES; ++n) {
for(nn = 0; nn < CHARS; ++nn) {
fputc(randomChar(), file_ptr);
if (ferror(file_ptr)) {
perror("Error writing file f1.txt");
}
}
fputc('\n', file_ptr);
}
fclose(file_ptr);
// Reverse order of lines
file_ptr = fopen("f1.txt", "r");
if (file_ptr == NULL) {
perror("Error opening file f1.txt");
}
file_ptr1 = fopen("f2.txt", "w");
if (file_ptr1 == NULL) {
perror("Error creating file f2.txt");
}
reverseOrder(file_ptr, 0, file_ptr1);
fclose(file_ptr1);
fclose(file_ptr);
printf("OK\n");
return EXIT_SUCCESS;
}
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Polynom : public vector<int>
{
public:
Polynom() {}
Polynom(int _degree, int* koef = 0);
inline int degree() const {
return (size() > 0) ? size() - 1 : 0;
}
string toString () const;
Polynom& operator << (const string& src);
void addDegree(int _degree, int koef);
protected:
static string itoa(int value);
static int readNumber(string::const_iterator& it, string::const_iterator end);
};
Polynom::Polynom(int _degree, int* koef)
{
if (_degree < 0)
_degree = 0;
resize(_degree + 1);
if (koef) {
for (int n = 0; n <= _degree; ++n)
(*this)[n] = koef[n];
}
else {
for (int n = 0; n < size(); ++n)
(*this)[n] = 0;
}
}
string Polynom::itoa(int value)
{
char buf[50];
sprintf(buf, "%d", value);
return string(buf);
}
string Polynom::toString () const
{
string result;
for(int n = degree(); n >= 0; --n) {
if (at(n) != 0) {
if (at(n) > 0) {
if (n != degree())
result += "+";
}
result += itoa(at(n));
if (n > 0) {
result += "x";
if (n > 1) {
result += "^";
result += itoa(n);
}
}
}
}
return result;
}
inline basic_ostream<char>& operator << (basic_ostream<char>& os,const Polynom& p)
{
os << p.toString();
return os;
}
void Polynom::addDegree(int _degree, int koef)
{
if (_degree >= degree()) {
int oldSize = size();
resize(_degree + 1);
for(int n = oldSize; n < size(); ++n )
(*this)[n] = 0;
}
(*this)[_degree] += koef;
}
int Polynom::readNumber(string::const_iterator& it, string::const_iterator end)
{
enum { ReadSign, ReadDigit } state = ReadSign;
enum { Positive, Negative } sign = Positive;
int result = 0;
while(it != end)
{
switch (state) {
case ReadSign:
if (*it == '+' || *it == '-') {
if (*it == '+')
sign = Positive;
else
sign = Negative;
state = ReadDigit;
}
else if (*it == 'x') {
return 1;
}
else if (*it < '0' || *it > '9') {
string err("Not a digit '");
err += *it;
err += "' while reading number";
throw(err);
}
else {
state = ReadDigit;
result = *it - '0';
}
break;
case ReadDigit:
if (*it == 'x' || *it == '+' || *it == '-') {
return result * ((sign == Positive) ? 1 : -1);
}
else if (*it < '0' || *it > '9') {
string err("Not a digit '");
err += *it;
err += "' while reading number";
throw(err);
}
else {
state = ReadDigit;
result = result * 10 + *it - '0';
}
break;
}
it++;
}
return result * ((sign == Positive) ? 1 : -1);
}
Polynom& Polynom::operator << (const string& src)
{
enum { ReadKoef, ReadPower } state = ReadKoef;
int koef;
int power = 0;
for(string::const_iterator it = src.begin(); it != src.end(); it++)
{
switch(state) {
case ReadKoef:
koef = readNumber(it, src.end());
if (it == src.end()) {
addDegree(0, koef);
return *this;
}
if (*it == 'x') {
state = ReadPower;
}
else if (*it == '+' || *it == '-') {
addDegree(0, koef);
}
break;
case ReadPower:
if (*it == '^') {
it++;
if (*it == '+') {
it++;
}
if (*it >= '0' && *it <= '9') {
power = readNumber(it, src.end());
addDegree(power, koef);
if (it == src.end())
return *this;
it--;
state = ReadKoef;
}
else {
string err("Unexpected symbol '");
err += *it;
err += "' while reading polynom from \"";
err += src;
err += '"';
throw(err);
}
}
else if (*it == '+' || *it == '-') {
addDegree(1, koef);
it--;
state = ReadKoef;
}
else {
string err("Unexpected symbol '");
err += *it;
err += "' while reading polynom from \"";
err += src;
err += '"';
throw(err);
}
break;
}
}
if (state == ReadPower) {
string err("Unexpected end of string while reading polynom from \"");
err += src;
err += '"';
throw(err);
}
return *this;
}
int main()
{
int deg = 2;
int koef[3] = { 3, 4, 5 };
Polynom polynom(deg, koef);
cout << polynom << endl;
Polynom p2;
p2 << "-18x^3+4x-2";
cout << p2 << endl;
return 0;
}
#include <vector>
#include <algorithm>
...
std::vector<int> array(SIZE);
std::vector<int> array2(array.size());
std::copy(array.begin(), array.end(), array2.begin());
char tmp;
...
tmp = text[0];
text[0] = text[2];
text[2] = tmp;
fputs(text, file_ptr);
sin(x) * sin(x)
даёт функцию 0.5 * sin(2x) + 0.5
.y = (sin(x) * sin(x) - 0.5) * 2
y = 0.75 * sin(2x) + (1 - 0.75) * sin(x)
BOOL FlushFileBuffers(
HANDLE hFile
);