Thursday, May 23, 2013

Variadic Function Example #2

// This is just a quick sample of another very basic variadic function; this one returns which of however many // strings are sent to it is the longest.  I think that's probably a poorly phrased sentence, but there ya go.

#include <iostream>
#include <stdarg.h>
using namespace std;
const char* longest(char* lon,...);
int main(){
    char* lon;
    int g;
    cout << longest(lon,"He","heee","heeeeee",'\0');
    cin >> g;
    return 0;
}
const char* longest(char* lon,...){
    va_list whi;
    va_start(whi,lon);
    int i=0,j=0,k=0;
    char* bonk;
    char* bink;
    while(bonk=va_arg(whi,char*)){
        for(i=0;bonk[i];i++){
            j++;
            if(j>k)bink=bonk;
        }
    }
    return bink;
}

frustration=nstrcat(des,...);(ver 1.1)

#include <iostream>
#include <stdarg.h>

const char* nstrcat(char* des,...);
void merge(char*, char*);

const char* nstrcat(char* des,...){
      va_list pass;
      va_start(pass,des);
      char* base;
      while(base=va_arg(pass,char*)){
          if(base)merge(des,base);
          else break;
      }
      va_end(pass);
      return des;
}
void merge(char* des,char* base){
     int i=0,j=0;
     for(i;des[i];i++);
     for(i;base[j];i++,j++){
         des[i]=base[j];
     }  
}
The hardest thing to wrap your head around when you first start working with va_list's (it seems to me) is the idea that each time you reference the list with va_arg it advances the the address pointer for the parameter list.  Once you figure out how to work with that the rest goes pretty easy. 

ASCTOINT 1.2 (Now with range checking,bad input rejection, and negitive numbers!)

#include <iostream>
#include <cstdlib>
#define MAX 99

int asctoint(char*);
int pow(int,int);

using namespace std;

int main(){
    char ghost[MAX];
    int alter=0;
    int p=0;
    cin >> ghost;
    alter=asctoint(ghost);
    cout << alter;
    cin >> p;
    return 0;
}
int asctoint(char* a){
    int grab[MAX];
    bool flip=false;
    int i=0,j=0;
    int num=0;
    int toast=0;
    if(a[0]=='-'){
        flip=true;
        i++;
    }
    for(i;a[i];i++,num++){
        if(a[i]>47&&a[i]<58)grab[i]=a[i]-48;
        else return 0;
    }
    if(flip){
        j++;
    }
    if(num>10)return 0;
    for(j;num>0;j++,num--){
        toast+=pow(grab[j],num);
    }
    if(flip)toast*=-1;
    return toast;
}
int pow(int dig,int exp){
    int i=0;
    if(exp>1){
        for(i=0;i<exp-1;i++){
            dig*=10;
        }
    }
    return dig;
}

ASCTOINT

ASCTOINT function:
(I didn't bother to add any boundaries on the input, but remember that an int has a practical max, and too high of a number will cause this function to return a REALLY weird number)

#include <iostream>
#include <cstdlib>
#define MAX 99
int asctoint(char*);
int pow(int,int);
using namespace std;
int main(){
    char ghost[MAX];
    int alter=0;
    int p=0;
    cin >> ghost;
    alter=asctoint(ghost);
    cout << alter;
    cin >> p;
    return 0;
}
int asctoint(char* a){
    int grab[MAX];
    int i=0,j=0;
    int num=0;
    int toast=0;
    for(i=0;a[i];i++){
        grab[i]=a[i]-48;
        num++;
    }
    for(j=0;num>0;j++,num--){                         
        toast+=pow(grab[j],num);
    }
    return toast;
}
int pow(int dig,int exp){
    int i=0;
    if(exp>1){
        for(i=0;i<exp-1;i++){
            dig*=10;
        }
    }
    return dig;
}

Tuesday, May 7, 2013

Here is the OOP344 blog, as requested.
Looking forward to this class, learning some desktop presentation libraries like conio and curses.
Should prove to be interesting.