Harlow people... It's been quite long since I update my blog. Reason? No inspiration and no motivation. =p
Just got ffk-ed again yesterday when trying to sell the HTC Diamond. Anyone want to buy? Zzz.
Anyway... just thought i would share a little part of my engineering course with everyone: my C++ program code for the recent lab 4. A glorious 199 lines of codes (It's considered short). =D
// Lab4_1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdio.h>
#include <string.h>
/**********Bunch of stuffs given**********/
const int IDLEN=10;
const int POLARITYLEN=3;
const int MAXSTOCKITEMS=10;
struct TransistorRec {
    char manufacturersID[IDLEN];
    char polarity[POLARITYLEN];
    float power;
    float gain;
    int stock;
};
typedef struct TransistorRec Transistor;
struct StockRec{
    int size;
    Transistor stocklist[MAXSTOCKITEMS];
};
typedef struct StockRec Stock;
/**********Function prototypes**********/
int fgetdata(Stock *ptr);
void fprintinfo(Stock);
void fsearch(Stock);
/**********Main**********/
int main()
{
    //Declaring variables used
    int choice,exit,countt;
    Stock StockInfo;
    StockInfo.size=0;
    //Print header for menu
    printf("Transistor Stock Menu\n");
    printf("==================================================\n");
    printf("[1] Input transistor stock data.\n");
    printf("[2] Show current transistor stock.    d-(^^,)z\n");
    printf("[3] Search for transistor.\n");
    printf("[4] Exit\n");
    printf("--------------------------------------------------\n");
    exit=0;    //Initialize variable "exit"
    //Do-while loop to run program until users ends it
    do{
        printf("\nPlease enter a choice: ");
        scanf("%d",&choice);
        //Switch statement to let user choose what to do
        switch (choice){
            case 1:{
                if(StockInfo.size<MAXSTOCKITEMS){
                countt=fgetdata(&StockInfo);    //Input data
                break;
                }
                else{
                    printf("Cannot input anymore transistor.\n");
                    break;
                }
                   }
            case 2:{
                fprintinfo(StockInfo);        //Print data
                break;
                   }
            case 3:{
                fsearch(StockInfo);        //Search data
                break;
                   }
            case 4:{
                exit=1;        //Exit
                break;
                   }
            default:{
                printf("Please enter a valid choice.\n");    //Prompt user for correct choice
                break;
                    }
        }
    }while(exit!=1);
    return 0;
}
/**********Function definitions**********/
//Function to get data
int fgetdata(Stock *inptr)
{
    int count=inptr->size; //Initialize counters
    int next=0,cont=1;   
    char temppol[POLARITYLEN+1],tempid[IDLEN+1];
    //Loop to input data
    do{
        printf("==================================================\n");
        printf("Please enter manufacturer's ID: ");
        scanf("%s",&(inptr->stocklist[count].manufacturersID));
        //Loop to input and validate polarity
        do{
            printf("Please enter polarity: ");
            scanf("%s",&temppol);
            if(!strcmp(temppol,"PNP")|!strcmp(temppol,"NPN")){
                strcpy(inptr->stocklist[count].polarity,temppol);
                cont=0;
            }
            else{
                printf("Please enter a valid polarity.\n");
                for(int j=0;j<POLARITYLEN;j++){
                temppol[j]='\0';
                }
                cont=1;
            }
        }while(cont);
        printf("Please enter power: ");
        scanf("%f",&(inptr->stocklist[count].power));
        printf("Please enter gain: ");
        scanf("%f",&(inptr->stocklist[count].gain));
        printf("Please enter amount in stock: ");
        scanf("%d",&(inptr->stocklist[count].stock));
        count++;//Increases type of transistor count
        inptr->size=count;
        if((inptr->size)<MAXSTOCKITEMS){
        //Prompts to repeat data input
        printf("Thank you, input another? (YES=1, NO=0): ");
        scanf("%d",&next);
        }
        else{
            printf("Maximum stock items reached!\n");
            next=0;
        }
    }while(next!=0);
    return count;
}
//Function to print data
void fprintinfo(Stock input)
{
    //Print table header
    printf("\n================= CURRENT STOCK ==================\n");
    printf("|_Mft. ID_|_Polarity_|_Power_|_Gain_|_Stock_|\n\n");
    //Loop to print transistor info until number of transistor types
    for(int i=0;i<input.size;i++)
    {
        printf("  %s\t\t%s\t%5.2f\t%2.0f\t%2.0d\n",
            input.stocklist[i].manufacturersID,
            input.stocklist[i].polarity,
            input.stocklist[i].power,
            input.stocklist[i].gain,
            input.stocklist[i].stock);
    }
    printf("\n==================================================\n");
}
//Function to search transistor type
void fsearch(Stock input)
{
    //Define variables and print header
    int sel=99;        //Set sel to 99 (Unreachable value for type of transistors)
    char searchID[IDLEN]={0};
    printf("==================================================\n");
    printf("Search: ");
    scanf("%s",&searchID);        //Receive input for search
    //Loop to compare searchID and manufacturer's ID
    for(int i=0;i<input.size;i++)
    {
        if((strcmp(searchID,input.stocklist[i].manufacturersID))==0)
        sel=i;
    }
    if(sel!=99)
    {
        //Print out results
        printf("==================================================\n");
        printf("Found transistor...\n");
        printf("Manufacturers ID = %s\n",input.stocklist[sel].manufacturersID);
        printf("Polarity = %s\n",input.stocklist[sel].polarity);
        printf("Power = %f\n",input.stocklist[sel].power);
        printf("Gain = %.0f\n",input.stocklist[sel].gain);
        printf("Stock = %.0d\n",input.stocklist[sel].stock);
    }
    else
        printf("Transistor not found. \n");        //Print if transistor type doesnt exist
}
                    
yeay! It's an update! XD
Subscribe to:
Post Comments (Atom)
 
 
No comments:
Post a Comment