Text Editor (15)
Requirements:
(1) editing the text;
(2) Save and open the text file at the specified location;
(3) with input and output interface.
Code: (This code runs correctly in vc6.0 and is compiled in VS2010)
#include <fstream.h> #include <string.h> #include <iostream.h> #include <stdio.h> #include <
stdlib.h> void input () {char str[100];
Ofstream out ("D:\\test.txt");
if (!out) {cout<< "Cannot open output file.\n";
} cout<< "Please input:" <<endl;
Gets (str);
out<<str;
cout<<endl;
Out.close ();
} void Output () {Ifstream in ("D:\\test.txt");
if (!in) {cout<< "Cannot open input file.\n";
} char str;
while (In.get (str)) {cout<<str;
} cout<<endl;
In.close ();
} void Main () {int j=1;
Char i[10];
int n;
do {cout<< "1. Write text" <<endl;
cout<< "2. Read Text" <<endl;
cout<< "3. Exit program" <<endl;
cout<< "Please select:";
cin>>i; if ((i[0]<49) | | |
(i[0]>51)) {cout<< "Input error!!"
<<endl;
Goto end;
} else n=i[0]-48;
Switch (n) {case 1:input ();
Break
Case 2:output ();
Break
Case 3:exit (0); Default:cout<< "Input Error!"
<<endl;
} end:;
}while (j==1); }
2
.
#include <iostream> #include <cstdio> using namespace std;
struct TreeNode {char data;
TreeNode *left;
TreeNode *right;
};
void GetHeight (TreeNode *t, int &h) {if (T = = NULL) H = 0;
else {int left_h;
GetHeight (T->left, Left_h);
int right_h;
GetHeight (T->right, Right_h);
H = 1 + max (Left_h, Right_h);
}} TreeNode *createbitree (TreeNode *&t) {//In order to enter the value of the node in the binary tree (one character), the empty characters represents the empty tree,//constructs a two-forked list representing the two-fork tree T.
Char ch;
CIN >> ch;
if (ch = = ' # ') T = NULL; else {if (! (
T = (TreeNode *) malloc (sizeof (TreeNode)))) return 0; T->data = ch; Generate root node Createbitree (t->left); Constructs left subtree Createbitree (t->right);
Construct right subtree} return T;
}//Createbitree void free (TreeNode *&t) {if (T = = NULL) return;
Free (t->left);
T->left = NULL;
Free (t->right);
T->right = NULL;
Free (T);
T = NULL;
} int main () {TreeNode *t = NULL;
Createbitree (T);
int height;
GetHeight (T, height); cout <<
Height << Endl;
Free (T);
return 0; }
3. There are several points on the two-dimensional plane, which can find a line that crosses the most points.
#include <iostream> using namespace std;
const double inf=2100000000;
struct point {int x, y;};
int cmp (const void *a, const void *b) {if (* (double*) a> * (double *) b) return 1;
else return-1;
} point p[1001];
Double k[1001];
int main () {int N;
int I,j,l,counter,ans;
while (scanf ("%d", &n)!=eof && N) {for (i=0;i<n;i++)
scanf ("%d%d", &p[i].x,&p[i].y); for (ans=0,i=0;i<n-1;i++) {for (j=i+1,l=0;j<n;j++,l + +) {if (P[i].x==p[j].
x) K[l]=inf;
Else K[l]=double (P[I].Y-P[J].Y)/(p[i].x-p[j].x);
} qsort (K,l,sizeof (k[0]), CMP);
for (j=0;j<l;j++) {
Counter=1;
while (J+1<l && k[j]==k[j+1]) {
j + +;
counter++;
} if (Counter>ans)
Ans=counter; }} printf ("%d/n", ans+1);
} return 0; }
Idea: To find out the slope of one point with other points, and to sort the same line if the slope is the same.
#include <iostream>
using namespace std;
void FindStr (char* str)
{
if (str = = NULL)
return;
char* pStr = str;
char* qstr = str+1;
char* index = str;
char* maxindex = str;
int num = 0;
int maxnum = 0;
while (*pstr! = ')
{
while (*qstr! = *pstr) {
num = 0;
qstr++;
}
while (*qstr = = *pstr && *pstr! = ' num++ ' && *qstr! = ' + ')
}
index = PSTR;
qstr++;
pstr++;
}
if (num>=1)
index = index-num+1;
if (num > Maxnum) {
maxnum = num;
Maxindex = index;
}
Qstr = pstr+1;
}
cout << "Result:";
for (int i=0;i<maxnum;++i)
cout << *maxindex++ << ";
cout << Endl;
}
int main ()
{
char* str = "ABCABCDABCD";
FINDSTR (str);
return 0;
}