lundi 30 mars 2015

find preorder given postorder and inorder



it is the code of uva 536 http://ift.tt/19njc3i


But I want to ask,if the questions changes, that means asking preorder given postorder and inorder, how can i change my code???any one helps?it is urgent,thanks(NOT A HOMEWORK)



#include <stdio.h>
#include <string.h>
char inorder[100], preorder[100];
char line[200];
void trace(int ihead, int itail, int phead, int ptail)
{
char split = preorder[phead];
int i;
int len;
if (ihead > itail) return;
for (i=ihead; inorder[i] != split; ++i) ;
len = i - ihead;
trace(ihead, i-1, phead+1, phead+len);
len = itail - i - 1;
trace(i+1, itail, ptail - len, ptail);
printf("%c", split);
}
int main() {
int n;
for (;;) {
if (gets(line) == NULL) break;
sscanf(line, "%s %s", preorder, inorder);
n = strlen(inorder);
trace(0, n-1, 0, n-1);
printf("\n");}
return 0;
}



GetProcessAffinityMask returns null process affinity



In a very simple test console application, I tried to get process' affinity mask:



#include <cstdlib>
#include <cstdio>
#include <windows.h>

int main()
{
while (1)
{
DWORD dwProcessAffinityMask = 0;
DWORD dwSystemAffinityMask = 0;

BOOL res = GetProcessAffinityMask(
GetCurrentProcess(),
(PDWORD_PTR)&dwProcessAffinityMask,
(PDWORD_PTR)&dwSystemAffinityMask);

printf("%d 0x%X 0x%X\n",
res,
dwProcessAffinityMask,
dwSystemAffinityMask);

Sleep(1000);
}

return 0;
}


Here is the output (64-bit executable, 64-bit system, meaning I do not fall into the WoW64 special case):



1 0x0 0x3
1 0x0 0x3
...


Running on my laptop, which has a 2 cores CPU, the resulting system's mask looks correct. But I don't understand the meaning of the dwProcessAffinityMask value I get here. Just for the sake of it, I also tried to toy around with the Task Manager by changing the process' affinity mask but the output remains the same.


This behavior doesn't seem to be documented.




C++ Passing user input to a parameter for a constructors



Here is in C++ Passing user input to a variable to a parameter for a function in. But I need pass parameters from user for constructor parameters. Please help me with simple example. Don't have to be same as mine, you can do all different but I just need it.



#include <iostream>
#include <cmath>

using namespace std;

float stockMarketCalculator(float p, float r, int t){
float a;

for(int day = 1; day <=t; day++){
a = p * pow(1+r, day);
cout << a << endl;
}

}

int main()
{
float p;
float r;
int t;

cout << "Please enter the principle" << endl;
cin >> p >> endl;

cout << "Please enter the rate" << endl;
cin >> r >> endl;

cout << "Please enter the time in days" << endl;
cin >> t >> endl;
cout << stockMarketCalculator(p, r, t);

return 0;
}



C assembler function casting



I came across this piece of code (for the whole program see this page, see the program named "srop.c").


My question is regarding how func is used in the main method. I have only kept the code which I thought could be related.


It is the line *ret = (int)func +4; that confuses me.


There are three questions I have regarding this:



  1. func(void) is a function, should it not be called with func() (note the brackets)

  2. Accepting that that might be some to me unknown way of calling a function, how can it be casted to an int when it should return void?

  3. I understand that the author doesn't want to save the frame pointer nor update it (the prologue), as his comment indicates. How is this skipping-two-lines ahead achieved with casting the function to an int and adding four?


.



(gdb) disassemble func
Dump of assembler code for function func:
0x000000000040069b <+0>: push %rbp
0x000000000040069c <+1>: mov %rsp,%rbp
0x000000000040069f <+4>: mov $0xf,%rax
0x00000000004006a6 <+11>: retq
0x00000000004006a7 <+12>: pop %rbp
0x00000000004006a8 <+13>: retq
End of assembler dump.


Possibly relevant is that when compiled gcc tells me the following:

warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]


Please see below for the code.



void func(void)
{
asm("mov $0xf,%rax\n\t");
asm("retq\n\t");
}

int main(void)
{
unsigned long *ret;

/*...*/

/* overflowing */
ret = (unsigned long *)&ret + 2;
*ret = (int)func +4; //skip gadget's function prologue

/*...*/

return 0;
}



Error in my switch case



So I'm trying to make an input function That takes in things separated by a comma and puts each thing into it's own array. I think I almost have it figured with this switch, but it gets stuck in the second statement. I don't know why.



/*************************************************************************

3/25/2015
This program takes in a file of the format
PART,2.000,-1,0.050,V
PART,0.975,-1,0.025,V
PART,3.000,+1,0.010,F
GAP,0.000,0.080
does the tolerance analysis
**************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#define BUFFER_SIZE 1024
#define ARRAYSIZE 100
void input(char *gapPart, float *nom,float *tollerance,int *SIGNS,char *V_F,float Spec_Minnimum,float Spec_Maximum);
void toleracningPt1(int size, char gapPart[], float nom[],float tollerance[],int SIGNS[],char V_F[],float Spec_Minnimum,float Spec_Maximum);
int main(){
/**Decs**/
float nom[ARRAYSIZE]; //holds the nominal values (2.00, .975 ect)
float tollerance[ARRAYSIZE]; //holds the third value (.05, .025, ect)
int SIGNS[ARRAYSIZE]; // signifies if the value goes up or down
char gapPart[ARRAYSIZE];// holds the value if it's a gap or part
char V_F[ARRAYSIZE]; // F things cannot be changed, V things can be
int size=0;
float Spec_Minnimum=0, Spec_Maximum=0;
/**custom functions**/
input(gapPart, nom, tollerance, SIGNS, V_F, Spec_Minnimum, Spec_Maximum);
toleracningPt1(size, gapPart,nom, tollerance, SIGNS, V_F, Spec_Minnimum, Spec_Maximum);
return 0;
}
/***********************************************************************************************************/
void input(char *gapPart,float *nom,float *tollerance,int *SIGNS,char *V_F,float Spec_Minnimum,float Spec_Maximum){

const char *delimiterCharacters = " ";
const char *delimiterCharacters2 = ",";
const char *filename = "tin.txt";
FILE *inputFile = fopen( filename, "r" );
char buffer[ BUFFER_SIZE ];
char *lastToken;
int i=1, step;

printf("File Data\n");
/* usual error check*/
if(inputFile == NULL ){
fprintf( stderr, "Unable to open file %s\n", filename );
}else{
/**Prints out contents of the file **/
while( fgets(buffer, BUFFER_SIZE,inputFile) != NULL ){// while there is stuff to do this with
lastToken = strtok( buffer, delimiterCharacters );
while( lastToken != NULL ){//same song..
printf( "%s\n", lastToken );
lastToken = strtok( NULL, delimiterCharacters );// clear out lastToken
}

}

rewind(inputFile);

while( fgets(buffer, BUFFER_SIZE,inputFile) != NULL ){// while there is stuff to do this with
lastToken = strtok( buffer, delimiterCharacters2 );
while( lastToken != NULL ){//same song..
//strtok into seperate arrays
while(i=1,i<5,++i){
switch(i){
case 1:
fscanf(inputFile,"%s\n", &gapPart[i]);
printf("debug1");
++i;
break;
case 2:
printf("debug2");
fscanf(inputFile,"%f\n", &nom[i]);
++i;
break;
case 3:
printf("debug3");
fscanf(inputFile,"%d\n", &SIGNS[i]);
++i;
break;
case 4:
printf("debug4");
fscanf(inputFile,"%f\n", &tollerance[i]);
break;
case 5:
printf("debug5");
fscanf(inputFile,"%c\n", &V_F[i]);
break;
default:
printf("Error");
}
}


}
lastToken = strtok( NULL, delimiterCharacters2 );// clear out lastToken
}
}



fclose(inputFile );

}


/*****************************************************************************************************************/
void toleracningPt1(int size, char gapPart[], float nom[],float tollerance[],int SIGNS[],char V_F[],float Spec_Minnimum,float Spec_Maximum)
{
int x;
float Act_Gap, Act_Tollerance, Maximum_Gap = 0.0, Minnimum_Gap = 0.0;
for ( x=0, Act_Gap = 0; x<size; x++){ //does tolerance math
Act_Gap = Act_Gap + (nom[x]*SIGNS[x]);
}
for ( x=0, Act_Tollerance = 0; x<size; x++){
Act_Tollerance = Act_Tollerance + (tollerance[x]);
}
for (x= 0, Maximum_Gap = 0; x<size; x++){
Maximum_Gap = (nom[x]*SIGNS[x]+tollerance[x])+Maximum_Gap;
Minnimum_Gap = (nom[x]*SIGNS[x]-tollerance[x])+Minnimum_Gap;
}

printf("Actual Gap Mean: %.3f\"\n", Act_Gap); //printing
printf("Actual Gap Tolerance: %.3f\"\n", Act_Tollerance);
if (Maximum_Gap > Spec_Maximum){
printf("The maximum gap (%.3f\") is (Greater) than specified (%.3f\")\n", Maximum_Gap, Spec_Maximum);
}
if (Maximum_Gap < Spec_Maximum){
printf("The maximum gap (%.3f\") is (Less) than specified (%.3f\")\n", Maximum_Gap, Spec_Maximum);
}
if (Minnimum_Gap > Spec_Minnimum){
printf("The minimum gap (%.3f\") is (Greater) than specified (%.3f\")\n", Minnimum_Gap, Spec_Minnimum);
}
if (Minnimum_Gap < Spec_Minnimum){
printf("The minimum gap (%.3f\") is (Less) than specified (%.3f\")\n", Minnimum_Gap, Spec_Minnimum);
}
}



using fork function after getting user's input



How do you get the fork function to work after getting the user's input from the fgets() function and getting tokens from the user's input?


My code:



#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <errno.h>

/* the line can have at most 2000 words*/
void tokeniseLine(char *Line, char **Words, int *Wordn);

/* break line into words separated by whitespace, placing them in the
array words, and setting the count to Wordn */

void search&execute();

int main()
{
char Line[4000], *Words[2000], string[4000];
int Stops=0,Wordn=0;
char *end = "exit";

while(1)
{
printf("Enter program: ");
fgets(Line, 4000, stdin ); /* read a line of text here */

/* use of exitting begins when user enters 'exit' or when the program finally locates/can't locate the user's requested file*/
if ( strcmp(Line, end) == 0 ){
exit(0);
}
else
if ( strcmp(Line, end) != 0 ) {
printf("file successfully found.");
tokeniseLine(Line,Words,&Wordn);
search&execute();//using fork function to make process
}
return 0;
}

void tokeniseLine(char *Line, char **Words, int *Wordn)
{
char *token;

/* get the first token */
token = strtok(Line, " \t\n");

/* walk through other tokens */
while( token != NULL )
{
token = strtok(NULL, " \t\n");
}
return;
}

void search&execute()//this is the function which I wanted to work last after the user input is tokenised
{
pid_t childpid; /* variable to store the child's pid */
int retval; /* child process: user-provided return code */
int status; /* parent process: child's exit status */

/* only 1 int variable is needed because each process would have its
own instance of the variable
here, 2 int variables are used for clarity */

/* now create new process */
childpid = fork();

if (childpid >= 0) /* fork succeeded */
{
if (childpid == 0) /* fork() returns 0 to the child process */
{
printf("CHILD: I am the child process!\n");
printf("CHILD: Here's my PID: %d\n", getpid());
printf("CHILD: My parent's PID is: %d\n", getppid());
printf("CHILD: The value of my copy of childpid is: %d\n",childpid);
printf("CHILD: Sleeping for 1 second...\n");
sleep(1); /* sleep for 1 second */
printf("CHILD: Enter an exit value (0 to 255): ");
scanf(" %d", &retval);
printf("CHILD: Goodbye!\n");
exit(retval); /* child exits with user-provided return code */
}
else /* fork() returns new pid to the parent process */
{
printf("PARENT: I am the parent process!\n");
printf("PARENT: Here's my PID: %d\n", getpid());
printf("PARENT: The value of my copy of childpid is %d\n",childpid);
printf("PARENT: I will now wait for my child to exit.\n");
wait(&status); /* wait for child to exit, and store its status */
printf("PARENT: Child's exit code is: %d\n", WEXITSTATUS(status));
printf("PARENT: Goodbye!\n");
exit(0); /* parent exits */
}
}
else /* fork returns -1 on failure */
{
perror("fork"); /* display error message */
exit(0);
}


}


I tried to have the fork function to return the fork value, but it doesn't work when I tried to add in user input. How do you fix that?




Getline in loop consuming constantly more memory in embedded linux



After running application over weekend I noticed in the morning that it was killed because no memory was left.


After commenting some parts of code, I see that this procedure might be a problem, but I can't figure out why.



void readData (char * path) {

// Reading and parsing config from file
FILE * fp;
size_t len = 0;
size_t read;

char *begin, *end, line[100], data[100];

fp = fopen(path, "r");
if (fp != NULL)
{
while ((read = getline(&line, &len, fp)) != -1) {
/* begin = strstr(line, "\"data\":[")+8;
end = strstr(begin, "]");
strncpy(data, begin,strlen(begin) - strlen(end));
data[strlen(begin) - strlen(end)] = 0;
*/
int tmp;

// sscanf(data,"%d,%d,%d,%d,%d,%d",&tmp,&ss.tensionRaw,&tmp,&tmp,&ss.depthRaw,&ss.speedRaw);
ss.tension = cs.tensionCoeff * ss.tensionRaw;
ss.speed = cs.speedCoeff * ss.speedRaw;
ss.depth = cs.depthCoeff * ss.depthRaw;

/* begin = strstr(line, "\"failedRequests\":\"")+18;
end = strstr(begin, "\"");
strncpy(data, begin,strlen(begin) - strlen(end));
data[strlen(begin) - strlen(end)] = 0;
ss.connectionOK = atoi(data);*/
}
}
fclose(fp);
}


Can getline cause problems like this? I monitor memory use of app by "top" and every 15-20 sec it gets around 4K more. When i comment whole while loop, it's not increasing.