示例#1
0
/*
    Точка входа в программу. Для исполняемых файлов наличие данной функции является обязательной в языке С.
*/
int main()
{
    double a, b, c, discriminant;
    double x1, x2;

    // вызов ранее объявленной функции, круглые скобки - обязательны.
    print_data_size();

    printf("Program to solve quadratic equation a * x^2 + b * x + c = 0\n");
    
    printf("Enter the a: ");
    /*
        Функция scanf - используется для пользовательского ввода значение нужного типа.
        Здесь используются теже %-последовательности, что и в функции printf.
        
        Ниже указанная конструкция работает следующим образом:
        Вы вводите число, нажимаете Enter и scanf приводит это число к действительному типу (double) и записывает значение по адресу памяти, выделенному для переменной a. 
        
        Амперсент (знак '&') как раз и означает получение адреса памяти для переменной a.
    */
    scanf("%f", &a);

    printf("Enter the b: ");
    scanf("%f", &b);

    printf("Enter the c: ");
    scanf("%f", &c);

    discriminant = calculate_discriminant(a, b, c);

    if ( discriminant > 0 ) {
        x1 = ( -b + sqrt( discriminant ) ) / 2 * c;
        x2 = ( -b - sqrt( discriminant ) ) / 2 * c;
        
        printf("x1 is: %lf\n", x1);
        printf("x2 is: %lf\n", x2);
    } else {
        printf("The equation has no real roots\n");
    }
    
    return 0;
}
示例#2
0
void digest_input(string filename)
{
	// Print basic information and advance read cursor past author line.
	// (Author line must always exist and must be the first line.)
	cout << "Processing started..." << endl << endl;
	cout << "Input: " << filename << endl << endl;
	string filename_input = "Texts/" + filename;
	ifstream file_text(filename_input);
	print_data_size(file_text);
	string author_name;
	getline(file_text, author_name);
	string filename_author = "Authors/" + author_name + ".csv";
	string filename_summary = create_file_and_name("Digests/", filename, "-SUM.txt");
	string filename_words = create_file_and_name("Digests/", filename, "-WORD.csv");
	string filename_sentences = create_file_and_name("Digests/", filename, "-SENT.csv");
	cout << "Author: " << author_name << endl << endl;
	cout << "Output: " << filename + "-SUM.txt" << endl <<
		"        " << filename + "-WORD.csv" << endl <<
		"        " << filename + "-SENT.csv" << endl << endl;

	Memory RAM;
	string raw_input;
	cout << "Parsing text: |";
	const int bar_width = 50;
	for (int i = 0; i<bar_width; ++i) { cout << " "; }
	cout << "|  0.00%";
	for (int i = 0; getline(file_text, raw_input); ++i) {
		add_data_from_line(RAM, raw_input);
		// TODO: Make constants settable via command-line options (i.e. 10000, 3000)
		if (RAM.word_list.size() > 10000) {
			cout << endl << endl << "Flushing buffer..." << endl << endl;
			combine_list_file(	RAM.word_list,
								filename_words,
								RAM.word_list.begin() + 3000,
								RAM.word_list.end()	);
			cout << "Parsing text: |";
			for (int i = 0; i<bar_width; ++i) { cout << " "; }
			cout << "|  0.00%";
		}
		// Progress bar stuff:
		if (i % 120 == 0) {
			cout << "\b\b\b\b\b\b\b\b"; // "| ##.##%"
			for (int i = 0; i < bar_width; ++i) { cout << "\b"; }
			ifstream file_sizer(filename_input);
			file_sizer.seekg(0, ios::end);
			float size = static_cast<float>(file_sizer.tellg());
			file_sizer.close();
			float current = static_cast<float>(file_text.tellg());
			float percentage = current / size * 100;
			int chars_filled = static_cast<int>(floor(percentage/100.0*bar_width));
			for (int i = 0; i < bar_width; ++i) {
				if (i < chars_filled) {
					cout << "#";
				} else {
					cout << " ";
				}
			}
			std::streamsize precision_init = cout.precision();
			int correct_precision = get_precision(5, percentage);
			cout.precision(correct_precision);
			std::streamsize width_init = cout.width();
			cout << "| " << std::setw(5) << percentage << "%";
			cout.precision(precision_init);
			cout << std::setw(width_init);
		}
	}
	cout << "\b\b\b\b\b\b\b\b"; // "| ##.##%"
	for (int i = 0; i < bar_width; ++i) { cout << "\b"; }
	for (int i = 0; i < bar_width; ++i) { cout << "#"; }
	cout << "| -DONE-" << endl << endl;

	// Final write:
	cout << "Writing frequency files..." << endl << endl;
	std::sort(RAM.word_list.begin(), RAM.word_list.end(), word_compare());
	combine_list_file(RAM.word_list, filename_words);
	get_list_from_file(RAM.word_list, filename_words);
	ofstream file_sentences(filename_sentences);
	file_sentences << "WORDS" << endl;
	for (unsigned int i = 0; i < RAM.sentence_len.size(); ++i) {
		file_sentences << RAM.sentence_len[i] << endl;
	}
	file_sentences.close();
	cout << "Writing summary file..." << endl << endl;
	write_summary(filename_summary, &RAM, author_name);

	cout << endl << "Done!" << endl;
}