#include<stdio.h> #include<gmp.h> #include<gmpxx.h> #include<iostream> #include<vector> #include<cstring> #include<fstream> using namespace std; mpz_t n; void read_n(); int main() { FILE *fp=fopen("../outputs/t1.txt", "r"); char ch; vector<string> blocks_str; // vector<mpz_t> blocks; vector<string> :: iterator itr; string str,s; //to strore temp read file contents mpz_t F,f,b_size,temp; mpz_inits(F,f,b_size,temp,n,NULL); mpz_set_ui(b_size,8); // size of each block int i=0,t=0,t1=0; fseek(fp, 0, SEEK_END); fseek(fp, 0, SEEK_SET); while((ch=getc(fp))!=EOF){ t=(int) ch; str+=to_string(t); } mpz_set_ui(F,str.length()); mpz_cdiv_q(f,F,b_size); // no of total blocks // gmp_printf("b_size -> %Zd\nF -> %Zd\nf -> %Zd\n",b_size,F,f); mpz_t blocks[mpz_get_ui(f)] ; for (i = 0; i < mpz_get_ui(f); i++) { mpz_init(blocks[i]); } read_n(); // gmp_printf("%Zd\n",n); i=0; char *cstr; while(i!=str.length()){ if(mpz_cmp_ui(b_size,s.length())==0){ cstr=(char *) malloc(1+s.length()); strcpy(cstr,s.c_str()); mpz_set_str(temp,cstr,10); // needs c style char string , not string mpz_set(blocks[t1],temp); mpz_mod(blocks[t1],blocks[t1],n); t1++; s.clear(); } s+=str[i]; i++; } if(s.length()!=0){ while(mpz_cmp_ui(b_size,s.length())!=0) s+='0'; cstr=(char *) malloc(1+s.length()); strcpy(cstr,s.c_str()); mpz_set_str(temp,cstr,10); // needs c style char string , not string mpz_set(blocks[t1],temp); mpz_mod(blocks[t1],blocks[t1],n); t1++; s.clear(); } // cout<<"index\tblock\n"; for (i = 0; i < mpz_get_ui(f); i++) { // cout<<i<<"\t"; gmp_printf("%d %Zd\n",i,blocks[i]); } fclose(fp); // closing the file pointer return 0; } void read_n(){ fstream key_file; string word,filename,n_keygen; // read values from keygen and file_blocks filename = "../outputs/key_gen_out.txt"; key_file.open(filename.c_str()); int i=1; while (key_file >> word) { if(i%3==0 && i%6!=0 && i%9!=0 && i%12!=0 && i%15!=0) n_keygen=word; i++; } mpz_set_str (n,n_keygen.c_str(),10); }
Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++
and start coding!
OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "Enter name:";
getline (cin, name);
cout << "Hello " << name;
return 0;
}
C++ is a widely used middle-level programming language.
When ever you want to perform a set of operations based on a condition If-Else is used.
if(conditional-expression) {
//code
}
else {
//code
}
You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to If-Else-If ladder.
switch(conditional-expression){
case value1:
// code
break; // optional
case value2:
// code
break; // optional
......
default:
code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while (condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
// code
}