void rewind(int* p, int n, float ave) { //Create a mirror array b [], one by one corresponding to the states of the elements in a []. 1 represents large, 0 represents small, and - 1 represents that there is no need to change or has changed. Remember to put the average nearest - 1 in the middle, //The large 1 is on the left, the 0 is on the right, and then traverse from left to right and from right to left. Find the left 0 and the right one, and exchange them one by one int x[100]; int* p0 = p; //rewind p; //Definition of judgment controller: int fixed = 0; //Are you ready for the first step int single = 1; //Odd or even if ((n % 2) == 0) { single = 0; } int big = 0; //Did you find the big one? int small = 0; //Did you find the small one int ready = 0; //Find the big and the small. int changetime = n / 2; //the times of changes ; for (int i = 0; i < n; i++) { if (*(p + i) > ave) { x[i] = 1; } if (*(p + i) < ave) { x[i] = 0; } if (*(p + i) == ave) { x[i] = -1; } } int nearsit = 0; double distance = 114514; double u; int mindis = 114514; while (fixed == 0) { for (int i = 0; i < n; i++) { distance = (*(p + i)) - long long(ave); u = (fabs(distance)); if (u < mindis) { nearsit = i; mindis = u; } } cout << "\n\ntesting....\n" << nearsit + 1 << " = the nearest one to the average number \t" << mindis << " is the nearset distance \n"; fixed = 1; } p = p0; //return p; if (single == 1) { int temp = 0; temp = *(p + n / 2); *(p + n / 2) = *(p + nearsit); *(p + nearsit) = temp; //Swap the nearest number with the middle number... When there are odd projects p = p0; int r = 0; for (int i = 0; ready == 1; i++) { int j = n - 1; big = 0; small = 0; ready = 0; if (x[i] == 1) { big = 1; } if (x[j] == 0) { small = 1; } if (big == 1 && small == 1) { int t = 0; t = *(p + i); *(p + i) = *(p + j); *(p + j) = t; x[i] = -1; x[j] = -1; } r++; j--; if (changetime == r) ready = 1; } } else { p = p0; int r = 0; for (int i = 0; ready == 1; i++) { int j = n - 1; big = 0; small = 0; ready = 0; if (x[i] == 1) { big = 1; } if (x[j] == 0) { small = 1; } if (big == 1 && small == 1) { int t = 0; t = *(p + i); *(p + i) = *(p + j); *(p + j) = t; x[i] = -1; x[j] = -1; } j--; r++; if (changetime == r) ready = 1; } } }
I vaguely remember that day, I began to like putting several int variables where I need to judge the state. 1 is true, 0...
Then there is the application of pointers in the array. What each position represents begins to be understood slowly...
It's very tedious. You need to find the nearest distance and judge, but the standard answer doesn't have this process at all. It's too complicated to do, and it's even a semi-finished product - finally, you fill in the homework with the only successful case:)
Teacher: there is no change at all!
Me: full of absurd words and bitter tears.
int main() { long out=0; char in[10]; gets_s(in,10); int n = strlen(in); int i; char* p = in; for (i = 0; i < n; i++) { out = out * 10 + (*(p + i) - '0'); //Multiply 10 all the way from one bit to improve the number of bits! } cout << out; } //(* (p + i) - '0') really doesn't poke. 1 is in the 49th bit and 2 is in the 50th bit.
From that day on, I finally found out what the watch at the back of the textbook was for.
This problem says that the string into an integer output, in fact, a simpler method was found when writing the paper.
out=out × 10 + (* (p+i - '0')) is stolen. Stealing? What can a scholar call stealing
//Idea: traverse for and find an "integer" -- find the result as above, then put it o[] inside as above, and finally output. int main() { char a[50]; cin >> a; int n = strlen(a); int o[10] = { 1,1,1,1,1,1,1,1,1,1 };//Square result -- no -1; char* p = a; //funtion //ascii ma calculation: int output; int i; int io=0; int k = 1; int t; for (i = 0; i < n; i++) { if ((a[i] - '0') >= 0 && (a[i] - '0') <= 9) { o[io] = 0; o[io] = o[io]*(pow(10,k))+((p[i]) - '0'); k = 1; if(i!=n-1) { for (t = i+1;;) { if ((a[t] - '0') >= 0 && (a[t] - '0') <= 9) { o[io] = o[io] * (pow(10, k)) + ((p[t]) - '0'); k++; t++; continue; } else { k = 0; io++; i = t; break; } } } } } //Output: / / output function for (i = 0; i < 10; i++) { if(o[i]!=1) cout << o[i]<<" "; } return 0; }
subject
10)Write a program to convert a digital string in a string into the corresponding integer output (It is assumed that all are integers, and the decimal case does not need to be considered) For example: input string: abs123efe45sefe89sef Output integer: 123 45 89 (3 integers are output)
Structure, etc...
From here on, I'm used to giving all header files at the beginning. Next, I use block comments to continuously output.
struct student { char num[6] = { 0,0,0,1 }; char name[8]="noname"; char sex='m'; double score[3] = {0.0,0.0,0.0}; double all = 0.0; }; //void fun(student stu[],int); void input(student stu[],int); void output(student stu[],int N); int main() { int N; cout << "How many man are there? :\n"; cin >> N; student stu[50]; input(stu,N); int i; int j; double s=0; for (i = 0; i < N; i++) { for (j = 0; j < 3; j++) { s = s + stu[i].score[j]; } stu[i].all = s; } output(stu, N); return 0; } void input(student stu[], int N) { int i, j; for (i = 0; i < N; i++) { cout << endl << "input the scores of the students now : " << endl; cout << "NO. :"; cin >> stu[i].num; cout << "name : "; cin >> stu[i].name; cout << "sex :(M or W)"; cin >> stu[i].sex; cout << endl; for (j = 0; j < 3; j++) { cout << "score" << j + 1 << ":"; cin >> stu[i].score[j]; if (stu[i].score[j] < 0 || stu[i].score[j]>100) { cout << "error!from 0~100 "; cin >> stu[i].score[j]; } } } } void output(student stu[],int N) { int i, j; cout << endl << endl << " NO. name sex score1 score2 score3 add-all" << endl; for (i = 0; i < N; i++) { cout << " "<<setw(8) << stu[i].num << " " << setw(10) << stu[i].name << " " << setw(2) << stu[i].sex << " "; for (j = 0; j < 3; j++) { cout << setw(3) << stu[i].score[j] << " "; } cout<<stu[i].all << endl; }
I Purpose of the experiment:
Familiar with object-oriented programming method.
Master the use of classes and objects.
II Experiment content:
1) Define Triangle class. Requirements:
- You can define objects in the main function as follows:
Triangle a;
Triangle a(2.5);
Triangle a(2.5,2.4);
Triangle a(2.5,2.4,2.7);
- Displays information about triangles: length and perimeter of three sides.
2) Define Student class students, including Student number, name, age and scores of several courses. Requirements:
- Initialize a student object with keyboard input information:
- Ask students for average grades.
- Output all information.
3) Write Time class, requirements
- Information including year, month and day;
- The constructor initializes the object of the class (the default is January 1, 2013, and the user can also provide several parameters to specify the date);
- The time represented by the object (yyyy MM DD) can be output in standard format.
4) Establish an object array, put the data (student number and grade) of 5 students, and point to the number with a pointer
Group the first element and output the data of students 1, 3 and 5.
5) Establish an object array, put the data (student number and grade) of 5 students, and set up a function
max, use the object pointer as the function parameter, find out the highest score among the five students in max, and
Output its student number.
III Experimental steps or procedures (correct source program after debugging)
//All header files required below
#include<iostream>
#include<iostream>
#include<cmath>
#include<cstdio>
#include<iostream>
#include<stdlib.h>
#include<iomanip>
#include<cmath>
#include <stdio.h>
#include <tchar.h>
#include<string>
using namespace std;
I:
class Triangle
{
public:
Triangle(float m= 1, float n= 1, float i=1):a(m), b(n), c(i) {};
void display()
{
cout << "The triangle is that : \n";
cout << "a: " << a << "\nb: " << b << "\nc: " << c<<"\n\ncircle: "<<circle()<<"\n\n\n\n";
}
float circle()
{
return ci = a + b + c;
}
private:
float a;
float b;
float c;
float ci=0;
};
int main()
{
Triangle t1, t2(2.5), t3(2.5,2.4),t4(2.5,2.4,2.7);
t1.display();
t2.display();
t3.display();
t4.display();
return 0;
}
II:
//Define Student class students, including Student number, name, age and scores of several courses. Requirements: input information on the keyboard, initialize a Student object, and find the average score of students. Output all information.
const int N = 3; / / several courses amout
class student
{
public:
student(int u = 100, int a = 18) :num(u), age(a) {};
void input()
{
cout << "put in the data of this student \n\n";
cin >> num >> name >> age;
cout << "cput in score * 3\n";
for (int i = 0; i < N; i++)
{
cin >> score[i];
}
}
void output()
{
cout << "\n\n"<<num << " " << name << " " << age << " \n\n The scores are that : ";
for (int j = 0; j < N; j++)
{
cout << score[j]<<" ";
}
cout << "\n\nthe average score is "<<average()<<"\n\n\n";
}
float average()
{
float sum=0;
for (int j = 0; j < N; j++)
{
sum += score[j];
}
return sum/N;
}
private:
int num; / / student number
char name[10]="unknown"; / / name
int age; / / age
float score[N] = {0,0,0}; / / achievement
};
int main()
{
student s;
s.input();
s.output();
return 0;
}
III:
//Write the Time class, which is required to contain the information of year, month and day; The constructor initializes the object of the class (the default is January 1, 2013, and the user can also provide several parameters to specify the date);
//The time represented by the object can be output in standard format (yyyy - mm - dd).
class Time
{
public:
Time(int = 2013, int = 1, int = 1);
void show();
private:
int year;
int month;
int day;
};
Time::Time(int y, int m, int d) { year = y; month = m; day = d; }
int main()
{
int a, b, c;
cin >> a >> b >> c;
Time t1(a, b, c), t2(a), t3(a, b);
t1.show();
t2.show();
t3.show();
return 0;
}
void Time::show()
{
cout << year << " - " << month << " - " << day << endl;
}
IV:
//Establish an object array, put the data of 5 students (student number and grade), point to the first element of the array with a pointer, and output the data of the 1st, 3rd and 5th students.
class student
{
public:
student(int = 100, float = 100);
void output();
int num;
float score;
};
void student::output()
{
cout << num << " " << score << endl;
}
student::student(int i, float j) :num(i), score(j) {};
int main()
{
student stu[5] = { student(101, 78.5), student(102, 88), student(103, 99) ,student(155, 89) ,student(177, 79) };
student* p = stu;
for (int i = 0; i < 3; p=p+2, i++)
{
p->output();
}
return 0;
}
V:
class student
{
public:
student(int = 100, float = 100);
void output();
int num;
float score;
};
void student::output()
{
cout << num << " " << score << endl;
}
student::student(int i, float j) :num(i), score(j) {};
int main()
{
student stu[5] = { student(101, 78.5), student(102, 88), student(103, 99),student(155, 89) ,student(177, 79) };
student* p = stu;
for (int i = 0; i < 3; p++, i++)
{
p->output();
}
void max(student * arr);
student* arr = stu;
max(arr);
return 0;
}
//p[0].score; !!!
void max(student* p)
{
float max = p[0].score;
int m = 0;
for (int i = 1; i < 5; i++)
{
if (p[i].score > max)
{
max = p[i].score;
m = i;
}
}
cout << "\nthe best person:\t" << p[m].num << endl;
}
class Circle {public: Circle() { x = 0; y = 0; r = 0; } Circle(double xi,double yi,double ri):x(xi),y(yi),r(ri){} double operator +(Circle &a) { double s1 = 3.14 * this->r * this->r; double s2 = 3.14 * a.r * a.r; return (s1 + s2); } double operator +(double t) { return ((3.14 * this->r * this->r)+t); } friend istream& operator >>(istream& input, Circle& c); friend double area(Circle*a); private: double x; double y; double r; }; istream& operator >>(istream& cin, Circle &a) { cout << "put in the (x,y) and r for this circle !:\n\n"; cin >> a.x >> a.y >> a.r; return cin; } double area(Circle *a) { return (3.14 * (a->r) * (a->r)); } int main() { double S=0; Circle a, b(3,4,2), c; cin >> a; Circle *p[3] = { &a,&b,&c }; int i; for (i = 0; i < 3; i++) { S += area(p[i]); } cout << "the sum of area = " << S << endl<<endl; cout << "that can be another way :) \n\n"; double t = 0; t = a + b; t =c+t; cout << t << endl << endl; return 0; }
Class is used for the first time.
//Define the Point class, derive the Rectangle class and Circle class from it, and output the information of each derived class object: // Circle to output: center point coordinates, radius and area. //Rectangle to output: coordinates, length, width and area of the upper left corner. class Point { public: virtual double area() = 0; virtual void output() = 0; private: // virtual base class; }; class Rectangle :public Point { public: Rectangle(double ai,double bi,double xi,double yi):a(ai),b(bi),x(xi),y(yi){} Rectangle() { x = y = 0; a = b = 0; } virtual void output() { cout << "this is a rectangle :\n"; cout << "length and width: " << a << ", " << b<<endl; cout << "position of left up :" << "(" << x << " ," << y << ")\n\n" ; cout << "the area is : " << area() << endl << endl; } virtual double area() { return (a * b); } private: double a; double b; double x; double y; }; class Triangle :public Point { public: Triangle(double r1,double xi, double yi) :r(r1), x(xi), y(yi) {} Triangle() { x = y =r= 0; } virtual void output() { cout << "this is a triangle :\n"; cout << "banjing :" << x<<endl; cout << "position of circle:" << "(" << x << " ," << y << ")\n\n"; cout << "the area is : " << area() << endl<<endl; } virtual double area() { return (3.14*r * r); } private: double r; double x; double y; }; int main() { Rectangle r(3,5,1,1); Triangle t(3,1,1); Point* p1, * p2; p1 = &r; p2 = &t; p1->output(); p2->output(); return 0; }
The last few assignments are taken away in a wave, which is not different from that behind the textbook
//Shape derives three derivatives, using an output function to output their perimeter class Shape { public: virtual double ci() = 0; private: //Virtual base class,, }; class Circle :public Shape { public: virtual double ci() { return 2 * 3.1415926 * r; } Circle(double ra) { r = ra; } private: double r; }; class Triangle :public Shape { public: virtual double ci() { return a + b + c; } Triangle(double ai, double bi, double ci) { a = ai; b = bi; c = ci; } private: double a; double b; double c; }; class Rectangle :public Shape { public: virtual double ci() { return 2 * (a + b); } Rectangle(double ai, double bi) { a = ai; b = bi; } private: double a; double b; }; //output function void printci(Shape* s) { cout << s->ci() << endl; } int main() { Circle c(3); Triangle t(3, 4, 5); Rectangle r(3, 5); Shape* p[3] = { &c,&t,&r }; int i; for (i = 0; i < 3; i++) { printci(p[i]); } return 0; }
Oh, finished.
Then play games.