Работа с памятью

  • Автор темы dementrus
  • Дата начала
D

dementrus

Гость
#1
написал такую прогу:

Код:
#include "stdafx.h"
#include <iostream>
#include <stdio.h>
#include <conio.h>
#include <string.h>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
char ch=' ';
do{ 
int a;
char *str;
int number;
printf( "Massiv's length: ");
scanf ( "%d", &number);
str = (char *) malloc(number);
if ((str = (char *) malloc(number)) == NULL)
{
printf("Ошибка\n");	 
exit(1);						  
}
char *name,n;

int number1;
printf( " Vvedite Dlinnu massiva dlya imeni:  ");
scanf ( "%d", &number1);
name = (char *) malloc(number1);
printf( " Vvedite imya faila:  ");
scanf ( "%s", name);
FILE *f; f = fopen (name, "wt");
printf( "weight, t:  ");
scanf ( "%d", &a);
if(a<=5)
printf( "\nVes gruza ne prevyshaet 5t, vam potrebuetsya Tu-134\n\n");
if(a<=5)
fprintf(f, "Tu-134");
if(a>5 && a<=30)
printf( "\nVes gruza bolshe 5t i ne prevyshaet 30t, vam potrebuetsya Il-86\n\n");
if(a>5 && a<=30)
fprintf(f,"Il 86" );
if(a>30 && a<=45)
printf( "\nVes gruza bolshe 30t i ne prevyshaet 45t, vam potrebuetsya Ruslan\n\n");
if(a>30 && a<=45)
fprintf(f,"Ruslan" );
if(a>45)
printf( "\nVes gruza prevyshaet 45t, my ne v silah vam pomoch\n\n");
fclose(f);
f = fopen (name, "rt");
fgets(str,30,f);
printf("\n",str);
free(str);
delete(str);
free(name);
delete(name);
cout<<"\n\nChoose: Press [e] to exit or [r] to repeat "; 
ch=getch();
cout<<"\n";
}
while (ch!='e');	
getch();
return 0;
}

у меня вопрос как сократить строки?
 
R

Rififi

Гость
#2
dementrus

у меня вопрос как сократить строки?

комбинируй на каждой строке программы несколько операторов:


C++:
#include "stdafx.h"
#include <iostream>
#include <stdio.h>
#include <conio.h>
#include <string.h>
using namespace std;int _tmain(int argc, _TCHAR* argv[])
{char ch=' ';do{ int a;char *str;int number;printf( "Massiv's length: ");scanf ( "%d", &number);str = (char *)
malloc(number); if ((str = (char *) malloc(number)) == NULL){  printf("Ошибка\n"); exit(1); }char *name,
n;int number1;printf( " Vvedite Dlinnu massiva dlya imeni:  ");scanf ( "%d", &number1);name = (char *)
malloc(number1);printf( " Vvedite imya faila:  ");scanf ( "%s", name);FILE *f; f = fopen (name, "wt");printf
( "weight, t:  ");scanf ( "%d", &a);if(a<=5)printf( "\nVes gruza ne prevyshaet 5t, vam potrebuetsya Tu-134\n\n");
if(a<=5)fprintf(f, "Tu-134");if(a>5 && a<=30)printf( "\nVes gruza bolshe 5t i ne prevyshaet 30t, vam potrebuetsya Il-86\n\n");
if(a>5 && a<=30)fprintf(f,"Il 86" );if(a>30 && a<=45) printf( "\nVes gruza bolshe 30t i ne prevyshaet 45t, vam potrebuetsya Ruslan\n\n");
if(a>30 && a<=45)fprintf(f,"Ruslan" );if(a>45)printf( "\nVes gruza prevyshaet 45t, my ne v silah vam pomoch\n\n");
fclose(f);f = fopen (name, "rt");fgets(str,30,f);printf("\n",str);free(str);delete(str);free(name);delete(name);
cout<<"\n\nChoose: Press [e] to exit or [r] to repeat "; ch=getch();cout<<"\n";}while (ch!='e'); getch();return 0; }