Archive for February 9, 2012

Answer to challenging question

Posted: February 9, 2012 in OOP344

Here is my one line statement, which will work for this program…

int validMonth(int mon, char* errmes){
  return (strcpy(errmes, "Invalid month (1<=month<=12)") && mon > 0 && mon <=12);
}

NOTE:
Since someone has posted already the more efficient answer,

int validMonth(int mon, char* errmes){
  return (mon > 0 && mon <=12) || !strcpy(errmes, "Invalid month (1<=month<=12)");
}

I am just to show you here, this can be done with && operator. Also, my return statement will modify/replaceĀ  the errmes even if it is true. For good programming practice, we have to avoid extra process. So please don’t use this type of method.

Thanks!

MiniPrint – bugs fixed version

Posted: February 9, 2012 in OOP344
#include <cstdio>
#include <cstdarg>
using namespace std;

void miniprint(const char *format, ...){
  va_list ap;
  va_start(ap, format);
  while(*format){
    if(*format == '%'){
      format++;
      switch(*format){
        case 'd':
          printf("%d", va_arg(ap,int));
          break;
        case 'f':
          printf("%0.2f", va_arg(ap,double));
          break;
        case 's':
          printf("%s", va_arg(ap,char*));
          break;
        case 'x':
          printf("%x", va_arg(ap,int));
          break;
        case 'X':
          printf("%X", va_arg(ap,int));
          break;
        case 'p':
          printf("%p", va_arg(ap,void*));
          break;
        default:
          putchar(*format);
      }
      format++;
    }
    else{
      putchar(*format++);
    }
  }
  va_end(ap);
}

int main(){
  miniprint("hello I am %s and I make %f Dollars a year\n", "Homer", 12345.67);
  return 0;
}

Concatenate by command line argument

Posted: February 9, 2012 in OOP344

Basic Code without error message and safe guard.

#include <cstdio>

int concat(char* file1, char* file2) {
  FILE* fp1 = fopen(file1, "a");
  FILE* fp2 = fopen(file2, "r");
  char ch;
  int status = 0;
  if (fp2 && fp1) {
    for(;(ch=fgetc(fp2)) != EOF; fputc(ch, fp1));
    fclose(fp2);
    fclose(fp1);
    status = 1;
  }
  return status;
}

int main(int argc, char** argv) {
  int i=1;
  if (argc > 2){
    for (i = 1; i < argc-1 && concat(argv[1], argv[i+1]); i++);
    if (i == argc-1)
      printf("Concatenation is Success!!\n");
  }
  return 0;
}

Detailed Code with error messages and safe guard.

#include <cstdio>

bool concat(char* file1, char* file2) {
  FILE* fp1 = fopen(file1, "r");
  FILE* fp2 = fopen(file2, "r");
  char ch;
  bool status = 0;
  bool err = 0;

  if(!fp1) {
    fp1 = fopen(file1, "a");
    if(!fp1) {
      printf("ERROR: Can't open %s!!\n", file1);
    }
    else {
      err = 1;
      printf("%s is created!\n", file1);
    }
  }
  else{
    fclose(fp1);
    fp1 = fopen(file1, "a");
    if(!fp1) {
      printf("ERROR: Can't open %s!!\n", file1);
    }
    else err = 1;
  }

  if(!fp2 && err) {
    printf("ERROR: Can't open %s!!\n", file2);
  }

  if (fp2 && fp1) {
    for(;(ch=fgetc(fp2)) != EOF; fputc(ch, fp1));
    fclose(fp2);
    fclose(fp1);
    status = 1;
  }
  return status;
}

int main(int argc, char** argv) {
  int i=1;
  if (argc == 1)
    printf("No destination file  selected to perform this action!\n");
  else if (argc == 2)
    printf("No files were selected to merge with %s.\n",argv[1]);
  else if (argc > 2){
    for (i = 1; i < argc-1 && concat(argv[1], argv[i+1]); i++){
      printf("%s << %s... success\n",argv[1],argv[i+1]);
    }
    if (i == argc-1)
      printf("Concatenation is Success!!\n");
  }
  return 0;
}