Post your C/C++ Programs Here

Status
Not open for further replies.

Neuron

Electronic.
Re: Post ur C/C++ Programs Here

Okay ,a file encryptor and decryptor.

Code:
#include<conio.h>
#include<stdio.h>
#include<string.h>

void main() {

	FILE *fs,*ft;
	char c=0,n1[89],n2[89],pass[20],pass1[20],p,n3[89],ext[7];
	int passlen,i,skp=0,nlen,tflg=0,slen,k,j=0,l;
	while(1) {
		i=0;
		printf("\n1.Encrypt\t2.Decrypt\t3.Exit\n>>>");
		c=getch();
		switch(c) {
			case '1': 
				printf("Enter the name of the source file>>>");
				scanf("%s",n1);
				strcpy(n2,n1);
				strcat(n2,".ncrpt");
				slen=strlen(n1);
				if(n1[slen-4]=='.'&&n1[slen-3]=='t'&&n1[slen-2]=='x'&&n1[slen-1]=='t')
					tflg=1;
				else tflg=0;

				fs=fopen(n1,"rb");
	
				if(fs==NULL) {
					puts("Unable to open the source file");
					break;
				}
				printf("Enter the password>>>");
				while((p=getch())&&p!=13) {
					pass[i]=p;
					i++;
				}
				pass[i]='\0';
				printf("\nConfirm password>>>");
				i=0;
				while((p=getch())&&p!=13) {
					pass1[i]=p;
					i++;
				}
				pass1[i]='\0';
				if(!strcmp(pass,pass1)) {

					printf("\n\nEncrypting...\n");
					passlen=strlen(pass);
					ft=fopen(n2,"wb");
					if(ft==NULL) {
						puts("Unable to open the destination file");
						break;
					}
					
					i=0;
					if(!feof(fs))
					do 	{
						if(tflg==1) {
							c=fgetc(fs);
							fputc(c+pass[i],ft);
							i++;
							if(i==passlen)
								i=0;
						}
						else if(skp<180) {
							c=fgetc(fs);
							fputc(c,ft);
							skp++;
						}
						else {
							c=fgetc(fs);
							fputc(c+pass[i],ft);
							i++;
							if(i==passlen)
								i=0;
						}
						if(feof(fs))
							break;
					} while(!feof(fs));
				}
				else	{
					printf("\nPassword Confirmation failed");
					break;
				}
				skp=0;
				printf("\nDone!!!\n");
				printf("\n\nThe encrypted file is <<<%s",n2);
				fclose(fs);
				fclose(ft);
				getch();
				break;
			case '2':
				printf("\nEnter the name of the file to be decrypted>>>");
				scanf("%s",n1);
				nlen=strlen(n1);
				if(n1[nlen-6]!='.') {
					printf("\nCannot decrypt\n");
					break;
				}
				if(n1[nlen-5]!='n')  {
					printf("\nCannot decrypt\n");
					break;
				}
				if(n1[nlen-4]!='c')  {
					printf("\nCannot decrypt\n");
					break;
				}
				if(n1[nlen-3]!='r') {
					printf("\nCannot decrypt\n");
					break;
				}
				if(n1[nlen-2]!='p') {
					printf("\nCannot decrypt\n");
					break;
				}
				if(n1[nlen-1]!='t') {
					printf("\nCannot decrypt\n");
					break;
				}
				if(n1[nlen-10]=='.'&&n1[nlen-9]=='t'&&n1[nlen-8]=='x'&&n1[nlen-7]=='t')
					tflg=1;
				printf("\nEnter the password>>>");
				while((p=getch())&&p!=13) {
					pass[i]=p;
					i++;
				}
				pass[i]='\0';
				printf("\nConfirm password>>>");
				i=0;
				while((p=getch())&&p!=13) {
					pass1[i]=p;
					i++;
				}
				pass1[i]='\0';
				passlen=strlen(pass);

				if(!strcmp(pass,pass1)) {
					printf("\n\nDecrypting...\n");

					for(i=0;i<nlen-6;i++)
						n2[i]=n1[i];
					n2[i]='\0';
					for(i=nlen-7,j=0;n2[i]!='.';i--,j++)
						ext[j]=n2[i];
					l=strlen(n2);
					
					ext[j]='\0';
					n2[l-strlen(ext)-1]='_';
					n2[l-strlen(ext)]='.';
					for(i=(l-strlen(ext)+1),j--;j>=0;j--,i++)
						n2[i]=ext[j];
					n2[i]='\0';

					

					fs=fopen(n1,"rb");
	
					if(fs==NULL) {
						puts("Unable to open the source file");
						break;
					}

					ft=fopen(n2,"wb");
					if(ft==NULL) {
						puts("Unable to open the destination file");
						break;
					}
					i=0;

					if(!feof(fs))	
					do 	{
						if(tflg==1) {
							c=fgetc(fs);
							if(!feof(fs))
							fputc(c-pass[i],ft);
							i++;
							if(i==passlen)
								i=0;
						}
						else if(skp<180) {
							c=fgetc(fs);
							fputc(c,ft);
							skp++;
						}
					    else {
							c=fgetc(fs);
							fputc(c-pass[i],ft);
							i++;
							if(i==passlen)
								i=0;
						}
					} while(!feof(fs));
					printf("\nDone!!!\n");
					printf("\n\nThe decrypted file is>>%s",n2);
				
				}
				else {
					printf("\nPassword confirmation failed");
					break;
				}
				fclose(fs);
				fclose(ft);
	
				break;

				case '3':
					exit(0);
				default:
					printf("\nInvalid choice\n");
					break;
			}
	}
}

In Vista or Win7 run as admin.
File path format eg: c:/abc.exe
No spaces allowed in path.eg: c:/Program Files/abc.exe is invalid.
 
Last edited:

Neuron

Electronic.
Re: Post ur C/C++ Programs Here

(sin(3.22))^2+(cos(3.22))^2
will have the output 1.01 (come on!! its close to 1!!)

Fixed a simple but major bug.The inputs of trigonometric functions must be in radians.So to get the correct answer for cos(90 degrees) you will have pass cos(90*3.14/180) as the argument.Replace
Code:
float uoperation(float o1,int o)
{
      switch(o)
      {
               case 0: return sin(o1);
               case 1: return cos(o1);
               case 2: return tan(o1);
               case 3: return 1/tan(o1);
               case 4: return 1/cos(o1);
               case 5: return 1/sin(o1);
               case 6: return mod(o1);
               case 7: if(o1>0)
               return log(o1);
               else
               return 1000;
               case 8: return floor(o1);
               case 9: return ceil(o1);
      }
}
with

Code:
float uoperation(float o1,int o)
{
      switch(o)
      {
               case 0: return sin(o1*3.14/180);
               case 1: return cos(o1*3.14/180);
               case 2: return tan(o1*3.14/180);
               case 3: return 1/tan(o1*3.14/180);
               case 4: return 1/cos(o1*3.14/180);
               case 5: return 1/sin(o1*3.14/180);
               case 6: return mod(o1*3.14/180);
               case 7: if(o1>0)
               return log(o1);
               else
               return 1000;
               case 8: return floor(o1);
               case 9: return ceil(o1);
      }
}

Now check the output.
 

nims11

BIOS Terminator
Re: Post ur C/C++ Programs Here

IMO adding a separate function for trigonometric functions taking argument in degree will be good as in many cases, arguments in radians would be required(lyk in plotting a trig func with another function). What about adding new functions by prefixing the trig functions with a 'd' indicating that it takes argument in degree?
 

Garbage

God of Mistakes...
Re: Post ur C/C++ Programs Here

Couldn't resist posting this link here : visual c++ - Why is this program erroneously rejected by three C++ compilers? - Stack Overflow

:p
 
T

The Conqueror

Guest
Re: Post ur C/C++ Programs Here

Anyone for this problem?
Can you solve this problem?

Most mobile phones have their own private dictionary of words to allow users to type messages
quicker. On a typical phone, each number key is assigned a subset of the alphabet
{a, b, ..., z}: 2 is assigned the subset {a, b, c}, 3 is assigned {d, e, f}, 4 is assigned {g, h, i}, 5 is
assigned {j, k, l}, 6 is assigned {m,n, o}, 7 is assigned {p, q, r, s}, 8 is assigned {t, u, v} and 9
is assigned {w, x, y, z}.
When the user types a sequence of numbers, this sequence is mapped to all possible
words that can be constructed from the key assignment. For instance, if the user types 66,
this could correspond to any one of the letter sequences “mm”, “mn”, “mo”, “nm”, “nn”,
“no”, “om”, “on” or “oo”. These letter sequences are looked up in the dictionary stored
in the phone and all matches are reported. For instance, if the phone’s dictionary contains
only “on” and “no” from this set of sequences, the user will be offered a choice of “on”
or “no” to insert in the message. Similarly, the input 4663 might be interpreted as either
“good” or “home”. An input sequence may have a unique interpretation—for example, the
only word in the dictionary matching the input 28 may be “at”. Other sequences may not
match any word in the dictionary—for instance, 99999.
Your task is the following. Given the typical assignment from number keys to letters of
the alphabet given above and given a dictionary of words, report the input sequence that
matches the largest number of words in the dictionary. For example, if the dictionary consists
of the words {at, on, good, no} then the answer is 66, because 66 matches both “on” and
“no” while no other input matches more than one word in the dictionary. On the other
hand, with the dictionary {at, on, good, no, home, gone}, the answer is 4663, because 4663
matches three words, “good”, “home” and “gone” in the dictionary.
Input format
The first line of input is an integer M, the number of words in the dictionary. This is
followed by M lines of input. Each line contain one word from the dictionary, where a word
is sequence of characters from the lowercase alphabet {a, b, c, ..., z}.
Note: Each word in the dictionary is, in general, an arbitrary sequence of letters from
{a, b, c, ..., z}. In particular, it is not assumed that the words stored in the dictionary are
valid words in English or any other language.
Output format
A single line containing the input sequence that matches the maximum number of words in
the dictionary. If multiple input sequences qualify for the maximum number of matches, it
suffices to report any one.
Test data
For all inputs, 1 < or equal to M < or equal to 100000. Each word in the dictionary is at most 8 characters long.
In 50% of the inputs, 1 < or equal to M < or equal to1000.

Example
Here is the sample input and output corresponding to the example discussed above.
Sample input 1
4
at
on
good
no
Sample output 1
66
Sample input 2
6
at
on
good
no
home
gone
Sample output 2
4663
Note: Your program should not print anything other than what is specified in the output
format. Please remove all diagnostic print statements before making your final submission.
A program with extraneous output will be treated as incorrect!
 

nims11

BIOS Terminator
Re: Post ur C/C++ Programs Here

Can it be done using this simple method?-
input thw dictionary content. Convert and store them to their nmbr representation on keypad. Output the number with most occurances.
 

Neuron

Electronic.
Re: Post ur C/C++ Programs Here

Can it be done using this simple method?-
input thw dictionary content. Convert and store them to their nmbr representation on keypad. Output the number with most occurances.

That's exactly how it's supposed to be done I guess.

Like this,
a t-----on-----g oo d-----no
2 8-----66-----4 66 3-----66

66 appears thrice in words

a t-----o n-----g oo d-----n o-----h om e-----g on e
2 8-----6 6-----4 66 3-----6 6-----4 66 3-----4 66 3


66 appears 4 times,but since '4663' has 4 digits and 3 appearances.4663 is selected.Maybe 4*3 > 2*4 logic can be used.
 

nims11

BIOS Terminator
Re: Post ur C/C++ Programs Here

That's exactly how it's supposed to be done I guess.

Like this,
a t-----on-----g oo d-----no
2 8-----66-----4 66 3-----66

66 appears thrice in words

why are you counting 66 three tyms? shouldnt it be just two according to the question?
 

Neuron

Electronic.
Re: Post ur C/C++ Programs Here

^^You are right.I was overthinking.So the solution is a lot more simple.
 
T

The Conqueror

Guest
Anyone for the source code for that problem[Dictionary] ? Need it to prepare for olympiad. Which books should I refer to gain insight on these algorithms and logic/?
 

nims11

BIOS Terminator
Yes. How to prepare for that?

look, there will be 2 questions. one will be easy and you will be easily able to create an algo for it.
the other one is usually tough and you will need to apply some popular algo. it will seem difficult as we r not taught these algos in schools. it usually gives a problem where knapsack and dynamic programming is applicable.
only those who solve both problems are selected(as i wasnt selected in two ZCOs where i had solved only one problem as i wasnt aware of knapsack algo).

the MIT OCW has a good lecture on knapsack problems and application of dynamic programming on such probs.
also there are many tutorials for it.

i also suggest you to learn "dijkstra algorithm"(for shortest path problems) from somewhere(i cant find the page from which i learnt it, so google it)
 

nims11

BIOS Terminator
^^ nah.. it was some tutorial which took some cities as example which were showed as some colored circles with links between them. looks like a typical dijkstra algorithm tutorial but it explained really well.
 

mohityadavx

Youngling
Hi!

I have made a calculator but i am not able to debug it as it is not accepting the password i have set for it. Please help me out.
Code:
#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<string.h>

int addition(int x ,int y)
{
int d;
d=x+y;
return(d);
}


int subtraction(int x ,int y)
{
int d;
if(x>y)
d=x-y;
else
y-x;
return(d);
}


float division(float x ,float y)
{
float d;
d=x/y;
return(d);
}


int multiplication(int x ,int y)
{
int d;
d=x*y;
return(d);
}


float sqrt(float x)
{
float d;
d=sqrt(x);
return(d);
}

float nlog(float x)
{
float d;
d=log(x);
return(d);
}

float tlog(float x)
{
float d;
d=log10(x);
return(d);
}

float antilog(float x)
{
float d,e; int z = 10;
e=log(x);

d=pow(z,e);
return(d);
}

int fact(int x)
{
int i;
for(i=1;i<=x;i++)
x=x*i;
return(x);
}

float sin(float x)
{
float d;
d= sin(x);
return(d);
}

float cos(float x)
{
float d;
d= cos(x);
return(d);
}

float tan(float x)
{
float d;
d= tan(x);
return(d);
}

float asin(float x)
{
float d;
d= asin(x);
return(d);
}

float acos(float x)
{
float d;
d= acos(x);
return(d);
}

float atan(float x)
{
float d;
d= atan(x);
return(d);
}


void main()
{
textcolor(RED);
clrscr();
int a,b,ch,i=0; char pass[10]; 
float q,w;
Again:

cout<<"This file is password protected"<<endl;
cout<<endl<<"Enter password"<<endl;

while((pass[i]=getch())!='\r')
{
cout<<"*";
i++;
}

clrscr();

if(strcmp(pass,"mohit")==0)
{
cout<<'/t'<<'/t'<<'/t'<<"MAIN MENU"<<endl<<endl;

cout<<endl<<"1 Standard Calculator"<<endl;
cout<<endl<<"2 Scientific calculator"<<endl;


cout<<endl<<"Enter your choice (1 or 2)"<<endl;
cin>>ch;

if(ch==1)
{
int ch1,k;
float k1;

cout<<"1 Addition"<<endl;
cout<<"2 Subtraction"<<endl;
cout<<"3 Divison"<<endl;
cout<<"4 Multiplication"<<endl;
cout<<"5 Square Root"<<endl;

cout<<endl<<"Enter your choice"<<endl;
cin>>ch1;

switch(ch1)
{
int k;
case 1:
cout<<endl<<"Enter first number"<<endl;
cin>>a;
cout<<endl<<"Enter second number"<<endl;
cin>>b;
k=addition(a,b);
cout<<endl<<"Your Solution is -"<<'/t'<<k;
break;

case 2:
cout<<endl<<"Enter first number"<<endl;
cin>>a;
cout<<endl<<"Enter second number"<<endl;
cin>>b;
k=subtraction(a,b);
cout<<endl<<"Your Solution is -"<<'/t'<<k;
break;


case 3:
cout<<endl<<"Enter first number"<<endl;
cin>>q;
cout<<endl<<"Enter second number"<<endl;
cin>>w;
k1=division(q,w);
cout<<endl<<"Your Solution is -"<<'/t'<<k1;
break;

case 4:
cout<<endl<<"Enter first number"<<endl;
cin>>a;
cout<<endl<<"Enter second number"<<endl;
cin>>b;
k=multiplication(a,b);
cout<<endl<<"Your Solution is -"<<'/t'<<k;
break;

case5:
cout<<endl<<"Enter number"<<endl;
cin>>q;
k1=sqrt(q);
cout<<endl<<"Your Solution is -"<<'/t'<<k1;
break;
}
}

if(ch==2)
{
int ch2,l;
cout<<"1 Addition"<<endl;
cout<<"2 Subtraction ( |x-y|)"<<endl;
cout<<"3 Divison"<<endl;
cout<<"4 Multiplication"<<endl;
cout<<"5 Square Root"<<endl;
cout<<"6 Natural Logaritham"<<endl;
cout<<"7 Logaritham (Base 10)"<<endl;
cout<<"8 Antilogaritham (Base 10)"<<endl;
cout<<"9 Factorial"<<endl;
cout<<"10 Sine"<<endl;
cout<<"11 Cosine"<<endl;
cout<<"12 Tangent"<<endl;
cout<<"13 Arc Sine"<<endl;
cout<<"14 Arc Cosine"<<endl;
cout<<"15 Arc Tangent"<<endl;


cout<<endl<<"Enter your choice"<<endl;
cin>>ch2;

switch(ch2)
{
int l;
float l1;

case 1:
cout<<endl<<"Enter first number"<<endl;
cin>>a;
cout<<endl<<"Enter second number"<<endl;
cin>>b;
l=addition(a,b);
cout<<endl<<"Your Solution is -"<<'/t'<<l;
break;

case 2:
cout<<endl<<"Enter first number"<<endl;
cin>>a;
cout<<endl<<"Enter second number"<<endl;
cin>>b;
l=subtraction(a,b);
cout<<endl<<"Your Solution is -"<<'/t'<<l;
break;


case 3:
cout<<endl<<"Enter first number"<<endl;
cin>>q;
cout<<endl<<"Enter second number"<<endl;
cin>>w;
l1=division(q,w);
cout<<endl<<"Your Solution is -"<<'/t'<<l1;
break;


case 4:
cout<<endl<<"Enter first number"<<endl;
cin>>a;
cout<<endl<<"Enter second number"<<endl;
cin>>b;
l=multiplication(a,b);
cout<<endl<<"Your Solution is -"<<'/t'<<l;
break;


case 5:
cout<<endl<<"Enter number"<<endl;
cin>>a;
l1=sqrt(q);
cout<<endl<<"Your Solution is -"<<'/t'<<l1;
break;

case 6:
cout<<endl<<"Enter number"<<endl;
cin>>q;
l1=nlog(a);
cout<<endl<<"Your Solution is -"<<'/t'<<l1;
break;

case 7:
cout<<endl<<"Enter number"<<endl;
cin>>q;
l1=tlog(a);
cout<<endl<<"Your Solution is -"<<'/t'<<l1;
break;

case 8:
cout<<endl<<"Enter number"<<endl;
cin>>q;
l1=antilog(a);
cout<<endl<<"Your Solution is -"<<'/t'<<l1;
break;

case 9:
cout<<endl<<"Enter number"<<endl;
cin>>a;
l=fact(a);
cout<<endl<<"Your Solution is -"<<'/t'<<l;
break;

case 10:
cout<<endl<<"Enter number"<<endl;
cin>>q;
l1=sin(q);
cout<<endl<<"Your Solution is -"<<'/t'<<l1;
break;

case 11:
cout<<endl<<"Enter number"<<endl;
cin>>q;
l1=cos(q);
cout<<endl<<"Your Solution is -"<<'/t'<<l1;
break;

case 12:
cout<<endl<<"Enter number"<<endl;
cin>>q;
l1=tan(q);
cout<<endl<<"Your Solution is -"<<'/t'<<l1;
break;
 
case 13:
cout<<endl<<"Enter number"<<endl;
cin>>q;
l1=asin(q);
cout<<endl<<"Your Solution is -"<<'/t'<<l1;
break;

case 14:
cout<<endl<<"Enter number"<<endl;
cin>>q;
l1=acos(q);
cout<<endl<<"Your Solution is -"<<'/t'<<l1;
break;

case 15:
cout<<endl<<"Enter number"<<endl;
cin>>q;
l1=atan(q);
cout<<endl<<"Your Solution is -"<<'/t'<<l1;
break;
}
}
}

else
{
cout<<"Access Denied! Incorrect Password"<<endl;
goto Again;
}
getch();
}
 
Status
Not open for further replies.
Top Bottom