📄 pex2_13.cpp
字号:
#include <iostream.h>
#include <strstream.h>
#include <fstream.h>
// start at char p and skip all blanks in a character array.
// return a pointer to the next non-blank char and NULL if
// the NULL character is encountered (end of string)
char *SkipWhiteSpace(char *p)
{
// return if the string is empty
if (*p == NULL)
return NULL;
// look for a non-blank character
while(*p == ' ')
p++;
if (*p == NULL)
// all remaining characters in the array are blank
return NULL;
else
// return the address of the non-blank character
return p;
}
void main(void)
{
char line[82], outstring[128];
char *p;
int numx;
ofstream fout;
fout.open("funcs.val");
// read lines until end of file
while(cin.getline(line,82,'\n'))
{
// declare an output array local to the block. each
// iteration of the while will create a new outs
ostrstream outs(outstring, sizeof(outstring));
// output "FuncName(x)"
outs << line[0] << "(x) = ";
// skip leading blanks
p = SkipWhiteSpace(&line[1]);
numx = 0;
// cycle through string and build the array
while(p != NULL)
{
// for each 'x', increment numx
if (*p == 'x')
numx++;
else
{
// a + or - found
if (numx > 1)
// output x**numx to array
outs << "x**" << numx << " ";
else
// output "x"
outs << "x ";
// write out the + or -
outs << *p << " ";
// starting a new collection of x's
numx = 0;
}
// find next non-blank char or end of string
p = SkipWhiteSpace(p+1);
}
// output trailing term
if (numx > 1)
outs << "x**" << numx << " ";
else
outs << "x ";
outs << ends;
// write formatted array to file
fout << outstring << endl;
}
}
/*
<Run>
F x x x + x x - x
G x x - x x x + x x x x
H x + xxxxx - xxxxxxx + xx x
<output file "funcs.val">
F(x) = x**3 + x**2 - x
G(x) = x**2 - x**3 + x**4
H(x) = x + x**5 - x**7 + x**3
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -