int processTable(char* line1, FILE* in, FILE* out) { numberOfCols = 0; cols = NULL; int i=0; while(line1[i] != '\0' && line1[i] != '\n') { if(line1[i] == '|') { numberOfCols++; } i++; } numberOfCols -= 1; //Reduce for the first and last '|' which means we get 1 more than we should cols = malloc(numberOfCols * sizeof(Col)); if(cols == NULL) { fprintf(stderr, "Out of memory\nUnable to process table."); return 1; } for(i=0; i<numberOfCols; i++) { Col c = {FALSE, FALSE, DEFAULT}; cols[i] = c; } //Read the next line char c = 0; i = -1; //the col we are in do { c = getc(in); if(c == '|') { i++; if( i > numberOfCols) { // There should be more cols than first predicted (using the first line of the table) numberOfCols++; cols = realloc (cols, numberOfCols*sizeof(Col)); if(cols == NULL) outOfMemoryError(); Col c = {FALSE, FALSE, DEFAULT}; cols[i-1] = c; } c = getc(in); if(c == ':') { cols[i].l = TRUE; } } else if(c == ':') { c = getc(in); if(c == '|') { cols[i].r = TRUE; i++; if( i > numberOfCols) { // There should be more cols than first predicted (using the first line of the table) numberOfCols++; cols = realloc (cols, numberOfCols*sizeof(Col)); if(cols == NULL) outOfMemoryError(); Col c = {FALSE, FALSE, DEFAULT}; cols[i-1] = c; } c = getc(in); if(c == ':') { cols[i].l = TRUE; } } } } while(c != '\n'); //use up to the new line if not there already while(c != '\n') c = getc(in); for(i=0; i<numberOfCols; i++) { if(cols[i].r && cols[i].l) { cols[i].align = CENTER; } else if(cols[i].r) { cols[i].align = RIGHT; } else if(cols[i].l) { cols[i].align = LEFT; } } //Do we need table x? (If there are any defaults then yes) char tablex = false; for(i=0; i<numberOfCols; i++) { if(cols[i].align == DEFAULT) { tablex = true; break; } } fprintf(out, "\\begin{center}\n\\rowcolors{3}{tableShade}{white}\n"); if(tablex) { fprintf(out, "\\begin{tabularx}{\\linewidth}"); } else { fprintf(out, "\\begin{tabular}"); } fprintf(out, "{ "); for(i=0; i<numberOfCols; i++) { putc(toChar(cols[i].align), out); putc(' ', out); if(i < numberOfCols-1) { putc('|', out); putc(' ', out); } } fprintf(out, "}\n\\hiderowcolors\n"); int pos = 0; i = 0; //number of cols we have written c = line1[pos]; pos++; int buf_size = CELL_BUF_SIZE; char* buf = malloc(sizeof(char) * buf_size); if( buf == NULL ) outOfMemoryError(); int buf_pos = 0; while(c != '\n' && c != '\0') { c = line1[pos]; pos++; if(c == '\0') break; if(c == '|') { buf[buf_pos] = '\0'; parseLine(buf, buf_pos, in, out); //Use the markdownlatex method to process the cell buf_pos = 0; buf[0] = '\0'; if(i < numberOfCols-1) { putc(' ', out); putc('&', out); putc(' ', out); } i++; } else if (c != '\n'){ if(buf_pos >= buf_size-1) { //We need a bigger buffer! buf_size *= 2; char* newBuf = malloc(sizeof(char) * buf_size); if( newBuf == NULL ) outOfMemoryError(); for(int i=0; i<buf_pos; i++) { newBuf[i] = buf[i]; } free(buf); buf = newBuf; } buf[buf_pos] = c; buf_pos++; } } // Put some empty cells at the end if there are not enough while(i < numberOfCols-1) { putc(' ', out); putc('&', out); putc(' ', out); i++; } fprintf(out, "\\\\\n\\showrowcolors \n\\hline\n"); //while the lines start with a '|' while((c=getc(in)) == '|') { i=0; do { c = getc(in); if(c == '|') { buf[buf_pos] = '\0'; parseLine(buf, buf_pos, in, out); //Use the markdownlatex method to process the cell buf_pos = 0; buf[0] = '\0'; if(i != -1 && i < numberOfCols-1) putc('&', out); i++; } else if (c != '\n'){ if(buf_pos >= buf_size-1) { //We need a bigger buffer! buf_size *= 2; char* newBuf = malloc(sizeof(char) * buf_size); if( newBuf == NULL ) outOfMemoryError(); for(int i=0; i<buf_pos; i++) { newBuf[i] = buf[i]; } free(buf); buf = newBuf; } buf[buf_pos] = c; buf_pos++; } } while(c != '\n'); fprintf(out, "\\\\\n"); } free(buf); if(tablex) { fprintf(out, "\\end{tabularx}"); } else { fprintf(out, "\\end{tabular}"); } fprintf(out, "\n\\end{center}\n\\vspace{5mm}\n"); free(cols); return 0; }
BNum BNum::normalMinus(const BNum First, const BNum Second) { BNum first=First; BNum second=Second; //we don't want to change this and other nums int maxSize= first.size > second.size ? first.size : second.size; if(maxSize==first.size) { //lets improve the second one to upper digits char *tempDigits=new char[maxSize]; for(int i=0;i<second.size;i++) tempDigits[i]=second.digits[i]; for(int i=second.size;i<maxSize;i++) tempDigits[i]='0'; //delet second.digits and reset char *remover=new char[second.size]; remover=second.digits; second.size=maxSize; second.digits=tempDigits; delete[] remover; } if(maxSize==second.size) { //lets improve the second one to upper digits char *tempDigits=new char[maxSize]; for(int i=0;i<first.size;i++) tempDigits[i]=first.digits[i]; for(int i=first.size;i<maxSize;i++) tempDigits[i]='0'; //delet first.digits and reset char *remover=new char[first.size]; remover=first.digits; first.size=maxSize; first.digits=tempDigits; delete[] remover; } BNum result; //we will return result.size=maxSize; char *remover=new char[result.size]; remover=result.digits; result.digits=new char[maxSize]; delete[] remover; int minCarry=0; //now we have twe num with imporoved size for(int i=0;i<maxSize;i++) { if(toInt(first.digits[i])>=toInt(second.digits[i])+minCarry) { result.digits[i]=toChar(toInt(first.digits[i])-toInt(second.digits[i])-minCarry); minCarry=0; } else { result.digits[i]=toChar(10 + toInt(first.digits[i]) - toInt(second.digits[i])-minCarry); minCarry=1; } } result.removeZero(); //there are some extra zero return result; }
void MainWindow::InsertPeoson() { strcpy(temp->major,toChar(stuForm.major)); strcpy(temp->name,toChar(stuForm.stuname)); strcpy(temp->psubinfo.subname,toChar(stuForm.subname)); temp->stunum = stuForm.stunum.toLong(); temp->psubinfo.marks = stuForm.marks.toLong(); int i = stuForm.id.toInt(); pstustr ptmp; if(i==1) { temp->id = head->id; temp->pnext = head; head = temp; ptmp = temp->pnext; while(NULL != ptmp) { ptmp->id = ptmp->id+1; ptmp = ptmp->pnext; } } else if(i>1&&i<=(data->id+1)) { for(ptmp = head;NULL!=ptmp&&(ptmp->id)<(i-2);ptmp = ptmp->pnext) { ; } if((i-ptmp->id)==2) { temp->id = ptmp->pnext->id; temp->pnext = ptmp->pnext; ptmp->pnext = temp; ptmp = temp->pnext; while(NULL!=ptmp) { ptmp->id = ptmp->id+1; ptmp = ptmp->pnext; } } else{ QMessageBox::warning(this,tr("警告"),tr("哈哈,出错了"),QMessageBox::Yes); free(temp); } } else { QMessageBox::warning(this,tr("警告"),tr("哈哈,出错了"),QMessageBox::Yes); free(temp); } stuForm.upToFirst(); stuForm.close(); Resetbro(); this->show(); }
BNum BNum::normalPlus(const BNum First, const BNum Second) { BNum first=First; BNum second=Second; //we don't want to change First and Second nums // first=*this; // second // qDebug()<<this->size<<other.size; int maxSize= first.size > second.size ? first.size : second.size; if(maxSize==first.size) { //lets improve the second one to upper digits char *tempDigits=new char[maxSize]; for(int i=0;i<second.size;i++) tempDigits[i]=second.digits[i]; for(int i=second.size;i<maxSize;i++) tempDigits[i]='0'; //delet second.digits and reset char *remover=new char[second.size]; remover=second.digits; second.size=maxSize; second.digits=tempDigits; delete[] remover; } else if(maxSize==second.size) { //lets improve the second one to upper digits char *tempDigits=new char[maxSize]; for(int i=0;i<first.size;i++) tempDigits[i]=first.digits[i]; for(int i=first.size;i<maxSize;i++) tempDigits[i]='0'; //delet first.digits and reset char *remover=new char[first.size]; remover=first.digits; first.size=maxSize; first.digits=tempDigits; delete[] remover; } BNum result(0); //we will return result.size=maxSize; char *remover=new char[maxSize]; remover=result.digits; result.digits=new char[maxSize]; //maybe we have carry at last delete[] remover; //now we add some zero to the lower one //lets add to nums int carry=0; int tempInt; for(int i=0;i<maxSize;i++) { tempInt=toInt(first.digits[i])+toInt(second.digits[i])+carry; result.digits[i]=toChar(tempInt % 10); carry=(tempInt-(tempInt%10))/10; } if(carry!=0) { char *tempDigits=new char[maxSize+1]; char *remover=new char[maxSize]; remover=result.digits; result.size++; for(int i=0;i<maxSize;i++) tempDigits[i]=result.digits[i]; tempDigits[maxSize]=toChar(carry); result.digits=new char[maxSize+1]; result.digits=tempDigits; delete[] remover; } return result; }