0 / 0 / 0
Регистрация: 21.04.2009
Сообщений: 44
1

Перевести с С на С++

09.11.2009, 09:28. Показов 495. Ответов 0
Метки нет (Все метки)

Author24 — интернет-сервис помощи студентам
Помогите пожалуйста перевести эти программы на С++

books_create.c
C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "book.h"
        
/* Заполняет структуру Book_t */        
Book_t createBook(void){
        Book_t book;
        char buf[STR_LEN];
        printf("Author: ");
        fgets(buf, STR_LEN, stdin);
        buf[strlen(buf) - 1] = '\0'; /* удалить символ новой строки */
        strcpy(book.author, buf);
        printf("Name: ");
        fgets(buf, STR_LEN, stdin);
        buf[strlen(buf) - 1] = '\0'; /* удалить символ новой строки */
        strcpy(book.name, buf);
        printf("Year: ");
        fgets(buf, STR_LEN, stdin);
        book.year = atoi(buf);
        printf("Place: ");
        fgets(buf, STR_LEN, stdin);
        book.place = atoi(buf);
        
        return book;
}
 
void showBook(const Book_t *pBook){
        printf("Author:\t%s\n", pBook->author);
        printf("Name:\t%s\n", pBook->name);
        printf("Year:\t%4d\n", pBook->year);
        printf("Place:\t%d\n", pBook->place);
}
 
/* Записывает в конец файла информацию о книгах. По умолчанию используется books.db
        если файл не существует - он создаётся */
int main(){
        Book_t book;
        FILE *dbf;
        char buf[STR_LEN];
        int answ;
        
        if ( (dbf = fopen(DEFAULT_DB, "ab")) == NULL ){
                printf("Can't open file %s\n", DEFAULT_DB);
                exit(1);
        }
        
        while ( 1 ) {
                printf("\n%s\nEnter 1 for add book or 0 for quit: ", SEP_LINE);
                answ = getchar();
                getchar();
                if ( answ == '0' || answ == EOF )
                        break; /* на выход с вещами */
                if ( answ != '1' ){
                        printf("Bad choice\n");
                        continue;
                }
                book = createBook();
                printf("%s\nYou entered:\n", SEP_LINE);
                showBook(&book);
                printf("Is it correct? (y/n): ");
                answ = getchar();
                getchar();
                if ( answ == 'y' || answ == 'Y' ){
                        if ( fwrite(&book, sizeof(Book_t), 1, dbf) != 1 ) {
                                printf("Can't save data to file!\n");
                                exit(1);
                        }
                }
        }
        
        fclose(dbf);
        exit(0);
}

books_view.c
C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "book.h"
 
/* Выводит в одну строку значения структуры */  
void prnBook(const Book_t *pB, int num_row){
        printf("%-3d%-20s%-30s%-7d%-5d\n", num_row, pB->author, pB->name, pB->year, pB->place);
}
 
/* Выводит заголовок */
void prnHead(void){
        printf("%s\n#  Author              Name                          Year   Place\n%s\n", SEP_LINE, SEP_LINE);
}
 
int main(void){
        int count, answ, stop, i, same_year;
        long int file_size;
        FILE *dbf;
        char buf[STR_LEN];
        Book_t *books;
        Book_t dummy;
        
        if ( (dbf = fopen(DEFAULT_DB, "rb")) == NULL ){
                printf("Can't open file %s\n", DEFAULT_DB);
                exit(1);
        }
        
        fseek(dbf, 0, SEEK_END);
        file_size = ftell(dbf);
        rewind(dbf);
        
        /* файл должен содержать некоторое количество объектов без остатка */
        if ( file_size % sizeof(Book_t) ){
                printf("ERROR: file %s corrupt!\n", DEFAULT_DB);
                exit(1);
        }
        count = file_size / sizeof(Book_t);
        if ( !count ){
                printf("ERROR: file %s is empty!\n", DEFAULT_DB);
                exit(1);
        }
        
        if ( (books = (Book_t *)calloc(count, sizeof(Book_t))) == NULL ){
                printf("Not enough memory!\n");
                exit(1);
        }
        
        if ( fread((void *)books, sizeof(Book_t), count, dbf) != count ){
                printf("Unexpected end of file!\n");
                exit(1);
        }
        fclose(dbf);
        
        stop = 0;
        while ( !stop ){
                printf("\n%s\nBooks in db: %d\n", SEP_LINE, count);
                printf("1 - show all, 2 - find place by author and name, 3 - show all of same author,\n");
                printf("4 - count all of same year, 0 - exit\nChoice one: ");
                answ = getchar();
                getchar();
                switch(answ){
                        case '1' :
                                prnHead();
                                for ( i = 0; i < count; i++ )
                                        prnBook(books + i, i + 1);
                                printf("%s\n", SEP_LINE);
                                break;
                        case '2' :
                                printf("Author: ");
                                fgets(buf, STR_LEN, stdin);
                                buf[strlen(buf) - 1] = '\0';
                                strcpy(dummy.author, buf);
                                printf("Name: ");
                                fgets(buf, STR_LEN, stdin);
                                buf[strlen(buf) - 1] = '\0';
                                strcpy(dummy.name, buf);
                                for ( i = 0; i < count; i++ )
                                        if ( !strcmp(books[i].author, dummy.author) && !strcmp(books[i].name, dummy.name) )
                                                printf("\n*** Found in place %d\n", books[i].place);
                                break;
                        case '3' :
                                printf("Author: ");
                                fgets(buf, STR_LEN, stdin);
                                buf[strlen(buf) - 1] = '\0';
                                prnHead();
                                for ( i = 0; i < count; i++ )
                                        if ( !strcmp(books[i].author, buf) )
                                                prnBook(&books[i], i + 1);
                                break;
                        case '4' :
                                same_year = 0;
                                printf("Year: ");
                                fgets(buf, STR_LEN, stdin);
                                dummy.year = atoi(buf);
                                for ( i = 0; i < count; i++ )
                                        if ( books[i].year == dummy.year )
                                                same_year++;
                                printf("\n*** Fount %d books of %d year.\n", same_year, dummy.year);
                                break;
                        case '0' :
                                stop = 1;
                                break;
                        default :
                                printf("Bad choice - %c\n", answ);
                                break;
                }
        }
        free(books);                    
        
        return 0;
}
0
Programming
Эксперт
94731 / 64177 / 26122
Регистрация: 12.04.2006
Сообщений: 116,782
09.11.2009, 09:28
Ответы с готовыми решениями:

Перевести фиолетовый (105, 62, 151) из RGB в HSB, уменьшить яркость в 2 раза и перевести обратно
Помогите пожалуйста! Перевести фиолетовый (105, 62, 151) из RGB в HSB, уменьшить яркость в 2 раза...

QString перевести в char* || QChar перевести в char*
Подскажите пожалуйста как сделать: Исходники в utf-8, qt4.8 setlocale(LC_ALL, &quot;Russian&quot;); ...

Как презентации SWF флэш сайта перевести в HTML5 с сохранением эфектов ) Как правильно и полноценно Перевести SWF в HTML5
программа Sothink SWF Decompiler конвертирует SWF файл в HTML5 разбивая его на HTML и JS ...но она...

Текстовый файл перевести в двоичный, а потом полученный двоичный файл перевести обратно в текстовый
Всем привет. Есть такая задачка: &quot;текстовый файл перевести в двоичный, а потом полученный двоичный...

0
09.11.2009, 09:28
IT_Exp
Эксперт
87844 / 49110 / 22898
Регистрация: 17.06.2006
Сообщений: 92,604
09.11.2009, 09:28
Помогаю со студенческими работами здесь

Перевести с С++
#include&lt;iostream&gt; #include&lt;cstdio&gt; #include&lt;cstring&gt; using namespace std; int n; int...

Перевести с С++ на С#
#include &lt;iostream&gt; using namespace std; struct ST { int a; int b; int c; int d; };...

Перевести с С++ в С
Кто может помочь перевести с С++ в С? Буду очень признателен #include &lt;iostream&gt; #include...

перевести на C++)
Помогите перевести на C++, или скажите почему если ввожу 3, то бесконечные +++++, а остальные числа...

Перевести на c++
:sorry: type sotrudnik = record FI : string; Staj : integer; Age:...

Перевести с Си на PL/1
main() { int i,j; double a,b; scanf(&quot;%le%le&quot;,&amp;a,&amp;b); s1=0; s2=0; for (i=0; i&lt;=10; i=i+2) {...


Искать еще темы с ответами

Или воспользуйтесь поиском по форуму:
1
Ответ Создать тему
Опции темы

КиберФорум - форум программистов, компьютерный форум, программирование
Powered by vBulletin
Copyright ©2000 - 2024, CyberForum.ru